【问题标题】:split text with inextricably phrases用千篇一律的短语分割文本
【发布时间】:2014-09-24 11:13:46
【问题描述】:

我有一个列表,其中包含一些不可分割的单词,例如

List<String> lookUp = new ArrayList<>();
lookUp.add("New York");
lookUp.add("Big Apple");

对于一个句子,我想将它拆分成单词,但不要拆分我列表中给出的密不可分的单词。举个例子

String sentence = "New York is also called Big Apple";

它应该还给我

["New York", "is", "also", "called", "Big Apple"]

我开始编写一个算法,首先用空格分割句子,然后我做一个循环:对于每个单词,我检查这个单词和它的右邻是否出现在查找列表中,如果是,则一起解析这些单词.

1) 想象一下,我的查找列表还包含包含两个以上单词的密不可分的短语,例如“George W. Bush”-> 我的算法只会查找“George W”。和“W. Bush”,并且不会在查找列表中找到它,因此会将其拆分为 3 个单词。

2) 更重要的问题(您可以忽略问题 1):是否已经有库甚至 GATE 插件(这样我就不必重新发明轮子了)?德语短语也存在这种情况吗?没找到=(

【问题讨论】:

  • 这是一个微不足道的问题,所以我相信:没有任何特殊的库。
  • 如果你得到“a b c”并且在你的查找中有“a b”和“b c”怎么办?
  • 替代方法:1) 按lookUp 条目拆分,2) 迭代,查看是否是查找词,3) 如果是,则继续,4) 如果不是,则在空格处拆分.
  • 您能否详细说明您的第 1 步?
  • @aioobe: 好点,我想我更愿意收到 ["a b", "c", "a", "b c"] @jensgram: "1) 按查找条目拆分"你意思是: sentence.split(lookUp.get(i)) ?!或者只是 for(phrase:lookUp){检查句子是否包含短语}

标签: java nlp gate


【解决方案1】:

Java 7 上的另一个实现,它不使用 regular expressions

    List<String> lookUp = new ArrayList<>();
    lookUp.add("New York");
    lookUp.add("New Jersey");
    lookUp.add("Big Apple");
    lookUp.add("George W. Bush");

    String sentence = "New York is also called Big Apple . New Jersey is located near to New York . George W. Bush doesn't live in New Mexico`";

    String currentPhrase = "";
    List<String> parseResult = new ArrayList<>();

    for (String word : sentence.split("\\s+")) {
        currentPhrase += (currentPhrase.isEmpty() ? "" : " ") + word;
        if (lookUp.contains(currentPhrase)) {
            parseResult.add(currentPhrase);
            currentPhrase = "";
            continue;
        }
        boolean phraseFound = false;
        for (String look : lookUp)
            if (look.startsWith(currentPhrase)) {
                phraseFound = true;
                break;
            }

        if (!phraseFound) {
            parseResult.addAll(Arrays.asList(currentPhrase.split("\\s+")));
            currentPhrase = "";
        } 
    }

    System.out.println(parseResult);

输出是:

[New York, is, also, called, Big Apple, ., New Jersey, is, located, near, to, New York, ., George W. Bush, doesn't, live, in, New, Mexico]

【讨论】:

  • 有效!现在想象一下,你有“乔治 W”。 “George W. Bush”在您的查找列表中:它应该被解析为“George W. Bush”(不是:“George W”、“Bush”)....但这很远超出我的问题:D
  • 我玩了一下。当我的查找列表包含很多项目时,它会失败。具体来说:如果您在查找中添加 100 个以上的短语,那么它将逐字拆分句子(是的,该句子包含来自查找的短语!)我对此感到困惑 =/
  • 忘记我的最后评论,这是我的错误:P
  • 解决我的第一条评论的小方法:使用String[] splittedSentence = sentence.split("\\s+"); 并使用i 进行循环。在if (lookUp.contains(currentPhrase)) 之后插入if(i&lt;splittedSentence.length-1 &amp;&amp; lookUp.contains(currentPhrase+" "+splittedSentence[i+1])){continue;}
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-14
  • 1970-01-01
  • 2020-09-12
  • 2014-04-06
相关资源
最近更新 更多