【发布时间】:2011-05-06 11:34:03
【问题描述】:
我有一个文件中的单词列表。它们可能包含诸如谁,没有等词。因此,在从中阅读时,我需要使它们正确,例如“谁是”和“没有”。这必须在 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->are not;) -
“他决定去”表明“的”可以是“他有”。还有,何苦呢?你知道宫缩在某种程度上是不恰当的吗?你打算如何处理“'tisn't”或“wouldn't've”?
-
@tchrist 是对的,这取决于上下文。您可以设计并考虑到算法,但是如果您想正确执行此操作,则可能必须进行完整解析。这可能不值得付出努力,因为无论如何你从这个扩展中得到的大部分东西都在停止名单上。
-
@larsmans 是的。在这方面付出那么多努力是不值得的。我担心的是我不想在索引中搜索“re”“ve”等没有意义的词
-
或者实际上,完全解析可能不是必需的,但需要一些 NLP 复杂性(结果仍然是矫枉过正)。