Ravinthiran commited on
Commit
9eef501
·
verified ·
1 Parent(s): 4c2af21

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +55 -1
README.md CHANGED
@@ -41,4 +41,58 @@ new_text = "I recently started a new fitness program at a local wellness center,
41
  predicted_sentiment, sentiment_score = predict_sentiment(new_text, model, tokenizer, label_encoder)
42
 
43
  print(f"Predicted Sentiment: {predicted_sentiment}")
44
- print(f"Sentiment Scores: {sentiment_score}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41
  predicted_sentiment, sentiment_score = predict_sentiment(new_text, model, tokenizer, label_encoder)
42
 
43
  print(f"Predicted Sentiment: {predicted_sentiment}")
44
+ print(f"Sentiment Scores: {sentiment_score}")
45
+
46
+ ## Using HDFS (H5)
47
+
48
+ ```python
49
+ import numpy as np
50
+ import pandas as pd
51
+ import re
52
+ import matplotlib.pyplot as plt
53
+ import seaborn as sns
54
+ from sklearn.preprocessing import LabelEncoder
55
+ from tensorflow.keras.preprocessing.text import Tokenizer
56
+ from tensorflow.keras.preprocessing.sequence import pad_sequences
57
+ from tensorflow.keras.models import load_model
58
+
59
+ # Load the saved Keras model
60
+ model_hybrid = load_model('< DistilSentiNet-42M.h5 File Path >')
61
+
62
+ # Sample data
63
+ df = pd.read_csv("./train.csv")
64
+
65
+ # Preprocessing
66
+ df['text'] = df['text'].str.lower().str.replace('[^\w\s]', '', regex=True)
67
+
68
+ # Encode labels
69
+ label_encoder = LabelEncoder()
70
+ df['label'] = label_encoder.fit_transform(df['sentiment'])
71
+
72
+ # Tokenization and padding
73
+ tokenizer = Tokenizer(num_words=5000)
74
+ tokenizer.fit_on_texts(df['text'])
75
+ X = tokenizer.texts_to_sequences(df['text'])
76
+ X = pad_sequences(X, maxlen=100)
77
+
78
+ # Function to predict sentiment of new input text
79
+ def predict_sentiment(text, tokenizer, model):
80
+ # Preprocess the input text
81
+ text = text.lower()
82
+ text = re.sub(r'[^\w\s]', '', text)
83
+ sequence = tokenizer.texts_to_sequences([text])
84
+ padded_sequence = pad_sequences(sequence, maxlen=100)
85
+
86
+ # Predict sentiment
87
+ pred = model.predict(padded_sequence)
88
+ sentiment = label_encoder.inverse_transform(pred.argmax(axis=1))
89
+ sentiment_score = pred[0]
90
+
91
+ return sentiment[0], sentiment_score
92
+
93
+ # Example usage
94
+ new_text = "I recently started a new fitness program at a local wellness center, and it has been an incredibly positive experience. The trainers are highly knowledgeable and provide personalized guidance to help me achieve my fitness goals. The facilities are state-of-the-art, with a wide range of equipment and classes to choose from. The supportive community and motivating environment have made working out enjoyable and rewarding. I have already noticed significant improvements in my health and fitness levels, and the positive changes have greatly enhanced my overall well-being."
95
+
96
+ predicted_sentiment, sentiment_score = predict_sentiment(new_text, tokenizer, model_hybrid)
97
+
98
+ print(f"The sentiment of the input text is: {predicted_sentiment} with scores {sentiment_score}")