This project implements a simple translator between Ilocano and English languages using Context-Free Grammars (CFG) and the Cocke-Younger-Kasami (CYK) algorithm. The system validates sentences in both languages against their respective grammars and provides translations for recognized Ilocano sentences.
- Context-Free Grammar parsing using CYK algorithm
- Grammar validation for both Ilocano and English sentences
- Basic translation from Ilocano to English
- Support for imperative sentences
- Python 3.x
- NLTK library (
pip install nltk)
CYKAlgorithm.py- Main implementation file containing the CYK algorithm and translation logicenglish_cfg.txt- Context-free grammar rules for Englishenglish_imperative.txt- Sample English sentences for testingilocano_cfg.txt- Context-free grammar rules for Ilocanoilocano_imperative.txt- Sample Ilocano sentences for testing
- The system reads grammar rules from text files.
- Input sentences are tokenized using NLTK.
- The CYK algorithm validates if sentences conform to their respective grammars.
- For Ilocano sentences, translations to English are provided via a dictionary mapping.
The CYK algorithm is implemented in the cyk_alg function and works by:
- Building a table to check if the input string belongs to the language defined by the grammar
- Filling the table bottom-up, first with terminals and then with non-terminals
- Determining if the input belongs to the grammar based on whether the top-level structure exists
The translation is handled by the ilocano_to_english_translator function using a dictionary mapping.
Run the main script to validate and translate the example sentences:
python CYKAlgorithm.pyThe output will show:
- The parsing table for each input sentence
- Whether the sentence belongs to the grammar
- Translations for the Ilocano sentences
- Add more grammar rules to
ilocano_cfg.txtandenglish_cfg.txt - Extend the translation dictionary in the
ilocano_to_english_translatorfunction - Add more test sentences to
ilocano_imperative.txtandenglish_imperative.txt
- Currently supports only a small set of predefined sentences
- Grammar is limited to simple imperative structures
- Translation is based on exact matches rather than compositional semantics
Open source