【发布时间】:2020-09-12 07:38:42
【问题描述】:
我是 Python 和文本分析的新手,我想标记我的文本语料库:
<s> c a b c b c </s>
<s> a c b a </s>
<s> c a c a c </s>
I wanted to tokenize them into ['<s>','c','a','b','c','</s>'], but what i got is:
['<', 's', '>', 'c', 'a', 'b', 'c', 'b', 'c', '<', '/s', '>']
“s”和“/s”用 分隔,作为不同的标记。有没有办法解决这个问题?
代码如下:
import nltk
#read file
f = open('Text Corpus.txt','r')
corpus = f.read()
print (corpus)
#tokenize
tokens = nltk.word_tokenize(corpus)
print(tokens)
【问题讨论】:
-
您应该打开文件“rt” - 对于您想要的,只需
f.split()就可以了。