1 package org.apache.lucene.analysis.nl; 2 3 18 19 import java.io.File ; 20 import java.io.FileReader ; 21 import java.io.IOException ; 22 import java.io.LineNumberReader ; 23 import java.util.HashMap ; 24 25 32 public class WordlistLoader { 33 37 public static HashMap getWordtable(String path, String wordfile) { 38 if (path == null || wordfile == null) { 39 return new HashMap (); 40 } 41 return getWordtable(new File (path, wordfile)); 42 } 43 44 47 public static HashMap getWordtable(String wordfile) { 48 if (wordfile == null) { 49 return new HashMap (); 50 } 51 return getWordtable(new File (wordfile)); 52 } 53 54 61 public static HashMap getStemDict(File wordstemfile) { 62 if (wordstemfile == null) { 63 return new HashMap (); 64 } 65 HashMap result = new HashMap (); 66 try { 67 LineNumberReader lnr = new LineNumberReader (new FileReader (wordstemfile)); 68 String line; 69 String [] wordstem; 70 while ((line = lnr.readLine()) != null) { 71 wordstem = line.split("\t", 2); 72 result.put(wordstem[0], wordstem[1]); 73 } 74 } catch (IOException e) { 75 } 76 return result; 77 } 78 79 82 public static HashMap getWordtable(File wordfile) { 83 if (wordfile == null) { 84 return new HashMap (); 85 } 86 HashMap result = null; 87 try { 88 LineNumberReader lnr = new LineNumberReader (new FileReader (wordfile)); 89 String word = null; 90 String [] stopwords = new String [100]; 91 int wordcount = 0; 92 while ((word = lnr.readLine()) != null) { 93 wordcount++; 94 if (wordcount == stopwords.length) { 95 String [] tmp = new String [stopwords.length + 50]; 96 System.arraycopy(stopwords, 0, tmp, 0, wordcount); 97 stopwords = tmp; 98 } 99 stopwords[wordcount - 1] = word; 100 } 101 result = makeWordTable(stopwords, wordcount); 102 } 103 catch (IOException e) { 105 result = new HashMap (); 106 } 107 return result; 108 } 109 110 116 private static HashMap makeWordTable(String [] words, int length) { 117 HashMap table = new HashMap (length); 118 for (int i = 0; i < length; i++) { 119 table.put(words[i], words[i]); 120 } 121 return table; 122 } 123 } | Popular Tags |