【发布时间】:2016-10-19 07:51:58
【问题描述】:
我正在使用Standford Sentiment Treebank 数据集,我正在尝试提取叶子和节点。数据如下
(3 (2 (2 The) (2 Rock)) (4 (3 (2 is) (4 (2 destined) (2 (2 (2 (2 (2 to) (2 (2 be) (2 (2 the) (2 (2 21st) (2 (2 (2 Century) (2 's)) (2 (3 new) (2 (2 ``) (2 Conan)))))))) (2 '')) (2 and)) (3 (2 that) (3 (2 he) (3 (2 's) (3 (2 going) (3 (2 to) (4 (3 (2 make) (3 (3 (2 a) (3 splash)) (2 (2 even) (3 greater)))) (2 (2 than) (2 (2 (2 (2 (1 (2 Arnold) (2 Schwarzenegger)) (2 ,)) (2 (2 Jean-Claud) (2 (2 Van) (2 Damme)))) (2 or)) (2 (2 Steven) (2 Segal))))))))))))) (2 .)))
我想要的东西如下:
i) 带有标签的叶子(uni-gram):
[(2 The), (2 Rock), (2 is), (2 destined),...]
ii) 带有标签的上层节点(bi-gram):
[(2 (2 the) (2 Rock)), (2 (2 ``) (2 Conan)), (2 (2 Century) (2 's)),..]
直到我到达树的根部。
我尝试使用正则表达式来完成此操作,但无法正确输出。
我拥有的代码(用于 uni-gram):
import re
import nltk
location = '.../NLP/Standford_Sentiment_Tree_Data_Set/' +\
'trainDevTestTrees_PTB/trees/train.txt'
text = open(location, 'r')
test = text.readlines()[0]
text.close()
uni_regex = re.compile(r'(\([0-4] \w+\))')
temp01 = uni_regex.findall(test)
# bi-gram
bi_regex = re.compile(r'(\([0-4] \([0-4] \w+\) \([0-4] \w+\)\))')
temp02 = bi_regex.findall(test)
以上代码输出:
['(2 The)', '(2 Rock)', '(2 is)', '(2 destined)', '(2 to)', '(2 be)', '(2 the)', '(2 21st)', '(2 Century)', '(3 new)',...]
未能捕获(2 ``)、(2 '') 并提取(2 Jean) 而不是(2 Jean-Claude)
输出捕获(2 (2``) (2 Conan))失败
有没有办法使用nltk 或regex 的某些配置获得我想要的结果,不会错过任何令牌?
我已经查看并尝试修改NLTK tree data structure, finding a node, it's parent or children 中提供的解决方案,但该问题似乎涉及在休假中查找特定单词并显示树结构,而我需要缩进的解决方案类似于以上 n-gram。
【问题讨论】:
标签: python tree nlp nltk nodes