KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > SnowMailClient > Language > SentenceDictionary


1 package SnowMailClient.Language;
2
3
4 import snow.utils.storage.*;
5
6 import java.util.*;
7 import java.util.zip.*;
8 import java.io.*;
9 import java.net.URL JavaDoc;
10 import javax.swing.table.*;
11 import javax.swing.event.*;
12 import java.awt.event.*;
13
14
15 /** Contain the sentences for a given language,
16      and store the sentences that are not already translated
17
18 */

19 public final class SentenceDictionary //implements Vectorizable
20
{
21   // STORED: ... contain All Sentences, also the untranslated
22
final List<Sentence> allSentencesVector = new Vector<Sentence>();
23
24   // contain sentences, key= english, value= translation in the language
25
// this is used at SnowMail runtime for quick translation access
26
final Map<String JavaDoc, String JavaDoc> sentences = new Hashtable<String JavaDoc, String JavaDoc>(); // quick access
27

28   // the language
29
private String JavaDoc language;
30
31   // true for dictionaries read from writable file
32
// (i.e. false for dictionaries embedded in jar files and readonly external files)
33
// is set during read from file...
34
private boolean isEditable = true;
35
36   public SentenceDictionary(String JavaDoc language)
37   {
38      this.language = language;
39   }
40
41
42   public String JavaDoc getLanguage() {return language;}
43   public List<Sentence> getAllSentences()
44   {
45     return allSentencesVector;
46   }
47
48   public boolean getIsEditable() { return isEditable; }
49
50
51   /** try to translate the sentence.
52       serch first for an exact mactch and then for a match
53       ignoring spaces at the beginning and at the end.
54
55       If not found, add a request and return the original english sentence
56   */

57   public String JavaDoc getTranslatedSentence(String JavaDoc sentence)
58   {
59      String JavaDoc transl = sentences.get(sentence);
60      if(transl!=null)
61      {
62         return transl;
63      }
64      else
65      {
66         // not found... we try to find a similar string, just ignoring spaces
67
String JavaDoc try2 = getTranslationIgnoringSpaces(sentence);
68         if(try2!=null)
69         {
70           // found a version without spaces !
71
return try2;
72         }
73
74         // the sentence is not translated yet, we must request his translation
75
Sentence sent = new Sentence(sentence, "Occured at runtime",-1, -1);
76         allSentencesVector.add(sent);
77
78         // return the original sentence
79
return sentence;
80      }
81   }
82
83   public boolean hasTranslatedSentence(String JavaDoc sentence)
84   {
85      String JavaDoc transl = sentences.get(sentence);
86      if(transl!=null)
87      {
88         return true;
89      }
90      else
91      {
92         // not found... we try to find a similar string, just ignoring spaces
93
String JavaDoc try2 = getTranslationIgnoringSpaces(sentence);
94         if(try2!=null) return true;
95
96         // the sentence is not translated yet, we must request his translation
97
return false;
98      }
99   }
100
101   /** @return null if not found.
102       try to search for the string, ignoring spaces.
103       The same number of spaces are then added at begining and end of the answer
104   */

105   private String JavaDoc getTranslationIgnoringSpaces(String JavaDoc sentence)
106   {
107         // not found... we try to find a similar string, just ignoring spaces
108
String JavaDoc trimmed = sentence.trim();
109
110         for(String JavaDoc elt : sentences.keySet())
111         {
112           if(elt.trim().equals(trimmed))
113           {
114              // found a trimmed match...
115
// => add the blanks (spaces, tabs, returns) before and after
116
int pos = sentence.indexOf(trimmed);
117              StringBuffer JavaDoc reply = new StringBuffer JavaDoc();
118              reply.append( sentence.substring(0,pos) );
119              reply.append( sentences.get(elt).trim());
120              reply.append( sentence.substring(pos+trimmed.length()) );
121              return reply.toString();
122           }
123         }
124
125         return null;
126   }
127
128
129   /** this add the new sentences parsed in source and
130       remove the old ones (no more used)
131   */

132   public void updateSentencesFromSource(Vector<Sentence> allEnglishSentencesFoundInSource)
133   {
134      System.out.println("Update dictionary with sentences parsed from source for language "+getLanguage());
135
136      // 1. add all english sentences parsed
137
for(int p=0; p<allEnglishSentencesFoundInSource.size(); p++)
138      {
139         Sentence sent = allEnglishSentencesFoundInSource.elementAt(p);
140         //if(sent.getSentence().equals("Yes")) System.out.println("* 1 *");
141
// verify (or add) and mark as present
142
verifyIfSentenceIsPresent(sent);
143      }
144
145      // 2. remove the sentences that have not been found
146
int removed = 0;
147      List<Sentence> toRemove = new Vector<Sentence>();
148      for(int i=0; i<allSentencesVector.size(); i++)
149      {
150         Sentence s = allSentencesVector.get(i);
151         if(s.visited==false)
152         {
153            removed++;
154            toRemove.add(s);
155         }
156      }
157
158      for(int i=0; i<toRemove.size();i++)
159      {
160          Sentence s = toRemove.get(i);
161          sentences.remove(s.getSentence());
162          allSentencesVector.remove(s);
163      }
164
165      fireChangeEvent();
166
167      System.out.println("" +removed +" removed unused sentences");
168   }
169
170
171   public String JavaDoc getStringOfAllTranslatedSentences()
172   {
173      StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
174      for(int i=0; i<allSentencesVector.size();i++)
175      {
176        Sentence s = allSentencesVector.get(i);
177        if(s.hasTranslation())
178        {
179           sb.append("\n\n"+s.getTranslation());
180        }
181      }
182      return sb.toString();
183   }
184
185   /** look if a sentence is present. if not found, add it to the translation requests
186   */

187   private void verifyIfSentenceIsPresent(Sentence sent)
188   {
189      String JavaDoc sentence = sent.getSentence();
190
191      int pos = positionInAllSentenceVector(sentence);
192      if(pos==-1)
193      {
194         //if(sent.getSentence().equals("Yes")) System.out.println("* 2 *");
195
allSentencesVector.add(sent);
196         sent.visited = true;
197      }
198      else
199      {
200         //if(sent.getSentence().equals("Yes")) System.out.println("* 2b *");
201
Sentence s = allSentencesVector.get(pos);
202         s.visited = true;
203         s.setLocationClass(sent.getLocationClass(), sent.getLinePosition());
204      }
205
206      /*
207      // 1. look if the translation is available
208      String transl = (String) sentences.get(sentence);
209      if(transl==null)
210      {
211        // 2. the translation is not present => look for a trimmed version
212        String reply = getTranslationIgnoringSpaces(sentence);
213        if(reply==null {
214          // 3. the sentence is really not present => add it
215          allSentencesVector.addElement(sent);
216          // mark it as visited
217          sent.visited = true;
218          fireChangeEvent();
219        }
220      }
221
222      // 2. mark the sentence as used
223      int pos = positionInAllSentenceVector(sentence);
224      if(pos==-1)
225      {
226         System.out.println("Problem: sentence not in all vector "+sentence);
227      }
228      else
229      {
230         Sentence s = (Sentence) allSentencesVector.elementAt(pos);
231         s.visited = true;
232      } */

233   }
234
235   /** @return the position of sent in allSentencesVector,
236      using the trimmed strings
237   */

238   private int positionInAllSentenceVector(String JavaDoc sent)
239   {
240     String JavaDoc st = sent.trim();
241
242     for(int i=0; i<allSentencesVector.size(); i++)
243     {
244       Sentence s = allSentencesVector.get(i);
245       if(s.getSentence().trim().equals(st)) return i;
246     }
247     return -1;
248   }
249
250
251
252   public int getNumberOfTranslatedSentences() { return sentences.size(); }
253   public int getNumberOfSentences() { return allSentencesVector.size(); }
254
255
256
257   /** add a translation
258   */

259   public void addTranslation(String JavaDoc english, String JavaDoc translation)
260   {
261      // add /or update
262
sentences.put(english, translation);
263      int pos= this.positionInAllSentenceVector(english);
264      if(pos==-1)
265      {
266         Sentence se = new Sentence(english, "Added during runtime", 0, -1);
267         se.setTranslation(translation);
268
269         allSentencesVector.add(se);
270         System.out.println("### sentence not in dic "+english);
271      }
272      else
273      {
274         Sentence se = allSentencesVector.get(pos);
275         se.setTranslation(translation);
276      }
277
278      // inform listeners
279
fireChangeEvent();
280   }
281
282   // Listeners
283
//
284
List<ChangeListener> changeListeners = new Vector<ChangeListener>();
285   public void addChangeListener(ChangeListener cl)
286   {
287     changeListeners.add(cl);
288   }
289   public void removeChangeListener(ChangeListener cl)
290   {
291     changeListeners.remove(cl);
292   }
293   private void fireChangeEvent()
294   {
295     for(ChangeListener cl: changeListeners)
296     {
297        cl.stateChanged(new ChangeEvent(this));
298     }
299   }
300
301
302   // Vectorizable
303
//
304
public SentenceDictionary()
305   {
306   }
307
308   public Vector<Object JavaDoc> getVectorRepresentation()
309   {
310      Vector<Object JavaDoc> v = new Vector<Object JavaDoc>();
311      v.addElement(4); // 0. version
312
v.addElement(language); // 1. language
313
Vector<Object JavaDoc> sentv = new Vector<Object JavaDoc>();
314      v.addElement(sentv); // 2. sentences
315
for(Sentence s : allSentencesVector)
316      {
317        sentv.addElement(s.getVectorRepresentation());
318      }
319
320      return v;
321   }
322
323 @SuppressWarnings JavaDoc("unchecked")
324   public void createFromVectorRepresentation( List<Object JavaDoc> v )
325   {
326     int version = (Integer JavaDoc) v.get(0);
327
328     if(version==4)
329     {
330       language = (String JavaDoc) v.get(1);
331       sentences.clear();
332       Vector<Vector> sentv = (Vector<Vector>)v.get(2);
333       for(Vector sv: sentv)
334       {
335          Sentence s = new Sentence();
336          s.createFromVectorRepresentation(sv);
337          allSentencesVector.add(s);
338          if(!s.getTranslation().equals(""))
339          {
340             sentences.put(s.getSentence(), s.getTranslation());
341          }
342
343          // sentences.put(key, val);
344
}
345     }
346
347     else
348     {
349        throw new RuntimeException JavaDoc("Bad version "+version);
350     }
351   }
352
353
354
355   public void saveToFile() throws Exception JavaDoc
356   {
357      // no english
358
if(language.compareToIgnoreCase("english")==0) return;
359
360      if(!isEditable) return;
361
362      File file = new File("SnowMailClient/Language/"+language+".translation");
363      if(file.exists() && !file.canWrite())
364      {
365         throw new Exception JavaDoc("Language file "+language+" is read-only");
366      }
367
368      System.out.println("saving dic "+file.getAbsolutePath());
369      FileUtils.saveVectorToFile(file, this.getVectorRepresentation());
370   }
371
372
373
374
375   /** look in the schmortopf jar file if french and deutsch are
376       available
377   */

378   public static String JavaDoc[] getInternalAvailableTranslations()
379   {
380      String JavaDoc[] internalLanguagesToLookFor = new String JavaDoc[]
381      {
382         Language.FRANCAIS, Language.DEUTSCH, Language.POLISH
383      };
384      ClassLoader JavaDoc cl = SentenceDictionary.class.getClassLoader();
385      if(cl==null)
386      {
387        return new String JavaDoc[0];
388      }
389
390      Vector<String JavaDoc> foundLanguagesNames = new Vector<String JavaDoc>();
391      for(int i=0; i<internalLanguagesToLookFor.length; i++)
392      {
393        InputStream is = null;
394        try
395        {
396           is = cl.getResourceAsStream("SnowMailClient/Language/"
397                          + internalLanguagesToLookFor[i] + ".translation");
398           if(is!=null)
399           {
400              foundLanguagesNames.addElement(internalLanguagesToLookFor[i]);
401              //System.out.println("Found language embedded in jar: "+languagesToLookFor[i]);
402
}
403        }
404        finally
405        {
406           if(is!=null)
407           {
408             try{is.close();} catch(Exception JavaDoc e){ e.printStackTrace();}
409           }
410        }
411      }
412      return foundLanguagesNames.toArray(new String JavaDoc[foundLanguagesNames.size()]);
413   }
414
415
416
417   public static SentenceDictionary readFromJarFile(String JavaDoc language) throws Exception JavaDoc
418   {
419      // no english
420
if(language.compareToIgnoreCase("english")==0) return null;
421
422
423      ClassLoader JavaDoc cl = SentenceDictionary.class.getClassLoader();
424      if(cl==null)
425      {
426        System.out.println("Class loader is null for class SentenceDictionary.");
427        // not found, maybe IDE mode ? not running from jar file
428
return null;
429      }
430
431      SentenceDictionary sd = new SentenceDictionary(language);
432
433      InputStream is = null;
434      try
435      {
436         is = cl.getResourceAsStream("SnowMailClient/Language/"+language+".translation");
437         if(is==null)
438         {
439            System.out.println("ResourceAsStream is null for SnowMailClient/Language/"+language);
440            return null;
441         }
442         Vector<Object JavaDoc> content = FileUtils.loadVector(is);
443         sd.createFromVectorRepresentation(content);
444         // read-only
445
sd.isEditable = false;
446
447         return sd;
448      }
449      finally
450      {
451         if(is!=null)
452         {
453           try{is.close();} catch(Exception JavaDoc e){ e.printStackTrace();}
454         }
455      }
456
457   }
458
459
460   public static SentenceDictionary ReadFromFile_ZIPPED(File file) throws Exception JavaDoc
461   {
462
463      SentenceDictionary sd = new SentenceDictionary("???");
464
465      // the read-only case
466
if(!file.canWrite()) sd.isEditable = false;
467
468      List<Object JavaDoc> rep = FileUtils.loadZippedVectorFromFile(file);
469      if(rep.size()==0) throw new Exception JavaDoc("File "+file.getAbsolutePath()+" is empty");
470      try
471      {
472        sd.createFromVectorRepresentation(rep);
473      }
474      catch(Exception JavaDoc e)
475      {
476        throw e;
477      }
478
479      return sd;
480   }
481
482   /** try to read the dictionary from file.
483       if not successful, try to use an internal embedded dictionary.
484   */

485   public static SentenceDictionary readFromFile(String JavaDoc language, boolean useEmbeddedIfFileNotFound) throws Exception JavaDoc
486   {
487      // no english
488
if(language.compareToIgnoreCase("english")==0) return null;
489
490
491      File file = new File("SnowMailClient/Language/"+language+".translation");
492
493      System.out.println("loading dic "+file.getAbsolutePath());
494
495      SentenceDictionary sd = new SentenceDictionary(language);
496
497      if(file.exists())
498      {
499        // the read-only case
500
if(!file.canWrite()) sd.isEditable = false;
501
502        Vector<Object JavaDoc> rep = FileUtils.loadVectorFromFile(file);
503        try
504        {
505          sd.createFromVectorRepresentation(rep);
506        }
507        catch(Exception JavaDoc e)
508        {
509          throw e;
510        }
511        //System.out.println(" Number of sentences = "+sd.getNumberOfTranslatedSentences());
512
//System.out.println(" translation requests "+sd.getRowCount());
513
}
514      else
515      {
516        System.out.println("Language not found in file "+file.getAbsolutePath());
517        // no file found, try to read from jar file
518
if(useEmbeddedIfFileNotFound)
519        {
520          SentenceDictionary sdj = readFromJarFile(language);
521          if(sdj!=null)
522          {
523            return sdj;
524          }
525          else
526          {
527
528          }
529        }
530      }
531      return sd;
532   }
533
534   public String JavaDoc getFileName()
535   {
536     File file = new File("SnowMailClient/Language/"+language+".translation");
537     return file.getAbsolutePath();
538   }
539
540
541
542 } // SentenceDictionary
Popular Tags