English grammar for parsing in NLTK

后端 未结 8 861
没有蜡笔的小新
没有蜡笔的小新 2020-11-29 20:02

Is there a ready-to-use English grammar that I can just load it and use in NLTK? I\'ve searched around examples of parsing with NLTK, but it seems like that I have to manual

8条回答
  •  独厮守ぢ
    2020-11-29 20:50

    I'm found out that nltk working good with parser grammar developed by Stanford.

    Syntax Parsing with Stanford CoreNLP and NLTK

    It is very easy to start to use Stanford CoreNLP and NLTK. All you need is small preparation, after that you can parse sentences with following code:

    from nltk.parse.corenlp import CoreNLPParser
    parser = CoreNLPParser()
    parse = next(parser.raw_parse("I put the book in the box on the table."))
    

    Preparation:

    1. Download Java Stanford model
    2. Run CoreNLPServer

    You can use following code to run CoreNLPServer:

    import os
    from nltk.parse.corenlp import CoreNLPServer
    # The server needs to know the location of the following files:
    #   - stanford-corenlp-X.X.X.jar
    #   - stanford-corenlp-X.X.X-models.jar
    STANFORD = os.path.join("models", "stanford-corenlp-full-2018-02-27")
    # Create the server
    server = CoreNLPServer(
       os.path.join(STANFORD, "stanford-corenlp-3.9.1.jar"),
       os.path.join(STANFORD, "stanford-corenlp-3.9.1-models.jar"),    
    )
    # Start the server in the background
    server.start()
    

    Do not forget stop server with executing server.stop()

提交回复
热议问题