-
Book Overview & Buying
-
Table Of Contents
Python Text Processing with NLTK 2.0 Cookbook: LITE
By :
It is often useful to reduce the vocabulary of a text by replacing words with common synonyms. By compressing the vocabulary without losing meaning, you can save memory in cases such as frequency analysis and text indexing. Vocabulary reduction can also increase the occurrence of significant collocations, which was covered in the Discovering word collocations recipe of Chapter 1,
You will need to have a defined mapping of a word to its synonym. This is a simple controlled vocabulary. We will start by hardcoding the synonyms as a Python dictionary, then explore other options for storing synonym maps.
We'll first create a WordReplacer class in replacers.py that takes a word replacement mapping:
class WordReplacer(object): def __init__(self, word_map): self.word_map = word_map def replace(self, word): return self.word_map.get(word, word)
Then we can demonstrate its usage for simple word replacement:
>>> from replacers import wordReplacer...
Change the font size
Change margin width
Change background colour