2017-02-24 79 views
0

我有一个文本,让我们用5句说:如何从python中的文本创建句子单词的二维数组?

Lorem存有简直是印刷的虚拟文本排版 行业。 Lorem Ipsum自从16世纪以来一直是业界标准的虚拟文本 ,当时一台未知的打印机采用 类型的厨房,并将其制作成样本书。它只存活了五个世纪不是 ,而且电子排版的飞跃, 基本保持不变。它在20世纪60年代推广使用,其中包含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 

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

+0

运行结果中删除您需要将文本分割成句子,然后进言。你如何决定一个句子的结束可能很困难。你看过Python的NLTK包吗? – James

+0

[i.split('')for string.split('。')]将给出包含单词列表的句子列表。希望这可以帮助! –

回答

1

删除逗号然后通过再次.和拆分拆分的空间(不带参数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']] 
2

虽然通常很难准确地判断句子的结束位置,但在这种情况下,每个句子都有标记句号的句号,所以我们可以使用它将句段分解为句子。你已经拥有的代码将其分割成话语权,但在这里它是:

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

只是一小步,在@roman_js给出的示例规范中, '如果我们以第一个句子为例,下面是我需要作为数组的第一个元素: ['lorem','ipsum ','是','简单','虚拟','文本','','','打印','和','排版','行业']'没有时期'。在列表的最后。 –

1

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

x = "Lorem Ipsum is simply dummy ..." 

    words = [sentence.split(" ") for sentence in x.split(". ")] 
1

假设每个句子以'。'结尾。 (就像在你陈述的例子中)。

设置:

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存有简直是印刷和排版 行业的虚拟文本。自从16世纪以来,Lorem Ipsum一直是业界标准的 虚拟文本,当时一台未知的打印机采用了一种类型的厨房 ,并将其打乱以制作样本书。它不仅存活了 不仅五个世纪,而且还跳跃到电子 排版,基本保持不变。它在20世纪60年代的 中随着Letraset床单的发布而得到推广。 Lorem Ipsum 段落,以及最近的桌面出版软件如 Aldus PageMaker包括Lorem Ipsum版本。

OUTPUT:

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

+0

感谢您提供的解决方案和链接,因为我的文本中还有其他分隔符。 –

+0

当然,没问题。乐意效劳。 –