Spaces:
Runtime error
Runtime error
adding rectangles on chart showing steep derivatives
Browse files
app.py
CHANGED
|
@@ -7,6 +7,7 @@ import argparse
|
|
| 7 |
|
| 8 |
import gensim.downloader as api
|
| 9 |
import matplotlib.pyplot as plt
|
|
|
|
| 10 |
import nltk
|
| 11 |
import numpy as np
|
| 12 |
import pandas as pd
|
|
@@ -183,6 +184,47 @@ def stats(text):
|
|
| 183 |
results = readability.getmeasures(text, lang='en')
|
| 184 |
return results
|
| 185 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 186 |
|
| 187 |
def predict(text, tokenizer=tokenizer):
|
| 188 |
model.eval()
|
|
@@ -391,8 +433,14 @@ def sliding_window(text):
|
|
| 391 |
plt.suptitle('Difficulty Score Across Text', fontsize=14, fontweight='bold')
|
| 392 |
plt.style.use('ggplot')
|
| 393 |
ax.set_facecolor('w')
|
| 394 |
-
|
| 395 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 396 |
mapd = [('', 0)]
|
| 397 |
maxy = max(inter_scores)
|
| 398 |
miny = min(inter_scores)
|
|
|
|
| 7 |
|
| 8 |
import gensim.downloader as api
|
| 9 |
import matplotlib.pyplot as plt
|
| 10 |
+
import matplotlib.patches as patches
|
| 11 |
import nltk
|
| 12 |
import numpy as np
|
| 13 |
import pandas as pd
|
|
|
|
| 184 |
results = readability.getmeasures(text, lang='en')
|
| 185 |
return results
|
| 186 |
|
| 187 |
+
def derive(x:list, y:list):
|
| 188 |
+
all_derivs = []
|
| 189 |
+
for idx, point in enumerate(x):
|
| 190 |
+
if idx != len(x) - 1:
|
| 191 |
+
next_x = x[idx + 1]
|
| 192 |
+
next_y = y[idx + 1]
|
| 193 |
+
h = next_x - point
|
| 194 |
+
if h != 0:
|
| 195 |
+
deriv = (next_y - y[idx])/h
|
| 196 |
+
else:
|
| 197 |
+
deriv = 0
|
| 198 |
+
all_derivs.append(abs(deriv))
|
| 199 |
+
return all_derivs
|
| 200 |
+
#(f(x+h) - f(x))/h
|
| 201 |
+
|
| 202 |
+
|
| 203 |
+
def generate_patches(x:list, y:list, range, deriv_threshold=2):
|
| 204 |
+
derivs = derive(x,y)
|
| 205 |
+
print('derivs', derivs)
|
| 206 |
+
in_patch = False
|
| 207 |
+
patches = []
|
| 208 |
+
start = []
|
| 209 |
+
end = []
|
| 210 |
+
for idx, der in enumerate(derivs):
|
| 211 |
+
if der > deriv_threshold:
|
| 212 |
+
if not in_patch:
|
| 213 |
+
start.append(x[idx])
|
| 214 |
+
in_patch = True
|
| 215 |
+
else:
|
| 216 |
+
if in_patch:
|
| 217 |
+
end.append(x[idx])
|
| 218 |
+
in_patch = False
|
| 219 |
+
else:
|
| 220 |
+
continue
|
| 221 |
+
|
| 222 |
+
print(start, end)
|
| 223 |
+
if len(start) != len(end):
|
| 224 |
+
#not doing len(x)-1 because the derivitive can't be taken at ending point so in derive() the x length is already -1 of original
|
| 225 |
+
end.append(len(x))
|
| 226 |
+
return list(zip(start,end))
|
| 227 |
+
|
| 228 |
|
| 229 |
def predict(text, tokenizer=tokenizer):
|
| 230 |
model.eval()
|
|
|
|
| 433 |
plt.suptitle('Difficulty Score Across Text', fontsize=14, fontweight='bold')
|
| 434 |
plt.style.use('ggplot')
|
| 435 |
ax.set_facecolor('w')
|
| 436 |
+
shaded_areas = generate_patches(x, y, .42)
|
| 437 |
|
| 438 |
+
for area in shaded_areas:
|
| 439 |
+
print(range_chart[0], range_chart[1])
|
| 440 |
+
ax.add_patch(patches.Rectangle((area[0],range_chart[0]), area[1]-area[0], range_chart[1]-range_chart[0], alpha=0.2))
|
| 441 |
+
|
| 442 |
+
fig = plt.gcf()
|
| 443 |
+
|
| 444 |
mapd = [('', 0)]
|
| 445 |
maxy = max(inter_scores)
|
| 446 |
miny = min(inter_scores)
|