gpt4 book ai didi

java - 使用 Java 处理单词缩略的有效方法是什么?

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:25:56 25 4
gpt4 key购买 nike

我在文件中有一个单词列表。它们可能包含 who's、didn't 等词。因此,在阅读它时,我需要使它们像“who is”和“did not”一样正确。这必须在 Java 中完成。我需要在不浪费太多时间的情况下完成这项工作。

这实际上是为了在使用 solr 的搜索过程中处理此类查询。

下面是我尝试使用 HashMap 的示例代码

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]);
}

最佳答案

如果您担心包含“谁”的查询查找包含“谁”的文档,那么您应该考虑使用专为此目的而设计的词干分析器。

您可以轻松地添加一个词干分析器购买,将其配置为您的 solr 配置中的过滤器。参见 http://wiki.apache.org/solr/AnalyzersTokenizersTokenFilters

编辑:
SnowballPorterFilterFactory 可能会为您完成这项工作。

关于java - 使用 Java 处理单词缩略的有效方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5910820/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com