Python nltk.util() Examples

The following are 3 code examples for showing how to use nltk.util(). These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.

You may check out the related API usage on the sidebar.

You may also want to check out all available functions/classes of the module nltk , or try the search function .

Example 1
Project: razzy-spinner   Author: rafasashi   File: wordnet.py    License: GNU General Public License v3.0 6 votes vote down vote up
def closure(self, rel, depth=-1):
        """Return the transitive closure of source under the rel
        relationship, breadth-first

            >>> from nltk.corpus import wordnet as wn
            >>> dog = wn.synset('dog.n.01')
            >>> hyp = lambda s:s.hypernyms()
            >>> list(dog.closure(hyp))
            [Synset('canine.n.02'), Synset('domestic_animal.n.01'),
            Synset('carnivore.n.01'), Synset('animal.n.01'),
            Synset('placental.n.01'), Synset('organism.n.01'),
            Synset('mammal.n.01'), Synset('living_thing.n.01'),
            Synset('vertebrate.n.01'), Synset('whole.n.02'),
            Synset('chordate.n.01'), Synset('object.n.01'),
            Synset('physical_entity.n.01'), Synset('entity.n.01')]

        """
        from nltk.util import breadth_first
        synset_offsets = []
        for synset in breadth_first(self, rel, depth):
            if synset._offset != self._offset:
                if synset._offset not in synset_offsets:
                    synset_offsets.append(synset._offset)
                    yield synset 
Example 2
Project: luscan-devel   Author: blackye   File: wordnet.py    License: GNU General Public License v2.0 6 votes vote down vote up
def closure(self, rel, depth=-1):
        """Return the transitive closure of source under the rel
        relationship, breadth-first

            >>> from nltk.corpus import wordnet as wn
            >>> dog = wn.synset('dog.n.01')
            >>> hyp = lambda s:s.hypernyms()
            >>> list(dog.closure(hyp))
            [Synset('domestic_animal.n.01'), Synset('canine.n.02'), Synset('animal.n.01'), Synset('carnivore.n.01'), Synset('organism.n.01'), Synset('placental.n.01'), Synset('living_thing.n.01'), Synset('mammal.n.01'), Synset('whole.n.02'), Synset('vertebrate.n.01'), Synset('object.n.01'), Synset('chordate.n.01'), Synset('physical_entity.n.01'), Synset('entity.n.01')]

        """
        from nltk.util import breadth_first
        synset_offsets = []
        for synset in breadth_first(self, rel, depth):
            if synset.offset != self.offset:
                if synset.offset not in synset_offsets:
                    synset_offsets.append(synset.offset)
                    yield synset 
Example 3
Project: struct-learning-with-flow   Author: jxhe   File: wsj10.py    License: MIT License 4 votes vote down vote up
def _generate_trees(self):
        # trees = util.load_obj(self.filename + '_gold_len10')
        trees = None

        if trees is None:
            print("Parsing treebank...")

            f = lambda t: len(t.leaves()) <= self.n
            m = lambda t: self._prepare(t)
            # f = lambda t: t
            # m = lambda t: t
            trees = [t for t in filter(f, map(m, self.parsed()))]

            # util.save_obj(trees, self.filename + '_gold_len10')

            # w/o filtering
            # trees = [t for t in self.parsed()]



        # add hmm induced tags
        if self.extra_tags is not None:
            # new_trees = util.load_obj(self.filename + '_35_len10')
            new_trees = None
            if new_trees is None:
                new_trees = []
                # cnt = 0
                for t, tags in zip(self.parsed(), self.extra_tags):
                    # if cnt % 100 == 0:
                    #     print cnt
                    # cnt += 1
                    for st, tag in zip(t.subtrees(lambda x: x.height() == 2), tags):
                        st.insert(0, tag)
                    t = self._prepare(t)
                    if len(t.leaves()) / 2 <= self.n:
                        new_trees += [t]
                # util.save_obj(new_trees, self.filename + '_35_len10')


        self.gold_tag_sents = [[(sub.label(), sub.leaves()[0]) \
                              for sub in t.subtrees(lambda x: x.height() == 2)] \
                              for t in trees]

        if self.extra_tags is not None:
            self.induce_tag_sents = [[(sub.leaves()[0], sub.leaves()[1]) \
                                  for sub in t.subtrees(lambda x: x.height() == 2)] \
                                  for t in new_trees]
        else:
            self.induce_tag_sents = None


        return trees