我在一个.txt
文件中有一个报纸文章语料库,我试图将其中的句子拆分为一个.csv
文件,以便为每个句子添加注解。
我被告知使用NLTK
来实现这个目的,我发现了下面的句子分割代码:
import nltk
from nltk.tokenize import sent_tokenize
sent_tokenize("Here is my first sentence. And that's a second one.")
不过,我想知道:
1.如何使用.txt
文件作为标记器的输入(这样就不必复制和粘贴所有内容),以及
1.如何输出一个.csv
文件,而不仅仅是在我的终端上打印句子呢?
1条答案
按热度按时间tkqqtvp11#
阅读
.txt
文件并标记其语句假设
.txt
文件与Python脚本位于同一个文件夹中,您可以读取.txt
文件并使用NLTK
对句子进行标记,如下所示:将句子标记列表写入
.csv
文件写
.csv
文件有很多种选择,选择哪个更方便(例如,如果你已经加载了pandas
,使用pandas
选项)。要使用
pandas
模块写入.csv
文件:要使用
numpy
模块写入.csv
文件:要使用
csv
模块写入.csv
文件: