使用textacy / spacy的NLP总结

问题描述

我想从这段文字中用一个句子来生成摘要。我正在使用textacy.py。 这是我的代码:

import textacy
import textacy.keyterms
import textacy.extract
import spacy
nlp = spacy.load('en_core_web_sm')
text = '''Sauti said,'O thou that art blest with longevity,I shall narrate the history of Astika as I heard it from my father. 
          O Brahmana,in the golden age,Prajapati had two daughters. 
          O sinless one,the sisters were endowed with wonderful beauty. 
          Named Kadru and Vinata,they became the wives of Kasyapa. 
          Kasyapa derived great pleasure from his two wedded wives and being gratified he,resembling Prajapati himself,offered to give each of them a boon. 
          Hearing that their lord was willing to confer on them their choice blessings,those excellent ladies felt transports of joy. 
          Kadru wished to have for sons a thousand snakes all of equal splendour. 
          And Vinata wished to bring forth two sons surpassing the thousand offsprings of Kadru in strength,energy,size of body,and prowess. 
          Unto Kadru her lord gave that boon about a multitude of offspring. 
          And unto Vinata also,Kasyapa said,'Be it so!' Then Vinata,having; obtained her prayer,rejoiced greatly. 
          Obtaining two sons of superior prowess,she regarded her boon fulfilled. 
          Kadru also obtained her thousand sons of equal splendour. 
          'Bear the embryos carefully,' said Kasyapa,and then he went into the forest,leaving his two wives pleased with his blessings.'''

doc = textacy.make_spacy_doc(text,'en_core_web_sm')
sentobj = nlp(text)
sentences = textacy.extract.subject_verb_object_triples(sentobj)
summary=''
for i,x in enumerate(sentences):
    subject,verb,fact = x
    print('Fact ' + str(i+1) + ': ' + str(subject) + ' : ' + str(verb) + ' : ' + str(fact))
    summary += 'Fact ' + str(i+1) + ': ' + (str(fact))

Results are as follows:
    Fact 1: I : shall narrate : history
    Fact 2: I : heard : it
    Fact 3: they : became : wives
    Fact 4: Kasyapa : derived : pleasure
    Fact 5: ladies : felt : transports
    Fact 6: Kadru : wished : have
    Fact 7: Vinata : wished : to bring
    Fact 8: lord : gave : boon
    Fact 9: Kasyapa : said : Be
    Fact 10: Vinata : obtained : prayer
    Fact 11: she : regarded : boon
    Fact 12: Kadru : obtained : sons

我尝试过

textacy.extract.words
textacy.extract.entities
textacy.extract.ngrams
textacy.extract.noun_chunks
textacy.ke.textrank

一切正常,但结果并不完美。 我想要类似“ Kasyapa已婚的Kadru和Vinata姐妹”或“ Kasyapa拥护Kadru和Vinata”的东西。 您能建议我该怎么做吗?还是建议我使用其他替代软件包?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)