-
Notifications
You must be signed in to change notification settings - Fork 0
/
predictions.py
41 lines (30 loc) · 1.08 KB
/
predictions.py
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
from nltk.corpus import stopwords
from nltk.stem import PorterStemmer
from joblib import load
MODELS_PATH = '\models\SVC_model2.pkl'
def load_model():
'''Load pretrained model'''
try:
with open(MODELS_PATH, 'rb') as file:
model = load(file)
return model
except FileNotFoundError:
print(f"Error: The model file '{MODELS_PATH}' was not found.")
return None
def preprocess_data(text):
''' Applying stopwords and stemming on raw data'''
stop_words = set(stopwords.words('english'))
porter = PorterStemmer()
words = [porter.stem(word.lower()) for word in text if word.lower() not in stop_words]
return words
def get_prediction(input_text):
''' Generating predictions from raw data'''
model = load_model()
data = [input_text]
processed_text = preprocess_data(data)
prediction = model.predict(processed_text)
result = ''.join(prediction)
print(f'Your product is in category: {result}')
if __name__ == '__main__':
text = input("Type a your product description:\n")
get_prediction(text)