forked from LinkedInLearning/learning-python-2896241
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTextAnalyzer.py
More file actions
49 lines (34 loc) · 1.32 KB
/
Copy pathTextAnalyzer.py
File metadata and controls
49 lines (34 loc) · 1.32 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
class TextAnalyzer(object):
rawText = ""
fmtText = ""
def __init__ (self, text):
# assign raw text
self.rawText = text
# remove punctuation
formattedText = text.replace('.','').replace('!','').replace('?','').replace(',','')
# make text lowercase
formattedText = formattedText.lower()
self.fmtText = formattedText
def freqAll(self):
# split text into words
wordList = self.fmtText.split(' ')
# Create dictionary
freqMap = {}
for word in set(wordList): # use set to remove duplicates in list
freqMap[word] = wordList.count(word)
return freqMap
def freqOf(self,word):
# get frequency map
freqDict = self.freqAll()
if word in freqDict:
return freqDict[word]
else:
return 0
givenstring="Lorem ipsum dolor! diam amet, consetetur Lorem magna. sed diam nonumy eirmod tempor. diam et labore? et diam magna. et diam amet."
analyzer = TextAnalyzer(givenstring)
print("Raw Text: " + analyzer.rawText)
print("Formatted Text: " + analyzer.fmtText)
freqMap = analyzer.freqAll()
print("Frequency All: ",freqMap)
word = "lorem"
print("Frequency of ", word ,": " , analyzer.freqOf(word))