當前位置: 首頁>>代碼示例>>Python>>正文


Python ParentedTree.fromstring方法代碼示例

本文整理匯總了Python中nltk.tree.ParentedTree.fromstring方法的典型用法代碼示例。如果您正苦於以下問題:Python ParentedTree.fromstring方法的具體用法?Python ParentedTree.fromstring怎麽用?Python ParentedTree.fromstring使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在nltk.tree.ParentedTree的用法示例。


在下文中一共展示了ParentedTree.fromstring方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: test_comments

# 需要導入模塊: from nltk.tree import ParentedTree [as 別名]
# 或者: from nltk.tree.ParentedTree import fromstring [as 別名]
def test_comments(self):
        '''
        Test that comments are correctly filtered out of tgrep search
        strings.
        '''
        tree = ParentedTree.fromstring('(S (NN x) (NP x) (NN x))')
        search1 = '''
        @ NP /^NP/;
        @ NN /^NN/;
        @NN
        '''
        self.assertEqual(list(tgrep.tgrep_positions(search1, [tree])),
                         [[(0,), (2,)]])
        search2 = '''
        # macros
        @ NP /^NP/;
        @ NN /^NN/;

        # search string
        @NN
        '''
        self.assertEqual(list(tgrep.tgrep_positions(search2, [tree])),
                         [[(0,), (2,)]]) 
開發者ID:Thejas-1,項目名稱:Price-Comparator,代碼行數:25,代碼來源:test_tgrep.py

示例2: tests_rel_indexed_children

# 需要導入模塊: from nltk.tree import ParentedTree [as 別名]
# 或者: from nltk.tree.ParentedTree import fromstring [as 別名]
def tests_rel_indexed_children(self):
        '''
        Test matching nodes based on their index in their parent node.
        '''
        tree = ParentedTree.fromstring('(S (A x) (B x) (C x))')
        self.assertEqual(list(tgrep.tgrep_positions('* >, S', [tree])),   [[(0,)]])
        self.assertEqual(list(tgrep.tgrep_positions('* >1 S', [tree])),   [[(0,)]])
        self.assertEqual(list(tgrep.tgrep_positions('* >2 S', [tree])),   [[(1,)]])
        self.assertEqual(list(tgrep.tgrep_positions('* >3 S', [tree])),   [[(2,)]])
        self.assertEqual(list(tgrep.tgrep_positions('* >\' S', [tree])),  [[(2,)]])
        self.assertEqual(list(tgrep.tgrep_positions('* >-1 S', [tree])),  [[(2,)]])
        self.assertEqual(list(tgrep.tgrep_positions('* >-2 S', [tree])),  [[(1,)]])
        self.assertEqual(list(tgrep.tgrep_positions('* >-3 S', [tree])),  [[(0,)]])
        tree = ParentedTree.fromstring(
            '(S (D (A x) (B x) (C x)) (E (B x) (C x) (A x)) '
            '(F (C x) (A x) (B x)))')
        self.assertEqual(list(tgrep.tgrep_positions('* <, A', [tree])),   [[(0,)]])
        self.assertEqual(list(tgrep.tgrep_positions('* <1 A', [tree])),   [[(0,)]])
        self.assertEqual(list(tgrep.tgrep_positions('* <2 A', [tree])),   [[(2,)]])
        self.assertEqual(list(tgrep.tgrep_positions('* <3 A', [tree])),   [[(1,)]])
        self.assertEqual(list(tgrep.tgrep_positions('* <\' A', [tree])),  [[(1,)]])
        self.assertEqual(list(tgrep.tgrep_positions('* <-1 A', [tree])),  [[(1,)]])
        self.assertEqual(list(tgrep.tgrep_positions('* <-2 A', [tree])),  [[(2,)]])
        self.assertEqual(list(tgrep.tgrep_positions('* <-3 A', [tree])),  [[(0,)]]) 
開發者ID:Thejas-1,項目名稱:Price-Comparator,代碼行數:26,代碼來源:test_tgrep.py

示例3: 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)]]) 
開發者ID:Thejas-1,項目名稱:Price-Comparator,代碼行數:27,代碼來源:test_tgrep.py

示例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])) 
開發者ID:Thejas-1,項目名稱:Price-Comparator,代碼行數:20,代碼來源:test_tgrep.py

示例5: 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 
開發者ID:gkotsis,項目名稱:negation-detection,代碼行數:27,代碼來源:negation_detection.py

示例6: get_all_parts_of_ctree

