【问题标题】:How to create a two-dimensional array of words of sentences from text in python?如何从python中的文本创建一个二维的句子单词数组?
【发布时间】:2017-02-24 03:56:59
【问题描述】:

我有一个文本,比如说 5 句话:

Lorem Ipsum 只是印刷和排版的虚拟文本 行业。 Lorem Ipsum 一直是业界标准的虚拟文本 自 1500 年代以来,当一位不知名的印刷商采用了一种类型的厨房时 并把它弄成一本活字标本簿。它没有幸存下来 仅仅五个世纪,又是电子排版的飞跃, 基本保持不变。它在 1960 年代流行于 发布 Letraset 表包含。 Lorem Ipsum 段落,以及 最近使用 Aldus PageMaker 等桌面排版软件 包括 Lorem Ipsum 的版本。

使用python,如何将其转换为两个demensianal数组,其中每个句子被拆分为单独的单词。

如果我们以第一句话为例,这就是我需要成为数组的第一个元素:

['lorem', 'ipsum', 'is', 'simply', 'dummy', 'text', 'of', 'the', 'printing', 'and', 'typesetting', 'industry']

我可以使用以下命令来实现:

string = 'Lorem Ipsum is simply dummy text of the printing and typesetting industry.'

string = string.lower()
arrWords = re.split('[^a-z]', string)
arrWords = filter(None, arrWords)
print arrWords

但是如何通过循环遍历句子的文本来制作这些元素的数组呢?

【问题讨论】:

  • 您需要将文本拆分为句子,然后再拆分为单词。你如何决定一个句子的结尾可能很困难。你看过 Python 的 NLTK 包吗?
  • [i.split(' ') for i in string.split('.')] 将给出包含单词列表的句子列表。希望这会有所帮助!

标签: python arrays list


【解决方案1】:

虽然通常很难准确判断句子的结束位置,但在这种情况下,每个句子都有句号标记结尾,因此我们可以使用它来将段落拆分为句子。您已经有了将其拆分为单词的代码,但这里是:

paragraph = "Lorem Ipsum ... "
sentences = []
while paragraph.find('.') != -1:
    index = paragraph.find('.')
    sentences.append(paragraph[:index+1])
    paragraph = paragraph[index+1:]

print sentences

输出:

['Lorem Ipsum is simply dummy text of the printing and typesetting industry.', 
"Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.", 
'It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.', 
'It was popularised in the 1960s with the release of Letraset sheets containing.', 
'Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.']

然后我们将它们全部转换为单词数组:

word_matrix = []
for sentence in sentences:
    word_matrix.append(sentence.strip().split(' '))

print word_matrix

哪些输出:

[['Lorem', 'Ipsum', 'is', 'simply', 'dummy', 'text', 'of', 'the', 'printing', 'and', 'typesetting', 'industry.'], 
['Lorem', 'Ipsum', 'has', 'been', 'the', "industry's", 'standard', 'dummy', 'text', 'ever', 'since', 'the', '1500s,', 'when', 'an', 'unknown', 'printer', 'took', 'a', 'galley', 'of', 'type', 'and', 'scrambled', 'it', 'to', 'make', 'a', 'type', 'specimen', 'book.'], 
['It', 'has', 'survived', 'not', 'only', 'five', 'centuries,', 'but', 'also', 'the', 'leap', 'into', 'electronic', 'typesetting,', 'remaining', 'essentially', 'unchanged.'], 
['It', 'was', 'popularised', 'in', 'the', '1960s', 'with', 'the', 'release', 'of', 'Letraset', 'sheets', 'containing.'], 
['Lorem', 'Ipsum', 'passages,', 'and', 'more', 'recently', 'with', 'desktop', 'publishing', 'software', 'like', 'Aldus', 'PageMaker', 'including', 'versions', 'of', 'Lorem', 'Ipsum.']]

【讨论】:

  • 只是一个小小的节制,在@roman_js 给出的示例规范中,If we take a first sentence as an example, here is what I need to be a first element of an array: ['lorem', 'ipsum', 'is', 'simply', 'dummy', 'text', 'of', 'the', 'printing', 'and', 'typesetting', 'industry'] 没有句号'.'在列表的末尾。
【解决方案2】:

删除逗号,然后用. 分割,再用空格分割(split 没有参数)。

paras = [[w for w in p.split()] for p in s.replace(',', '').split('.')]

这会在最后留下一个空列表,您可以通过切片或通过filter(None, ...) 运行结果来删除它

