Julien Matuszak commited on
Commit
8b36677
·
verified ·
1 Parent(s): d0e1ffd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -14
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
- result["spell_suggestions"].append({
51
- "original": text,
52
- "corrected": spell_checked,
53
- "type": "spell"
54
- })
 
 
 
 
 
 
55
 
56
- # Add example other suggestions (you would replace this with your actual logic)
57
- for word in text.split():
58
- if word.endswith('.') or word.endswith('?') or word.endswith('!'):
 
 
 
 
 
59
  continue
60
- result["other_suggestions"].append({
61
- "original": word,
62
- "corrected": word + "!",
63
- "type": "other"
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)