Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -210,15 +210,18 @@ def fDistancePlot(text2Party):
|
|
210 |
plt.tight_layout()
|
211 |
return safe_plot(plot_func)
|
212 |
|
213 |
-
|
214 |
def DispersionPlot(textParty):
|
215 |
"""Generates the word dispersion plot."""
|
|
|
216 |
try:
|
217 |
word_tokens_party = word_tokenize(textParty)
|
218 |
if not word_tokens_party:
|
|
|
219 |
return None
|
|
|
220 |
moby = Text(word_tokens_party)
|
221 |
fdistance = FreqDist(word_tokens_party)
|
|
|
222 |
common_words = fdistance.most_common(6)
|
223 |
if len(common_words) < 5:
|
224 |
word_Lst = [word for word, _ in common_words]
|
@@ -226,33 +229,40 @@ def DispersionPlot(textParty):
|
|
226 |
word_Lst = [common_words[x][0] for x in range(5)]
|
227 |
|
228 |
if not word_Lst:
|
|
|
229 |
return None
|
230 |
|
231 |
-
|
232 |
-
|
233 |
-
|
234 |
-
|
|
|
|
|
|
|
235 |
buf = BytesIO()
|
236 |
-
# Handle potential apply_aspect error for dispersion plot
|
237 |
try:
|
238 |
fig.savefig(buf, format='png', bbox_inches='tight')
|
239 |
except AttributeError as ae:
|
240 |
if "apply_aspect" in str(ae):
|
241 |
print(f"Warning: bbox_inches='tight' failed for Dispersion Plot ({ae}), saving without it.")
|
242 |
buf.seek(0)
|
243 |
-
buf = BytesIO()
|
244 |
fig.savefig(buf, format='png')
|
245 |
else:
|
246 |
-
raise
|
247 |
buf.seek(0)
|
248 |
img = Image.open(buf)
|
249 |
-
plt.close(fig) # Close the specific figure
|
250 |
return img
|
|
|
251 |
except Exception as e:
|
252 |
print(f"Dispersion plot error: {e}")
|
|
|
|
|
253 |
traceback.print_exc()
|
254 |
plt.close('all') # Aggressive close on error
|
255 |
-
return None
|
256 |
|
257 |
|
258 |
def word_cloud_generator(parsed_text_name, text_Party):
|
|
|
210 |
plt.tight_layout()
|
211 |
return safe_plot(plot_func)
|
212 |
|
|
|
213 |
def DispersionPlot(textParty):
|
214 |
"""Generates the word dispersion plot."""
|
215 |
+
buf = None # Initialize buffer
|
216 |
try:
|
217 |
word_tokens_party = word_tokenize(textParty)
|
218 |
if not word_tokens_party:
|
219 |
+
print("Warning: No tokens found for dispersion plot.")
|
220 |
return None
|
221 |
+
|
222 |
moby = Text(word_tokens_party)
|
223 |
fdistance = FreqDist(word_tokens_party)
|
224 |
+
# Get top 5 words, handle potential IndexError if less than 5 unique words
|
225 |
common_words = fdistance.most_common(6)
|
226 |
if len(common_words) < 5:
|
227 |
word_Lst = [word for word, _ in common_words]
|
|
|
229 |
word_Lst = [common_words[x][0] for x in range(5)]
|
230 |
|
231 |
if not word_Lst:
|
232 |
+
print("Warning: No common words found for dispersion plot.")
|
233 |
return None
|
234 |
|
235 |
+
# --- Key Fix: Manage figure explicitly without passing 'ax' ---
|
236 |
+
fig = plt.figure(figsize=(10, 5)) # Create figure explicitly
|
237 |
+
plt.title('Dispersion Plot')
|
238 |
+
# Call dispersion_plot without 'ax' argument
|
239 |
+
moby.dispersion_plot(word_Lst)
|
240 |
+
plt.tight_layout()
|
241 |
+
|
242 |
buf = BytesIO()
|
243 |
+
# Handle potential apply_aspect error for dispersion plot
|
244 |
try:
|
245 |
fig.savefig(buf, format='png', bbox_inches='tight')
|
246 |
except AttributeError as ae:
|
247 |
if "apply_aspect" in str(ae):
|
248 |
print(f"Warning: bbox_inches='tight' failed for Dispersion Plot ({ae}), saving without it.")
|
249 |
buf.seek(0)
|
250 |
+
buf = BytesIO() # Get a fresh buffer
|
251 |
fig.savefig(buf, format='png')
|
252 |
else:
|
253 |
+
raise # Re-raise if it's a different AttributeError
|
254 |
buf.seek(0)
|
255 |
img = Image.open(buf)
|
256 |
+
plt.close(fig) # Close the specific figure created
|
257 |
return img
|
258 |
+
|
259 |
except Exception as e:
|
260 |
print(f"Dispersion plot error: {e}")
|
261 |
+
if buf:
|
262 |
+
buf.close() # Ensure buffer is closed on error
|
263 |
traceback.print_exc()
|
264 |
plt.close('all') # Aggressive close on error
|
265 |
+
return None # Return None on error
|
266 |
|
267 |
|
268 |
def word_cloud_generator(parsed_text_name, text_Party):
|