Stanford Parser and NLTK

后端 未结 18 2343
既然无缘
既然无缘 2020-11-22 01:32

Is it possible to use Stanford Parser in NLTK? (I am not talking about Stanford POS.)

18条回答
  •  你的背包
    2020-11-22 02:16

    I am using nltk version 3.2.4. And following code worked for me.

    from nltk.internals import find_jars_within_path
    from nltk.tag import StanfordPOSTagger
    from nltk import word_tokenize
    
    # Alternatively to setting the CLASSPATH add the jar and model via their 
    path:
    jar = '/home/ubuntu/stanford-postagger-full-2017-06-09/stanford-postagger.jar'
    model = '/home/ubuntu/stanford-postagger-full-2017-06-09/models/english-left3words-distsim.tagger'
    
    pos_tagger = StanfordPOSTagger(model, jar)
    
    # Add other jars from Stanford directory
    stanford_dir = pos_tagger._stanford_jar.rpartition('/')[0]
    stanford_jars = find_jars_within_path(stanford_dir)
    pos_tagger._stanford_jar = ':'.join(stanford_jars)
    
    text = pos_tagger.tag(word_tokenize("Open app and play movie"))
    print(text)
    

    Output:

    [('Open', 'VB'), ('app', 'NN'), ('and', 'CC'), ('play', 'VB'), ('movie', 'NN')]
    

提交回复
热议问题