-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpreprocessing.py
More file actions
52 lines (40 loc) · 1.29 KB
/
Copy pathpreprocessing.py
File metadata and controls
52 lines (40 loc) · 1.29 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
import joblib
import nltk
from nltk.corpus import stopwords
from nltk.stem.porter import PorterStemmer
from sklearn.feature_extraction.text import CountVectorizer
import re
import os
# Download stopwords once
nltk.download('stopwords')
# Get absolute path to models directory
base_path = os.path.dirname(__file__)
model_path = os.path.join(base_path, "models", "model1.pkl")
vectorizer_path = os.path.join(base_path, "models", "vectorizer.pkl")
# Load them safely
model = joblib.load(model_path)
vectorizer = joblib.load(vectorizer_path)
# Initialize stemmer and stopwords
ps = PorterStemmer()
stop_words = set(stopwords.words('english'))
# Preprocessing functions
def clean_html(text):
clean = re.compile('<.*?>')
return re.sub(clean, '', text)
def convert_lower(text):
return text.lower()
def remove_special(text):
return ''.join([ch if ch.isalnum() else ' ' for ch in text])
def remove_stopwords(text):
return [word for word in text.split() if word not in stop_words]
def stem_words(text):
return [ps.stem(word) for word in text]
def join_back(words):
return ' '.join(words)
def preprocess(text):
text = clean_html(text)
text = convert_lower(text)
text = remove_special(text)
words = remove_stopwords(text)
words = stem_words(words)
return join_back(words)