Python 如何将nltk.RegexpParser的输出写入文件

Python 如何将nltk.RegexpParser的输出写入文件,python,nltk,Python,Nltk,我使用RegexpParser将已标记、转换为小写并带有词性标记的输入分块。我想将chunker的输出写入一个文件,以供以后使用。有关守则如下: poscalltokens = nltk.pos_tag(lccalltokens) pattern = "NP:{<DT>?<JJ>*<NN>}" NPChunker = nltk.RegexpParser(pattern) textchunks = NPChunker.parse(poscalltokens) c

我使用RegexpParser将已标记、转换为小写并带有词性标记的输入分块。我想将chunker的输出写入一个文件,以供以后使用。有关守则如下:

poscalltokens = nltk.pos_tag(lccalltokens)
pattern = "NP:{<DT>?<JJ>*<NN>}"
NPChunker = nltk.RegexpParser(pattern)
textchunks = NPChunker.parse(poscalltokens)
chunksfile = open ('chunksfile.txt', 'w')
chunksfile = ' '.join(textchunks)
结果显示一条错误消息

TypeError: sequence item 0: expected string, tuple found
已尝试使用'/n'.jointextchunks,但收到相同的错误消息

NPChunker的前10行打印输出为:

Columbus/VBZ
Southern/JJ
2009/CD
(NP review/NN)
Acitivities/NNS
Feel/VBP
Comfortable/JJ
Impacted/VBN
(NP seet/NN)
Activities/NNS

非常感谢您提供的任何帮助,特别是帮助我理解.join函数在这种情况下为何不起作用的见解。

看起来textchunks不是一个简单的列表或包含字符串的元组,可能包含元组。事实上,这表明返回的是某种树,而不是字符串。打印测试块时会发生什么情况?打印到屏幕上的前10行文本块是;哥伦布/VBZ Southern/JJ 2009/CD NP review/NN Activities/NNS Feel/VBP Comfort/JJ Impacted/VBN NP seet/NN Activities/NNSHmmm…我对Michail0x2a的回复中的打印输出应显示为一列…请参阅我引用NPChunke输出的原始帖子;它与文本块的打印输出相同
Columbus/VBZ
Southern/JJ
2009/CD
(NP review/NN)
Acitivities/NNS
Feel/VBP
Comfortable/JJ
Impacted/VBN
(NP seet/NN)
Activities/NNS