Spaces:
Sleeping
Sleeping
Commit
·
ff871f8
1
Parent(s):
166f16e
functions needed
Browse files- functions.py +42 -0
functions.py
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
import re
|
| 3 |
+
from contractions import contractions_dict
|
| 4 |
+
from nltk.corpus import stopwords
|
| 5 |
+
from nltk.tokenize import word_tokenize
|
| 6 |
+
|
| 7 |
+
def sentiment_analysis_LR(input):
|
| 8 |
+
# Assuming you have a Logistic Regression model and TfidfVectorizer in the pipeline
|
| 9 |
+
input = preprocess_text(input)
|
| 10 |
+
|
| 11 |
+
vectorizer = model_LR.named_steps['tfidfvectorizer']
|
| 12 |
+
lr_classifier = model_LR.named_steps['logisticregression']
|
| 13 |
+
|
| 14 |
+
# Transform the user input using the TF-IDF vectorizer
|
| 15 |
+
user_input_tfidf = vectorizer.transform([input])
|
| 16 |
+
|
| 17 |
+
# Make predictions
|
| 18 |
+
user_pred = lr_classifier.predict(user_input_tfidf)
|
| 19 |
+
|
| 20 |
+
# Display the prediction
|
| 21 |
+
if user_pred[0] == 0:
|
| 22 |
+
return 0
|
| 23 |
+
else:
|
| 24 |
+
return 1
|
| 25 |
+
|
| 26 |
+
def sentiment_analysis_NB(input):
|
| 27 |
+
input = preprocess_text(input)
|
| 28 |
+
|
| 29 |
+
vectorizer = model_NB.named_steps['tfidf']
|
| 30 |
+
nb_classifier = model_NB.named_steps['nb']
|
| 31 |
+
|
| 32 |
+
# Transform the user input using the TF-IDF vectorizer
|
| 33 |
+
user_input_tfidf = vectorizer.transform([input])
|
| 34 |
+
|
| 35 |
+
# Make predictions
|
| 36 |
+
user_pred = nb_classifier.predict(user_input_tfidf)
|
| 37 |
+
|
| 38 |
+
# Display the prediction
|
| 39 |
+
if user_pred[0] == 0:
|
| 40 |
+
return 0
|
| 41 |
+
else:
|
| 42 |
+
return 1
|