Last active
May 25, 2017 00:40
-
-
Save roopalgarg/56ef079ecd4cf6b248d09d2481437fb7 to your computer and use it in GitHub Desktop.
using stanford parsers with nltk
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
from nltk.parse.stanford import StanfordParser, StanfordDependencyParser | |
os.environ["CLASSPATH"]= "/usr/local/stanford-models/stanford-postagger-full-2016-10-31/:usr/local/stanford-models/stanford-ner-2016-10-31/:/usr/local/stanford-models/stanford-parser-full-2016-10-31/" | |
os.environ["STANFORD_MODELS"]= "/usr/local/stanford-models/stanford-postagger-full-2016-10-31/models:/usr/local/stanford-models/stanford-ner-2016-10-31/classifiers" | |
stan_parser = StanfordParser() | |
stan_dep_parser = StanfordDependencyParser() | |
sents = ["The Mavericks won against the Jets", "Golden State Warriors thrashed LA Lakers"] | |
trees = [list(parse)[0] for parse in stan_parser.raw_parse_sents(sents)] | |
for tree in trees: | |
tree.pretty_print() | |
""" | |
ROOT | |
| | |
S | |
_____________|____________ | |
| VP | |
| ____________|___ | |
| | PP | |
| | _________|___ | |
NP | | NP | |
___|______ | | ___|___ | |
DT NNPS VBD IN DT NNS | |
| | | | | | | |
The Mavericks won against the Jets | |
ROOT | |
| | |
SINV | |
______|___________________ | |
FRAG | | | |
| | | | |
NP VP NP | |
______|______ | ___|____ | |
NNP NNP NNPS VBD NNP NNP | |
| | | | | | | |
Golden State Warriors thrashed LA Lakers | |
""" | |
trees = [list(parse)[0] for parse in stan_dep_parser.raw_parse_sents(sents)] | |
for tree in trees: | |
tree.tree().pretty_print() | |
""" | |
won | |
________|_____ | |
Mavericks Jets | |
| _____|____ | |
The against the | |
thrashed | |
________|_______ | |
Warriors Lakers | |
_______|________ | | |
Golden State LA | |
""" | |
for tree in trees: | |
print list(tree.triples()) | |
""" | |
[((u'won', u'VBD'), u'nsubj', (u'Mavericks', u'NNPS')), ((u'Mavericks', u'NNPS'), u'det', (u'The', u'DT')), ((u'won', u'VBD'), u'nmod', (u'Jets', u'NNS')), ((u'Jets', u'NNS'), u'case', (u'against', u'IN')), ((u'Jets', u'NNS'), u'det', (u'the', u'DT'))] | |
[((u'thrashed', u'VBD'), u'dep', (u'Warriors', u'NNPS')), ((u'Warriors', u'NNPS'), u'compound', (u'Golden', u'NNP')), ((u'Warriors', u'NNPS'), u'compound', (u'State', u'NNP')), ((u'thrashed', u'VBD'), u'nsubj', (u'Lakers', u'NNP')), ((u'Lakers', u'NNP'), u'compound', (u'LA', u'NNP'))] | |
""" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment