Lucky
Lucky

Reputation: 905

How to use "Universal dependencies, enhanced" parser from stanford NLP in nltk?

I am able to use universal dependencies parser from Stanford in NLTK, But is there any way to use universal dependencies, enhanced in NLTK? As shown here Stanford Parser

Thanks

Upvotes: 3

Views: 849

Answers (1)

Liang Tan
Liang Tan

Reputation: 11

My way is to Bypass the interface NLTK provided and directly look at their implementation.

Find the source code ./nltk/parse/corenlp.py.

In GenericCoreNLPParser class, there is a method called api_call. when you construct your dependency parser object. you could call this method to get a raw JSON object.

You will get a JSON object with keys: basic dependencies, enhancedDependencies, enhancedPlusPlusDependencies, tokens. When getting the result. we could write a simple function to parse the result into the format which is same as calling their interface.

Here are some snippets

    def parse_sentence(self, sentence: str) -> object:
        """
        Parse a sentence for given sentence and with user-defined properties
        :type properties: object
        :type sentence: str
        The pizza is overpriced
        return : Json Object from the NLP server.
        """
        return self.get_parser().api_call(sentence)["sentences"][0]

Once you get the result.


    def create_parsing_tuples(self, response: object, parse_type: str) -> list:
        """
        According to raw parse result, create dependency tuples by parse_type.
        parse_type options: basicDependencies , enhancedDependencies, enhancedPlusPlusDependencies

        :param response:
        :param parse_type:
        :return:
        """

        tuple_list = []
        for dependency in response[parse_type]:
            if dependency['dep'] == 'ROOT':
                continue
            governor = (dependency['governorGloss'], response['tokens'][dependency['governor'] - 1]['pos'])
            relation = dependency['dep']
            dependent = (dependency['dependentGloss'], response['tokens'][dependency['dependent'] - 1]['pos'])
            tuple_list.append((governor, relation, dependent))

        return [tuple_list]

In their source code, they are going to transform the JSON object to a tree structure, which is more generic in most cases.

Here is a demo picture

Wish my post would help somehow.

Upvotes: 1

Related Questions