【问题标题】:Python :: NLTK concatenating list of sentencesPython :: NLTK 连接句子列表
【发布时间】:2016-10-08 03:09:38
【问题描述】:

NLTK http://www.nltk.org/ 是计算语言学的工具包。

我正在尝试使用sents() 方法来操纵句子:

from nltk.corpus import gutenberg

它通过fileid获取文本:

hamlet = gutenberg.sents('shakespeare-hamlet.txt')

输出是:

print hamlet
[['[', 'The', 'Tragedie', 'of', 'Hamlet', 'by', 'William', 'Shakespeare', '1599', ']'], ['Actus', 'Primus', '.'], ...]

但是假设我想按作者而不是按书制作句子列表。 以重复的方式(它不会让我extend() 列出):

shakespeare = []

hamlet = gutenberg.sents('shakespeare-hamlet.txt')
macbeth = gutenberg.sents('shakespeare-macbeth.txt')
caesar = gutenberg.sents('shakespeare-caesar.txt')

shakespeare.append(hamlet)
shakespeare.append(macbeth)
shakespeare.append(caesar)

但随后一切都变得嵌套:

print shakespeare

[[['[', 'The', 'Tragedie', 'of', 'Hamlet', 'by', 'William', 'Shakespeare', '1599', ']'], ['Actus', 'Primus', '.'], ...], [['[', 'The', 'Tragedie', 'of', 'Macbeth', 'by', 'William', 'Shakespeare', '1603', ']'], ['Actus', 'Primus', '.'], ...], [['[', 'The', 'Tragedie', 'of', 'Julius', 'Caesar', 'by', 'William', 'Shakespeare', '1599', ']'], ['Actus', 'Primus', '.'], ...]]

有没有办法让我得到一个包含所有连接句子的列表,而不是嵌套的,像这样?

['[', 'The', 'Tragedie', 'of', 'Hamlet', 'by', 'William', 'Shakespeare', '1599', ']'], ['Actus', 'Primus', '.'], ...], [['[', 'The', 'Tragedie', 'of', 'Macbeth', 'by', 'William', 'Shakespeare', '1603', ']'], ['Actus', 'Primus', '.'], ...], [['[', 'The', 'Tragedie', 'of', 'Julius', 'Caesar', 'by', 'William', 'Shakespeare', '1599', ']'], ['Actus', 'Primus', '.'], ...]]

【问题讨论】:

  • 您在底部的示例不是有效列表。如果您提供有关您希望实现的目标的更多信息(和/或仔细检查您的示例),这将有所帮助。
  • @Daniel 你去吧,我已经编辑了底部的例子。谢谢你提醒我。上述示例的语法完全正确。
  • 我很高兴 - 虽然,它仍然不完全存在(你写的例子抛出了SyntaxError)。我想我明白了你想要什么:看起来你想要这个['[', 'The', 'Tragedie', 'of', 'Hamlet', 作为第一个元素而不是这个[', 'The', 'Tragedie', 'of', 'Hamlet',,但这只是一个猜测。不过,刚刚看到您最近的编辑,这使它更加清晰 - 谢谢!
  • 你也有机会看看stackoverflow.com/questions/16176742/… 吗?

标签: python list nltk nested-lists


【解决方案1】:

我同意 Alexis 的观点,理想的做法是从古腾堡语料库中一次性获取它们。对于将来希望连接来自不同语料库的句子的任何人,您还可以尝试这种 Python 方法:

hamlet = gutenberg.sents('shakespeare-hamlet.txt')
macbeth = gutenberg.sents('shakespeare-macbeth.txt')
caesar = gutenberg.sents('shakespeare-caesar.txt')

shakespeare = hamlet + macbeth + caesar

【讨论】:

    【解决方案2】:

    最好的解决方案是一次性获取所有句子——句子按照你想要的方式出现。 nltk 的语料库阅读器接受单个文件名或文件列表:

    shakespeare = gutenberg.sents(['shakespeare-hamlet.txt',
                     'shakespeare-macbeth.txt', 'shakespeare-caesar.txt'])
    

    在其他情况下,如果您有多个列表并且想要连接它们,您应该使用extend(),而不是append()

    shakespeare.extend(macbeth)
    shakespeare.extend(caesar)
    

    【讨论】:

      【解决方案3】:

      您可以在添加到列表shakespeare 后使用itertools.chain

      from itertools import chain
      
      lis = list(chain.from_iterable(shakespeare))
      
      # output:
      # [
      #   ['[', 'The', 'Tragedie', 'of', 'Hamlet', 'by', 'William', 'Shakespeare', '1599', ']'],
      #   ['Actus', 'Primus', '.'],
      #   ['[', 'The', 'Tragedie', 'of', 'Macbeth', 'by', 'William', 'Shakespeare', '1603', ']'],
      #   ['Actus', 'Primus', '.'],
      #   ['[', 'The', 'Tragedie', 'of', 'Julius', 'Caesar', 'by', 'William', 'Shakespeare', '1599', ']'],
      #   ['Actus', 'Primus', '.']
      # ]
      

      您也可以选择带有双循环的list comprehension

      lis = [y for x in shakespeare for y in x]
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2010-10-28
        • 2013-06-13
        • 2016-10-12
        • 2020-12-10
        • 1970-01-01
        • 2016-04-09
        • 2014-01-25
        相关资源
        最近更新 更多