# 需要導入模塊: from nltk.tree import ParentedTree [as 別名]
# 或者: from nltk.tree.ParentedTree import fromstring [as 別名]
def get_all_parts_of_ctree(self, cparse, clabeldict, learn_features):
        self.cparse = ParentedTree.fromstring(str(cparse))
        if len(cparse.leaves()) != len(self.tokens):
            raise Exception("sentences do not line up!")

        # replacing leaves with node-ids
        idx = 0
        for pos in self.cparse.treepositions('leaves'):
            self.cparse[pos] = idx
            idx += 1
        # replacing internal nodes with node-ids
        for st in self.cparse.subtrees():
            # if x[0] in parentedp.leaves(): continue
            self.idxlabelmap[idx] = clabeldict.addstr(st.label())
            st.set_label(idx)
            idx += 1
        self.get_all_constit_spans()

        if not learn_features:
            return
        # get stuff for constit features
        self.leafnodes = [k for k in self.cparse.subtrees(lambda t: t.height() == 2)]
        for a in xrange(len(self.leafnodes)):
            if self.leafnodes[a][0] != a:
                raise Exception("order mixup!")
        self.get_cpath_to_root()

        # get all lowest common ancestors
        for j in xrange(len(self.leafnodes)):
            for k in xrange(j, len(self.leafnodes)):
                lca, lcaid = self.get_lca(self.leafnodes[j], self.leafnodes[k])
                self.lca[(j, k)] = (lca, lcaid) 
開發者ID:Noahs-ARK,項目名稱:open-sesame,代碼行數:34,代碼來源:sentence.py

示例7: test_node_simple

# 需要導入模塊: from nltk.tree import ParentedTree [as 別名]
# 或者: from nltk.tree.ParentedTree import fromstring [as 別名]
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)]]) 
開發者ID:Thejas-1,項目名稱:Price-Comparator,代碼行數:16,代碼來源:test_tgrep.py

示例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]))) 
開發者ID:Thejas-1,項目名稱:Price-Comparator,代碼行數:9,代碼來源:test_tgrep.py

示例9: test_node_encoding

# 需要導入模塊: from nltk.tree import ParentedTree [as 別名]
# 或者: from nltk.tree.ParentedTree import fromstring [as 別名]
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]))) 
開發者ID:Thejas-1,項目名稱:Price-Comparator,代碼行數:16,代碼來源:test_tgrep.py

示例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,)]]) 
開發者ID:Thejas-1,項目名稱:Price-Comparator,代碼行數:9,代碼來源:test_tgrep.py

示例11: 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,)]]) 
開發者ID:Thejas-1,項目名稱:Price-Comparator,代碼行數:11,代碼來源:test_tgrep.py

示例12: test_node_regex_2

# 需要導入模塊: from nltk.tree import ParentedTree [as 別名]
# 或者: from nltk.tree.ParentedTree import fromstring [as 別名]
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,)]]) 
開發者ID:Thejas-1,項目名稱:Price-Comparator,代碼行數:13,代碼來源:test_tgrep.py

示例13: test_node_tree_position

# 需要導入模塊: from nltk.tree import ParentedTree [as 別名]
# 或者: from nltk.tree.ParentedTree import fromstring [as 別名]
def test_node_tree_position(self):
        '''
        Test matching on nodes based on NLTK tree position.
        '''
        tree = ParentedTree.fromstring('(S (NP-SBJ x) (NP x) (NNP x) (VP x))')
        # test all tree positions that are not leaves
        leaf_positions = set([tree.leaf_treeposition(x)
                              for x in range(len(tree.leaves()))])
        tree_positions = [x for x in tree.treepositions()
                          if x not in leaf_positions]
        for position in tree_positions:
            node_id = 'N{0}'.format(position)
            tgrep_positions = list(tgrep.tgrep_positions(node_id, [tree]))
            self.assertEqual(len(tgrep_positions[0]), 1)
            self.assertEqual(tgrep_positions[0][0], position) 
開發者ID:Thejas-1,項目名稱:Price-Comparator,代碼行數:17,代碼來源:test_tgrep.py

示例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)),
                         [[]]) 
開發者ID:Thejas-1,項目名稱:Price-Comparator,代碼行數:11,代碼來源:test_tgrep.py

示例15: 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])) 
開發者ID:Thejas-1,項目名稱:Price-Comparator,代碼行數:11,代碼來源:test_tgrep.py


注:本文中的nltk.tree.ParentedTree.fromstring方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。