Skip to main content
 首页 » 编程设计

python之如何在 Python 中逐句解析文件

2024年09月07日9lautakyan007

我需要阅读大量的大型文本文件。

对于每个文件,我都需要打开它并逐句阅读文本。

我发现的大多数方法都是逐行阅读的。

我怎样才能用 Python 做到这一点?

请您参考如下方法:

如果您想要句子标记化,nltk 可能是最快的方法。 http://www.nltk.org/api/nltk.tokenize.html#module-nltk.tokenize.punkt 会让你走得很远。

即来自文档的代码

>>> import nltk.data 
>>> text = ''' 
... Punkt knows that the periods in Mr. Smith and Johann S. Bach 
... do not mark sentence boundaries.  And sometimes sentences 
... can start with non-capitalized words.  i is a good variable 
... name. 
... ''' 
>>> sent_detector = nltk.data.load('tokenizers/punkt/english.pickle') 
>>> print('\n-----\n'.join(sent_detector.tokenize(text.strip()))) 
 
 
Punkt knows that the periods in Mr. Smith and Johann S. Bach 
do not mark sentence boundaries. 
----- 
And sometimes sentences 
can start with non-capitalized words. 
----- 
i is a good variable 
name.