-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcode.txt
More file actions
67 lines (57 loc) · 2.38 KB
/
Copy pathcode.txt
File metadata and controls
67 lines (57 loc) · 2.38 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
import os
from collections import Counter
negativeReviews = []
positiveReviews = []
negativeWords = []
positiveWords = []
combinedWords = []
stopWords = ['is', 'a', 'of', 'the', 'it', 'and', 'for', 'with', 'be', 'in',
'this', 'an', 'to', 'has', 'that', 'she', 'he', 'it', 'i', 'was',
'as', 'are', 'on', 'his', 'her', 'at', 'have']
# This part scans trough all reviews of a certain folder and separates all words used into one list.
def positive_reviews(path):
counter = 0
for filename in os.listdir(path):
try:
if filename.endswith(".txt"):
positiveReviews.append(str(filename))
counter += 1
with open(path+filename, 'r', encoding='utf-8') as f:
for line in f:
for word in line.split():
if word.lower() not in stopWords:
positiveWords.append(word)
except Exception as e:
raise e
print("No files found!")
print('In total', counter, 'positive reviews were scanned')
return positiveWords
def negative_reviews(path):
counter = 0
for filename in os.listdir(path):
try:
if filename.endswith(".txt"):
negativeReviews.append(str(filename))
counter += 1
with open(path + filename, 'r', encoding='utf-8') as f:
for line in f:
for word in line.split():
if word.lower() not in stopWords:
negativeWords.append(word)
except Exception as e:
raise e
print("No files found!")
print('In total', counter, 'negative reviews were scanned')
return negativeWords
def word_frequency(listOfWords, top):
listOfWords = Counter(listOfWords)
listOfWords = listOfWords.most_common(top)
return listOfWords
positive_reviews('positive/')
print(word_frequency(positiveWords, 1000))
negative_reviews('negative/')
print(word_frequency(negativeWords, 1000))
print('Total positive words:',len(positiveWords))
print('Total negative words:',len(negativeWords))
combinedWords = positiveWords + negativeWords
print('Total unique words in all classes:',len(set(combinedWords)))