forked from raamav/StandalonePrograms_Python
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSentenceAnalyser.py
More file actions
71 lines (49 loc) · 1.73 KB
/
SentenceAnalyser.py
File metadata and controls
71 lines (49 loc) · 1.73 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
# Incorporates tuples, lists and disctionaries - all in a single use case
"""
Create a program to analyse song lyrics with the followinng three features/functions:
- a frequency dictionary that maps strings to integers; Return type is a dictionary, format {word;frequency}
- word(s) that appears most often. Return type is a tuple, format (list, int) for ([List of most freq. occoring word(s)],frequency)
- words that appear at-least x number of times (x - user input); Return type is a list of tuples, each tuple has format (word,freq)
Comment: Not entirely straightforward, but will get the job done
"""
s = "one two three one one two three three three five six six six six six six"
def word_freq(s):
s = s.lower()
L = s.split(" ")
L.sort()
song_dict = {}
freq = 1
for i in range(len(L)-1):
if L[i] == L[i+1]:
freq +=1
else:
song_dict[L[i]] = freq
freq = 1
return song_dict
#quick test
word_freq(s)
def freq_word(s,func):
song_dict = word_freq(s)
max_list = []
max1 = 0
for i in song_dict.keys():
if song_dict[i]> max1:
max1 = song_dict[i]
for i in song_dict.keys():
if song_dict[i]== max1:
max_list.append(i)
return (max_list,max1)
#quick test
freq_word(s,word_freq)
def atleast_x(s,func1,x): #badly named function that returns a list of tuples
song_dict = word_freq(s)
output_list = []
for i in song_dict.keys():
if song_dict[i]>= x:
output_list.append((i,song_dict[i]))
return output_list
#quick test
atleast_x(s,word_freq,4)
"""
Happy to have written efficient code
"""