Update src/streamlit_app.py
Browse files- src/streamlit_app.py +31 -11
src/streamlit_app.py
CHANGED
|
@@ -2,15 +2,8 @@ import streamlit as st
|
|
| 2 |
import pyphen
|
| 3 |
import re
|
| 4 |
from typing import List, Tuple
|
| 5 |
-
import nltk
|
| 6 |
-
from nltk.tokenize import word_tokenize
|
| 7 |
import string
|
| 8 |
-
|
| 9 |
-
# Download required NLTK data
|
| 10 |
-
try:
|
| 11 |
-
nltk.data.find('tokenizers/punkt')
|
| 12 |
-
except LookupError:
|
| 13 |
-
nltk.download('punkt')
|
| 14 |
|
| 15 |
# Initialize pyphen for syllable splitting
|
| 16 |
dic = pyphen.Pyphen(lang='en')
|
|
@@ -200,9 +193,36 @@ class PronunciationAnalyzer:
|
|
| 200 |
|
| 201 |
def tokenize_text(self, text: str) -> List[str]:
|
| 202 |
"""Tokenize text into words while preserving punctuation"""
|
| 203 |
-
#
|
| 204 |
-
|
| 205 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 206 |
|
| 207 |
def analyze_text(self, text: str) -> Tuple[List[Tuple[str, List[str]]], dict]:
|
| 208 |
"""Analyze text and return word-syllable pairs and statistics"""
|
|
|
|
| 2 |
import pyphen
|
| 3 |
import re
|
| 4 |
from typing import List, Tuple
|
|
|
|
|
|
|
| 5 |
import string
|
| 6 |
+
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
# Initialize pyphen for syllable splitting
|
| 9 |
dic = pyphen.Pyphen(lang='en')
|
|
|
|
| 193 |
|
| 194 |
def tokenize_text(self, text: str) -> List[str]:
|
| 195 |
"""Tokenize text into words while preserving punctuation"""
|
| 196 |
+
# Enhanced regex-based tokenization
|
| 197 |
+
# This pattern matches:
|
| 198 |
+
# - Words (including contractions like "don't")
|
| 199 |
+
# - Numbers
|
| 200 |
+
# - Punctuation marks
|
| 201 |
+
# - Preserves spacing
|
| 202 |
+
|
| 203 |
+
# Split text into tokens while preserving structure
|
| 204 |
+
pattern = r"(?:\w+(?:'\w+)?|\d+|[^\w\s])"
|
| 205 |
+
tokens = re.findall(pattern, text)
|
| 206 |
+
|
| 207 |
+
# Add spaces back where needed
|
| 208 |
+
result = []
|
| 209 |
+
text_pos = 0
|
| 210 |
+
|
| 211 |
+
for token in tokens:
|
| 212 |
+
# Find the token's position in the original text
|
| 213 |
+
token_pos = text.find(token, text_pos)
|
| 214 |
+
|
| 215 |
+
# Add any whitespace before the token
|
| 216 |
+
if token_pos > text_pos:
|
| 217 |
+
whitespace = text[text_pos:token_pos]
|
| 218 |
+
if whitespace.strip() == '': # Only add if it's pure whitespace
|
| 219 |
+
result.extend(list(whitespace))
|
| 220 |
+
|
| 221 |
+
result.append(token)
|
| 222 |
+
text_pos = token_pos + len(token)
|
| 223 |
+
|
| 224 |
+
# Filter out empty strings and normalize
|
| 225 |
+
return [token for token in result if token.strip()]
|
| 226 |
|
| 227 |
def analyze_text(self, text: str) -> Tuple[List[Tuple[str, List[str]]], dict]:
|
| 228 |
"""Analyze text and return word-syllable pairs and statistics"""
|