Julien Matuszak commited on
Update app.py
Browse files
app.py
CHANGED
|
@@ -44,24 +44,38 @@ def preprocess_text(text: str):
|
|
| 44 |
})
|
| 45 |
text = capitalized_text # Update text for further processing
|
| 46 |
|
| 47 |
-
# Transformer spell check
|
| 48 |
spell_checked = spell_checker(text, max_length=512)[0]['generated_text']
|
| 49 |
if spell_checked != text:
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
|
| 56 |
-
# Add
|
| 57 |
-
|
| 58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
continue
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
"
|
| 64 |
-
|
|
|
|
|
|
|
|
|
|
| 65 |
|
| 66 |
# Add entities and tags
|
| 67 |
doc = nlp(text)
|
|
|
|
| 44 |
})
|
| 45 |
text = capitalized_text # Update text for further processing
|
| 46 |
|
| 47 |
+
# Transformer spell check - only for words that look misspelled
|
| 48 |
spell_checked = spell_checker(text, max_length=512)[0]['generated_text']
|
| 49 |
if spell_checked != text:
|
| 50 |
+
spell_words = spell_checked.split()
|
| 51 |
+
text_words = text.split()
|
| 52 |
+
|
| 53 |
+
# Only suggest spell corrections for words that are actually different
|
| 54 |
+
for orig, corrected in zip(text_words, spell_words):
|
| 55 |
+
if orig.lower() != corrected.lower(): # Compare case-insensitive
|
| 56 |
+
result["spell_suggestions"].append({
|
| 57 |
+
"original": orig,
|
| 58 |
+
"corrected": corrected,
|
| 59 |
+
"type": "spell"
|
| 60 |
+
})
|
| 61 |
|
| 62 |
+
# Add fluency/style suggestions (other suggestions)
|
| 63 |
+
# Only add if the word isn't already in spell suggestions
|
| 64 |
+
spell_originals = {s["original"] for s in result["spell_suggestions"]}
|
| 65 |
+
words = text.split()
|
| 66 |
+
|
| 67 |
+
for word in words:
|
| 68 |
+
# Skip words that have spell suggestions or already have punctuation
|
| 69 |
+
if word in spell_originals or word.endswith(('.', '?', '!')):
|
| 70 |
continue
|
| 71 |
+
|
| 72 |
+
# Add style suggestions (like adding punctuation)
|
| 73 |
+
if not word.endswith('!'): # Only suggest adding exclamation if it doesn't already have one
|
| 74 |
+
result["other_suggestions"].append({
|
| 75 |
+
"original": word,
|
| 76 |
+
"corrected": word + "!",
|
| 77 |
+
"type": "other"
|
| 78 |
+
})
|
| 79 |
|
| 80 |
# Add entities and tags
|
| 81 |
doc = nlp(text)
|