-
Notifications
You must be signed in to change notification settings - Fork 3
/
quizzer.py
86 lines (64 loc) · 1.85 KB
/
quizzer.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
import wikipedia
import sys
import spacy
import operator
import re
import random
nlp=spacy.load('en')
keyword = input("Enter the keyword: ")
try:
search = wikipedia.page(keyword,redirect=False)
except wikipedia.exceptions.PageError:
print("The page was not found, sorry.")
#return to the enter keyword page.
sys.exit()
except wikipedia.exceptions.DisambiguationError:
print("Multiple Results Found.")
options = wikipedia.search(keyword)
options.pop(0)
i=1
for option in options:
print(i,option)
i=i+1
option = int(input("Which one did you mean: "))
print(f"You choosed to search for {options[option-1]}")
search = wikipedia.page(options[option-1])
#return to the enter keyword page.
except:
print("Connection Error")
sys.exit()
print(search)
sections = search.sections
needed_sent=[]
final_sent=[]
def gen_sents(doc):
for line in doc.sents:
string = (re.sub(r'\([^\(]*?\)',r'', str(line)))
value=0
tmp = nlp(str(string))
for ne in tmp.ents:
value = value+1
if(len(line)>5 and len(line)<25 and value>1 and value<6):
#print(string, len(string), value)
needed_sent.append(string)
if(len(needed_sent)>2):
temp = (random.sample(needed_sent, 2))
for x in temp:
final_sent.append(x)
else:
for x in needed_sent:
final_sent.append(x)
for sectio in sections:
doc = nlp(search.section(sectio))
gen_sents(doc)
#sorted_list = sorted(no_of_ner.items(), key=operator.itemgetter(1), reverse=True)
#to sort dict in descending order
final_text = ' '.join(final_sent)
#print(final_text)
#text = nlp(final_text)
for line in final_text:
tp = nlp(line)
for word in tp:
if(word.tag_ == 'PRP' or word.tag_ == 'PRP$'):
line.replace(word.text,keyword)
print(final_text)