我的程序需要一个文本文件,并使用split('.')
这意味着当它注册一个句号但它可能不准确,将拆分拆分每个句子到一个列表。
str='i love carpets. In fact i own 2.4 km of the stuff.'
listOfSentences = ['i love carpets', 'in fact i own 2', '4 km of the stuff']
listOfSentences = ['i love carpets', 'in fact i own 2.4 km of the stuff']
我的问题是:我如何拆分句子的末尾,而不是在每一个句号。
如果你有两个句子结尾的“” “”和,你可以尝试正则表达式:
import re
text = "your text here. i.e. something."
sentences = re.split(r'(?<!\w\.\w.)(?<![A-Z][a-z]\.)(?<=\.|\?)\s', text)
来源:Python - RegEx for splitting text into sentences (sentence-tokenizing)
像“我看到史密斯先生。”任何基于正则表达式的方法无法处理的情况下,加入黑客对这些案件是不可扩展的。由于用户EST曾评论,任何严肃的实现使用数据。
如果你需要处理的英语才spaCy比NLTK更好:
from spacy.en import English
en = English()
doc = en(u'i love carpets. In fact i own 2.4 km of the stuff.')
for s in list(doc.sents):
print s.string
更新:spaCy现在支持多国语言。
最简单的方法是拆就点后面有一个空格为:
>>> s = 'i love carpets. In fact i own 2.4 km of the stuff.'
>>> s.split('. ')
['i love carpets', 'In fact i own 2.4 km of the stuff.']