From a82afad92c0b14c3a27e722cbb6d0562ad074865 Mon Sep 17 00:00:00 2001 From: amontano Date: Sun, 6 Feb 2005 23:19:44 +0000 Subject: [PATCH] Updated methods to help with the dictionary clean up. --- .../thdl/tib/scanner/BitDictionarySource.java | 2 +- .../tib/scanner/ByteDictionarySource.java | 13 ++++ .../thdl/tib/scanner/DictionarySource.java | 5 +- .../tib/scanner/FileSyllableListTree.java | 63 ++++++++++++++----- .../thdl/tib/scanner/LocalTibetanScanner.java | 46 +++----------- .../org/thdl/util/SimplifiedLinkedList.java | 53 ++++++++++++++++ 6 files changed, 128 insertions(+), 54 deletions(-) diff --git a/source/org/thdl/tib/scanner/BitDictionarySource.java b/source/org/thdl/tib/scanner/BitDictionarySource.java index 7725d33..fc6f52c 100644 --- a/source/org/thdl/tib/scanner/BitDictionarySource.java +++ b/source/org/thdl/tib/scanner/BitDictionarySource.java @@ -49,7 +49,7 @@ public class BitDictionarySource extends DictionarySource } /** Returns an instance of DictionarySource with all dictionaries selected */ - public BitDictionarySource getAllDictionaries() + public static BitDictionarySource getAllDictionaries() { BitDictionarySource ds = new BitDictionarySource(); ds.setDicts(allDicts); diff --git a/source/org/thdl/tib/scanner/ByteDictionarySource.java b/source/org/thdl/tib/scanner/ByteDictionarySource.java index 94fc31d..a95efe0 100644 --- a/source/org/thdl/tib/scanner/ByteDictionarySource.java +++ b/source/org/thdl/tib/scanner/ByteDictionarySource.java @@ -113,6 +113,19 @@ public class ByteDictionarySource extends DictionarySource return dicts[i]; } + public int getDicts() + { + int i; + BitDictionarySource availableDicts = new BitDictionarySource(); + + if (dicts == null) return 0; + + for (i=0; i< dicts.length; i++) + availableDicts.add(dicts[i]); + + return availableDicts.getDicts(); + } + public void dubDef(int n) { BitDictionarySource newDicts[] = new BitDictionarySource[dicts.length+1]; diff --git a/source/org/thdl/tib/scanner/DictionarySource.java b/source/org/thdl/tib/scanner/DictionarySource.java index 613bbd0..fcc8251 100644 --- a/source/org/thdl/tib/scanner/DictionarySource.java +++ b/source/org/thdl/tib/scanner/DictionarySource.java @@ -64,5 +64,8 @@ public abstract class DictionarySource public abstract boolean hasBrothers(); /** Returns true if dict is a selected dictionary. */ - public abstract boolean contains(int dict); + public abstract boolean contains(int dict); + + /** Returns an array of bits representing the selected dictionaries. */ + public abstract int getDicts(); } \ No newline at end of file diff --git a/source/org/thdl/tib/scanner/FileSyllableListTree.java b/source/org/thdl/tib/scanner/FileSyllableListTree.java index 9ccd510..8b30f47 100644 --- a/source/org/thdl/tib/scanner/FileSyllableListTree.java +++ b/source/org/thdl/tib/scanner/FileSyllableListTree.java @@ -18,6 +18,7 @@ Contributor(s): ______________________________________. package org.thdl.tib.scanner; import java.io.*; +import org.thdl.util.*; /** Searches the words directly in a file; not the preferred implementation. The search is too slow! @@ -34,17 +35,17 @@ import java.io.*; public class FileSyllableListTree implements SyllableListTree { - private String sil; + protected String sil; private long def[]; - private long posLista; - private DictionarySource defSource; + protected long posLista; + protected DictionarySource defSource; public static BitDictionarySource defSourcesWanted; public static RandomAccessFile wordRaf=null; private static RandomAccessFile defRaf=null; public static int versionNumber; - /** Creates the root */ - public FileSyllableListTree(String archivo, int defSourcesWanted) throws Exception + /** Creates the root. */ + public FileSyllableListTree(String archivo) throws Exception { sil = null; def = null; @@ -52,15 +53,11 @@ public class FileSyllableListTree implements SyllableListTree this.openFiles(archivo); posLista = this.wordRaf.getFilePointer(); - - /* if versionNumber is 2 use BitDictionarySource - else use ByteDictionarySource. */ - this.defSourcesWanted.setDicts(defSourcesWanted); } /** Used to create each node (except the root) */ - public FileSyllableListTree(String sil, long []def, DictionarySource defSource, long posLista) + protected FileSyllableListTree(String sil, long []def, DictionarySource defSource, long posLista) { this.sil=sil; this.def=def; @@ -100,7 +97,12 @@ public class FileSyllableListTree implements SyllableListTree e.printStackTrace(); } } - + + /** Initiates all static variables, it is called by the constructor of the root + FileSyllableListTree (in the case of a pure file tree) or by the + constructor of CachedSyllableListTree in the case of the root being loaded + into memory. + */ public static void openFiles(String archivo, boolean backwardCompatible) throws Exception { long fileSize; @@ -162,12 +164,41 @@ public class FileSyllableListTree implements SyllableListTree } } - /* if versionNumber is 2 use BitDictionarySource else use - ByteDictionarySource. */ - defSourcesWanted = new BitDictionarySource(); + defSourcesWanted = BitDictionarySource.getAllDictionaries(); wordRaf.seek(pos); } + + public static String[] getDictionaryDescriptions(String archivo) + { + int n; + try + { + BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(archivo + ".dic"))); + SimplifiedLinkedList ll1 = new SimplifiedLinkedList(), ll2 = new SimplifiedLinkedList(); + String s; + while ((s=br.readLine())!=null) + { + n = s.indexOf(","); + if (n < 0) + { + ll1.addLast(null); + ll2.addLast(s); + } + else + { + ll1.addLast(s.substring(0,n).trim()); + ll2.addLast(s.substring(n+1).trim()); + } + } + DictionarySource.setTags(ll2.toStringArray()); + return ll1.toStringArray(); + } + catch (Exception e) + { + return null; + } + } public String getDef() { @@ -242,7 +273,7 @@ public class FileSyllableListTree implements SyllableListTree int i; - if (silStr==null) return null; + if (silStr==null || posLista==-1) return null; try { wordRaf.seek(posLista); @@ -276,5 +307,5 @@ public class FileSyllableListTree implements SyllableListTree { } return null; - } + } } diff --git a/source/org/thdl/tib/scanner/LocalTibetanScanner.java b/source/org/thdl/tib/scanner/LocalTibetanScanner.java index bb5c1b8..4d57b4e 100644 --- a/source/org/thdl/tib/scanner/LocalTibetanScanner.java +++ b/source/org/thdl/tib/scanner/LocalTibetanScanner.java @@ -73,7 +73,7 @@ public class LocalTibetanScanner extends TibetanScanner private void scanSyllable(String sil) { SyllableListTree resultado=null; - Enumeration enum; + Enumeration enumeration; Word w; String silSinDec; boolean aadded; @@ -171,10 +171,10 @@ public class LocalTibetanScanner extends TibetanScanner wordList.addLast(w); this.resetAll(); - enum = floatingSil.elements(); + enumeration = floatingSil.elements(); floatingSil = new Vector(); - while (enum.hasMoreElements()) - scanSyllable((String)enum.nextElement()); + while (enumeration.hasMoreElements()) + scanSyllable((String)enumeration.nextElement()); scanSyllable(sil); } @@ -199,7 +199,7 @@ public class LocalTibetanScanner extends TibetanScanner public void finishUp() { - Enumeration enum; + Enumeration enumeration; Word w; while (lastCompSil!=null) @@ -208,10 +208,10 @@ public class LocalTibetanScanner extends TibetanScanner wordList.addLast(w); this.resetAll(); - enum = floatingSil.elements(); + enumeration = floatingSil.elements(); floatingSil = new Vector(); - while (enum.hasMoreElements()) - scanSyllable((String)enum.nextElement()); + while (enumeration.hasMoreElements()) + scanSyllable((String)enumeration.nextElement()); } if (silActual!=null) @@ -379,40 +379,14 @@ outAHere: } } - /** Looks for .dic file, and returns the dictionary descriptions. Also updates the definitionTags in the Definitions class. */ public String[] getDictionaryDescriptions() { - int n; - try - { - BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(archivo + ".dic"))); - SimplifiedLinkedList ll1 = new SimplifiedLinkedList(), ll2 = new SimplifiedLinkedList(); - String s; - while ((s=br.readLine())!=null) - { - n = s.indexOf(","); - if (n < 0) - { - ll1.addLast(null); - ll2.addLast(s); - } - else - { - ll1.addLast(s.substring(0,n).trim()); - ll2.addLast(s.substring(n+1).trim()); - } - } - DictionarySource.setTags(ll2.toStringArray()); - return ll1.toStringArray(); - } - catch (Exception e) - { - return null; - } + return FileSyllableListTree.getDictionaryDescriptions(archivo); } + public void destroy() { FileSyllableListTree.closeFiles(); diff --git a/source/org/thdl/util/SimplifiedLinkedList.java b/source/org/thdl/util/SimplifiedLinkedList.java index b471149..7a302c0 100644 --- a/source/org/thdl/util/SimplifiedLinkedList.java +++ b/source/org/thdl/util/SimplifiedLinkedList.java @@ -30,6 +30,7 @@ import java.util.*; public class SimplifiedLinkedList { protected Link cabeza; + public SimplifiedLinkedList() { cabeza=null; @@ -69,6 +70,58 @@ public class SimplifiedLinkedList temp.siguiente = currentLink; } } + + /** If the object is already in the list return. It is more efficient since it + assumes that the list is sorted. */ + public Object getSorted(Comparable o) + { + Link currentLink = cabeza; + Object currentObject; + int comparison; + + while(currentLink != null) + { + currentObject = currentLink.get(); + comparison = ((Comparable) currentObject).compareTo(o); + if (comparison==0) return currentObject; + else if(comparison>0) return null; + currentLink = currentLink.next(); + } + return null; + } + + /** Does not add repetitions. */ + public void addSortedUnique (Comparable o) + { + Link previous, currentLink, temp; + int comp; + + if (cabeza==null || o.compareTo(cabeza.get())<0) + { + addLast(o); + } + else + { + if (o.compareTo(cabeza.get())==0) return; + currentLink = cabeza; + while (true) + { + previous = currentLink; + currentLink = currentLink.next(); + if (currentLink != null) + { + comp = o.compareTo(currentLink.get()); + if (comp==0) return; + else if (comp<0) break; + } + else break; + } + + temp = new Link (o); + previous.siguiente = temp; + temp.siguiente = currentLink; + } + } public Object getLast() {