-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNLPproj.py
188 lines (125 loc) · 5.52 KB
/
NLPproj.py
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
def optimize(text):
i = input('Enter the compression level (High/Medium/low):')
if i.lower() == 'high':
x = 3
if i.lower() == 'medium':
x = 5
if i.lower() == 'low':
x = 8
y = ((len(text)*x*10)/100)
r = text.find('.', int(y), len(text))
print(text[0:int(r)+1])
#=======================================================================================================================
def common(article_text):
import re
import nltk
# Removing Square Brackets and Extra Spaces
article_text = re.sub(r'\[[0-9]*\]', ' ', article_text)
article_text = re.sub(r'\s+', ' ', article_text)
# Removing special characters and digits
formatted_article_text = re.sub('[^a-zA-Z]', ' ', article_text)
formatted_article_text = re.sub(r'\s+', ' ', formatted_article_text)
sentence_list = nltk.sent_tokenize(article_text)
print("Sentence list as follows:")
print(sentence_list)
print("===================================================")
stopwords = nltk.corpus.stopwords.words('english')
word_frequencies = {}
for word in nltk.word_tokenize(formatted_article_text):
if word not in stopwords:
if word not in word_frequencies.keys():
word_frequencies[word] = 1
else:
word_frequencies[word] += 1
print("Word frequencies is a s follows:")
print(word_frequencies)
print("====================================================")
maximum_frequncy = max(word_frequencies.values())
for word in word_frequencies.keys():
word_frequencies[word] = (word_frequencies[word] / maximum_frequncy)
sentence_scores = {}
for sent in sentence_list:
for word in nltk.word_tokenize(sent.lower()):
if word in word_frequencies.keys():
if len(sent.split(' ')) < 30:
if sent not in sentence_scores.keys():
sentence_scores[sent] = word_frequencies[word]
else:
sentence_scores[sent] += word_frequencies[word]
print("Sentence scores are as follows:")
print(sentence_scores)
print("===============================================")
import heapq
summary_sentences = heapq.nlargest(7, sentence_scores, key=sentence_scores.get)
summary = ' '.join(summary_sentences)
print("The Summary is:\n")
print(summary)
print("=========================================================")
print("The optimized text is as follows:\n")
optimize(summary)
#=======================================================================================================================
#======================================================one main=========================================================
def onemain(text):
article_text = text
common(article_text)
#=======================================================two main========================================================
def twomain(val1):
import bs4 as bs
import urllib.request
scraped_data = urllib.request.urlopen(val1)
article = scraped_data.read()
parsed_article = bs.BeautifulSoup(article, "html.parser")
paragraphs = parsed_article.find_all('p')
article_text = ""
for p in paragraphs:
article_text += p.text
common(article_text)
#======================================================three main=======================================================
def threemain():
import speech_recognition as sr
import requests
r = sr.Recognizer()
with sr.Microphone() as source:
print("Say something")
r.adjust_for_ambient_noise(source)
audio = r.listen(source, timeout=3, phrase_time_limit=20)
print("Time over, Thanks")
file1 = open("myfile.txt", "w")
tex = r.recognize_google(audio)
dat = {'text': tex}
r = requests.post("http://bark.phon.ioc.ee/punctuator", data=dat)
aarush = r.text
file1.writelines(aarush)
print("The input text is as follows:")
print(aarush)
print("===================================================================")
file1.close()
file1 = open("myfile.txt", "r+")
text = file1.read()
common(text)
#>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
print("Make a choice: 1. Normal Text. 2. Website. 3. Audio Input.")
#========================================================Choice Normal Text=============================================
def one():
val1 = input("Enter the Normal text\n")
print("U hve entered :" + val1)
onemain(val1)
#========================================================Choice Website=================================================
def two():
val1 = input("Enter the website url (including https://)\n")
print("U hve entered :" + val1)
twomain(val1)
#========================================================Choice Audio Input=============================================
def three():
print("You have chosen audio input\n")
threemain()
#-------------------------------------------------------------switch----------------------------------------------------
def switch_func(value, x):
return {
'1': lambda x: one(),
'2': lambda x: two(),
'3': lambda x: three(),
}.get(value)(x)
inp = input('Enter the Choice : ')
switch_func(inp, 2)
#>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>