-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest2.py
More file actions
61 lines (52 loc) · 2.4 KB
/
test2.py
File metadata and controls
61 lines (52 loc) · 2.4 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
Directory = "C:\\Users\\guyst\\Desktop\\alice.txt"
def getWord(word): #takes a word, makes it lowercase,
# and removes non-english characters
#from the first and last characters.
#(words like "it's" will not have
#their special characters removed.
word = list(word.lower())
removedCharacters = False
while not removedCharacters and len(word) > 0: #loop runs until all special characters are
#removed from the start
if not (97 <= ord(word[0]) <= 122): # if the first character is not
word.pop(0) # a lowercase english character
else:
removedCharacters = True
removedCharacters = False
while not removedCharacters and len(word) > 0:#loop runs until all special characters are
#removed from the start
if not (97 <= ord(word[len(word)-1]) <= 122): #checking the last character
word.pop(len(word)-1) # a lowercase english character
else:
removedCharacters = True
return ''.join(word)
def getMatches(text): #function takes in text as string, and returns an array
#that represents the number of times each word appeared in it.
words = {} #this represents the dictionary of words.
#the key is the word itself, and the value is the number of words.
text_split = text.split() #represents the text variable splitted into an array
for word in text_split:
word = getWord(word)
if word != "":
if word in words:
words[word] += 1
else:
words[word] = 1
return words
def printResult(words, text): #function takes in an array of the number of words,
#and the text from the document. prints the result.
print("----Program Start-----")
print("**Your input - **")
print(text)
print("\n**Number of words: **")
for word in words:
print('- Word "'+word+'": appeared '+str(words[word])+' times.')
def main(): #main function
file = open(Directory, 'r')
text = file.read() #represents the string representation of the file
if text == "":
print("You did not input anything in the text file.")
else:
word_count = getMatches(text)
printResult(word_count, text)
main()