Spaces:
Runtime error
Runtime error
Create new file
Browse files- rhyme-with-ai/utils.py +49 -0
rhyme-with-ai/utils.py
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import itertools
|
| 2 |
+
import string
|
| 3 |
+
|
| 4 |
+
|
| 5 |
+
def color_new_words(new: str, old: str, color: str = "#eefa66") -> str:
|
| 6 |
+
"""Color new words in strings with a span."""
|
| 7 |
+
|
| 8 |
+
def find_diff(new_, old_):
|
| 9 |
+
return [ii for ii, (n, o) in enumerate(zip(new_, old_)) if n != o]
|
| 10 |
+
|
| 11 |
+
new_words = new.split()
|
| 12 |
+
old_words = old.split()
|
| 13 |
+
forward = find_diff(new_words, old_words)
|
| 14 |
+
backward = find_diff(new_words[::-1], old_words[::-1])
|
| 15 |
+
|
| 16 |
+
if not forward or not backward:
|
| 17 |
+
# No difference
|
| 18 |
+
return new
|
| 19 |
+
|
| 20 |
+
start, end = forward[0], len(new_words) - backward[0]
|
| 21 |
+
return (
|
| 22 |
+
" ".join(new_words[:start])
|
| 23 |
+
+ " "
|
| 24 |
+
+ f'<span style="background-color: {color}">'
|
| 25 |
+
+ " ".join(new_words[start:end])
|
| 26 |
+
+ "</span>"
|
| 27 |
+
+ " "
|
| 28 |
+
+ " ".join(new_words[end:])
|
| 29 |
+
)
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
def find_last_word(s):
|
| 33 |
+
"""Find the last word in a string."""
|
| 34 |
+
# Note: will break on \n, \r, etc.
|
| 35 |
+
alpha_only_sentence = "".join([c for c in s if (c.isalpha() or (c == " "))]).strip()
|
| 36 |
+
return alpha_only_sentence.split()[-1]
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
def pairwise(iterable):
|
| 40 |
+
"""s -> (s0,s1), (s1,s2), (s2, s3), ..."""
|
| 41 |
+
# https://stackoverflow.com/questions/5434891/iterate-a-list-as-pair-current-next-in-python
|
| 42 |
+
a, b = itertools.tee(iterable)
|
| 43 |
+
next(b, None)
|
| 44 |
+
return zip(a, b)
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
def sanitize(s):
|
| 48 |
+
"""Remove punctuation from a string."""
|
| 49 |
+
return s.translate(str.maketrans("", "", string.punctuation))
|