Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hypernyms related functions in Wordnets can be unified #2270

Open
alvations opened this issue Apr 18, 2019 · 2 comments
Open

Hypernyms related functions in Wordnets can be unified #2270

alvations opened this issue Apr 18, 2019 · 2 comments

Comments

@alvations
Copy link
Contributor

alvations commented Apr 18, 2019

These functions in wordnet's Synset() object could be unified:

  • hypernym_paths
  • min_depth
  • max_depth
  • root_hypernyms

The while loops / recursion achieve the same purpose of reaching the root hypernyms, while doing so, the min and max depth should be logged and so should the hypernym paths.

We can derive the other three attributes given the hypernym_paths, e.g.

from nltk.corpus import wordnet as nltk_wn
ss = nltk_wn.synset('sweet.n.1')
hypernym_paths = ss.hypernym_paths()
assert ss.max_depth() == max(len(path) for path in hypernym_paths) - 1
assert ss.min_depth() == min(len(path) for path in hypernym_paths) - 1
assert list(set([path[0] for path in hypernym_paths])) == ss.root_hypernyms()

Something like this:

    def init_hypernym_paths(self):
        """
        Get the path(s) from this synset to the root, where each path is a
        list of the synset nodes traversed on the way to the root.
        :return: A list of lists, where each list gives the node sequence
        connecting the initial ``Synset`` node and a root node.
        """
        self._hyperpaths = []
        hypernyms = self.hypernyms() + self.instance_hypernyms()
        if len(hypernyms) == 0:
            paths = [[self]]
        for hypernym in hypernyms:
            for ancestor_list in hypernym.hypernym_paths():
                ancestor_list.append(self)
                self._hyperpaths.append(ancestor_list)
        # Compute the path related statistics.
        self._min_depth = min(len(path) for path in self._hyperpaths)
        self._max_depth = max(len(path) for path in self._hyperpaths)
        # Compute the store the root hypernyms.
        self._root_hypernyms = list(set([path[0] for path in self._hyperpaths]))

    def hypernym_paths():
        return self._hyperpaths

    def min_depth():
        return self._min_depth

    def max_depth():
        return self._max_depth

    def root_hypernyms():
        return self._root_hypernyms
@Patil2099
Copy link

@alvations Can I work on this issue?

@alvations
Copy link
Contributor Author

@Patil2099 Actually this is resolved but not in NLTK on https://github.com/alvations/wordnet/blob/master/wn/synset.py#L152 , it'll be nice to fix it in NLTK too. The wn library does a little more clean up to the wordnet access though.

Feel free to take up the issue and create a pull-request (PR) whenever you're ready, someone should/will review the PR before merging the code.

Do take a look at https://github.com/nltk/nltk/blob/develop/CONTRIBUTING.md too =)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants