本文整理汇总了Python中nltk.tgrep.tgrep_positions函数的典型用法代码示例。如果您正苦于以下问题:Python tgrep_positions函数的具体用法?Python tgrep_positions怎么用?Python tgrep_positions使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了tgrep_positions函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_use_macros
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]
),
)
示例2: test_node_nocase
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,)]])
示例3: test_node_printing
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])))
示例4: test_labeled_nodes
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])))
示例5: test_node_noleaves
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)),
[[]])
示例6: test_node_quoted
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,)]])
示例7: test_rel_sister_nodes
def test_rel_sister_nodes(self):
'''
Test matching sister nodes in a tree.
'''
tree = ParentedTree.fromstring('(S (A x) (B x) (C x))')
self.assertEqual(list(tgrep.tgrep_positions('* $. B', [tree])), [[(0,)]])
self.assertEqual(list(tgrep.tgrep_positions('* $.. B', [tree])), [[(0,)]])
self.assertEqual(list(tgrep.tgrep_positions('* $, B', [tree])), [[(2,)]])
self.assertEqual(list(tgrep.tgrep_positions('* $,, B', [tree])), [[(2,)]])
self.assertEqual(list(tgrep.tgrep_positions('* $ B', [tree])), [[(0,), (2,)]])
示例8: test_node_regex_2
def test_node_regex_2(self):
'''
Test regex matching on nodes.
'''
tree = ParentedTree.fromstring('(S (SBJ x) (SBJ1 x) (NP-SBJ x))')
self.assertEqual(list(tgrep.tgrep_positions('/^SBJ/', [tree])),
[[(0,), (1,)]])
# This is a regular expression that matches any node whose
# name includes SBJ, including NP-SBJ:
self.assertEqual(list(tgrep.tgrep_positions('/SBJ/', [tree])),
[[(0,), (1,), (2,)]])
示例9: test_trailing_semicolon
def test_trailing_semicolon(self):
'''
Test that semicolons at the end of a tgrep2 search string won't
cause a parse failure.
'''
tree = ParentedTree.fromstring(
'(S (NP (DT the) (JJ big) (NN dog)) ' '(VP bit) (NP (DT a) (NN cat)))'
)
self.assertEqual(list(tgrep.tgrep_positions('NN', [tree])), [[(0, 2), (2, 1)]])
self.assertEqual(list(tgrep.tgrep_positions('NN;', [tree])), [[(0, 2), (2, 1)]])
self.assertEqual(
list(tgrep.tgrep_positions('NN;;', [tree])), [[(0, 2), (2, 1)]]
)
示例10: test_multiple_conjs
def test_multiple_conjs(self):
'''
Test that multiple (3 or more) conjunctions of node relations are
handled properly.
'''
sent = ParentedTree.fromstring(
'((A (B b) (C c)) (A (B b) (C c) (D d)))')
# search = '(A < B < C < D)'
# search_tworels = '(A < B < C)'
self.assertEqual(list(tgrep.tgrep_positions('(A < B < C < D)', [sent])),
[[(1,)]])
self.assertEqual(list(tgrep.tgrep_positions('(A < B < C)', [sent])),
[[(0,), (1,)]])
示例11: test_node_simple
def test_node_simple(self):
'''
Test a simple use of tgrep for finding nodes matching a given
pattern.
'''
tree = ParentedTree.fromstring(
'(S (NP (DT the) (JJ big) (NN dog)) '
'(VP bit) (NP (DT a) (NN cat)))')
self.assertEqual(list(tgrep.tgrep_positions('NN', [tree])),
[[(0,2), (2,1)]])
self.assertEqual(list(tgrep.tgrep_nodes('NN', [tree])),
[[tree[0,2], tree[2,1]]])
self.assertEqual(list(tgrep.tgrep_positions('NN|JJ', [tree])),
[[(0, 1), (0, 2), (2, 1)]])
示例12: test_rel_precedence
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)]])
示例13: test_node_encoding
def test_node_encoding(self):
'''
Test that tgrep search strings handles bytes and strs the same
way.
'''
tree = ParentedTree.fromstring(
'(S (NP (DT the) (JJ big) (NN dog)) '
'(VP bit) (NP (DT a) (NN cat)))')
self.assertEqual(list(tgrep.tgrep_positions(b('NN'), [tree])),
list(tgrep.tgrep_positions('NN', [tree])))
self.assertEqual(list(tgrep.tgrep_nodes(b('NN'), [tree])),
list(tgrep.tgrep_nodes('NN', [tree])))
self.assertEqual(list(tgrep.tgrep_positions(b('NN|JJ'), [tree])),
list(tgrep.tgrep_positions('NN|JJ', [tree])))
示例14: test_node_regex
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,)]])
示例15: test_bad_operator
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])
)