im am using this code(Python) to get the n -grams for a word :
N = 6;
f_in = open("test.txt", 'r');
ln = f_in.read()
wlen = len(ln);
i = 0;
while (i < wlen - N + 1 ):
for k in ln [i:i+N]: print k,
i = i + 1;
# close file
The file "text.txt" contain the word "text"
the result i get for N = 2 is (te,ex,xt) but the correct result is ( =t,te,ex,xt,t=) where ( = is space) and the biggest N i can use N=4 the number of the letters. but 1 want to use it for bigger e.g. N=5 where the result would be (=text,text=,ext==,xt===,t====)
any ideas to solve it would be very helpfull thanx
Here is an example that would work. The clue was to add one space less than N to both the front and the back of the input string, so it becomes preformatted. Then it was simply a matter of selecting the appropriate slices and put those in a return list value.
# File: n-gram.py
NList =  # start with an empty list
if N> 1:
space = " " * (N-1) # add N - 1 spaces
text = space + text + space # add both in front and back
# append the slices [i:i+N] to NList
for i in range( len(text) - (N - 1) ):
return NList # return the list
# test code
for i in range(5):
# more test code
nList = N_Gram(7,"Here is a lot of text to print")
for ngram in iter(nList):
print '"' + ngram + '"'
The function N_Gram outputs exactly what you seem to want.
How serious is the sparse data problem? Investigate the performance of n-gram taggers as n increases from 1 to 6. Tabulate the accuracy score. Estimate the training data required for these taggers, assuming a vocabulary size of 10 in 5degree and a tagset size of 10 in 2 degree. Please help me to solve this exersise!!!