nepali_unicoder 0.1.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- nepali_unicoder-0.1.1/LICENSE +21 -0
- nepali_unicoder-0.1.1/MANIFEST.in +1 -0
- nepali_unicoder-0.1.1/PKG-INFO +183 -0
- nepali_unicoder-0.1.1/README.md +165 -0
- nepali_unicoder-0.1.1/pyproject.toml +36 -0
- nepali_unicoder-0.1.1/setup.cfg +4 -0
- nepali_unicoder-0.1.1/src/nepali_unicoder/__init__.py +0 -0
- nepali_unicoder-0.1.1/src/nepali_unicoder/__main__.py +32 -0
- nepali_unicoder-0.1.1/src/nepali_unicoder/convert.py +13 -0
- nepali_unicoder-0.1.1/src/nepali_unicoder/engine.py +97 -0
- nepali_unicoder-0.1.1/src/nepali_unicoder/loader.py +84 -0
- nepali_unicoder-0.1.1/src/nepali_unicoder/rules.py +262 -0
- nepali_unicoder-0.1.1/src/nepali_unicoder/tokenizer.py +69 -0
- nepali_unicoder-0.1.1/src/nepali_unicoder/trie.py +47 -0
- nepali_unicoder-0.1.1/src/nepali_unicoder/word_maps.json +67 -0
- nepali_unicoder-0.1.1/src/nepali_unicoder.egg-info/PKG-INFO +183 -0
- nepali_unicoder-0.1.1/src/nepali_unicoder.egg-info/SOURCES.txt +20 -0
- nepali_unicoder-0.1.1/src/nepali_unicoder.egg-info/dependency_links.txt +1 -0
- nepali_unicoder-0.1.1/src/nepali_unicoder.egg-info/requires.txt +3 -0
- nepali_unicoder-0.1.1/src/nepali_unicoder.egg-info/top_level.txt +1 -0
- nepali_unicoder-0.1.1/tests/test_components.py +70 -0
- nepali_unicoder-0.1.1/tests/test_conversion.py +156 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 RealSanjeev
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
include src/nepali_unicoder/*.json
|
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: nepali_unicoder
|
|
3
|
+
Version: 0.1.1
|
|
4
|
+
Summary: A Python package for Roman to Nepali (Devanagari) transliteration
|
|
5
|
+
Author-email: realsanjeev <075bei033.sanjeev@pcampus.edu.np>
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/realsanjeev/nepali_unicoder
|
|
8
|
+
Keywords: nepali,unicoder,roman to devanagari,nepali transliteration,preeti to unicode
|
|
9
|
+
Classifier: Programming Language :: Python
|
|
10
|
+
Classifier: Programming Language :: Python :: 3
|
|
11
|
+
Classifier: Operating System :: OS Independent
|
|
12
|
+
Requires-Python: >=3.7
|
|
13
|
+
Description-Content-Type: text/markdown
|
|
14
|
+
License-File: LICENSE
|
|
15
|
+
Provides-Extra: dev
|
|
16
|
+
Requires-Dist: pre-commit; extra == "dev"
|
|
17
|
+
Dynamic: license-file
|
|
18
|
+
|
|
19
|
+
# Nepali Unicoder
|
|
20
|
+
|
|
21
|
+
A robust Python package for converting Romanized Nepali text and Preeti font text into Unicode Devanagari script. It uses a greedy matching algorithm for Roman transliteration and a two-phase conversion process for Preeti with contextual rules.
|
|
22
|
+
|
|
23
|
+
## Features
|
|
24
|
+
|
|
25
|
+
- **Accurate Transliteration**: Uses a greedy matching algorithm to prioritize longer phonetic matches (e.g., 'kha' is matched before 'k' and 'h').
|
|
26
|
+
- **Preeti Font Support**: Full support for Preeti to Unicode conversion with 30+ contextual rules for accurate transformation.
|
|
27
|
+
- **Smart Vowel Handling**: Distinguishes between independent vowels (e.g., 'aa' -> 'आ') and vowel signs/matras (e.g., 'ka' -> 'क', 'kaa' -> 'का').
|
|
28
|
+
- **Contextual Rules**: Handles complex Devanagari rules like reph positioning, matra reordering, and special character combinations.
|
|
29
|
+
- **Mixed Content Support**: Allows keeping English words or specific text in Roman script using `{}` blocks.
|
|
30
|
+
- **Customizable**: Supports custom word-level overrides via `word_maps.json`.
|
|
31
|
+
- **CLI Support**: Can be used directly from the command line.
|
|
32
|
+
|
|
33
|
+
## Installation
|
|
34
|
+
|
|
35
|
+
You can install the package locally:
|
|
36
|
+
|
|
37
|
+
```bash
|
|
38
|
+
pip install nepali-unicoder
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
## Usage
|
|
42
|
+
|
|
43
|
+
### Command Line Interface (CLI)
|
|
44
|
+
|
|
45
|
+
You can use the converter directly from the terminal:
|
|
46
|
+
|
|
47
|
+
```bash
|
|
48
|
+
# Direct argument
|
|
49
|
+
python -m nepali_unicoder "namaste"
|
|
50
|
+
# Output: नमस्ते
|
|
51
|
+
|
|
52
|
+
# Pipe input
|
|
53
|
+
echo "mero naam sanjeev ho" | python -m nepali_unicoder
|
|
54
|
+
# Output: मेरो नाम सन्जीव् हो
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
### Python API
|
|
58
|
+
|
|
59
|
+
```python
|
|
60
|
+
from nepali_unicoder.convert import Converter
|
|
61
|
+
|
|
62
|
+
converter = Converter()
|
|
63
|
+
|
|
64
|
+
# Basic conversion
|
|
65
|
+
text = "namaste nepal"
|
|
66
|
+
print(converter.convert(text))
|
|
67
|
+
# Output: नमस्ते नेपाल
|
|
68
|
+
|
|
69
|
+
# Using 'as-is' blocks for English text
|
|
70
|
+
mixed_text = "mero naam {Sanjeev} ho"
|
|
71
|
+
print(converter.convert(mixed_text))
|
|
72
|
+
# Output: मेरो नाम Sanjeev हो
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
### Preeti Mode
|
|
76
|
+
|
|
77
|
+
Convert Preeti font text to Unicode with full support for contextual rules:
|
|
78
|
+
|
|
79
|
+
```python
|
|
80
|
+
from nepali_unicoder.convert import Converter
|
|
81
|
+
|
|
82
|
+
# Create converter in Preeti mode
|
|
83
|
+
preeti_converter = Converter(mode="preeti")
|
|
84
|
+
|
|
85
|
+
# Basic conversion
|
|
86
|
+
preeti_text = "s{sf" # Preeti characters
|
|
87
|
+
print(preeti_converter.convert(preeti_text))
|
|
88
|
+
# Output: र्कर्का
|
|
89
|
+
|
|
90
|
+
# The converter handles:
|
|
91
|
+
# - Reph positioning: { → र् (moves before consonant)
|
|
92
|
+
# - Matra reordering: l (ि) moves after consonant
|
|
93
|
+
# - Special m transformations
|
|
94
|
+
# - Vowel combinations
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
#### Preeti Character Examples
|
|
98
|
+
|
|
99
|
+
| Preeti | Unicode | Description |
|
|
100
|
+
|--------|---------|-------------|
|
|
101
|
+
| `s` | `क` | Consonant ka |
|
|
102
|
+
| `s{` | `र्क` | Reph + ka (contextual) |
|
|
103
|
+
| `sl` | `कि` | ka + short i (reordered) |
|
|
104
|
+
| `qm` | `क्र` | Special m transformation |
|
|
105
|
+
| `!@#` | `१२३` | Nepali numbers |
|
|
106
|
+
|
|
107
|
+
#### CLI for Preeti
|
|
108
|
+
|
|
109
|
+
```bash
|
|
110
|
+
python -m nepali_unicoder --preeti "s{sf"
|
|
111
|
+
# Output: र्कर्का
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
## Transliteration Rules
|
|
116
|
+
|
|
117
|
+
- **Consonants**: `k` -> `क्`, `ka` -> `क`, `kh` -> `ख्`, `kha` -> `ख`
|
|
118
|
+
- **Vowels**: `a` -> `अ`, `aa` -> `आ`, `i` -> `इ`, `u` -> `उ`
|
|
119
|
+
- **Matras**: `ki` -> `कि`, `ko` -> `को`
|
|
120
|
+
- **Special**: `.` -> `।`, `..` -> `॥`
|
|
121
|
+
- **Numbers**: `0-9` -> `०-९` (Decimal points are preserved: `1.5` -> `१.५`)
|
|
122
|
+
|
|
123
|
+
## Advanced Usage
|
|
124
|
+
|
|
125
|
+
### Handling Complex Text
|
|
126
|
+
|
|
127
|
+
The converter handles mixed content gracefully. You can use `{}` to keep text as-is (e.g., for English words or code snippets).
|
|
128
|
+
|
|
129
|
+
```python
|
|
130
|
+
text = "mero naam {Sanjeev} ho ra ma 12.5 barsa ko bhaye."
|
|
131
|
+
print(converter.convert(text))
|
|
132
|
+
# Output: मेरो नाम Sanjeev हो र म १२.५ बर्स को भए।
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
### Configuration
|
|
136
|
+
|
|
137
|
+
The package uses `word_maps.json` for custom word-level overrides, located in the `src/nepali_unicoder` directory.
|
|
138
|
+
|
|
139
|
+
1. **`word_maps.json`**: Defines custom word-level overrides. Use this for words that don't follow standard phonetic rules.
|
|
140
|
+
|
|
141
|
+
Example `word_maps.json`:
|
|
142
|
+
```json
|
|
143
|
+
{
|
|
144
|
+
"nepal": "नेपाल",
|
|
145
|
+
"kathamandu": "काठमाडौँ"
|
|
146
|
+
}
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
## Contribution
|
|
150
|
+
|
|
151
|
+
We welcome contributions! Here's how you can help:
|
|
152
|
+
|
|
153
|
+
1. **Clone the repository**:
|
|
154
|
+
```bash
|
|
155
|
+
git clone https://github.com/realsanjeev/nepali_unicoder.git
|
|
156
|
+
cd nepali_unicoder
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
2. **Set up a virtual environment**:
|
|
160
|
+
```bash
|
|
161
|
+
python3 -m venv .venv
|
|
162
|
+
source .venv/bin/activate
|
|
163
|
+
pip install -e .
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
3. **Run tests**:
|
|
167
|
+
```bash
|
|
168
|
+
python -m unittest discover tests
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
4. **Submit a Pull Request**: Create a new branch, make your changes, and submit a PR.
|
|
172
|
+
|
|
173
|
+
## Development
|
|
174
|
+
|
|
175
|
+
To run tests:
|
|
176
|
+
|
|
177
|
+
```bash
|
|
178
|
+
python -m unittest discover tests
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
## License
|
|
182
|
+
|
|
183
|
+
[MIT](./LICENSE)
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
# Nepali Unicoder
|
|
2
|
+
|
|
3
|
+
A robust Python package for converting Romanized Nepali text and Preeti font text into Unicode Devanagari script. It uses a greedy matching algorithm for Roman transliteration and a two-phase conversion process for Preeti with contextual rules.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- **Accurate Transliteration**: Uses a greedy matching algorithm to prioritize longer phonetic matches (e.g., 'kha' is matched before 'k' and 'h').
|
|
8
|
+
- **Preeti Font Support**: Full support for Preeti to Unicode conversion with 30+ contextual rules for accurate transformation.
|
|
9
|
+
- **Smart Vowel Handling**: Distinguishes between independent vowels (e.g., 'aa' -> 'आ') and vowel signs/matras (e.g., 'ka' -> 'क', 'kaa' -> 'का').
|
|
10
|
+
- **Contextual Rules**: Handles complex Devanagari rules like reph positioning, matra reordering, and special character combinations.
|
|
11
|
+
- **Mixed Content Support**: Allows keeping English words or specific text in Roman script using `{}` blocks.
|
|
12
|
+
- **Customizable**: Supports custom word-level overrides via `word_maps.json`.
|
|
13
|
+
- **CLI Support**: Can be used directly from the command line.
|
|
14
|
+
|
|
15
|
+
## Installation
|
|
16
|
+
|
|
17
|
+
You can install the package locally:
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
pip install nepali-unicoder
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## Usage
|
|
24
|
+
|
|
25
|
+
### Command Line Interface (CLI)
|
|
26
|
+
|
|
27
|
+
You can use the converter directly from the terminal:
|
|
28
|
+
|
|
29
|
+
```bash
|
|
30
|
+
# Direct argument
|
|
31
|
+
python -m nepali_unicoder "namaste"
|
|
32
|
+
# Output: नमस्ते
|
|
33
|
+
|
|
34
|
+
# Pipe input
|
|
35
|
+
echo "mero naam sanjeev ho" | python -m nepali_unicoder
|
|
36
|
+
# Output: मेरो नाम सन्जीव् हो
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
### Python API
|
|
40
|
+
|
|
41
|
+
```python
|
|
42
|
+
from nepali_unicoder.convert import Converter
|
|
43
|
+
|
|
44
|
+
converter = Converter()
|
|
45
|
+
|
|
46
|
+
# Basic conversion
|
|
47
|
+
text = "namaste nepal"
|
|
48
|
+
print(converter.convert(text))
|
|
49
|
+
# Output: नमस्ते नेपाल
|
|
50
|
+
|
|
51
|
+
# Using 'as-is' blocks for English text
|
|
52
|
+
mixed_text = "mero naam {Sanjeev} ho"
|
|
53
|
+
print(converter.convert(mixed_text))
|
|
54
|
+
# Output: मेरो नाम Sanjeev हो
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
### Preeti Mode
|
|
58
|
+
|
|
59
|
+
Convert Preeti font text to Unicode with full support for contextual rules:
|
|
60
|
+
|
|
61
|
+
```python
|
|
62
|
+
from nepali_unicoder.convert import Converter
|
|
63
|
+
|
|
64
|
+
# Create converter in Preeti mode
|
|
65
|
+
preeti_converter = Converter(mode="preeti")
|
|
66
|
+
|
|
67
|
+
# Basic conversion
|
|
68
|
+
preeti_text = "s{sf" # Preeti characters
|
|
69
|
+
print(preeti_converter.convert(preeti_text))
|
|
70
|
+
# Output: र्कर्का
|
|
71
|
+
|
|
72
|
+
# The converter handles:
|
|
73
|
+
# - Reph positioning: { → र् (moves before consonant)
|
|
74
|
+
# - Matra reordering: l (ि) moves after consonant
|
|
75
|
+
# - Special m transformations
|
|
76
|
+
# - Vowel combinations
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
#### Preeti Character Examples
|
|
80
|
+
|
|
81
|
+
| Preeti | Unicode | Description |
|
|
82
|
+
|--------|---------|-------------|
|
|
83
|
+
| `s` | `क` | Consonant ka |
|
|
84
|
+
| `s{` | `र्क` | Reph + ka (contextual) |
|
|
85
|
+
| `sl` | `कि` | ka + short i (reordered) |
|
|
86
|
+
| `qm` | `क्र` | Special m transformation |
|
|
87
|
+
| `!@#` | `१२३` | Nepali numbers |
|
|
88
|
+
|
|
89
|
+
#### CLI for Preeti
|
|
90
|
+
|
|
91
|
+
```bash
|
|
92
|
+
python -m nepali_unicoder --preeti "s{sf"
|
|
93
|
+
# Output: र्कर्का
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
## Transliteration Rules
|
|
98
|
+
|
|
99
|
+
- **Consonants**: `k` -> `क्`, `ka` -> `क`, `kh` -> `ख्`, `kha` -> `ख`
|
|
100
|
+
- **Vowels**: `a` -> `अ`, `aa` -> `आ`, `i` -> `इ`, `u` -> `उ`
|
|
101
|
+
- **Matras**: `ki` -> `कि`, `ko` -> `को`
|
|
102
|
+
- **Special**: `.` -> `।`, `..` -> `॥`
|
|
103
|
+
- **Numbers**: `0-9` -> `०-९` (Decimal points are preserved: `1.5` -> `१.५`)
|
|
104
|
+
|
|
105
|
+
## Advanced Usage
|
|
106
|
+
|
|
107
|
+
### Handling Complex Text
|
|
108
|
+
|
|
109
|
+
The converter handles mixed content gracefully. You can use `{}` to keep text as-is (e.g., for English words or code snippets).
|
|
110
|
+
|
|
111
|
+
```python
|
|
112
|
+
text = "mero naam {Sanjeev} ho ra ma 12.5 barsa ko bhaye."
|
|
113
|
+
print(converter.convert(text))
|
|
114
|
+
# Output: मेरो नाम Sanjeev हो र म १२.५ बर्स को भए।
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
### Configuration
|
|
118
|
+
|
|
119
|
+
The package uses `word_maps.json` for custom word-level overrides, located in the `src/nepali_unicoder` directory.
|
|
120
|
+
|
|
121
|
+
1. **`word_maps.json`**: Defines custom word-level overrides. Use this for words that don't follow standard phonetic rules.
|
|
122
|
+
|
|
123
|
+
Example `word_maps.json`:
|
|
124
|
+
```json
|
|
125
|
+
{
|
|
126
|
+
"nepal": "नेपाल",
|
|
127
|
+
"kathamandu": "काठमाडौँ"
|
|
128
|
+
}
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
## Contribution
|
|
132
|
+
|
|
133
|
+
We welcome contributions! Here's how you can help:
|
|
134
|
+
|
|
135
|
+
1. **Clone the repository**:
|
|
136
|
+
```bash
|
|
137
|
+
git clone https://github.com/realsanjeev/nepali_unicoder.git
|
|
138
|
+
cd nepali_unicoder
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
2. **Set up a virtual environment**:
|
|
142
|
+
```bash
|
|
143
|
+
python3 -m venv .venv
|
|
144
|
+
source .venv/bin/activate
|
|
145
|
+
pip install -e .
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
3. **Run tests**:
|
|
149
|
+
```bash
|
|
150
|
+
python -m unittest discover tests
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
4. **Submit a Pull Request**: Create a new branch, make your changes, and submit a PR.
|
|
154
|
+
|
|
155
|
+
## Development
|
|
156
|
+
|
|
157
|
+
To run tests:
|
|
158
|
+
|
|
159
|
+
```bash
|
|
160
|
+
python -m unittest discover tests
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
## License
|
|
164
|
+
|
|
165
|
+
[MIT](./LICENSE)
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=61.0"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "nepali_unicoder"
|
|
7
|
+
version = "0.1.1"
|
|
8
|
+
description = "A Python package for Roman to Nepali (Devanagari) transliteration"
|
|
9
|
+
authors = [
|
|
10
|
+
{ name="realsanjeev", email="075bei033.sanjeev@pcampus.edu.np" },
|
|
11
|
+
]
|
|
12
|
+
readme = "README.md"
|
|
13
|
+
license = "MIT"
|
|
14
|
+
requires-python = ">=3.7"
|
|
15
|
+
dependencies = []
|
|
16
|
+
classifiers = [
|
|
17
|
+
"Programming Language :: Python",
|
|
18
|
+
"Programming Language :: Python :: 3",
|
|
19
|
+
"Operating System :: OS Independent",
|
|
20
|
+
]
|
|
21
|
+
keywords = ["nepali", "unicoder", "roman to devanagari", "nepali transliteration", "preeti to unicode"]
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
[project.urls]
|
|
25
|
+
"Homepage" = "https://github.com/realsanjeev/nepali_unicoder"
|
|
26
|
+
|
|
27
|
+
[project.optional-dependencies]
|
|
28
|
+
dev = ["pre-commit"]
|
|
29
|
+
|
|
30
|
+
[tool.pytest.ini_options]
|
|
31
|
+
pythonpath = [
|
|
32
|
+
"src"
|
|
33
|
+
]
|
|
34
|
+
testpaths = [
|
|
35
|
+
"tests",
|
|
36
|
+
]
|
|
File without changes
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import sys
|
|
2
|
+
|
|
3
|
+
from nepali_unicoder.convert import Converter
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
def main():
|
|
7
|
+
args = sys.argv[1:]
|
|
8
|
+
mode = "roman"
|
|
9
|
+
|
|
10
|
+
if "--preeti" in args:
|
|
11
|
+
mode = "preeti"
|
|
12
|
+
args.remove("--preeti")
|
|
13
|
+
|
|
14
|
+
if len(args) < 1:
|
|
15
|
+
# Check if there is stdin
|
|
16
|
+
if not sys.stdin.isatty():
|
|
17
|
+
input_text = sys.stdin.read()
|
|
18
|
+
else:
|
|
19
|
+
print('Usage: python -m nepali_unicoder "your roman text here"')
|
|
20
|
+
print(' python -m nepali_unicoder --preeti "your preeti text here"')
|
|
21
|
+
print(' or: echo "namaste" | python -m nepali_unicoder')
|
|
22
|
+
return
|
|
23
|
+
else:
|
|
24
|
+
input_text = " ".join(args)
|
|
25
|
+
|
|
26
|
+
converter = Converter(mode=mode)
|
|
27
|
+
result = converter.convert(input_text)
|
|
28
|
+
print(result)
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
if __name__ == "__main__":
|
|
32
|
+
main()
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
from nepali_unicoder.engine import Engine
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
class Converter:
|
|
5
|
+
"""
|
|
6
|
+
Wrapper around Engine for backward compatibility.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
def __init__(self, mode: str = "roman"):
|
|
10
|
+
self.engine = Engine(mode=mode)
|
|
11
|
+
|
|
12
|
+
def convert(self, text: str) -> str:
|
|
13
|
+
return self.engine.transliterate(text)
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import re
|
|
2
|
+
from typing import Optional
|
|
3
|
+
|
|
4
|
+
from nepali_unicoder.loader import PreetiLoader, RuleLoader
|
|
5
|
+
from nepali_unicoder.rules import PREETI_TO_UNICODE_MAPPING
|
|
6
|
+
from nepali_unicoder.tokenizer import Tokenizer
|
|
7
|
+
from nepali_unicoder.trie import Trie
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class Engine:
|
|
11
|
+
def __init__(
|
|
12
|
+
self,
|
|
13
|
+
trie: Optional[Trie] = None,
|
|
14
|
+
tokenizer: Optional[Tokenizer] = None,
|
|
15
|
+
mode: str = "roman",
|
|
16
|
+
):
|
|
17
|
+
self.mode = mode
|
|
18
|
+
self.post_rules = []
|
|
19
|
+
|
|
20
|
+
if trie is None:
|
|
21
|
+
if mode == "preeti":
|
|
22
|
+
loader = PreetiLoader()
|
|
23
|
+
# Load post-processing rules for Preeti mode
|
|
24
|
+
self.post_rules = PREETI_TO_UNICODE_MAPPING.get("post_rules", [])
|
|
25
|
+
else:
|
|
26
|
+
loader = RuleLoader()
|
|
27
|
+
self.trie = loader.load()
|
|
28
|
+
else:
|
|
29
|
+
self.trie = trie
|
|
30
|
+
|
|
31
|
+
if tokenizer is None:
|
|
32
|
+
self.tokenizer = Tokenizer()
|
|
33
|
+
else:
|
|
34
|
+
self.tokenizer = tokenizer
|
|
35
|
+
|
|
36
|
+
def transliterate(self, text: str) -> str:
|
|
37
|
+
"""
|
|
38
|
+
Convert Roman text to Devanagari using the Trie.
|
|
39
|
+
"""
|
|
40
|
+
if not text:
|
|
41
|
+
return ""
|
|
42
|
+
|
|
43
|
+
tokens = self.tokenizer.tokenize(text)
|
|
44
|
+
result = []
|
|
45
|
+
|
|
46
|
+
for token in tokens:
|
|
47
|
+
if token.type == "BLOCK":
|
|
48
|
+
result.append(token.value)
|
|
49
|
+
elif token.type == "LITERAL":
|
|
50
|
+
result.append(token.value)
|
|
51
|
+
elif token.type == "NUMBER":
|
|
52
|
+
# Process Number chunk: transliterate digits, keep others (like .) as is
|
|
53
|
+
for char in token.value:
|
|
54
|
+
if char.isdigit():
|
|
55
|
+
# Use Trie to find digit mapping (digits are single chars in rules)
|
|
56
|
+
match_val, _ = self.trie.longest_match(char)
|
|
57
|
+
if match_val:
|
|
58
|
+
result.append(match_val)
|
|
59
|
+
else:
|
|
60
|
+
result.append(char)
|
|
61
|
+
else:
|
|
62
|
+
result.append(char)
|
|
63
|
+
elif token.type == "ROMAN":
|
|
64
|
+
# Process Roman chunk with Trie
|
|
65
|
+
chunk = token.value
|
|
66
|
+
idx = 0
|
|
67
|
+
chunk_len = len(chunk)
|
|
68
|
+
|
|
69
|
+
while idx < chunk_len:
|
|
70
|
+
# Try to find longest match starting at idx
|
|
71
|
+
sub = chunk[idx:]
|
|
72
|
+
match_val, match_len = self.trie.longest_match(sub)
|
|
73
|
+
|
|
74
|
+
if match_val:
|
|
75
|
+
result.append(match_val)
|
|
76
|
+
idx += match_len
|
|
77
|
+
else:
|
|
78
|
+
# No match, keep character as is
|
|
79
|
+
result.append(chunk[idx])
|
|
80
|
+
idx += 1
|
|
81
|
+
|
|
82
|
+
output = "".join(result)
|
|
83
|
+
|
|
84
|
+
# Apply post-processing rules for Preeti mode
|
|
85
|
+
if self.mode == "preeti" and self.post_rules:
|
|
86
|
+
output = self._apply_post_rules(output)
|
|
87
|
+
|
|
88
|
+
return output
|
|
89
|
+
|
|
90
|
+
def _apply_post_rules(self, text: str) -> str:
|
|
91
|
+
"""
|
|
92
|
+
Apply post-processing rules (regex replacements) to the text.
|
|
93
|
+
Used for Preeti mode to handle contextual transformations.
|
|
94
|
+
"""
|
|
95
|
+
for pattern, replacement in self.post_rules:
|
|
96
|
+
text = re.sub(pattern, replacement, text)
|
|
97
|
+
return text
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import json
|
|
2
|
+
import os
|
|
3
|
+
|
|
4
|
+
from nepali_unicoder.rules import PREETI_TO_UNICODE_MAPPING, ROMAN_TO_UNICODE_RULES
|
|
5
|
+
from nepali_unicoder.trie import Trie
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class RuleLoader:
|
|
9
|
+
def __init__(self):
|
|
10
|
+
self.word_maps_path = os.path.join(os.path.dirname(__file__), "word_maps.json")
|
|
11
|
+
|
|
12
|
+
def load(self) -> Trie:
|
|
13
|
+
"""Load rules and custom mappings into a Trie."""
|
|
14
|
+
trie = Trie()
|
|
15
|
+
self._load_rules(trie)
|
|
16
|
+
self._load_custom_mappings(trie)
|
|
17
|
+
return trie
|
|
18
|
+
|
|
19
|
+
def _load_rules(self, trie: Trie):
|
|
20
|
+
data = ROMAN_TO_UNICODE_RULES
|
|
21
|
+
|
|
22
|
+
consonants = data.get("consonants", {})
|
|
23
|
+
vowels = data.get("vowels", {})
|
|
24
|
+
matras = data.get("matras", {})
|
|
25
|
+
special = data.get("special", {})
|
|
26
|
+
digits = data.get("digits", {})
|
|
27
|
+
|
|
28
|
+
# 1. Independent Vowels
|
|
29
|
+
for rom, dev in vowels.items():
|
|
30
|
+
trie.add(rom, dev)
|
|
31
|
+
|
|
32
|
+
# 2. Consonants and Combinations
|
|
33
|
+
halanta = "्"
|
|
34
|
+
|
|
35
|
+
for rom_cons, dev_cons in consonants.items():
|
|
36
|
+
# Case 1: Consonant alone (halanta form) -> 'k' -> 'क्'
|
|
37
|
+
trie.add(rom_cons, dev_cons + halanta)
|
|
38
|
+
|
|
39
|
+
# Case 2: Consonant + 'a' (Schwa form) -> 'ka' -> 'क'
|
|
40
|
+
trie.add(rom_cons + "a", dev_cons)
|
|
41
|
+
|
|
42
|
+
# Case 3: Consonant + other vowels -> 'ki' -> 'कि'
|
|
43
|
+
for rom_vowel, matra in matras.items():
|
|
44
|
+
if rom_vowel == "a":
|
|
45
|
+
continue # Handled above
|
|
46
|
+
trie.add(rom_cons + rom_vowel, dev_cons + matra)
|
|
47
|
+
|
|
48
|
+
# 3. Special, Digits, Punctuation
|
|
49
|
+
for rom, dev in special.items():
|
|
50
|
+
trie.add(rom, dev)
|
|
51
|
+
|
|
52
|
+
for rom, dev in digits.items():
|
|
53
|
+
trie.add(rom, dev)
|
|
54
|
+
|
|
55
|
+
# Ensure 'a' maps to 'अ' (already in vowels, but good to double check)
|
|
56
|
+
trie.add("a", vowels.get("a", "अ"))
|
|
57
|
+
|
|
58
|
+
def _load_custom_mappings(self, trie: Trie):
|
|
59
|
+
if not os.path.exists(self.word_maps_path):
|
|
60
|
+
return
|
|
61
|
+
|
|
62
|
+
try:
|
|
63
|
+
with open(self.word_maps_path, "r", encoding="utf-8") as f:
|
|
64
|
+
mappings = json.load(f)
|
|
65
|
+
for roman, devanagari in mappings.items():
|
|
66
|
+
trie.add(roman.lower(), devanagari)
|
|
67
|
+
except Exception as e:
|
|
68
|
+
print(f"Error reading word_maps.json: {e}")
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
class PreetiLoader:
|
|
72
|
+
def __init__(self):
|
|
73
|
+
pass
|
|
74
|
+
|
|
75
|
+
def load(self) -> Trie:
|
|
76
|
+
"""Load Preeti rules into a Trie."""
|
|
77
|
+
trie = Trie()
|
|
78
|
+
data = PREETI_TO_UNICODE_MAPPING
|
|
79
|
+
|
|
80
|
+
mappings = data.get("mappings", {})
|
|
81
|
+
for key, value in mappings.items():
|
|
82
|
+
trie.add(key, value)
|
|
83
|
+
|
|
84
|
+
return trie
|