Revisiting Tokenization: A Comparison of a Persian Tokenizer with Byte Pair Encoding and Perplexity
In the rapidly evolving field of Natural Language Processing (NLP), tokenization remains a cornerstone for enabling machines to process human languages. Four years ago, I released an open-source Persian tokenizer on GitHub. It was designed to address specific challenges in tokenizing Persian text. Today, I want to revisit this project and compare its approach with two widely discussed methods: Byte Pair Encoding (BPE) and perplexity-based tokenization.
Context: The Persian Language Challenges
Persian, a highly inflected language with a unique script, presents numerous tokenization challenges. Spaces are not always reliable word delimiters due to affixes, and compounding can blur word boundaries. Additionally, the absence of robust, widely available tools for Persian NLP four years ago motivated the creation of a simple, effective tokenizer for practical use.
The Approach Behind My Tokenizer
The tokenizer I developed followed a straightforward lookup-based method. It segmented text into tokens using a precompiled dictionary of Persian words and rules for handling affixes and common compounds. While basic, this method excelled in:
- Simplicity: A rule-based approach requiring no training data.
- Speed: Lightweight, with minimal computational overhead.
- Interpretability: Easy to modify or extend for specific tasks.
This approach was particularly suited to the Persian language at the time, given the scarcity of labeled data for more sophisticated tokenization models.
Byte Pair Encoding (BPE): A Compact Alternative
BPE is a subword tokenization method widely used in modern language models like GPT and BERT. It works by iteratively merging the most frequent pairs of characters or subwords, creating a compact and expressive vocabulary. When applied to Persian, BPE offers several advantages:
- Data-Driven: Learns the most efficient segmentation based on the training corpus.
- Generalisation: Captures out-of-vocabulary (OOV) words by breaking them into subwords.
- Compatibility: Easily integrates into pre-training pipelines for transformer-based models.
However, BPE has its limitations. It requires a significant amount of training data and computational resources, which may not always be available for low-resource languages like Persian. Additionally, the subword units it generates can sometimes lack linguistic coherence.
Perplexity and Tokenization Optimization
Perplexity-based tokenization optimizes a tokenizer by minimizing the perplexity of the resulting language model. This approach is data-driven and inherently tied to the performance of downstream tasks. For Persian, this could mean:
- Optimised Language Models: Tokens are chosen to maximise model efficiency and accuracy.
- Dynamic Adaptation: Perplexity can adapt the tokeniser to specific datasets or domains.
Despite these advantages, perplexity-based tokenisation often involves heavy computational costs and complexity. It may also prioritize model performance over linguistic or interpretive clarity, which can be a drawback for languages with intricate morphology like Persian.
Comparing the Approaches
Reflections and Future Directions
Looking back, my tokenizer filled an important gap in Persian NLP, offering a lightweight and practical solution for developers and researchers. However, the landscape has changed. With the advent of BPE and perplexity-driven methods, modern NLP workflows benefit from greater scalability and task-specific optimization.
Get Samira Korani’s stories in your inbox
Join Medium for free to get updates from this writer.
That said, rule-based approaches like mine retain their relevance in:
- Low-Resource Scenarios: Where data scarcity limits the application of data-driven methods.
- Specific Use Cases: Where linguistic rules and interpretability are paramount.
Moving forward, integrating the strengths of rule-based systems with data-driven methods could provide the best of both worlds for Persian NLP. For instance, initializing BPE vocabularies with linguistically informed rules might enhance performance on morphologically rich languages.
Conclusion
Tokenization is not a one-size-fits-all problem. The Persian tokeniser demonstrated the power of simplicity and linguistic insight in addressing specific challenges. As we continue to innovate in NLP, revisiting and blending diverse approaches will be key to advancing the field, particularly for underrepresented languages like Persian.








