-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclassifier.py
More file actions
57 lines (49 loc) · 1.79 KB
/
classifier.py
File metadata and controls
57 lines (49 loc) · 1.79 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
"""This scripts uses trained model to predict the sentiment of a review."""
import argparse
from model.logistic_regression import BinaryLogisticRegression, BLRFileManager
from model.words2numbers import Words2Numbers, W2NFileManager
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Predict the sentiment of a review.")
parser.add_argument(
"-w",
"--weights",
nargs="+",
required=False,
help="Provide the weights file location.",
)
parser.add_argument(
"-r",
"--review",
nargs="+",
required=True,
help="Provide the review message.",
)
parser.add_argument(
"-m",
"--mapper",
nargs="+",
required=False,
help="Provide the mapper (words to vector) file location.",
)
args = parser.parse_args()
# Load the model weights.
if args.weights is None:
weights_file: str = "best_weights.tsv"
print("No weights file provided. Using the default weights file:", weights_file)
else:
weights_file = args.weights[0]
weights = BLRFileManager.load_weights(weights_file)
# Convert the review to a vector.
if args.mapper is None:
mapper_file: str = "dataset/word2vec.txt"
print("No mapper file provided. Using the default mapper file:", mapper_file)
else:
mapper_file = args.mapper[0]
mapper = W2NFileManager.load_feature_dictionary(mapper_file)
review: str = " ".join(args.review)
review_numeric = Words2Numbers(mapper).convert(review)
# Predict the sentiment of the review.
blg = BinaryLogisticRegression()
blg.set_weights(weights)
prediction = blg.predict(review_numeric)
print("The sentiment of the review is", "positive" if prediction else "negative", end=".\n")