【问题标题】:Python NLTK Shakespeare corpusPython NLTK 莎士比亚语料库
【发布时间】:2017-09-28 23:46:40
【问题描述】:

我正在尝试从莎士比亚的 NLTK 语料库中导入句子——关注this 帮助站点——但我无法访问这些句子(为了训练 word2vec 模型):

from nltk.corpus import shakespeare #XMLCorpusreader
shakespeare.fileids()
['a_and_c.xml', 'dream.xml', 'hamlet.xml', 'j_caesar.xml', ...]

play = shakespeare.xml('dream.xml') #ElementTree object
print(play)
<Element 'PLAY' at ...>

for i in range(9):
    print('%s: %s' % (play[i].tag, play[i].text))

返回以下内容:

TITLE: A Midsummer Night's Dream
PERSONAE: 

SCNDESCR: SCENE  Athens, and a wood near it.
PLAYSUBT: A MIDSUMMER NIGHT'S DREAM
ACT: None
ACT: None
ACT: None
ACT: None
ACT: None

为什么所有的行为都没有?

这里定义的所有方法 (http://www.nltk.org/howto/corpus.html#data-access-methods) (.sents(), tagged_sents(), chunked_sents(), parsed_sents()) 在应用于莎士比亚 XMLCorpusReader 时似乎都不起作用

我想了解:
1/ 如何获取句子

2/ 如何知道如何在 ElementTree 对象中查找它们

【问题讨论】:

    标签: python nlp nltk


    【解决方案1】:

    问题归结为如何从元素树的所有子元素中提取文本。 这与Python element tree - extract text from element, stripping tags 完全重复

    试试这个:

    for p in play:
        print('%s: %s' % (p.tag, list(p.itertext())))
    

    在这里插入你想做的逻辑

    【讨论】:

      猜你喜欢
      • 2013-08-07
      • 2014-06-12
      • 2013-02-09
      • 2020-11-04
      • 2016-06-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多