示例#1
0
 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])))
示例#2
0
文件: test_tgrep.py 项目: DrDub/nltk
 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])))
示例#3
0
 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(b"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])),
     )
示例#4
0
 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)]])
示例#5
0
文件: test_tgrep.py 项目: DrDub/nltk
 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)]])
示例#6
0
def tgrep(sent, search):
    """
    Uses tgrep to search a Sentence

    :param sents: Sentences from CoreNLP XML
    :type sents: `list` of `Sentence` objects

    :param search: A search query
    :type search: `str` -- Tgrep query
    """
    from nltk.tree import ParentedTree
    from nltk.tgrep import tgrep_nodes, tgrep_positions
    ps = sent.parse_string
    pt = ParentedTree.fromstring(ps)
    ptrees = [i for i in list(tgrep_nodes(search, [pt])) if i]
    return [item for sublist in ptrees for item in sublist]