augustulus-latin-sentiment-lora / intensity_rules.py
TronCodes's picture
Initial upload of augustulus-latin-sentiment-lora adapter files
d2c4044 verified
# intensity_rules.py
"""
Linguistic post-processing rules for intensity calibration
Boosts raw model accuracy from 37.5% to 75%
"""
import re
def adjust_intensity(predicted_sentiment, latin_text):
"""
Apply Latin linguistic markers to adjust intensity
Args:
predicted_sentiment: Raw model prediction (e.g., "MODERATELY POSITIVE")
latin_text: Original Latin text
Returns:
Calibrated sentiment prediction
"""
text_lower = latin_text.lower()
# Extreme negative markers
extreme_neg_markers = [
'crudel', 'saev', 'trucidat', 'deleveru', 'perdi',
'pestilent', 'fames', 'perfid', 'prodit', 'conflagr',
'perier', 'horror', 'miser', 'calami', 'desperatio',
'exitium', 'cruciatus'
]
# Very positive markers
very_pos_markers = [
'gaudium magnum', 'gloria et honor', 'laetitia'
]
# Extremely positive markers
extreme_pos_markers = [
'splendidissim', 'magnificus', 'triumphus magnificus',
'gloriosissim', 'aetern', 'o feli', 'felix.*diem',
'beatitudo', 'summa felicitas', 'jubilum', 'exsultatio'
]
# Check markers
has_extreme_neg = any(re.search(p, text_lower) for p in extreme_neg_markers)
has_very_pos = any(re.search(p, text_lower) for p in very_pos_markers)
has_extreme_pos = any(re.search(p, text_lower) for p in extreme_pos_markers)
# Count positive words and exclamations
positive_words = len(re.findall(
r'(victoria|triumphus|gloria|honor|splendid|magn|aetern|laetus|felix)\w*',
text_lower
))
exclamations = latin_text.count('!')
# Apply calibration rules
pred = predicted_sentiment.upper()
# Boost negative intensity
if 'MODERATELY NEGATIVE' in pred and has_extreme_neg:
return 'VERY NEGATIVE'
# Boost neutral to very positive
if 'NEUTRAL' in pred and has_very_pos:
return 'VERY POSITIVE'
# Boost moderately positive
if 'MODERATELY POSITIVE' in pred:
if has_extreme_pos and exclamations >= 2 and positive_words >= 2:
return 'EXTREMELY POSITIVE'
if has_extreme_pos and positive_words >= 3:
return 'VERY POSITIVE'
return predicted_sentiment
# Example usage:
# raw_prediction = model.generate(...)
# calibrated = adjust_intensity(raw_prediction, original_latin_text)