>>> filter(None,[[w for w in p.split()] for p in s.replace(',', '').split('.')])
[['Lorem', 'Ipsum', 'is', 'simply', 'dummy', 'text', 'of', 'the', 'printing', 'and', 'typesetting', 'industry'], ['Lorem', 'Ipsum', 'has', 'been', 'the', "industry's", 'standard', 'dummy', 'text', 'ever', 'since', 'the', '1500s', 'when', 'an', 'unknown', 'printer', 'took', 'a', 'galley', 'of', 'type', 'and', 'scrambled', 'it', 'to', 'make', 'a', 'type', 'specimen', 'book'], ['It', 'has', 'survived', 'not', 'only', 'five', 'centuries', 'but', 'also', 'the', 'leap', 'into', 'electronic', 'typesetting', 'remaining', 'essentially', 'unchanged'], ['It', 'was', 'popularised', 'in', 'the', '1960s', 'with', 'the', 'release', 'of', 'Letraset', 'sheets', 'containing'], ['Lorem', 'Ipsum', 'passages', 'and', 'more', 'recently', 'with', 'desktop', 'publishing', 'software', 'like', 'Aldus', 'PageMaker', 'including', 'versions', 'of', 'Lorem', 'Ipsum']]

【讨论】:

    【解决方案3】:

    这里的挑战是如何确定句子的结尾。我认为您可以使用 RegEx 来涵盖大部分内容,但是如下所示的简单列表理解将涵盖虚拟文本,因为所有内容都以句点结尾。

        x = "Lorem Ipsum is simply dummy ..."
    
        words = [sentence.split(" ") for sentence in x.split(". ")]
    

    【讨论】:

      【解决方案4】:

      假设每个句子都以“.”结尾(就像你所说的例子)。

      设置:

      para=input("Enter the Para : ")        #input : Paragraph
      sentence=[]         #Store list of sentences
      word=[]             #Store final list of 2D array
      

      句子列表:

      sentence=para.split('.')    #Split at '.' (periods)
      sentence.pop()              #Last Element will be '' due to usage of split. So pop the last element
      

      获取单词列表:

      for i in range(len(sentence)):                      #Go through each Sentence
          sentence[i]=str(sentence[i]).strip(" ")         #Strip the Whitespaces (For leading Whitespace at start of senetence)
          word.append(sentence[i].split(' '))             #Split to words and append the list to word
      

      打印结果:

      print(word)
      

      输入:

      输入段落:

      Lorem Ipsum 只是打印的虚拟文本,并且 排版行业。 Lorem Ipsum 已成为行业标准 自 1500 年代以来的虚拟文本,当时一位不知名的打印机拿走了厨房 的类型,并把它打乱成一个类型样本书。它活了下来 不仅是五个世纪,也是电子领域的飞跃 排版,基本保持不变。它在 1960 年代,随着 Letraset 表的发布,包含。 Lorem Ipsum 段落,以及最近使用桌面出版软件,如 Aldus PageMaker 包括 Lorem Ipsum 的版本。

      输出:

      [['Lorem', 'Ipsum', 'is', 'simply', 'dummy', 'text', 'of', 'the', 'printing', 'and', 'typesetting', 'industry'], 
      ['Lorem', 'Ipsum', 'has', 'been', 'the', "industry's", 'standard', 'dummy', 'text', 'ever', 'since', 'the', '1500s,', 'when', 'an', 'unknown', 'printer', 'took', 'a', 'galley', 'of', 'type', 'and', 'scrambled', 'it', 'to', 'make', 'a', 'type', 'specimen', 'book'], 
      ['It', 'has', 'survived', 'not', 'only', 'five', 'centuries,', 'but', 'also', 'the', 'leap', 'into', 'electronic', 'typesetting,', 'remaining', 'essentially', 'unchanged'], 
      ['It', 'was', 'popularised', 'in', 'the', '1960s', 'with', 'the', 'release', 'of', 'Letraset', 'sheets', 'containing'], 
      ['Lorem', 'Ipsum', 'passages,', 'and', 'more', 'recently', 'with', 'desktop', 'publishing', 'software', 'like', 'Aldus', 'PageMaker', 'including', 'versions', 'of', 'Lorem', 'Ipsum']]
      

      对于拆分成句号'.'以外的字符作为句尾,可以使用re.split()函数。欲了解更多信息,请访问此链接:Python: Split string with multiple delimiters

      【讨论】:

      • 感谢您提供的解决方案和提供的链接,因为我的文本中有其他分隔符。
      • 当然,没问题。很高兴能提供帮助。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-04
      • 1970-01-01
      • 1970-01-01
      • 2021-08-15
      • 2023-03-18
      • 2017-08-13
      相关资源
      最近更新 更多