Skip to main content
 首页 » 编程设计

python之如何从python中的文本创建一个二维的句子单词数组

2025年05月04日12zhengyun_ustc

我有一段文字,假设有 5 个句子:

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.

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

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

['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 

但是我怎样才能通过遍历句子的文本来制作这些元素的数组呢?

请您参考如下方法:

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

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.']]