|
(NB. This material presumes familiarity with the NLTK book, http://nltk.org/index.php/Book).
NLTK includes the MAC-MORPHO Brazilian Portuguese POS-tagged news text, with over a million words of journalistic texts extracted from ten sections of the daily newspaper Folha de Sao Paulo, 1994.
We can access this corpus as a sequence of words or tagged words as follows:
|
We can also access it in sentence chunks.
|
This data can be used to train taggers (examples below for the Floresta treebank).
The NLTK data distribution includes the "Floresta Sinta(c)tica Corpus" version 7.4, available from http://www.linguateca.pt/Floresta/.
We can access this corpus as a sequence of words or tagged words as follows:
|
The tags consist of some syntactic information, followed by a plus sign, followed by a conventional part-of-speech tag. Let's strip off the material before the plus sign:
|
Pretty printing the tagged words:
|
Count the word tokens and types, and determine the most common word:
|
List the 20 most frequent tags, in order of decreasing frequency:
|
We can also access the corpus grouped by sentence:
|
To view a parse tree, use the draw() method, e.g.:
|
Here's a function that takes a word and a specified amount of context (measured in characters), and generates a concordance for that word.
|
|
Let's begin by getting the tagged sentence data, and simplifying the tags as described earlier.
|
We already know that n is the most common tag, so we can set up a default tagger that tags every word as a noun, and see how well it does:
|
Evidently, about one in every six words is a noun. Let's improve on this by training up a unigram tagger:
|
Next a bigram tagger:
|
Punkt is a language-neutral sentence segmentation tool. It needs to be trained on plain text. The source text (from the Floresta Portuguese Treebank) contains one sentence per line. We read the text, split it into its lines, and then join these lines together using spaces. Now the information about sentence breaks has been discarded. We split this material into training and testing data:
|
Now we train the sentence segmenter (or sentence tokenizer) and use it on our test sentences:
|
Versions of NLTK more recent than 0.9b1 include a trained model for Portuguese sentence segmentation, which can be loaded as follows. It is faster to load a trained model than to retrain it.
|
NLTK includes the RSLP Portuguese stemmer. Here we use it to stem some Portuguese text:
|
NLTK includes Portuguese stopwords:
|
Now we can use these to filter text. Let's find the most frequent words (other than stopwords) and print them in descending order of frequency:
|
Python understands the common character encoding used for Portuguese, ISO 8859-1 (ISO Latin 1).
|