我在一个文件里有一个单词列表。它们可能包含像谁的,没有等。所以当我读它的时候,我需要使它们适当地像“谁是”和“没有”。这必须在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]);
}发布于 2011-05-06 19:38:37
如果您担心包含for的查询,例如"who's“查找包含for的文档,例如"who is”,那么您应该考虑使用Stemmer,它正是为此目的而设计的。
您可以很容易地添加一个词干分析器,并将其配置为solr配置中的过滤器。请参阅http://wiki.apache.org/solr/AnalyzersTokenizersTokenFilters
编辑:
SnowballPorterFilterFactory可能会为您完成这项工作。
发布于 2011-05-06 21:03:32
以下是@James Jithin的最后一句话:
因此,在我看来,实现这一点的最好方法是枚举少量常见和有效的收缩,而不考虑其余的。这也有一个好处,那就是您可以使用简单的字符串匹配而不是后缀匹配来实现它。
发布于 2011-05-06 19:46:14
代码可以写成
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 str = "where'd you're you'll would'nt hello";
for(String key : con.keySet()) {
str = str.replaceAll(key + "\\b" , con.get(key));
}用你拥有的逻辑。但是假设它的script's是一个表示占有的单词,把它改成script is会改变它的意思。
https://stackoverflow.com/questions/5910820
复制相似问题