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])))
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])))
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])), )
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)]])
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)]])
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]