我正在处理Stanford sentiment classification数据集,我正在尝试理解编码解析三的两个文件 STree.txt 和 SOStr.txt 每句话。
我如何解码例如这个解析三个?
Effective|but|too-tepid|biopic
6|6|5|5|7|7|0
README文件说:
- SOStr.txt和STree.txt编码解析树的结构。 STree以父指针格式对树进行编码。每一行 对应于datasetSentences.txt文件中的每个句子
醇>
是否有将句子转换为此格式的解析器?我如何解码这个解析三?
我用这个python脚本打印上一句的选区树:
with open( 'parents.txt') as parentsfile,\
open( 'sents.txt') as toksfile:
parents=[]
toks =[]
const_trees =[]
for line in parentsfile:
parents.append(map(int, line.split()))
for line in toksfile:
toks.append(line.strip().split())
for i in xrange(len(toks)):
const_trees.append(load_constituency_tree(parents[i], toks[i]))
#print (const_trees[i].left.word)
attrs = vars(const_trees[i])
print ', '.join("%s: %s" % item for item in attrs.items())
attrs = vars(const_trees[i].right)
print ', '.join("%s: %s" % item for item in attrs.items())
attrs = vars(const_trees[i].left)
print ', '.join("%s: %s" % item for item in attrs.items())
attrs = vars(const_trees[i].right.right)
print ', '.join("%s: %s" % item for item in attrs.items())
attrs = vars(const_trees[i].right.left)
print ', '.join("%s: %s" % item for item in attrs.items())
attrs = vars(const_trees[i].left.left)
print ', '.join("%s: %s" % item for item in attrs.items())
attrs = vars(const_trees[i].left.right)
print ', '.join("%s: %s" % item for item in attrs.items())
break
我意识到第一句话的树是以下内容:
6
|
+-------------+------------+
| |
5 4
+---------+---------+ +---------+---------+
| | | |
Effective but too-tepid biopic
就像在这个post中所描述的那样,非终端是短语的类型,但是在树的这种表示中这些是索引,可能是短语类型的字典,我的问题是这个字典在哪里?我怎样才能在一系列短语中转换这个int?
我的解决方案: 我不确定这是解决方案,但是我写了这个功能,将 nltk PTree 传递给相应的父指针列表:
# given the array returned by ptree.trepositions('postorder') of the nltk library i.e
# an array of tuple like this:
# [(0, 0), (0,), (1, 0, 0), (1, 0), (1, 1, 0), (1, 1, 1), (1, 1), (1,), ()]
# that describe the structure of a tree where each index of the array is the index of a node in the tree in a postorder fashion
# return a list of parents for each node i.e [2, 9, 4, 8, 7, 7, 8, 9, 0] where 0 means that is the root.
# the previous array describe the structure for this tree
# S
# ___________|___
# | VP
# | _________|___
# NP V NP
# | | ___|____
# I enjoyed my cookie
def make_parents_list(treepositions):
parents = []
for i in range(0,len(treepositions)):
if len(treepositions[i])==0:
parent = 0
parents.append(parent)
if len(treepositions[i])>0:
parent_s = [j+1 for j in range(0,len(treepositions)) if ((j > i) and (len(treepositions[j]) == (len(treepositions[i])-1))) ]
#print parent_s[0]
parents.append(parent_s[0])
return parents