我有一個文件中的單詞列表。它們可能包含諸如「誰」,「沒有」等詞。因此,當從中讀取時,我需要使它們適合「誰是誰」和「沒有」。這必須在Java中完成。我需要這樣做而不會浪費太多時間。什麼是使用Java處理單詞收縮的有效方法?
這實際上是在使用solr的搜索過程中處理這樣的查詢。
下面是一個示例代碼,我使用哈希表
Map<String, String> con = new HashMap<String, String>();
con.put("'s", " is");
con.put("'d", " would");
con.put("'re", " are");
con.put("'ll", " will");
con.put("n't", " not");
con.put("'nt", " not");
String temp = null;
String str = "where'd you're you'll would'nt hello";
String[] words = str.split(" ");
int index = -1 ;
for(int i = 0;i<words.length && (index =words[i].lastIndexOf('\''))>-1;i++){
temp = words[i].substring(index);
if(con.containsKey(temp)){
temp = con.get(temp);
}
words[i] = words[i].substring(0, index)+temp;
System.out.println(words[i]);
}
我喜歡'ain't' - >'不是';) – 2011-05-06 11:44:27
「他決定去」表明「的」可以是「他有」。另外,爲什麼要麻煩?你有一些想法,收縮是不恰當的嗎?你會怎麼做「不」或「不會」? – tchrist 2011-05-06 13:16:14
@tchrist是正確的,這是依賴於上下文的。你可以設計和算法考慮,但如果你想正確地做到這一點,你可能需要做全面的解析。這可能是不值得的,因爲無論如何,你從這個擴展中獲得的大部分內容都在停止列表中。 – 2011-05-06 14:19:32