Package: pynlp
pynlpApythonicwrapper for Stanford CoreNLP.DescriptionThis library provides a Python interface toStanford CoreNLPbuilt overcorenlp_protobuf.InstallationDownload Stanford CoreNLP from the officialdownload page.Unzip the file and set yourCORE_NLPenvironment variable to point to the directory.Installpynlpfrom pippip3 install pynlpQuick StartLaunch the serverLauch theStanfordCoreNLPServerusing the instruction givenhere.Alternatively, simply run the module.python3 -m pynlpBy default, this lauches the server on localhost using port 9000 and 4gb ram for the JVM. Use the--helpoption for instruction on custom configurations.ExampleLet's start off with an excerpt from a CNN article.text=('GOP Sen. Rand Paul was assaulted in his home in Bowling Green, Kentucky, on Friday, ''according to Kentucky State Police. State troopers responded to a call to the senator\'s ''residence at 3:21 p.m. Friday. Police arrested a man named Rene Albert Boucher, who they ''allege "intentionally assaulted" Paul, causing him "minor injury". Boucher, 59, of Bowling ''Green was charged with one count of fourth-degree assault. As of Saturday afternoon, he ''was being held in the Warren County Regional Jail on a $5,000 bond.')Instantiate annotatorHere we demonstrate the following annotators:Annotoators:tokenize, ssplit, pos, lemma, ner, entitymentions, coref, sentiment, quote, openieOptions:openie.resolve_coreffrompynlpimportStanfordCoreNLPannotators='tokenize, ssplit, pos, lemma, ner, entitymentions, coref, sentiment, quote, openie'options={'openie.resolve_coref':True}nlp=StanfordCoreNLP(annotators=annotators,options=options)Annotate textThenlpinstance is callable. Use it to annotate the text and return aDocumentobject.document=nlp(text)print(document)# prints 'text'Sentence splittingLet's test thessplitannotator. ADocumentobject iterates over itsSentenceobjects.forindex,sentenceinenumerate(document):print(index,sentence,sep=' )')Output:0) GOP Sen. Rand Paul was assaulted in his home in Bowling Green, Kentucky, on Friday, according to Kentucky State Police.
1) State troopers responded to a call to the senator's residence at 3:21 p.m. Friday.
2) Police arrested a man named Rene Albert Boucher, who they allege "intentionally assaulted" Paul, causing him "minor injury".
3) Boucher, 59, of Bowling Green was charged with one count of fourth-degree assault.
4) As of Saturday afternoon, he was being held in the Warren County Regional Jail on a $5,000 bond.Named entity recognitionHow about finding all the people mentioned in the document?[str(entity)forentityindocument.entitiesifentity.type=='PERSON']Output:Out[2]: ['Rand Paul', 'Rene Albert Boucher', 'Paul', 'Boucher']We may use named entities on a sentence level too.first_sentence=document[0]forentityinfirst_sentence.entities:print(entity,'({})'.format(entity.type))Output:GOP (ORGANIZATION)
Rand Paul (PERSON)
Bowling Green (LOCATION)
Kentucky (LOCATION)
Friday (DATE)
Kentucky State Police (ORGANIZATION)Part-of-speech taggingLet's find all the 'VB' tags in the first sentence. ASentenceobject iterates overTokenobjects.fortokeninfirst_sentence:if'VB'intoken.pos:print(token,token.pos)Output:was VBD
assaulted VBN
according VBGLemmatizationUsing the same words, lets see the lemmas.fortokeninfirst_sentence:if'VB'intoken.pos:print(token,'->',token.lemma)Output:was -> be
assaulted -> assault
according -> accordCoreference resultionLet's use pynlp to find the firstCorefChainin the text.chain=document.coref_chains[0]print(chain)Output:((GOP Sen. Rand Paul))-[id=4] was assaulted in (his)-[id=5] home in Bowling Green, Kentucky, on Friday, according to Kentucky State Police.
State troopers responded to a call to (the senator's)-[id=10] residence at 3:21 p.m. Friday.
Police arrested a man named Rene Albert Boucher, who they allege "(intentionally assaulted" Paul)-[id=16], causing him "minor injury.In the string representation, coreferences are marked with parenthesis and the referent with double parenthesis.
Each is also labelled with acoref_id. Let's have a closer look at the referent.ref=chain.referentprint('Coreference:{}\n'.format(ref))forattrin'type','number','animacy','gender':print(attr,getattr(ref,attr),sep=': ')# Note that we can also index coreferences by idassertchain[4].is_referentOutput:Coreference: Police

type: PROPER
number: SINGULAR
animacy: ANIMATE
gender: UNKNOWNQuotesExtracting quotes from the text is simple.print(document.quotes)Output:[<Quote: "intentionally assaulted">, <Quote: "minor injury">]TODO (annotation wrappers):ssplitnerposlemmacorefquotequote.attributionparsedepparseentitymentionsopeniesentimentrelationkbpentitylink'options' examples i.e openie.resolve_corefSaving annotationsWriteA pynlp document can be saved as a byte string.withopen('annotation.dat','wb')asfile:file.write(document.to_bytes())ReadTo load a pynlp document, instantiate aDocumentwith thefrom_bytesclass method.frompynlpimportDocumentwithopen('annotation.dat','rb')asfile:document=Document.from_bytes(file.read())