首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用simplenlg组合两个句子

如何使用simplenlg组合两个句子
EN

Stack Overflow用户
提问于 2017-09-28 00:58:39
回答 1查看 958关注 0票数 1

给出一组像"John有一只猫“和"John有一只狗”这样的句子,就会产生像"John有一只猫和一只狗“这样的句子。

我是否可以使用simplenlg来创建相同的。

EN

回答 1

Stack Overflow用户

发布于 2018-09-25 19:30:09

您询问的任务称为自然语言生成中的聚合( aggregation in Natural Language Generation,NLG)。虽然SimpleNLG确实通过其实现引擎支持聚合,但它不会直接聚合示例中的两个字符串。

但是,可以使用语法解析器和SimpleNLG来执行此任务。我将首先解释如何使用SimpleNLG语法生成目标句子:

代码语言:javascript
复制
import simplenlg.framework.*;
import simplenlg.lexicon.*;
import simplenlg.realiser.english.*;
import simplenlg.phrasespec.*;
import simplenlg.features.*;

public class TestMain {

  public static void main(String[] args) throws Exception {
    Lexicon lexicon = Lexicon.getDefaultLexicon();
    NLGFactory nlgFactory = new NLGFactory(lexicon);
    Realiser realiser = new Realiser(lexicon);

    // Create the SPhraseSpec object (sentence phrase).
    SPhraseSpec p = nlgFactory.createClause();

    // Create a noun phrase and set it as the subject of your sentence
    NPPhraseSpec john = nlgFactory.createNounPhrase("John");
    p.setSubject(john);

    // Create a verb phrase and set it as the verb of your sentence
    VPPhraseSpec have = nlgFactory.createVerbPhrase("have");
    // Note that the verb is "have" not "has".  Have is the base lemma.
    // The morphology of this will be handled based on the tense you set (see below)
    p.setVerb(have);

    // Create a determiner 'a'
    NPPhraseSpec a = nlgFactory.createNounPhrase("a");

    // Create two more noun phrases

    // One for dog
    NPPhraseSpec cat = nlgFactory.createNounPhrase("cat");
    // set the determiner
    cat.setDeterminer(a);;

    // And one for cat.
    NPPhraseSpec dog = nlgFactory.createNounPhrase("dog");
    // set the determiner
    dog.setDeterminer(a);

    // Create a coordinated phrase
    // This tells SimpleNLG that these objects are a collection which should be aggregated
    CoordinatedPhraseElement coord = nlgFactory.createCoordinatedPhrase(cat, dog);

    // Set the coordinated phrase as the object of your sentence
    p.setObject(coord);

    // Print it - 
    String output = realiser.realiseSentence(p);
    System.out.println(output);
    // => John has a cat and a dog.

    // Now lets see what SimpleNLG can do!

    // Change the tense to past (present was the default)
    p.setTense(Tense.PAST);
    output = realiser.realiseSentence(p);
    System.out.println(output);
    // => John had a cat and a dog.

    // Change the tense to future
    p.setTense(Tense.FUTURE);
    output = realiser.realiseSentence(p);
    System.out.println(output);
    // => John will will have a cat and a dog.
  }
}

这就是你在SimpleNLG realiser中使用语言的方式。但是,它并没有回答您直接聚合两个字符串的问题。可能还有其他方法,但我的第一个想法是使用语法分析,如StanfordNLPspaCy

我在自己的工作中使用spaCy (这是一个python库)。我将展示一个简短的例子来说明我在这里的意思。

代码语言:javascript
复制
import spacy

nlp = spacy.load('en_core_web_sm')
doc = nlp(u'John has a cat')

for token in doc:
    print(token.text, token.lemma_, token.pos_, token.tag_, token.dep_,
          token.shape_, token.is_alpha, token.is_stop)

这将输出以下内容:

代码语言:javascript
复制
John john PROPN NNP nsubj Xxxx True False
has have VERB VBZ ROOT xxx True True
a a DET DT det x True True
cat cat NOUN NN dobj xxx True False

从输出中可以看到,句子中的每个标记都被标记为名词、动词、限定词等。您可以使用此信息来格式化SimpleNLG的输入,然后聚合句子。我建议用SimpleNLG提供的XMLRealiser要比用Java语言编写语法要好。它接受XML作为输入。

NLP/NLG工作不是微不足道的。语言是非常复杂的。以上只是实现这一任务的一种方式。可能存在仅基于字符串聚合的工具,但SimpleNLG只是一个表面实现器,因此您必须以如上所示的适当格式将其与输入数据一起呈现。

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

https://stackoverflow.com/questions/46453388

复制
相关文章

相似问题

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