Stanford Parser and NLTK

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

    Deprecated Answer

    The answer below is deprecated, please use the solution on https://stackoverflow.com/a/51981566/610569 for NLTK v3.3 and above.


    Edited

    As of the current Stanford parser (2015-04-20), the default output for the lexparser.sh has changed so the script below will not work.

    But this answer is kept for legacy sake, it will still work with http://nlp.stanford.edu/software/stanford-parser-2012-11-12.zip though.


    Original Answer

    I suggest you don't mess with Jython, JPype. Let python do python stuff and let java do java stuff, get the Stanford Parser output through the console.

    After you've installed the Stanford Parser in your home directory ~/, just use this python recipe to get the flat bracketed parse:

    import os
    sentence = "this is a foo bar i want to parse."
    
    os.popen("echo '"+sentence+"' > ~/stanfordtemp.txt")
    parser_out = os.popen("~/stanford-parser-2012-11-12/lexparser.sh ~/stanfordtemp.txt").readlines()
    
    bracketed_parse = " ".join( [i.strip() for i in parser_out if i.strip()[0] == "("] )
    print bracketed_parse
    

提交回复
热议问题