View Javadoc

1   package net.sf.jhunlang.jmorph.sword;
2   
3   import java.util.Iterator;
4   import java.util.ListIterator;
5   import java.util.List;
6   import java.util.LinkedList;
7   
8   import net.sf.jhunlang.jmorph.DictEntryExtension;
9   
10  public class SwordExtension implements DictEntryExtension
11  {
12    protected List derivatives = new LinkedList();
13    protected List cases = new LinkedList();
14  
15    protected boolean add;
16  
17    protected String caseEnding;
18    protected String cas;
19  
20    public SwordExtension()
21    {}
22  
23    public SwordExtension(List cases)
24    {
25      this.cases = cases;
26    }
27  
28    public SwordExtension(SwordExtension ext)
29    {
30      derivatives = ext.getDerivatives();
31      cases = ext.getCases();
32    }
33    
34    public void setAdd(boolean b)
35    {
36      add = b;
37    }
38  
39    public POSName getPOSName()
40    {
41      int index = derivatives.size() - 1;
42      return index == -1 ? POSName.empty :
43        ((Derivative)derivatives.get(index)).getDerivative().getPOSName();
44    }
45  
46    public String getPOS()
47    {
48      return getPOSName().getName();
49    }
50    
51    public boolean isDerivator()
52    {
53      return !derivatives.isEmpty();
54    }
55  
56    public boolean isInflexion()
57    {
58      Iterator it = getAllCases().iterator();
59      while (it.hasNext())
60      {
61        Case c = (Case)it.next();
62        if (!c.zero())
63        {
64          return true;
65        }
66      }
67      return false;
68    }
69    
70    public List getCases()
71    {
72      return cases;
73    }
74  
75    // return cases AND postCases of last derivative
76    public List getAllCases()
77    {
78      if (derivatives.size() > 0)
79      {
80        List allCases = new LinkedList();
81        Derivative derivator =
82          (Derivative)derivatives.get(derivatives.size() - 1);
83        allCases.addAll(derivator.getPostCases());
84        allCases.addAll(cases);
85        return allCases;
86      }
87      else
88      {
89        return cases;
90      }
91    }
92  
93    public List getDerivatives()
94    {
95      return derivatives;
96    }
97  
98    public String getCaseEnding()
99    {
100     if (caseEnding == null)
101     {
102       StringBuffer sb = new StringBuffer();
103       if (cases.size() > 0)
104       {
105         Iterator it = cases.iterator();
106         while (it.hasNext())
107         {
108           sb.append("+" + it.next());
109         }
110       }
111       else if (derivatives.size() > 0)
112       {
113         ListIterator dit = derivatives.listIterator(derivatives.size());
114         Derivative der = (Derivative)dit.previous();
115 
116         Iterator it = der.getPostCases().iterator();
117         while (it.hasNext())
118         {
119           sb.append("+" + it.next());
120         }
121       }
122       caseEnding = sb.toString();
123     }
124     return caseEnding;
125   }
126 
127   public String getCase()
128   {
129     if (cas == null)
130     {
131       ListIterator it = cases.listIterator(cases.size());
132       if (it.hasPrevious())
133       {
134         cas = ((Case)it.previous()).getName();
135       }
136       else
137       {
138         it = derivatives.listIterator(derivatives.size());
139         if (it.hasPrevious())
140         {
141           Derivative der = (Derivative)it.previous();
142           cas = der.getPostCase();
143         }
144         else
145         {
146           cas = "";
147         }
148       }
149     }
150     return cas;
151   }
152 
153   public String toString()
154   {
155     StringBuffer sb = new StringBuffer();
156     Iterator it = derivatives.iterator();
157 
158     while (it.hasNext())
159     {
160       sb.append("+" + it.next());
161     }
162 
163     it = cases.iterator();
164     while (it.hasNext())
165     {
166       sb.append("+" + it.next());
167     }
168     return sb.toString();
169   }
170 
171   public String morphString()
172   {
173     StringBuffer sb = new StringBuffer();
174     Iterator it = derivatives.iterator();
175 
176     while (it.hasNext())
177     {
178       sb.append(((Derivative)it.next()).morphString());
179       if (it.hasNext())
180       {
181         sb.append(" ");
182       }
183     }
184 
185     if (derivatives.size() > 0 && cases.size() > 0)
186     {
187       sb.append(" ");
188     }
189 
190     it = cases.iterator();
191 
192     while (it.hasNext())
193     {
194       sb.append(((Case)it.next()).morphString());
195       if (it.hasNext())
196       {
197         sb.append(" ");
198       }
199     }
200     return sb.toString();
201   }
202   
203   public String inflexionString()
204   {
205     StringBuffer sb = new StringBuffer();
206 
207     Iterator it = cases.iterator();
208 
209     while (it.hasNext())
210     {
211       sb.append(((Case)it.next()).morphString());
212       if (it.hasNext())
213       {
214         sb.append(" ");
215       }
216     }
217     return sb.toString();
218   }
219   
220   public String derivatorString()
221   {
222     StringBuffer sb = new StringBuffer();
223     Iterator it = derivatives.iterator();
224 
225     while (it.hasNext())
226     {
227       Derivative der = (Derivative)it.next();
228       if (der.getPreCases().size() != 0)
229       {
230         break;
231       }
232       if (sb.length() != 0)
233       {
234         sb.append(' ');
235       }
236       sb.append(der.getDerivative().morphString());
237       /*
238       if (der.getPostCases().size() != 0)
239       {
240         break;
241       }
242       */
243     }
244     return sb.toString();
245   }
246 }