Skip to content

Commit ee0cef3

Browse files
authored
Create Term Frequency.py
1 parent 880a255 commit ee0cef3

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

interview_query/Term Frequency.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
Write a program in python to determine the TF (term_frequency) values for each term of this document.
2+
3+
Note: Round the term frequency to 2 decimal points.
4+
5+
6+
def term_frequency(sentences):
7+
8+
s = sentences.split()
9+
total = len(s)
10+
11+
d = dict()
12+
for el in s:
13+
14+
if el not in d:
15+
d[el] =1
16+
else:
17+
d[el]+=1
18+
19+
for k in d:
20+
d[k] = round(d[k]/total,2)
21+
22+
return d

0 commit comments

Comments
 (0)