Stanford Parser and NLTK

后端 未结 18 2613
既然无缘
既然无缘 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:21

    Note that this answer applies to NLTK v 3.0, and not to more recent versions.

    Here is the windows version of alvas's answer

    sentences = ('. '.join(['this is sentence one without a period','this is another foo bar sentence '])+'.').encode('ascii',errors = 'ignore')
    catpath =r"YOUR CURRENT FILE PATH"
    
    f = open('stanfordtemp.txt','w')
    f.write(sentences)
    f.close()
    
    parse_out = os.popen(catpath+r"\nlp_tools\stanford-parser-2010-08-20\lexparser.bat "+catpath+r"\stanfordtemp.txt").readlines()
    
    bracketed_parse = " ".join( [i.strip() for i in parse_out if i.strip() if i.strip()[0] == "("] )
    bracketed_parse = "\n(ROOT".join(bracketed_parse.split(" (ROOT")).split('\n')
    aa = map(lambda x :ParentedTree.fromstring(x),bracketed_parse)
    

    NOTES:

    • In lexparser.bat you need to change all the paths into absolute path to avoid java errors such as "class not found"

    • I strongly recommend you to apply this method under windows since I Tried several answers on the page and all the methods communicates python with Java fails.

    • wish to hear from you if you succeed on windows and wish you can tell me how you overcome all these problems.

    • search python wrapper for stanford coreNLP to get the python version


提交回复
热议问题