KameronB commited on
Commit
5452ae4
·
verified ·
1 Parent(s): fafc58d

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +88 -0
README.md CHANGED
@@ -127,4 +127,92 @@ for text in examples:
127
  print(f"Text: {text}")
128
  print(f"Sentiment: {sentiment} (Score: {score:.3f})")
129
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
130
  ```
 
127
  print(f"Text: {text}")
128
  print(f"Sentiment: {sentiment} (Score: {score:.3f})")
129
 
130
+ ```
131
+
132
+
133
+ To get the sentiment of a piece of text, use the function below. It gets the sentiment of each sentence and then returns a length-weighted average to get the final sentiment
134
+
135
+ ```python
136
+ # Install and import NLTK for sentence tokenization
137
+ import nltk
138
+ nltk.download('punkt', quiet=True)
139
+ from nltk.tokenize import sent_tokenize
140
+
141
+ def predict_paragraph_sentiment(text):
142
+ """
143
+ Predict sentiment for a paragraph by analyzing individual sentences
144
+ and calculating a weighted average based on sentence length.
145
+
146
+ Args:
147
+ text (str): The paragraph text to analyze
148
+
149
+ Returns:
150
+ tuple: (weighted_score, overall_sentiment, sentence_details)
151
+ """
152
+ # Break text into sentences using NLTK
153
+ sentences = sent_tokenize(text)
154
+
155
+ if not sentences:
156
+ return 0.0, 'Neutral', []
157
+
158
+ # Analyze each sentence
159
+ sentence_results = []
160
+ total_chars = len(text)
161
+
162
+ for sentence in sentences:
163
+ sentence = sentence.strip()
164
+ if sentence: # Skip empty sentences
165
+ score, sentiment = predict_sentence_sentiment(sentence)
166
+ char_weight = len(sentence) / total_chars
167
+
168
+ sentence_results.append({
169
+ 'sentence': sentence,
170
+ 'score': score,
171
+ 'sentiment': sentiment,
172
+ 'length': len(sentence),
173
+ 'weight': char_weight
174
+ })
175
+
176
+ # Calculate weighted average
177
+ if not sentence_results:
178
+ return 0.0, 'Neutral', []
179
+
180
+ weighted_score = sum(result['score'] * result['weight'] for result in sentence_results)
181
+
182
+ # Determine overall sentiment
183
+ if weighted_score <= -0.33:
184
+ overall_sentiment = 'Negative'
185
+ elif weighted_score >= 0.33:
186
+ overall_sentiment = 'Positive'
187
+ else:
188
+ overall_sentiment = 'Neutral'
189
+
190
+ return weighted_score, overall_sentiment, sentence_results
191
+
192
+ # Test the paragraph sentiment function
193
+ test_paragraph = """
194
+ The IT support experience was mixed today. The initial wait time was absolutely terrible - I was on hold for over an hour!
195
+ However, once I got through to Mike, he was fantastic. He quickly diagnosed the issue with my VPN connection and walked me
196
+ through the solution step by step. The whole resolution took about 15 minutes once we started working on it.
197
+ While the wait was frustrating, I'm satisfied with the technical support I received.
198
+ """
199
+
200
+ print("=== Paragraph Sentiment Analysis Example ===")
201
+ print(f"Text: {test_paragraph.strip()}")
202
+ print("\n" + "="*80)
203
+
204
+ weighted_score, overall_sentiment, sentence_details = predict_paragraph_sentiment(test_paragraph)
205
+
206
+ print(f"\nOVERALL RESULTS:")
207
+ print(f"Weighted Score: {weighted_score:.3f}")
208
+ print(f"Overall Sentiment: {overall_sentiment}")
209
+
210
+ print(f"\nSENTENCE BREAKDOWN:")
211
+ for i, detail in enumerate(sentence_details, 1):
212
+ print(f"{i}. \"{detail['sentence']}\"")
213
+ print(f" Score: {detail['score']:.3f} | Sentiment: {detail['sentiment']} | Weight: {detail['weight']:.3f}")
214
+ print()
215
+
216
+ print("="*80)
217
+
218
  ```