Stanford CoreNLP找到句子的同质部分

时间:2019-05-23 15:21:35

标签: java nlp stanford-nlp simplification

我正在尝试基于Stanford CoreNLP构建句子简化算法。我想做的一种简化-将句子的同质部分转换为几个句子。 例如

  

我爱我的妈妈,爸爸和妹妹。 ->我爱我妈妈。我爱我的爸爸。我爱我的妹妹。

首先,我为输入句子字符串建立语义图

    final Sentence parsed = new Sentence(sentence);
    final SemanticGraph dependencies = parsed.dependencyGraph();

这句话的依存关系图是

-> love/VBP (root)
  -> I/PRP (nsubj)
  -> mom/NN (dobj)
    -> my/PRP$ (nmod:poss)
    -> ,/, (punct)
    -> dad/NN (conj:and)
    -> and/CC (cc)
    -> sister/NN (conj:and)
  -> dad/NN (dobj)
  -> sister/NN (dobj)

然后我在图中发现了dobj条边和nsubj

for (SemanticGraphEdge edge : dependencies.edgeListSorted()) {
        if (edge.getRelation().getShortName().startsWith("dobj")) {
            modifiers.add(edge);
        } else if (edge.getRelation().getShortName().startsWith("nsubj")) {
            subj = edge;
        }
    }

所以现在我在modifiersnsubj中有I字的三个边。现在我的问题是如何将语义图分成3个单独的图。 当然,幼稚的解决方案只是在dobj的边缘建立基于subj和调控器/从属句的句子,但是我知道这是一个坏主意,不适用于更复杂的示例。

for (final SemanticGraphEdge edge : modifiers) {
                SemanticGraph semanticGraph = dependencies.makeSoftCopy();
                final IndexedWord governor = edge.getGovernor();
                final IndexedWord dependent = edge.getDependent();

                final String governorTag = governor.backingLabel().tag().toLowerCase();
                if (governorTag.startsWith("vb")) {
                    StringBuilder b = new StringBuilder(subj.getDependent().word());
                    b.append(" ")
                            .append(governor.word())
                            .append(" ")
                            .append(dependent.word())
                            .append(". ");
                    System.out.println(b);

                }
            }

有人可以给我一些建议吗?也许我错过了coreNLP文档中有用的东西? 谢谢。

1 个答案:

答案 0 :(得分:0)

感谢@JosepValls这个好主意。 在这里,一些代码示例了如何简化3个或更多同构单词的句子。

首先,我为案例定义了几个正则表达式

jj(optional) nn, jj(optional) nn, jj(optional) nn and jj(optional) nn
jj(optional) nn, jj(optional) nn, jj(optional) nn , jj(optional) nn ...
jj , jj , jj
jj , jj and jj
vb nn(optional) , vb nn(optional) , vb nn(optional)
 and  so on

正则表达式为

Pattern nounAdjPattern = Pattern.compile("(((jj)\\s(nn)|(jj)|(nn))\\s((cc)|,)\\s){2,}((jj)\\s(nn)|(jj)|(nn))");
Pattern verbPatter = Pattern.compile("((vb\\snn|vb)\\s((cc)|,)\\s){2,}((vb\\snn)|vb)");

这些模式将用于定义输入句子是否具有同质单词列表并查找边界。之后,我根据原始句子中的单词创建POS列表

final Sentence parsed = new Sentence(sentence);
final List<String> words = parsed.words();
List<String> pos = parsed.posTags().stream()
        .map(tag -> tag.length() < 2 ? tag.toLowerCase() : tag.substring(0, 2).toLowerCase())
        .collect(Collectors.toList());

要将此POS结构与正则表达式匹配-将concat列表转换为字符串

String posString = pos.stream().collect(Collectors.joining(" "));

如果句子与任何正则表达式都不匹配-让我们返回相同的字符串,否则让我们简化它。

if (!matcher.find()) {
    return new SimplificationResult(Collections.singleton(sentence));
}
return new SimplificationResult(simplify(posString, matcher, words));

在简化方法中,我正在寻找同质部分的边界,并从单词列表3的部分中提取内容-开始和结束(不变),同质部分将被分解为多个部分。在将同质部分分解为片段之后,我构建了一些简化的句子,例如begin + piece + end。

 private Set<String> simplify(String posString, Matcher matcher, List<String> words) {
        String startPOS = posString.substring(0, matcher.start());
        String endPPOS = posString.substring(matcher.end());
        int wordsBeforeCnt = StringUtils.isEmpty(startPOS) ? 0 : startPOS.trim().split("\\s+").length;
        int wordsAfterCnt = StringUtils.isEmpty(endPPOS) ? 0 : endPPOS.trim().split("\\s+").length;
        String wordsBefore = words.subList(0, wordsBeforeCnt)
                .stream()
                .collect(Collectors.joining(" "));
        String wordsAfter = words.subList(words.size() - wordsAfterCnt, words.size())
                .stream()
                .collect(Collectors.joining(" "));
        List<String> homogeneousPart = words.subList(wordsBeforeCnt, words.size() - wordsAfterCnt);
        Set<String> splitWords = new HashSet<>(Arrays.asList(",", "and"));
        Set<String> simplifiedSentences = new HashSet<>();
        StringBuilder sb = new StringBuilder(wordsBefore);
        for (int i = 0; i < homogeneousPart.size(); i++) {
            String part = homogeneousPart.get(i);
            if (!splitWords.contains(part)) {
                sb.append(" ").append(part);
                if (i == homogeneousPart.size() - 1) {
                    sb.append(" ").append(wordsAfter).append(" ");
                    simplifiedSentences.add(sb.toString());
                }
            } else {
                sb.append(" ").append(wordsAfter).append(" ");
                simplifiedSentences.add(sb.toString());
                sb = new StringBuilder(wordsBefore);
            }
        }
        return simplifiedSentences;

例如句子

 I love and kiss and adore my beautiful mom, clever dad and sister.
如果我们在上面使用2个正则表达式,

将简化为9个句子

I adore my clever dad . 
I love my clever dad . 
I love my sister . 
I kiss my sister . 
I kiss my clever dad . 
I adore my sister . 
I love my beautiful mom . 
I adore my beautiful mom . 
I kiss my beautiful mom . 

这些代码仅适用于3个或更多同构词,导致2个词存在很多异常。例如

Cat eats mouse, dog eats meat.

用这种方法不能简化句子。

相关问题