首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用Java处理单词缩写的有效方法是什么?

使用Java处理单词缩写的有效方法是什么?
EN

Stack Overflow用户
提问于 2011-05-06 19:34:04
回答 3查看 1.7K关注 0票数 3

我在一个文件里有一个单词列表。它们可能包含像谁的,没有等。所以当我读它的时候,我需要使它们适当地像“谁是”和“没有”。这必须在Java中完成。我需要在不浪费太多时间的情况下做到这一点。

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

下面是我尝试使用散列映射的示例代码

代码语言:javascript
运行
复制
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]);           
        }
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2011-05-06 19:38:37

如果您担心包含for的查询,例如"who's“查找包含for的文档,例如"who is”,那么您应该考虑使用Stemmer,它正是为此目的而设计的。

您可以很容易地添加一个词干分析器,并将其配置为solr配置中的过滤器。请参阅http://wiki.apache.org/solr/AnalyzersTokenizersTokenFilters

编辑:

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

票数 3
EN

Stack Overflow用户

发布于 2011-05-06 21:03:32

以下是@James Jithin的最后一句话:

  • ‘s ->“is”是“如果单词是所有格形式,则转换是不正确的。
  • ”“d”“->”“转换在古体形式中是不正确的,其中"'d”可以是“ed”的缩写。
  • "'nt“->”->“转换是不正确的,因为这实际上只是"n't”缩写的拼写错误。(我的意思是"wo'nt“是完全错误的……不是吗?)

因此,在我看来,实现这一点的最好方法是枚举少量常见和有效的收缩,而不考虑其余的。这也有一个好处,那就是您可以使用简单的字符串匹配而不是后缀匹配来实现它。

票数 1
EN

Stack Overflow用户

发布于 2011-05-06 19:46:14

代码可以写成

代码语言:javascript
运行
复制
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会改变它的意思。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5910820

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档