Python: define a class which will be used to model the
N-gram model over a text. The class must be called Ngram, and it
needs a constructor with arguments filename (the path to a file
from which the model will be extracted) and n (representing N). The
default value for filename must be the empty string, and n will be
zero if not specified otherwise. In addition to the filename and N,
the class must have three dictionaries as instance variables, which
need to be initialized by the constructor, but are going to be
filled during the later tasks. Their names must be raw_counts,
prob, and cond_prob.
Example usage:
>>> ngram_model = Ngram ("example .txt ", 2)
>>> ngram_model .n, ngram_model . filename (2, "example
.txt ")
>>> ngram_model.raw_counts , ngram_model.prob ,
ngram_model.cond_prob ({} , {}, {})
Code:
def _init_(self, filename="", n=0):
"""
Initialize an Ngram object.
:param filename: The name of the file to base the model
on.
:param n: The number of tokens in the n-gram tuples.
"""
Python: define a class which will be used to model the N-gram model over a text. The class must be called Ngram, and it
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am