| from stanza.server import CoreNLPClient |
|
|
| |
| print('---') |
| print('input text') |
| print('') |
|
|
| text = "Chris Manning is a nice person. Chris wrote a simple sentence. He also gives oranges to people." |
|
|
| print(text) |
|
|
| |
| print('---') |
| print('starting up Java Stanford CoreNLP Server...') |
|
|
| |
| with CoreNLPClient(annotators=['tokenize','ssplit','pos','lemma','ner','parse','depparse','coref'], timeout=60000, memory='16G') as client: |
| |
| ann = client.annotate(text) |
|
|
| |
| sentence = ann.sentence[0] |
|
|
| |
| print('---') |
| print('dependency parse of first sentence') |
| dependency_parse = sentence.basicDependencies |
| print(dependency_parse) |
| |
| |
| print('---') |
| print('constituency parse of first sentence') |
| constituency_parse = sentence.parseTree |
| print(constituency_parse) |
|
|
| |
| print('---') |
| print('first subtree of constituency parse') |
| print(constituency_parse.child[0]) |
|
|
| |
| print('---') |
| print('value of first subtree of constituency parse') |
| print(constituency_parse.child[0].value) |
|
|
| |
| print('---') |
| print('first token of first sentence') |
| token = sentence.token[0] |
| print(token) |
|
|
| |
| print('---') |
| print('part of speech tag of token') |
| token.pos |
| print(token.pos) |
|
|
| |
| print('---') |
| print('named entity tag of token') |
| print(token.ner) |
|
|
| |
| print('---') |
| print('first entity mention in sentence') |
| print(sentence.mentions[0]) |
|
|
| |
| print('---') |
| print('coref chains for the example') |
| print(ann.corefChain) |
|
|
| |
| pattern = '([ner: PERSON]+) /wrote/ /an?/ []{0,3} /sentence|article/' |
| matches = client.tokensregex(text, pattern) |
| |
| assert len(matches["sentences"]) == 3 |
| |
| assert matches["sentences"][1]["length"] == 1 |
| |
| matches["sentences"][1]["0"]["text"] == "Chris wrote a simple sentence" |
| matches["sentences"][1]["0"]["1"]["text"] == "Chris" |
|
|
| |
| pattern = '{word:wrote} >nsubj {}=subject >obj {}=object' |
| matches = client.semgrex(text, pattern) |
| |
| assert len(matches["sentences"]) == 3 |
| |
| assert matches["sentences"][1]["length"] == 1 |
| |
| matches["sentences"][1]["0"]["text"] == "wrote" |
| matches["sentences"][1]["0"]["$subject"]["text"] == "Chris" |
| matches["sentences"][1]["0"]["$object"]["text"] == "sentence" |
|
|
|
|