-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathN-gram.py
More file actions
72 lines (54 loc) · 1.43 KB
/
N-gram.py
File metadata and controls
72 lines (54 loc) · 1.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
"""
Author : Ashutosh Kumar
PRN : 19030142009
Assignment - To read a file name and N-grams(How many grams do you want to distribute) from the user
Then read the file line by line and convert each line into the given N-gram format
Sample Input : This is a test sentence
Sample Output :
1. For 2 gram
output : This is
is a
a test
test sentence
2.For 3 gram
output : This is a
is a test
a test sentence
3.For 4 gram
output : This is a test
is a test sentence
"""
user_file = ""
try:
user_file = input("Enter File Name")
user_file=open(user_file,'r')
gram = int(input("Enter how many grams "))
for line in user_file:
start=0
p=line.split()
for i in range(0,(len(p)-(gram-1))):
print(' '.join(p[start:(start+gram)]))
start = start+1
except NameError:
print("No such name defined")
except TypeError:
print(" Type not defined")
except AttributeError:
print("No such Attribute")
except IndexError:
print("Out of Index")
except ValueError:
print("Invalid Value")
except FileNotFoundError:
print("File not Found ")
except FileExistsError:
print("File Already Exists")
except PermissionError:
print("Permissison to open file is not granted ")
except IsADirectoryError:
print("It is a directory")
finally:
try:
user_file.close()
except AttributeError:
print("File not opened ")