Package: nlpnet-py3-mirror
Gitter is chat room for developers.nlpnet— Natural Language Processing with neural networksnlpnetis a Python library for Natural Language Processing tasks based on neural networks.
Currently, it performs part-of-speech tagging, semantic role labeling and dependency parsing.
Most of the architecture is language independent, but some functions were specially tailored for working
with Portuguese. This system was inspired bySENNA.Important:in order to use the trained models for Portuguese NLP, you will need to download the data fromhttp://nilc.icmc.usp.br/nlpnet/models.html.DependenciesnlpnetrequiresNLTKandnumpy. Additionally, it needs to download some data from NLTK. After installing it, call>>> nltk.download()go to theModelstab and select the Punkt tokenizer. It is used in order to split the text into sentences.Cythonis used to generate C extensions and run faster. You probably won’t need it, since the generated.cfile is already provided withnlpnet, but you will need a C compiler. On Linux and Mac systems this shouldn’t be a problem, but may be on Windows, becausesetuptoolsrequires the Microsoft C Compiler by default. If you don’t have it already, it is usually easier to installMinGWinstead and follow the instructionshere.Basic usagenlpnetcan be used both as a Python library or by its standalone scripts. Both usages are explained below.Library usageYou can usenlpnetas a library in Python code as follows:>>>importnlpnet>>>tagger=nlpnet.POSTagger('/path/to/pos-model/',language='pt')>>>tagger.tag('O rato roeu a roupa do rei de Roma.')[[(u'O',u'ART'),(u'rato',u'N'),(u'roeu',u'V'),(u'a',u'ART'),(u'roupa',u'N'),(u'do',u'PREP+ART'),(u'rei',u'N'),(u'de',u'PREP'),(u'Roma',u'NPROP'),(u'.','PU')]]In the example above, thePOSTaggerconstructor receives as the first argument the directory where its trained model is located. The second argument is the two letter language code (currently, onleptandenare supported). This only has impact in tokenization.Calling an annotation tool is pretty straightforward. The provided ones arePOSTagger,SRLTaggerandDependencyParser, all of them having a methodtagwhich receives strings with text to be tagged (inDependencyParser, there is an alias to the methodparse, which sounds more adequate). The tagger splits the text into sentences and then tokenizes each one (hence the return of the POSTagger is a list of lists).The output of the SRLTagger is slightly more complicated:>>> tagger = nlpnet.SRLTagger()
>>> tagger.tag(u'O rato roeu a roupa do rei de Roma.')
[<nlpnet.taggers.SRLAnnotatedSentence at 0x84020f0>]Instead of a list of tuples, sentences are represented by instances ofSRLAnnotatedSentence. This class serves basically as a data holder, and has two attributes:>>> sent = tagger.tag(u'O rato roeu a roupa do rei de Roma.')[0]
>>> sent.tokens
[u'O', u'rato', u'roeu', u'a', u'roupa', u'do', u'rei', u'de', u'Roma', u'.']
>>> sent.arg_structures
[(u'roeu',
  {u'A0': [u'O', u'rato'],
   u'A1': [u'a', u'roupa', u'do', u'rei', u'de', u'Roma'],
   u'V': [u'roeu']})]Thearg_structuresis a list containing all predicate-argument structures in the sentence. The only one in this example is for the verbroeu. It is represented by a tuple with the predicate and a dictionary mapping semantic role labels to the tokens that constitute the argument.Note that the verb appears as the first member of the tuple and also as the content of label ‘V’ (which stands for verb). This is because some predicates are multiwords. In these cases, the “main” predicate word (usually the verb itself) appears inarg_structures[0], and all the words appear under the key ‘V’.Here’s an example with the DependencyParser:>>> parser = nlpnet.DependencyParser('dependency', language='en')
>>> parsed_text = parser.parse('The book is on the table.')
>>> parsed_text
[<nlpnet.taggers.ParsedSentence at 0x10e067f0>]
>>> sent = parsed_text[0]
>>> print(sent.to_conll())
1       The     _       DT      DT      _       2       NMOD
2       book    _       NN      NN      _       3       SBJ
3       is      _       VBZ     VBZ     _       0       ROOT
4       on      _       IN      IN      _       3       LOC-PRD
5       the     _       DT      DT      _       6       NMOD
6       table   _       NN      NN      _       4       PMOD
7       .       _       .       .       _       3       PTheto_conll()method of ParsedSentence objects prints them in theCoNLLnotation. The tokens, labels and head indices are accessible through member variables:>>> sent.tokens
[u'The', u'book', u'is', u'on', u'the', u'table', u'.']
>>> sent.heads
array([ 1,  2, -1,  2,  5,  3,  2])
>>> sent.labels
[u'NMOD', u'SBJ', u'ROOT', u'LOC-PRD', u'NMOD', u'PMOD', u'P']Theheadsmember variable is a numpy array. The i-th position in the array contains the index of the head of the i-th token, except for the root token, which has a head of -1. Notice that these indices are 0-based, while the ones shown in theto_conll()function are 1-based.Standalone scriptsnlpnetalso provides scripts for tagging text, training new models and testing them. They are copied to thescriptssubdirectory of your Python installation, which can be included in the system PATH variable. You can call them from command line and give some text input.$nlpnet-tag.pypos--data/path/to/nlpnet-data/--langptOratoroeuaroupadoreideRoma.O_ARTrato_Nroeu_Va_ARTroupa_Ndo_PREP+ARTrei_Nde_PREPRoma_NPROP._PUIf--datais not given, the script will search for the trained models in the current directory.--langdefaults toen. If you have text already tokenized, you may use the-toption; it assumes tokens are separated by whitespaces.With semantic role labeling:$nlpnet-tag.pysrl/path/to/nlpnet-data/OratoroeuaroupadoreideRoma.OratoroeuaroupadoreideRoma.roeuA1:aroupadoreideRomaA0:OratoV:roeuThe first line was typed by the user, and the second one is the result of tokenization.And dependency parsing:$nlpnet-tag.pydependency--datadependency--langenThebookisonthetable.1The_DTDT_2NMOD2book_NNNN_3SBJ3is_VBZVBZ_0ROOT4on_ININ_3LOC-PRD5the_DTDT_6NMOD6table_NNNN_4PMOD7._.._3PTo learn more about training and testing new models, and other functionalities, refer to the documentation athttp://nilc.icmc.usp.br/nlpnetReferencesThe following references describe the design of nlpnet, as well as experiments carried out. Some improvements to the code have been implemented since their publication.Fonseca, Erick and Aluísio, Sandra M.A Deep Architecture for Non-Projective Dependency Parsing. Proceedings of the NAACL-HLT Workshop on Vector Space Modeling for NLP. 2015Fonseca, Erick and Rosa, João Luís G.A Two-Step Convolutional Neural Network Approach for Semantic Role Labeling. Proceedings of the International Joint Conference on Neural Networks. 2013.Fonseca, Erick, Rosa, João Luís G. and Aluísio, Sandra M.Evaluating word embeddings and a revised corpus for part-of-speech tagging in Portuguese. Journal of The Brazilian Computer Society. 2015.