1 package net.sf.jhunlang.jmorph.analysis;
2
3 import net.sf.jhunlang.jmorph.AffixEntry;
4 import net.sf.jhunlang.jmorph.DictEntry;
5 import net.sf.jhunlang.jmorph.PrefixEntry;
6 import net.sf.jhunlang.jmorph.SuffixEntry;
7
8 /***
9 * AnalysisEntry describes either an uninflexed dictionary word or an
10 * inflexion of a dictionary word either by a prefix rule or by a
11 * suffix rule or by a crossable prefix and a crossable suffix rule
12 */
13 public class AnalysisEntry implements Analysis
14 {
15 /***
16 * The dictionary entry
17 */
18 protected DictEntry dict;
19 /***
20 * The first applied inflexion if any
21 */
22 protected AffixEntry affix0;
23 /***
24 * The second applied inflexion if any
25 */
26 protected SuffixEntry affix1;
27
28 /***
29 * Create a new AnalysisEntry for the given dictionary word.
30 * @param dict the dictionary word
31 */
32 public AnalysisEntry(DictEntry dict)
33 {
34 this.dict = dict;
35 }
36
37 /***
38 * Create a new AnalysisEntry for the inflexion by <code>affix</code> of
39 * the dictionary word <code>dict</code>.
40 * by the given rule
41 * @param dict the dictionary word
42 * @param affix the applied affix rule
43 */
44 public AnalysisEntry(DictEntry dict, AffixEntry affix)
45 {
46 this(dict);
47 this.affix0 = affix;
48 }
49
50 /***
51 * Create a AnalysisEntry for the inflexion by <code>prefix</code> and
52 * <code>suffix</code> of the dictionary word <code>dict</code>.
53 * @param dict the dictionary word
54 * @param prefix the applied prefix rule
55 * @param suffix the applied suffix rule
56 */
57 public AnalysisEntry(DictEntry dict, PrefixEntry prefix, SuffixEntry suffix)
58 {
59 this(dict);
60 this.affix0 = prefix;
61 this.affix1 = suffix;
62 }
63
64 /***
65 * Return the 'absolute stem' of this entry.
66 * @return the 'aboslute stem' of this entry
67 * @see DictEntry#getAbsoluteRootWord()
68 */
69 public String getAbsoluteRootWord()
70 {
71 return dict.getAbsoluteRootWord();
72 }
73
74 /***
75 * Return the 'dictionary stem' of this entry.
76 * @return the 'dictionary stem' of this entry
77 * @see DictEntry#getDictionaryRootWord()
78 */
79 public String getDictionaryRootWord()
80 {
81 return dict.getDictionaryRootWord();
82 }
83
84 /***
85 * Return the 'relative stem' of this entry.
86 * @return the 'relative stem' of this entry
87 * @see DictEntry#getRelativeRootWord()
88 */
89 public String getRelativeRootWord()
90 {
91 return dict.getRelativeRootWord();
92 }
93
94 public boolean derived()
95 {
96 return dict.derived();
97 }
98
99 public String getPOS()
100 {
101 return dict.getPOS();
102 }
103
104 public String getCaseEnding()
105 {
106 if (affix0 != null && (affix0 instanceof SuffixEntry))
107 {
108 return affix0.getCase();
109 }
110 else if (affix1 != null)
111 {
112 return affix1.getCase();
113 }
114 else
115 {
116 return dict.getCaseEnding();
117 }
118 }
119
120 /***
121 * Return the inflexed word.
122 */
123 public String getInflexedWord()
124 {
125 if (affix1 != null)
126 {
127 return dict.inflex((PrefixEntry)affix0, affix1);
128 }
129 else if (affix0 != null)
130 {
131 return dict.inflex(affix0);
132 }
133 else
134 {
135 return dict.getWord();
136 }
137 }
138
139 public DictEntry getDictEntry()
140 {
141 return dict;
142 }
143
144 /***
145 * Set {@link #affix0}
146 * @param entry the new affix entry
147 */
148 public void setAffixEntry0(AffixEntry entry)
149 {
150 affix0 = entry;
151 }
152
153 /***
154 * Return the first inflexion rule if any
155 */
156 public AffixEntry getAffixEntry0()
157 {
158 return affix0;
159 }
160
161 public PrefixEntry getPrefixEntry()
162 {
163 if (affix0 != null && (affix0 instanceof PrefixEntry))
164 {
165 return (PrefixEntry)affix0;
166 }
167 else
168 {
169 return null;
170 }
171 }
172
173 public SuffixEntry getSuffixEntry()
174 {
175 if (affix0 != null && (affix0 instanceof SuffixEntry))
176 {
177 return (SuffixEntry)affix0;
178 }
179 else
180 {
181 return affix1;
182 }
183 }
184
185 /***
186 * Return the second inflexion rule if any.
187 */
188 public SuffixEntry getAffixEntry1()
189 {
190 return affix1;
191 }
192
193 /***
194 * Return if this entry represents an inflexed word.
195 */
196 public boolean inflexed()
197 {
198 return affix0 != null || dict.inflexed();
199 }
200
201 public boolean compound()
202 {
203 return dict.length() > 1;
204 }
205
206
207 public String morphString()
208 {
209 String s = dict.getPOS();
210 String d = dict.dictionaryDerivatorString();
211
212 if (d.length() > 0)
213 {
214 s += ' ' + d;
215 }
216
217 String inflexion;
218 SuffixEntry suffix = getSuffixEntry();
219
220 if (suffix != null)
221 {
222 inflexion = suffix.morphString();
223 }
224 else
225 {
226 inflexion = dict.inflexionString();
227 }
228
229 if (inflexion.length() == 0)
230 {
231 return s;
232 }
233 else if (s.length() == 0)
234 {
235 return inflexion;
236 }
237 else
238 {
239 return s + ' ' + inflexion;
240 }
241 }
242
243 /***
244 * Return internal String representation of this instance.
245 */
246 public StringBuffer contentString()
247 {
248 StringBuffer sb;
249 if (dict.inflexed())
250 {
251 sb = new StringBuffer(dict.getRelativeRootEntry().toString());
252
253 if (affix0 == null)
254 {
255 sb.append(", " + dict.getInflexion());
256 }
257 }
258 else
259 {
260 sb = new StringBuffer(dict.toString());
261 }
262
263 if (affix0 != null)
264 {
265 sb.append(", " + affix0);
266 }
267 if (affix1 != null)
268 {
269 sb.append(", " + affix1);
270 }
271 return sb;
272 }
273
274 public StringBuffer longContentString()
275 {
276 StringBuffer sb = new StringBuffer(dict.toLongString());
277 if (affix0 != null)
278 {
279 sb.append(", " + affix0.toLongString());
280 }
281 if (affix1 != null)
282 {
283 sb.append(", " + affix1.toLongString());
284 }
285 return sb;
286 }
287
288 protected String getClassNameString()
289 {
290 return "Se";
291 }
292
293 /***
294 * Return String representation of this instance reflecting the
295 * runtime type (class)
296 */
297 public String toLongString()
298 {
299 StringBuffer sb = new StringBuffer(getClassNameString());
300 sb.append("[");
301 sb.append(longContentString());
302 sb.append("]");
303 return new String(sb);
304 }
305
306 public String toString()
307 {
308 return contentString().toString();
309 }
310 }