-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathword_count.py
More file actions
45 lines (32 loc) · 1.07 KB
/
Copy pathword_count.py
File metadata and controls
45 lines (32 loc) · 1.07 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
import pandas as pd
import argparse
import re
def count_words(search_words):
# Open the file in read mode
text = open("tweets.txt", "r")
test_str = ""
# Loop through each line of the file
for line in text:
# Remove the leading spaces and newline character
line = line[20:]
line = line.strip()
line = re.sub('[^A-Za-z0-9]+', ' ', line)
#print(line)
line = re.sub('[^A-Za-z0-9]+', ' ', line)
#print(line)
# Convert the characters in line to
# lowercase to avoid case mismatch
line = line.lower()
# Split the line into words
words = line.split(" ")
#append each line to the test string
test_str = test_str + line
res = test_str.count(search_words)
print("The count of '", search_words, "'in tweets.txt:", res)
def main():
parser = argparse.ArgumentParser(description='word count')
parser.add_argument('--word', dest='words', type=str)
args = parser.parse_args()
count_words(args.words.lower())
if __name__ == "__main__":
main()