-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlab05-15.py
53 lines (46 loc) · 1.35 KB
/
lab05-15.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
def getMultilinesInput():
text = ""
while True:
line = input()
if not line:
break
text += line + ' '
return text
def preprocess_text(txt):
txt = txt.lower()
txt_ls = txt.split(" ")
return txt_ls
def count_uni(txt):
ls = preprocess_text(txt)
unique_ele = []
for i in ls:
if(i not in unique_ele): unique_ele.append(i)
counter2elements = dict()
counter_ls = []
for i in unique_ele:
if(i):
amount = ls.count(i)
try:
counter2elements[amount].extend([i])
except:
counter2elements[amount] = [i]
counter_ls.append(amount)
counter_ls = list(set(counter_ls))
counter_ls.sort()
counter_ls.reverse()
return counter_ls, counter2elements
def display_topk(txt, k):
counter_ls, counter2elements = count_uni(txt)
# print(counter2elements)
# print("----------")
for i in range(k):
#if(counter_ls[i]==counter_ls[i-1]): break
if(len(counter_ls)<=i): break
txt = ""
for j in counter2elements[counter_ls[i]]:
txt+=str(j)+": "+str(counter_ls[i])+", "
print(txt[:len(txt)-2])
print("Parse a long paragraph (or text) below, following an ENTER as needed:")
txt = getMultilinesInput()
k = int(input("Top K rank: "))
display_topk(txt, k)