import re
import math
import random
from collections import defaultdict, Counter
class TextSimplifier:
def __init__(self):
# Initialize vocabulary mappings
self.complex_to_simple = self._build_vocabulary_mapping()
self.internet_slang = self._build_internet_slang() # Build this first
self.phrase_mappings = self._build_phrase_mappings() # Now this can use internet_slang
self.greeting_patterns = self._build_greeting_patterns()
self.conversational_responses = self._build_conversational_responses()
# Simple neural network weights (initialized randomly)
self.weights = {}
self.bias = {}
self.learning_rate = 0.01
# Training data for the ML model
self.training_data = self._generate_training_data()
# Initialize and train the model
self._initialize_model()
self._train_model()
def _build_vocabulary_mapping(self):
"""Build comprehensive vocabulary mapping from complex to simple words"""
return {
# Formal greetings
'salutations': 'hey',
'greetings': 'hello',
'good_morning': 'hey',
'good_afternoon': 'hey',
'good_evening': 'hey',
'pleased_to_meet_you': 'nice to meet you',
# Modern internet expressions
'laughing': 'lol',
'hilarious': 'lmao',
'amusing': 'funny',
'humorous': 'funny',
'comical': 'funny',
'ridiculous': 'crazy',
'insane': 'crazy',
'unbelievable': 'omg',
'shocking': 'omg',
'surprising': 'wow',
'astonishing': 'wow',
# Formal inquiries
'how_are_you_doing': 'how are you',
'how_do_you_do': 'how are you',
'how_have_you_been': 'how are you',
'what_is_your_status': 'how are you',
'how_are_things': 'how are you',
'how_is_everything': 'how are you',
# Complex words to simple + modern slang
'excellent': 'great',
'outstanding': 'awesome',
'magnificent': 'amazing',
'superb': 'great',
'tremendous': 'awesome',
'extraordinary': 'amazing',
'remarkable': 'cool',
'incredible': 'amazing',
'fantastic': 'awesome',
'wonderful': 'great',
'marvelous': 'amazing',
'spectacular': 'awesome',
'phenomenal': 'sick',
'exceptional': 'dope',
'impressive': 'sick',
'astounding': 'crazy',
'stupendous': 'insane',
# Intensifiers
'extraordinarily': 'super',
'tremendously': 'really',
'exceptionally': 'really',
'remarkably': 'really',
'incredibly': 'super',
'particularly': 'really',
'extremely': 'super',
'highly': 'really',
'very_much': 'a lot',
'greatly': 'a lot',
'significantly': 'a lot',
'considerably': 'a lot',
'immensely': 'so much',
'profoundly': 'deeply',
# Formal words to casual
'certainly': 'sure',
'absolutely': 'totally',
'definitely': 'for sure',
'indeed': 'yeah',
'affirmative': 'yes',
'negative': 'no',
'perhaps': 'maybe',
'possibly': 'maybe',
'probably': 'probs',
'obviously': 'obv',
'apparently': 'seems like',
'evidently': 'clearly',
# Professional terms
'assist': 'help',
'assistance': 'help',
'facilitate': 'help',
'accommodate': 'help',
'provide': 'give',
'obtain': 'get',
'acquire': 'get',
'purchase': 'buy',
'utilize': 'use',
'implement': 'do',
'execute': 'do',
'accomplish': 'do',
'achieve': 'do',
'endeavor': 'try',
'attempt': 'try',
# Academic/formal to internet casual
'furthermore': 'also',
'moreover': 'plus',
'additionally': 'also',
'consequently': 'so',
'therefore': 'so',
'subsequently': 'then',
'previously': 'before',
'currently': 'rn',
'presently': 'now',
'immediately': 'asap',
'instantaneously': 'instantly',
'simultaneously': 'at the same time',
# Emotions with modern expressions
'delighted': 'happy',
'thrilled': 'hyped',
'ecstatic': 'super happy',
'content': 'happy',
'satisfied': 'happy',
'disappointed': 'bummed',
'devastated': 'crushed',
'frustrated': 'annoyed',
'irritated': 'annoyed',
'concerned': 'worried',
'anxious': 'stressed',
'melancholy': 'sad',
'miserable': 'feeling bad',
'furious': 'mad',
'enraged': 'pissed',
'exhausted': 'tired af',
'fatigued': 'tired',
# Actions
'communicate': 'talk',
'converse': 'chat',
'discuss': 'talk about',
'inquire': 'ask',
'respond': 'reply',
'reply': 'answer',
'depart': 'leave',
'arrive': 'come',
'attend': 'go to',
'participate': 'join',
'procrastinate': 'put off',
'contemplate': 'think about',
'comprehend': 'understand',
'appreciate': 'get',
# Common formal phrases to casual
'thank_you_very_much': 'thanks',
'i_appreciate_it': 'thanks',
'much_obliged': 'thanks',
'you_are_welcome': 'np',
'my_pleasure': 'np',
'excuse_me': 'sorry',
'i_beg_your_pardon': 'sorry',
'i_apologize': 'my bad',
'apologies': 'sorry',
# Internet specific
'information': 'info',
'photograph': 'pic',
'photographic': 'photo',
'application': 'app',
'advertisement': 'ad',
'examination': 'exam',
'mathematics': 'math',
'laboratory': 'lab',
'dormitory': 'dorm',
'refrigerator': 'fridge',
'automobile': 'car',
'telephone': 'phone',
'television': 'tv',
}
def _build_internet_slang(self):
"""Build modern internet slang and abbreviations"""
return {
# Common abbreviations
'by the way': 'btw',
'in my opinion': 'imo',
'in my humble opinion': 'imho',
'to be honest': 'tbh',
'for your information': 'fyi',
'as far as i know': 'afaik',
'oh my god': 'omg',
'oh my gosh': 'omg',
'laugh out loud': 'lol',
'rolling on floor laughing': 'rofl',
'laughing my ass off': 'lmao',
'what the fuck': 'wtf',
'what the hell': 'wth',
'i don\'t know': 'idk',
'i don\'t care': 'idc',
'nevermind': 'nvm',
'not gonna lie': 'ngl',
'shake my head': 'smh',
'in real life': 'irl',
'direct message': 'dm',
'private message': 'pm',
'be right back': 'brb',
'away from keyboard': 'afk',
'good game': 'gg',
'for the win': 'ftw',
'you only live once': 'yolo',
'fear of missing out': 'fomo',
'today i learned': 'til',
'too long didn\'t read': 'tldr',
'face to face': 'f2f',
'for what it\'s worth': 'fwiw',
'not safe for work': 'nsfw',
# Modern slang expressions
'that\'s suspicious': 'sus',
'no problem': 'no prob',
'definitely': 'def',
'probably': 'prob',
'obviously': 'obv',
'seriously': 'srsly',
'totally': 'totes',
'whatever': 'whatev',
'because': 'cuz',
'though': 'tho',
'about to': 'bout to',
'going to': 'gonna',
'want to': 'wanna',
'got to': 'gotta',
'kind of': 'kinda',
'sort of': 'sorta',
'don\'t know': 'dunno',
'let me': 'lemme',
'give me': 'gimme',
'come on': 'cmon',
'all right': 'aight',
# Gen Z expressions
'that\'s cool': 'that slaps',
'very good': 'bussin',
'looking good': 'drip',
'showing off': 'flexing',
'lying': 'cap',
'truth': 'no cap',
'basic': 'basic',
'dramatic': 'extra',
'awesome': 'fire',
'great': 'lit',
'cool': 'dope',
'amazing': 'sick',
'angry': 'salty',
'bitter': 'salty',
'throw away': 'yeet',
'excited': 'hyped',
'cringe': 'cringe',
'awkward': 'awk',
'embarrassing': 'cringe',
'understand': 'vibe with',
'relate': 'felt that',
'agree': 'facts',
'true': 'fax',
'gossip': 'tea',
'drama': 'tea',
'shocked': 'shook',
'dead': 'ded',
'crying': 'im crying',
'can\'t even': 'i cant even',
}
def _build_phrase_mappings(self):
"""Build phrase-level transformations"""
base_mappings = {
# Formal questions to casual
r'how are you doing today\?': 'how are you?',
r'how have you been lately\?': 'how are you?',
r'what is your current status\?': 'how are you?',
r'how are things going\?': 'how are you?',
r'how is everything with you\?': 'how are you?',
r'i hope you are doing well': 'hope you\'re good',
r'i trust you are well': 'hope you\'re good',
r'what\'s going on\?': 'what\'s up?',
r'what is happening\?': 'what\'s up?',
r'what are you up to\?': 'wyd?',
r'what are you doing\?': 'wyd?',
# Professional to casual
r'i would like to inquire about': 'i wanna ask about',
r'i am writing to inform you': 'just letting you know',
r'please be advised that': 'heads up',
r'i wanted to let you know': 'just fyi',
r'i am pleased to inform you': 'good news',
r'i regret to inform you': 'bad news',
r'for your information': 'fyi',
r'as soon as possible': 'asap',
r'at your earliest convenience': 'when you can',
# Modern conversational
r'that\'s really cool': 'that\'s dope',
r'that\'s very interesting': 'that\'s sick',
r'i\'m very tired': 'i\'m dead',
r'i\'m exhausted': 'i\'m done',
r'that\'s hilarious': 'i\'m dying',
r'that\'s so funny': 'lmaooo',
r'i can\'t believe': 'i can\'t even',
r'oh my goodness': 'omg',
r'what in the world': 'wtf',
r'are you serious': 'fr?',
r'for real\?': 'fr?',
r'no way': 'no cap?',
r'i\'m not lying': 'no cap',
r'to be honest': 'tbh',
r'not going to lie': 'ngl',
r'in my opinion': 'imo',
# Emotional expressions
r'i\'m feeling great': 'i\'m vibing',
r'i\'m doing well': 'i\'m good',
r'i\'m not doing well': 'not great tbh',
r'i\'m very happy': 'i\'m so happy rn',
r'that makes me sad': 'that\'s sad',
r'i don\'t understand': 'i\'m confused',
r'that\'s confusing': 'wait what',
r'i agree with you': 'facts',
r'you\'re right': 'ur right',
r'absolutely correct': 'facts',
r'i disagree': 'nah',
r'that\'s incorrect': 'that\'s cap',
# Time expressions
r'right now': 'rn',
r'at this moment': 'rn',
r'talk to you later': 'ttyl',
r'see you later': 'cya',
r'goodbye': 'bye',
r'have a good day': 'have a good one',
r'take care': 'tc',
# Reactions
r'that\'s amazing': 'that\'s fire',
r'that\'s terrible': 'that sucks',
r'i\'m shocked': 'i\'m shook',
r'unbelievable': 'can\'t even',
r'that\'s disgusting': 'ew',
r'that\'s weird': 'that\'s sus',
r'that\'s strange': 'weird flex but ok',
}
# Add internet slang phrases
for formal, slang in self.internet_slang.items():
pattern = r'\b' + formal.replace(' ', r'\s+') + r'\b'
base_mappings[pattern] = slang
return base_mappings
def _build_greeting_patterns(self):
"""Build greeting pattern recognition"""
return {
'formal_greetings': [
r'good morning.*', r'good afternoon.*', r'good evening.*',
r'greetings.*', r'salutations.*', r'hello there.*'
],
'casual_greetings': [
r'hey.*', r'hi.*', r'hello.*', r'yo.*', r'sup.*',
r'wassup.*', r'what\'s up.*', r'howdy.*'
],
'questions_about_wellbeing': [
r'how are you.*', r'how have you been.*', r'how are things.*',
r'how is everything.*', r'what.*up.*', r'how.*doing.*',
r'how.*going.*', r'you good.*', r'you okay.*'
],
'thanks_expressions': [
r'thank you.*', r'thanks.*', r'i appreciate.*', r'much obliged.*',
r'thx.*', r'ty.*', r'tysm.*', r'thank u.*'
]
}
def _build_conversational_responses(self):
"""Build conversational response patterns"""
return {
'agreement': [
'yeah totally', 'for sure', 'facts', 'definitely', 'yup',
'absolutely', 'you got it', 'right on', '100%', 'bet'
],
'disagreement': [
'nah', 'not really', 'idk about that', 'hmm not sure',
'that\'s cap', 'no way', 'doubt it'
],
'excitement': [
'omg yes', 'that\'s sick', 'no way', 'yooo', 'let\'s gooo',
'hype', 'that\'s fire', 'amazing'
],
'confusion': [
'wait what', 'huh', 'wdym', 'i\'m lost', 'confused af',
'what now', 'come again?'
],
'acknowledgment': [
'got it', 'makes sense', 'i see', 'ah ok', 'word',
'heard', 'copy that', 'roger'
]
}
def _generate_training_data(self):
"""Generate comprehensive training data for the ML model"""
training_examples = [
# Greetings
("Good morning, how are you doing today?", "hey, how are you?"),
("Greetings! I hope you are well.", "hey! hope you're good"),
("Good afternoon, how have you been?", "hey, how are you?"),
("Salutations, my friend!", "hey friend!"),
("Hello there, how are things?", "hey, what's up?"),
("What's going on with you?", "what's up?"),
("How's it going?", "how are you?"),
# Modern casual conversations
("That's absolutely hilarious!", "lmaooo"),
("I can't believe what just happened", "i can't even"),
("Are you being serious right now?", "fr?"),
("That's really suspicious behavior", "that's sus"),
("I'm extremely exhausted today", "i'm dead tired"),
("This food is exceptionally delicious", "this food is bussin"),
("Your outfit looks amazing", "your fit is fire"),
("I'm genuinely shocked by this", "i'm shook"),
("That's incredibly cool", "that's sick"),
("I completely agree with your opinion", "facts"),
# Internet expressions
("To be honest, I don't really care", "tbh idc"),
("I don't know what you mean", "idk wym"),
("Oh my god, that's incredible", "omg that's amazing"),
("Not going to lie, that's impressive", "ngl that's dope"),
("For your information, the meeting is cancelled", "fyi the meeting is cancelled"),
("By the way, did you see that?", "btw did you see that?"),
("In my opinion, that's not correct", "imo that's wrong"),
("What the hell is happening?", "wth is going on?"),
# Formal inquiries to casual
("I would like to inquire about your status", "wanna know how you are"),
("How is everything going with you?", "how's it going?"),
("I trust you are doing well", "hope you're good"),
("What is your current situation?", "what's up with you?"),
("How have you been lately?", "how you been?"),
# Professional language to casual
("I am writing to inform you about the meeting", "heads up about the meeting"),
("Please be advised that the event is cancelled", "heads up the event is cancelled"),
("I wanted to let you know about the update", "fyi there's an update"),
("Thank you for your assistance with this matter", "thanks for the help"),
("I appreciate your time and effort", "thanks"),
("At your earliest convenience", "when you can"),
("As soon as possible", "asap"),
# Emotional expressions
("I am absolutely delighted to hear that", "i'm so happy to hear that"),
("That is truly magnificent news", "that's amazing news"),
("I am quite disappointed about this", "i'm bummed about this"),
("This is extraordinarily wonderful", "this is awesome"),
("I am tremendously excited", "i'm hyped"),
("I'm feeling melancholy today", "feeling sad today"),
("That's making me furious", "that's making me mad"),
("I'm concerned about the situation", "i'm worried about this"),
# Modern reactions
("That's the best thing ever!", "that's fire!"),
("This situation is terrible", "this sucks"),
("I can't handle this anymore", "i'm done"),
("That's extremely weird", "that's sus af"),
("You're absolutely right about that", "you're right, facts"),
("I strongly disagree with that statement", "nah that's cap"),
("This is making me laugh so hard", "i'm dying lol"),
("That's genuinely surprising", "wait what"),
# Conversational flow
("What are your thoughts on this matter?", "what do you think?"),
("Could you elaborate on that point?", "wdym?"),
("I need to depart now", "gotta go"),
("It was pleasant talking with you", "nice talking to you"),
("Until we meet again", "see ya"),
("Have a wonderful day", "have a good one"),
("Take care of yourself", "take care"),
# Complex to simple with slang
("The weather is absolutely magnificent today", "the weather is amazing today"),
("This is an extraordinarily difficult problem", "this is a really hard problem"),
("I am tremendously grateful for your help", "thanks so much for your help"),
("That was an outstanding performance", "that was sick"),
("Your presentation was phenomenal", "your presentation was fire"),
("The party was incredibly enjoyable", "the party was lit"),
# Internet culture
("That's creating a lot of drama", "that's creating tea"),
("Stop showing off so much", "stop flexing"),
("You're being overly dramatic", "you're being extra"),
("I understand what you mean", "i vibe with that"),
("That's completely true", "that's fax"),
("I'm leaving now", "i'm out"),
("Let's do this", "let's gooo"),
("That's embarrassing", "that's cringe"),
# Modern youth expressions
("That music is really good", "that music slaps"),
("You look very stylish", "you got drip"),
("I'm throwing this away", "gonna yeet this"),
("Don't lie to me", "don't cap"),
("I'm telling the truth", "no cap"),
("That made me very angry", "that made me salty"),
("The food tastes amazing", "the food is bussin"),
("That's basic and unoriginal", "that's basic"),
]
return training_examples
def _initialize_model(self):
"""Initialize simple neural network model"""
# Feature dimensions
self.input_size = 100 # Word embedding size
self.hidden_size = 50
self.output_size = 100
# Initialize weights randomly
self.weights['W1'] = [[random.uniform(-1, 1) for _ in range(self.hidden_size)]
for _ in range(self.input_size)]
self.weights['W2'] = [[random.uniform(-1, 1) for _ in range(self.output_size)]
for _ in range(self.hidden_size)]
# Initialize biases
self.bias['b1'] = [random.uniform(-1, 1) for _ in range(self.hidden_size)]
self.bias['b2'] = [random.uniform(-1, 1) for _ in range(self.output_size)]
# Word to index mapping
self.word_to_index = {}
self.index_to_word = {}
self._build_vocabulary()
def _build_vocabulary(self):
"""Build vocabulary from training data"""
all_words = set()
for complex_text, simple_text in self.training_data:
words = self._tokenize(complex_text.lower()) + self._tokenize(simple_text.lower())
all_words.update(words)
# Add vocabulary mapping words
for word in self.complex_to_simple.keys():
all_words.add(word)
for word in self.complex_to_simple.values():
all_words.add(word)
# Add internet slang
for phrase in self.internet_slang.keys():
all_words.update(phrase.split())
for slang in self.internet_slang.values():
all_words.update(slang.split())
# Create word-to-index mapping
for i, word in enumerate(sorted(all_words)):
self.word_to_index[word] = i
self.index_to_word[i] = word
def _tokenize(self, text):
"""Simple tokenization"""
# Remove punctuation and split
text = re.sub(r'[^\w\s\']', '', text.lower())
return text.split()
def _text_to_vector(self, text):
"""Convert text to feature vector"""
words = self._tokenize(text)
vector = [0.0] * self.input_size
for word in words:
if word in self.word_to_index:
idx = self.word_to_index[word] % self.input_size
vector[idx] += 1.0
# Normalize
total = sum(vector)
if total > 0:
vector = [v / total for v in vector]
return vector
def _sigmoid(self, x):
"""Sigmoid activation function"""
return 1 / (1 + math.exp(-max(-500, min(500, x))))
def _forward_pass(self, input_vector):
"""Forward pass through the network"""
# Hidden layer
hidden = []
for i in range(self.hidden_size):
weighted_sum = sum(input_vector[j] * self.weights['W1'][j][i]
for j in range(self.input_size)) + self.bias['b1'][i]
hidden.append(self._sigmoid(weighted_sum))
# Output layer
output = []
for i in range(self.output_size):
weighted_sum = sum(hidden[j] * self.weights['W2'][j][i]
for j in range(self.hidden_size)) + self.bias['b2'][i]
output.append(self._sigmoid(weighted_sum))
return hidden, output
def _train_model(self):
"""Train the neural network model silently"""
epochs = 30 # Balanced for performance
for epoch in range(epochs):
total_error = 0
for complex_text, simple_text in self.training_data:
try:
# Convert to vectors
input_vec = self._text_to_vector(complex_text)
target_vec = self._text_to_vector(simple_text)
# Forward pass
hidden, output = self._forward_pass(input_vec)
# Calculate error (simplified)
if len(output) == len(target_vec):
error = sum((target_vec[i] - output[i]) ** 2 for i in range(len(output)))
total_error += error
# Simple weight update (simplified backpropagation)
for i in range(min(self.output_size, len(output))):
for j in range(self.hidden_size):
if i < len(target_vec):
gradient = 2 * (output[i] - target_vec[i]) * output[i] * (1 - output[i])
self.weights['W2'][j][i] -= self.learning_rate * gradient * hidden[j]
except Exception:
# Skip problematic training examples
continue
def _apply_vocabulary_mapping(self, text):
"""Apply direct vocabulary mappings"""
words = self._tokenize(text)
result = []
for word in words:
if word in self.complex_to_simple:
result.append(self.complex_to_simple[word])
else:
result.append(word)
return ' '.join(result)
def _apply_phrase_mappings(self, text):
"""Apply phrase-level transformations"""
result = text.lower()
for pattern, replacement in self.phrase_mappings.items():
result = re.sub(pattern, replacement, result, flags=re.IGNORECASE)
return result
def _classify_intent(self, text):
"""Classify the intent of the input text"""
text_lower = text.lower()
# Check for greetings
for pattern in self.greeting_patterns['formal_greetings']:
if re.search(pattern, text_lower):
return 'greeting'
for pattern in self.greeting_patterns['casual_greetings']:
if re.search(pattern, text_lower):
return 'casual_greeting'
# Check for wellbeing questions
for pattern in self.greeting_patterns['questions_about_wellbeing']:
if re.search(pattern, text_lower):
return 'wellbeing_question'
# Check for thanks
for pattern in self.greeting_patterns['thanks_expressions']:
if re.search(pattern, text_lower):
return 'thanks'
# Check for questions
if '?' in text or any(q in text_lower for q in ['what', 'who', 'where', 'when', 'why', 'how']):
return 'question'
# Check for excitement
if any(word in text_lower for word in ['amazing', 'awesome', 'incredible', 'fantastic', '!']):
return 'excitement'
# Check for agreement/disagreement
if any(word in text_lower for word in ['agree', 'right', 'correct', 'true']):
return 'agreement'
if any(word in text_lower for word in ['disagree', 'wrong', 'incorrect', 'false']):
return 'disagreement'
return 'general'
def _apply_intent_based_simplification(self, text, intent):
"""Apply simplification based on classified intent"""
if intent == 'greeting':
return random.choice(['hey', 'hi', 'hello', 'yo'])
elif intent == 'casual_greeting':
return text.lower()
elif intent == 'wellbeing_question':
return random.choice(['how are you?', 'what\'s up?', 'how\'s it going?', 'you good?'])
elif intent == 'thanks':
return random.choice(['thanks', 'thx', 'ty', 'thanks!'])
elif intent == 'excitement':
if 'amazing' in text.lower() or 'incredible' in text.lower():
return random.choice(['that\'s sick!', 'that\'s fire!', 'omg yes!', 'let\'s gooo!'])
elif intent == 'agreement':
return random.choice(self.conversational_responses['agreement'])
elif intent == 'disagreement':
return random.choice(self.conversational_responses['disagreement'])
elif intent == 'question' and 'what' in text.lower() and 'mean' in text.lower():
return 'wdym?'
return text
def _ml_simplify(self, text):
"""Use ML model to simplify text"""
input_vec = self._text_to_vector(text)
hidden, output = self._forward_pass(input_vec)
# Find most activated words in output
top_indices = sorted(range(len(output)), key=lambda i: output[i], reverse=True)[:5]
result_words = []
for idx in top_indices:
if idx < len(self.index_to_word) and output[idx] > 0.5:
result_words.append(self.index_to_word[idx])
return ' '.join(result_words) if result_words else text
def simplify(self, text):
"""Main simplification function combining all techniques"""
if not text or not text.strip():
return text
# Step 1: Classify intent
intent = self._classify_intent(text)
# Step 2: Apply intent-based simplification for common patterns
intent_result = self._apply_intent_based_simplification(text, intent)
if intent_result != text:
return intent_result
# Step 3: Apply phrase mappings
phrase_result = self._apply_phrase_mappings(text)
# Step 4: Apply vocabulary mappings
vocab_result = self._apply_vocabulary_mapping(phrase_result)
# Step 5: Apply ML model for additional simplification
ml_result = self._ml_simplify(vocab_result)
# Step 6: Post-processing cleanup
final_result = self._post_process(ml_result if ml_result.strip() else vocab_result)
return final_result
def _post_process(self, text):
"""Post-process the simplified text"""
# Remove extra spaces
text = re.sub(r'\s+', ' ', text.strip())
# Handle question marks
question_words = ['how', 'what', 'why', 'when', 'where', 'who', 'wyd', 'wdym', 'fr']
if any(word in text.lower() for word in question_words):
if not text.endswith('?'):
text += '?'
# Don't capitalize certain internet slang
no_caps = ['lol', 'lmao', 'omg', 'wtf', 'wth', 'idk', 'idc', 'ngl', 'tbh',
'imo', 'imho', 'fyi', 'asap', 'rn', 'af', 'btw', 'smh', 'rofl']
if text and not any(text.lower().startswith(word) for word in no_caps):
text = text[0].upper() + text[1:]
return text
def get_conversational_response(self, text):
"""Generate a conversational response based on the input"""
simplified = self.simplify(text)
intent = self._classify_intent(text)
# Add some conversational flair based on intent
if intent == 'wellbeing_question':
responses = [
"i'm good! " + simplified,
"doing great! " + simplified,
"all good here! " + simplified,
"vibing! " + simplified
]
return random.choice(responses)
return simplified
def batch_simplify(self, texts):
"""Simplify multiple texts at once"""
return [self.simplify(text) for text in texts]
def add_custom_mapping(self, complex_word, simple_word):
"""Add custom vocabulary mapping"""
self.complex_to_simple[complex_word.lower()] = simple_word.lower()
def get_simplification_confidence(self, original, simplified):
"""Calculate confidence score for simplification"""
original_words = set(self._tokenize(original.lower()))
simplified_words = set(self._tokenize(simplified.lower()))
# Calculate word reduction ratio
reduction_ratio = 1 - (len(simplified_words) / max(len(original_words), 1))
# Calculate known word mapping ratio
mapped_words = sum(1 for word in original_words if word in self.complex_to_simple)
mapping_ratio = mapped_words / max(len(original_words), 1)
# Check for slang usage
slang_words = sum(1 for word in simplified_words
if word in self.internet_slang.values())
slang_ratio = slang_words / max(len(simplified_words), 1)
# Combined confidence score
confidence = (reduction_ratio * 0.3) + (mapping_ratio * 0.4) + (slang_ratio * 0.3)
return min(1.0, max(0.0, confidence))
# Example usage and testing
def test_text_simplifier():
"""Test the text simplifier with various examples"""
simplifier = TextSimplifier()
test_cases = [
# Original test cases
"Good morning, how are you doing today?",
"I would like to inquire about your current status",
"Thank you very much for your assistance",
# Modern slang tests
"That's absolutely hilarious!",
"I can't believe what just happened",
"To be honest, I don't really care about that",
"Oh my god, that's incredible!",
"Not going to lie, that was impressive",
# Internet culture
"That outfit looks amazing on you",
"This food is exceptionally delicious",
"Are you being serious right now?",
"That's really suspicious behavior",
"I'm extremely exhausted today",
# Conversational
"What are your thoughts on this matter?",
"I strongly disagree with that statement",
"That's the best thing I've ever seen!",
"I'm feeling melancholy today",
"Your presentation was phenomenal",
# Mixed formal and casual
"By the way, the meeting is at 3pm",
"For your information, the project is complete",
"I appreciate your prompt response",
"The weather is absolutely magnificent today",
"That party was incredibly enjoyable"
]
print("š„ === Enhanced Text Simplification Results === š„\n")
for i, text in enumerate(test_cases, 1):
simplified = simplifier.simplify(text)
confidence = simplifier.get_simplification_confidence(text, simplified)
# Emoji based on confidence
if confidence >= 0.5:
emoji = "šÆ"
elif confidence >= 0.3:
emoji = "āØ"
else:
emoji = "š"
print(f"{i:2d}. Original: {text}")
print(f" Simplified: {simplified} {emoji}")
print(f" Confidence: {confidence:.2f}")
print()
def demo_modern_conversations():
"""Demo modern conversational examples"""
simplifier = TextSimplifier()
conversations = [
("User", "Hey there! How's everything going with you today?"),
("User", "I just saw the most incredible thing ever!"),
("User", "To be honest, I'm not feeling great about this situation"),
("User", "Oh my goodness, did you see what happened?"),
("User", "That's extremely suspicious if you ask me"),
("User", "I'm absolutely exhausted from all this work"),
("User", "Your new hairstyle looks phenomenal!"),
("User", "Not going to lie, that was pretty impressive"),
("User", "What do you think about the new update?"),
("User", "I completely disagree with that opinion"),
]
print("\nš¬ === Modern Conversation Demo === š¬\n")
for speaker, text in conversations:
response = simplifier.get_conversational_response(text)
print(f"{speaker}: {text}")
print(f"Bot: {response}")
print()
def interactive_simplifier():
"""Interactive text simplification with modern slang"""
simplifier = TextSimplifier()
print("š === Interactive Modern Text Simplifier === š")
print("š¬ Type any text and watch it get simplified with modern slang!")
print("š„ Try formal language, emotions, questions, reactions...")
print("ā Type 'quit', 'exit', or 'q' to stop")
print("=" * 60)
example_suggestions = [
"That's absolutely incredible!",
"I'm extremely tired from work today",
"Oh my god, did you see that?",
"To be honest, I don't understand",
"Your outfit looks amazing!",
"Not going to lie, that was awesome",
"I completely agree with your opinion",
"Are you being serious right now?"
]
print("\nš„ Try these examples:")
for i, example in enumerate(example_suggestions[:4], 1):
print(f"{i}. {example}")
print()
conversation_history = []
while True:
try:
user_input = input("š¤ You: ").strip()
if user_input.lower() in ['quit', 'exit', 'q']:
print("āļø Later! Thanks for chatting!")
break
if not user_input:
print("š Say something...")
continue
# Process and respond
response = simplifier.get_conversational_response(user_input)
confidence = simplifier.get_simplification_confidence(user_input, response)
# Show the bot response
print(f"š¤ Bot: {response}")
# Add to conversation history
conversation_history.append((user_input, response))
# Show stats occasionally
if len(conversation_history) % 5 == 0:
print(f"\nš Conversation streak: {len(conversation_history)} exchanges!")
print()
except KeyboardInterrupt:
print("\n\nāļø Peace out! Thanks for the chat!")
break
except Exception as e:
print(f"š¬ Oops: {e}")
print("Try again...")
continue
if __name__ == "__main__":
print("š Modern Text Simplification System Ready!")
print("=" * 50)
# Run tests first to show capabilities
test_text_simplifier()
# Then show conversation demo
demo_modern_conversations()
# Start interactive mode
print("\n" + "=" * 60)
interactive_simplifier()