ngram
index
/Users/dcavar/Documents/Teaching/DGfS Herbstschule 2005/Code/ngram.py

ngram.py
(C) 2005 by Damir Cavar
 
ngram class
 
License:
 
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
 
 
Description:
 
Generates frequency profiles over ngrams.
Serializes ngram models to files.

 
Modules
       
os
pickle
sys

 
Classes
       
Ngrams

 
class Ngrams
    Ngram class for counting ngrams and storing ngram models.
 
  Methods defined here:
__init__(self, n=2)
Constructor.
addNgram(self, ngram)
Adds an ngram to the collection.
deSerialize(self, filename='ngrams')
Read ngram model from filename.
frequencyProfile(self, increasing=True)
Returns the frequency profile of the ngram items. If increasing is
set to True, the returned frequency profile will be increasing,
if it is set to False, the returned frequency profile is
decreasing.
getLeastFrequent(self, ngram)
Returns the least frequent ngram.
getMostFrequent(self, ngram)
Returns the most frequent ngram.
getNgramFrequency(self, ngram)
Returns the absolute frequency of an ngram.
getNgramRelativeFrequency(self, ngram)
Returns the relative frequency of an ngram.
getNumberTokens(self)
Return number of tokens.
getNumberTypes(self)
return number of types.
relativeFrequencyProfile(self, increasing=True)
Returns the relative frequency profile of the ngram items. If increasing
is set to True, the returned profile will be increasing, if it is set to
False, it is decreasing.
removeNgram(self, ngram)
Removes one occurrence of an ngram from the collection by decreasing
its counter. If the counter equals 0 after decreasing, the ngram is
removed from the collection.
serialize(self, filename='ngrams')
Dump the ngram model to a file.