diff software/mpdl-services/mpiwg-mpdl-lt/src/de/mpg/mpiwg/berlin/mpdl/lt/dict/db/LexHandler.java @ 23:e845310098ba

diverse Korrekturen
author Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
date Tue, 27 Nov 2012 12:35:19 +0100
parents 7d6d969b10cf
children
line wrap: on
line diff
--- a/software/mpdl-services/mpiwg-mpdl-lt/src/de/mpg/mpiwg/berlin/mpdl/lt/dict/db/LexHandler.java	Wed Dec 14 13:57:09 2011 +0100
+++ b/software/mpdl-services/mpiwg-mpdl-lt/src/de/mpg/mpiwg/berlin/mpdl/lt/dict/db/LexHandler.java	Tue Nov 27 12:35:19 2012 +0100
@@ -49,7 +49,9 @@
       dbEnvLexica.closeDatabase(lexiconName);
     }
     dbEnvLexica.close();
-    LOGGER.info("Lexicon db cache: closed");
+    LOGGER.info("Lexicon cache: db closed");
+    dbEnvLexica = null;
+    instance = null;
   }
 
   /** 
@@ -60,7 +62,7 @@
    * @return lemmas
    * @throws ApplicationException
    */
-  public ArrayList<Lemma> getLemmas(String query, String type, String language, int normMode) throws ApplicationException {
+  public ArrayList<Lemma> getLemmas(String query, String type, String language, int normMode, boolean atLeastOneLemmaWithWordForm) throws ApplicationException {
     ArrayList<Lemma> lexLemmas = new ArrayList<Lemma>();
     // get lemmas of all forms in query
     MorphologyCache morphologyCache = MorphologyCache.getInstance();
@@ -79,8 +81,10 @@
       if (lemmas != null && ! lemmas.isEmpty()) {
         lexLemmas.addAll(lemmas);
       } else {
-        Lemma l = new Lemma("created dynamically cause no lemma is available", language, queryForm); // at least the word form is added for finding it in the lexicon
-        lexLemmas.add(l);
+        if (atLeastOneLemmaWithWordForm) {
+          Lemma l = new Lemma("created dynamically cause no lemma is available", language, queryForm); // at least the word form is added for finding it in the lexicon
+          lexLemmas.add(l);
+        }
       }
     }
     Collections.sort(lexLemmas);
@@ -170,12 +174,12 @@
     boolean hasLexEntry = false;
     if (language.equals("zh"))   // each chinese character always has a lexicon entry
       return true;
-    ArrayList<Lexicon> statLexicons = Lexica.getInstance().getLocalLexicons(language);
-    if (statLexicons != null) {
-      for (int i=0; i<statLexicons.size(); i++) {
-        Lexicon lexicon = statLexicons.get(i).clone(); // clone without lexicon entries
-        LexiconEntry lexEntry = readEntry(lexicon.getName(), formName);
-        if (lexEntry != null) {
+    String[] lexiconNames = Lexica.getInstance().getLocalLexiconNames(language);
+    if (lexiconNames != null) {
+      for (int i=0; i<lexiconNames.length; i++) {
+        String lexiconName = lexiconNames[i];
+        hasLexEntry = hasKey(lexiconName, formName);
+        if (hasLexEntry) {
           return true;
         }
       }
@@ -241,6 +245,28 @@
     return lexEntry;
   }
   
+  private boolean hasKey(String lexiconName, String formName) throws ApplicationException {
+    boolean hasKey = false;
+    try {
+      String keyStr = formName;
+      DatabaseEntry dbEntryKey = new DatabaseEntry(keyStr.getBytes("utf-8"));
+      Database lexDB = dbEnvLexica.getLexiconDB(lexiconName);
+      Cursor cursor = lexDB.openCursor(null, null);
+      DatabaseEntry foundValue = new DatabaseEntry();
+      foundValue.setPartial(0, 0, true);  // more performance: the value is not fetched: only the key is fetched
+      OperationStatus operationStatus = cursor.getSearchKey(dbEntryKey, foundValue, LockMode.DEFAULT);
+      if (operationStatus == OperationStatus.SUCCESS) {
+        hasKey = true;
+      }
+      cursor.close();
+    } catch (DatabaseException e) {
+      throw new ApplicationException(e);
+    } catch (UnsupportedEncodingException e) {
+      throw new ApplicationException(e);
+    }
+    return hasKey;
+  }
+
   private LexiconEntry readEntry(String lexiconName, String formName) throws ApplicationException {
     LexiconEntry retLexEntry = null;
     try {
@@ -337,7 +363,7 @@
       String lexiconName = lexicon.getName();
       dbEnvLexica.openDatabase(lexiconName);
     }
-    LOGGER.info("Lexicon db cache: opened");
+    LOGGER.info("Lexicon cache: db opened read only");
   }
   
   private void readSampleData() throws ApplicationException {