J-Barrert's picture
Initial Upload
8e9e50e verified
Raw
History Blame Contribute Delete
1.88 kB
import csv
import os
class SIGSParser:
def __init__(self, csv_path):
self.lexicon = {}
self.load_lexicon(csv_path)
def load_lexicon(self, csv_path):
"""Loads the SIGS CSV into a fast lookup dictionary."""
if not os.path.exists(csv_path):
raise FileNotFoundError(f"❌ Could not find lexicon file at: {csv_path}")
with open(csv_path, mode='r', encoding='utf-8') as f:
reader = csv.DictReader(f)
for row in reader:
# Key = wire_token (e.g., 'agr001')
# Value = Definition + Category
token = row.get('wire_token', '').strip()
if token:
self.lexicon[token] = {
"title": row.get('title', 'N/A'),
"definition": row.get('definition', 'N/A'),
"category": row.get('category_name', 'N/A')
}
print(f"✅ SIGS Parser loaded {len(self.lexicon)} tokens.")
def parse(self, sigs_message):
"""
Takes a SIGS string (e.g., 'agr001 tim030') and returns details.
"""
tokens = sigs_message.split()
results = []
for t in tokens:
data = self.lexicon.get(t)
if data:
results.append(f"[{t.upper()}] {data['title']}: {data['definition']}")
else:
results.append(f"[{t.upper()}] ⚠️ Unknown Token")
return "\n".join(results)
# --- Example Usage ---
if __name__ == "__main__":
# Point this to your actual CSV file
parser = SIGSParser("The_SIGS_Lexicon_v1-STATIC_COPY-Master_Lexicon.csv")
# Test it
print("\n--- Decoding Message: 'agr001 mot004' ---")
print(parser.parse("agr001 mot004"))