本文整理汇总了Python中nltk.tree.ParentedTree.fromstring方法的典型用法代码示例。如果您正苦于以下问题:Python ParentedTree.fromstring方法的具体用法?Python ParentedTree.fromstring怎么用?Python ParentedTree.fromstring使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类nltk.tree.ParentedTree
的用法示例。
在下文中一共展示了ParentedTree.fromstring方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_labeled_nodes
# 需要导入模块: from nltk.tree import ParentedTree [as 别名]
# 或者: from nltk.tree.ParentedTree import fromstring [as 别名]
def test_labeled_nodes(self):
'''
Test labeled nodes.
Test case from Emily M. Bender.
'''
search = '''
# macros
@ SBJ /SBJ/;
@ VP /VP/;
@ VB /VB/;
@ VPoB /V[PB]/;
@ OBJ /OBJ/;
# 1 svo
S < @SBJ=s < (@VP=v < (@VB $.. @OBJ)) : =s .. =v'''
sent1 = ParentedTree.fromstring(
'(S (NP-SBJ I) (VP (VB eat) (NP-OBJ (NNS apples))))')
sent2 = ParentedTree.fromstring(
'(S (VP (VB eat) (NP-OBJ (NNS apples))) (NP-SBJ I))')
search_firsthalf = (search.split('\n\n')[0] +
'S < @SBJ < (@VP < (@VB $.. @OBJ))')
search_rewrite = 'S < (/.*SBJ/ $.. (/VP/ < (/VB/ $.. /.*OBJ/)))'
self.assertTrue(list(tgrep.tgrep_positions(search_firsthalf, [sent1]))[0])
self.assertTrue(list(tgrep.tgrep_positions(search, [sent1]))[0])
self.assertTrue(list(tgrep.tgrep_positions(search_rewrite, [sent1]))[0])
self.assertEqual(list(tgrep.tgrep_positions(search, [sent1])),
list(tgrep.tgrep_positions(search_rewrite, [sent1])))
self.assertTrue(list(tgrep.tgrep_positions(search_firsthalf, [sent2]))[0])
self.assertFalse(list(tgrep.tgrep_positions(search, [sent2]))[0])
self.assertFalse(list(tgrep.tgrep_positions(search_rewrite, [sent2]))[0])
self.assertEqual(list(tgrep.tgrep_positions(search, [sent2])),
list(tgrep.tgrep_positions(search_rewrite, [sent2])))
示例2: lappinleasse
# 需要导入模块: from nltk.tree import ParentedTree [as 别名]
# 或者: from nltk.tree.ParentedTree import fromstring [as 别名]
def lappinleasse(parsetree, i):
global entitySet
for np in parsetree.subtrees(lambda x: x.label() == 'NP'):
if 'PRP' in np[0].label():
if np[0,0].lower() == 'it' and ispleonastic(np, parsetree): continue
maxsalience = -1
referent = None
e = Entity(np, parsetree, i)
for entity in entitySet:
if entity.sentencenum >= i - 4 and e.agreeswith(entity) and maxsalience < entity.salience:
maxsalience = entity.salience
referent = entity
try:
referent.salience += e.salience
referent.gender = e.gender
referent.phrases.add(np[0,0] + str(i))
orig = np[0,0]
if np[0].label() == 'PRP$':
np[0] = ParentedTree.fromstring('(SUB <'+ referent.name + "'s>)")
print('PRP$ substitution', orig, '-->', referent.name)
else:
np[0] = ParentedTree.fromstring('(SUB <' + referent.name + '>)')
print('PRP substitution', orig, '-->', referent.name)
except:
print('No substitution found for ', orig)
continue
elif np[0].label() == 'EX': continue
else: entitySet.add(Entity(np, parsetree, i))
# print('Discourse model after sentence', i + 1, ':')
# for entity in entitySet: print(entity)
halve()
示例3: test_exact_match
# 需要导入模块: from nltk.tree import ParentedTree [as 别名]
# 或者: from nltk.tree.ParentedTree import fromstring [as 别名]
def test_exact_match():
tree = ParentedTree.fromstring('(S (NP (DT the) (JJ big) (NN cat)) (VP bit) (NP (DT a) (NN cat)))')
node = search_by_exact_string_matching(tree, 'cat')
assert_equal(len(node), 2)
assert_equal(node[0], ParentedTree.fromstring('(NN cat)'))
node = search_by_exact_string_matching(tree, 'a cat')
assert_equal(len(node), 1)
assert_equal(node[0], ParentedTree.fromstring('(NP (DT a) (NN cat))'))
示例4: test_use_macros
# 需要导入模块: from nltk.tree import ParentedTree [as 别名]
# 或者: from nltk.tree.ParentedTree import fromstring [as 别名]
def test_use_macros(self):
'''
Test defining and using tgrep2 macros.
'''
tree = ParentedTree.fromstring(
'(VP (VB sold) (NP (DET the) '
'(NN heiress)) (NP (NN deed) (PREP to) '
'(NP (DET the) (NN school) (NN house))))'
)
self.assertEqual(
list(
tgrep.tgrep_positions(
'@ NP /^NP/;\[email protected] NN /^NN/;\[email protected] !< @NP !$.. @NN', [tree]
)
),
[[(1,), (2, 2)]],
)
# use undefined macro @CNP
self.assertRaises(
tgrep.TgrepException,
list,
tgrep.tgrep_positions(
'@ NP /^NP/;\[email protected] NN /^NN/;\[email protected] !< @NP !$.. @NN', [tree]
),
)
示例5: disfile2tree
# 需要导入模块: from nltk.tree import ParentedTree [as 别名]
# 或者: from nltk.tree.ParentedTree import fromstring [as 别名]
def disfile2tree(dis_filepath):
"""converts a *.dis file into a ParentedTree (NLTK) instance"""
with open(dis_filepath) as f:
rst_tree_str = f.read().strip()
rst_tree_str = fix_rst_treebank_tree_str(rst_tree_str)
rst_tree_str = convert_parens_in_rst_tree_str(rst_tree_str)
return ParentedTree.fromstring(rst_tree_str)
示例6: findSentencePTreeToken
# 需要导入模块: from nltk.tree import ParentedTree [as 别名]
# 或者: from nltk.tree.ParentedTree import fromstring [as 别名]
def findSentencePTreeToken(sentence, keyword):
import nltk
from nltk.tree import ParentedTree
stemmed = _lemma_(keyword)
tmp = proc.parse_doc(sentence)
i = 0
numSentences = len(tmp['sentences'])
rs = []
for i in range(0, numSentences):
p = tmp['sentences'][i]['parse']
ptree = ParentedTree.fromstring(p)
# rs = []
for i in range(0, len(ptree.leaves())):
tree_position = ptree.leaf_treeposition(i)
node = ptree[tree_position]
if _stem_(node)==stemmed:
tree_position = tree_position[0:len(tree_position)-1]
rs.append(ptree[tree_position])
# if len(rs)>0:
# return rs
return rs
示例7: merge_tree_nnps
# 需要导入模块: from nltk.tree import ParentedTree [as 别名]
# 或者: from nltk.tree.ParentedTree import fromstring [as 别名]
def merge_tree_nnps(tree):
"""
Takes a parse tree and merges any consecutive leaf nodes that come from NNPs
For example if there is a segment of:
(NP
(JJ old)
(NNP Pierre)
(NNP Vinken)
)
Returns:
(NP
(JJ old)
(NNP PierreVinken)
)
"""
# require a parented tree to get a subtrees tree position
p = ParentedTree.convert(tree)
# iterates subtrees of height 3. This is where NP's leading to NNP's leading to lexicalizations will be
for s in p.subtrees(filter=lambda s: s.height() == 3):
# merge NNP's in the list representation of this trees children: [(POS, word), ...]
new_noun_phrase = merge_tagged_nnps([(c.label(), c[0]) for c in s])
child_str = " ".join("(%s %s)" % (pos, word) for pos, word in new_noun_phrase)
# create new subtree with merged NNP's
new_s = ParentedTree.fromstring("(%s %s)" % (s.label(), child_str))
# replace old subtree with new subtree
p[s.treeposition()] = new_s
return Tree.convert(p)
示例8: test_node_printing
# 需要导入模块: from nltk.tree import ParentedTree [as 别名]
# 或者: from nltk.tree.ParentedTree import fromstring [as 别名]
def test_node_printing(self):
'''Test that the tgrep print operator ' is properly ignored.'''
tree = ParentedTree.fromstring('(S (n x) (N x))')
self.assertEqual(list(tgrep.tgrep_positions('N', [tree])),
list(tgrep.tgrep_positions('\'N', [tree])))
self.assertEqual(list(tgrep.tgrep_positions('/[Nn]/', [tree])),
list(tgrep.tgrep_positions('\'/[Nn]/', [tree])))
示例9: getConsituentTreeDistribution
# 需要导入模块: from nltk.tree import ParentedTree [as 别名]
# 或者: from nltk.tree.ParentedTree import fromstring [as 别名]
def getConsituentTreeDistribution(core_nlp_files):
diff_productions = dict()
production_dict_for_files = dict()
for genre_file_path, genre_file_name in core_nlp_files:
production_dict = dict()
dictionary = dict()
with open(genre_file_path) as f:
lines = f.readlines()
assert len(lines) == 1
line = lines[0]
line = 'dictionary=' + line
exec(line)
# print genre_file_path, dictionary
sentences = dictionary[SENTENCES]
for sent in sentences:
parsetree = sent[PARSE_TREE]
t = ParentedTree.fromstring(parsetree)
prods = t.productions()
for prod in prods:
if prod not in diff_productions:
diff_productions[prod] = 0.0
if prod not in production_dict:
production_dict[prod] = 0.0
diff_productions[prod] += 1.0
production_dict[prod] += 1.0
production_dict_for_files[genre_file_name.replace('_corenlp1000.txt', '.txt')] = production_dict
return production_dict_for_files, diff_productions
示例10: test_node_nocase
# 需要导入模块: from nltk.tree import ParentedTree [as 别名]
# 或者: from nltk.tree.ParentedTree import fromstring [as 别名]
def test_node_nocase(self):
'''
Test selecting nodes using case insensitive node names.
'''
tree = ParentedTree.fromstring('(S (n x) (N x))')
self.assertEqual(list(tgrep.tgrep_positions('"N"', [tree])), [[(1,)]])
self.assertEqual(list(tgrep.tgrep_positions('[email protected]"N"', [tree])), [[(0,), (1,)]])
示例11: test_rel_precedence
# 需要导入模块: from nltk.tree import ParentedTree [as 别名]
# 或者: from nltk.tree.ParentedTree import fromstring [as 别名]
def test_rel_precedence(self):
'''
Test matching nodes based on precedence relations.
'''
tree = ParentedTree.fromstring('(S (NP (NP (PP x)) (NP (AP x)))'
' (VP (AP (X (PP x)) (Y (AP x))))'
' (NP (RC (NP (AP x)))))')
self.assertEqual(list(tgrep.tgrep_positions('* . X', [tree])),
[[(0,), (0, 1), (0, 1, 0)]])
self.assertEqual(list(tgrep.tgrep_positions('* . Y', [tree])),
[[(1, 0, 0), (1, 0, 0, 0)]])
self.assertEqual(list(tgrep.tgrep_positions('* .. X', [tree])),
[[(0,), (0, 0), (0, 0, 0), (0, 1), (0, 1, 0)]])
self.assertEqual(list(tgrep.tgrep_positions('* .. Y', [tree])),
[[(0,), (0, 0), (0, 0, 0), (0, 1), (0, 1, 0),
(1, 0, 0), (1, 0, 0, 0)]])
self.assertEqual(list(tgrep.tgrep_positions('* , X', [tree])),
[[(1, 0, 1), (1, 0, 1, 0)]])
self.assertEqual(list(tgrep.tgrep_positions('* , Y', [tree])),
[[(2,), (2, 0), (2, 0, 0), (2, 0, 0, 0)]])
self.assertEqual(list(tgrep.tgrep_positions('* ,, X', [tree])),
[[(1, 0, 1), (1, 0, 1, 0), (2,), (2, 0), (2, 0, 0),
(2, 0, 0, 0)]])
self.assertEqual(list(tgrep.tgrep_positions('* ,, Y', [tree])),
[[(2,), (2, 0), (2, 0, 0), (2, 0, 0, 0)]])
示例12: test_node_regex
# 需要导入模块: from nltk.tree import ParentedTree [as 别名]
# 或者: from nltk.tree.ParentedTree import fromstring [as 别名]
def test_node_regex(self):
'''
Test regex matching on nodes.
'''
tree = ParentedTree.fromstring('(S (NP-SBJ x) (NP x) (NNP x) (VP x))')
# This is a regular expression that matches any node whose
# name starts with NP, including NP-SBJ:
self.assertEqual(list(tgrep.tgrep_positions('/^NP/', [tree])), [[(0,), (1,)]])
示例13: test_bad_operator
# 需要导入模块: from nltk.tree import ParentedTree [as 别名]
# 或者: from nltk.tree.ParentedTree import fromstring [as 别名]
def test_bad_operator(self):
'''
Test error handling of undefined tgrep operators.
'''
tree = ParentedTree.fromstring('(S (A (T x)) (B (N x)))')
self.assertRaises(
tgrep.TgrepException, list, tgrep.tgrep_positions('* >>> S', [tree])
)
示例14: test_node_noleaves
# 需要导入模块: from nltk.tree import ParentedTree [as 别名]
# 或者: from nltk.tree.ParentedTree import fromstring [as 别名]
def test_node_noleaves(self):
'''
Test node name matching with the search_leaves flag set to False.
'''
tree = ParentedTree.fromstring('(S (A (T x)) (B (N x)))')
self.assertEqual(list(tgrep.tgrep_positions('x', [tree])),
[[(0, 0, 0), (1, 0, 0)]])
self.assertEqual(list(tgrep.tgrep_positions('x', [tree], False)),
[[]])
示例15: test_node_quoted
# 需要导入模块: from nltk.tree import ParentedTree [as 别名]
# 或者: from nltk.tree.ParentedTree import fromstring [as 别名]
def test_node_quoted(self):
'''
Test selecting nodes using quoted node names.
'''
tree = ParentedTree.fromstring('(N ("N" x) (N" x) ("\\" x))')
self.assertEqual(list(tgrep.tgrep_positions('"N"', [tree])), [[()]])
self.assertEqual(list(tgrep.tgrep_positions('"\\"N\\""', [tree])), [[(0,)]])
self.assertEqual(list(tgrep.tgrep_positions('"N\\""', [tree])), [[(1,)]])
self.assertEqual(list(tgrep.tgrep_positions('"\\"\\\\\\""', [tree])), [[(2,)]])