Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unicode to English literals #7

Open
wants to merge 17 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,24 @@ print(NepaliMoment.fromAD(DateTime.parse('2019-06-02T18:22:14'),
// १३ दिन पहिले
```

### English literals to Unicode
Converts English literal (Roman Literals) into Nepali Unicode.
```dart
print(
NepaliUnicode.convert(
"sayau' thu''gaa fUlakaa haamii, euTai maalaa nepaalii",
),
);
// सयौं थुँगा फूलका हामी, एउटै माला नेपाली
```

### Unicode to English iterals
Converts Nepali Unicode into English literals.
```dart
print(NepaliUnicode.convert("सयौं थुँगा फूलका हामी, एउटै माला नेपाली"));
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this correct ?

// sayau' thu''gaa fUlakaa haamii, euTai maalaa nepaalii
```


## Example
Find more detailed example [here](https://github.com/sarbagyastha/nepali_utils/tree/master/example/main.dart).
Expand Down
4 changes: 4 additions & 0 deletions example/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// found in the LICENSE file.

import 'package:nepali_utils/nepali_utils.dart';
import 'package:nepali_utils/src/unicode_to_english.dart';

void main(List<String> arguments) {
heading('Nepali Date Time');
Expand Down Expand Up @@ -74,6 +75,9 @@ void main(List<String> arguments) {
"sayau' thu''gaa fUlakaa haamii, euTai maalaa nepaalii"));
print(NepaliUnicode.convert(
'saarwabhauma bhai failiekaa, mecii-mahaakaalii\n'));
heading('Unicode to English');
print(UnicodeToEnglish.convert('सयौं थुँगा फूलका हामी, एउटै माला नेपाली'));
print(UnicodeToEnglish.convert('सार्वभौम भै फैलिएका, मेची-महाकाली\n'));
}

void heading(String text) {
Expand Down
124 changes: 124 additions & 0 deletions lib/src/unicode_to_english.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
// Copyright 2020 Sarbagya Dhaubanjar. All rights reserved.
// Use of this source code is governed by a MIT license that can be
// found in the LICENSE file.

/// Converts Nepali Unicode (Devnagari Literals) into English(Roman) literals.
class UnicodeToEnglish {
/// Converts specifies [text] into english literals.
///
/// if live is true, texts will convert in live manner.
/// i.e. as you go on typing
/// Default for live is false.
static String convert(String text, {bool live = false}) {
final characters = text.split('');
final convertedString = <String>[];
for (var i = 0; i < characters.length; i++) {
if (i < characters.length - 1 && characters[i + 1] == '\u094D') {
var character = _unicodeMap[characters[i]].toString();
character = character.substring(0, character.length - 1);
convertedString.add(character);
} else if (characters[i] != '\u094D') {
convertedString.add(
_unicodeMap[characters[i]] ?? characters[i],
);
}
}
for (var i = 0; i < convertedString.length; i++) {
// if the converted string contains symbols
if (_symbolMap[convertedString[i]] != null) {
var preecedingCharacter = convertedString[i - 1].toString();
print('$preecedingCharacter ${preecedingCharacter.endsWith('a')}');
// eg. tha + ु (u) = thu
if (preecedingCharacter.endsWith('a')) {
preecedingCharacter =
preecedingCharacter.substring(0, preecedingCharacter.length - 1);
Comment on lines +33 to +34
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where is this used ?

convertedString[i - 1] = preecedingCharacter;
}

convertedString[i] = _symbolMap[convertedString[i]] ?? '';
}
}
return convertedString.join('');
}
}

const Map<String, String> _unicodeMap = {
//ka - gya
'\u0915': 'ka',
'\u0916': 'kha',
'\u0917': 'ga',
'\u0918': 'gha',
'\u0919': 'ng',
'\u091A': 'ca',
'\u091B': 'cha',
'\u091C': 'ja',
'\u091D': 'jha',
'\u091E': 'ya',
'\u091F': 'Ta',
'\u0920': 'Tha',
'\u0921': 'Da',
'\u0922': 'Dha',
'\u0923': 'na',
'\u0924': 'ta',
'\u0925': 'tha',
'\u0926': 'da',
'\u0927': 'dha',
'\u0928': 'na',
'\u092A': 'pa',
'\u092B': 'fa',
'\u092C': 'ba',
'\u092D': 'bha',
'\u092E': 'ma',
'\u092F': 'ya',
'\u0930': 'ra',
'\u0932': 'la',
'\u0935': 'wa',
'\u0936': 'sha',
'\u0937': 'sha',
'\u0938': 'sa',
'\u0939': 'ha',
'\u0915\u094D\u0937': 'xa',
'\u0924\u094D\u0930': 'tra',
'\u091C\u094D\u091E': 'gya',
// a - ahm
'\u0905': 'a',
'\u0906': 'aa',
'\u0907': 'i',
'\u0908': 'ii',
'\u0909': 'u',
'\u090A': 'oo',
'\u090F': 'e',
'\u0910': 'ai',
'\u0913': 'o',
'\u0914': 'au',
'\u0935\u0902': 'am',
'\u0935\u0903': 'ah',

//numbers
'\u0966': '0',
'\u0967': '1',
'\u0968': '2',
'\u0969': '3',
'\u096a': '4',
'\u096b': '5',
'\u096c': '6',
'\u096d': '7',
'\u096e': '8',
'\u096f': '9',
//symbols
'%2c': ',',
};
const Map<String, String> _symbolMap = {
'\u093E': 'aa',
'\u093F': 'i',
'\u0940': 'ii',
'\u0941': 'u',
'\u0942': 'U',
'\u0947': 'e',
'\u0948': 'ai',
'\u094B': 'o',
'\u094C': 'au',

'\u0901': "''", //chandrabindu
'\u0902': "'", //shreebindu
};
20 changes: 20 additions & 0 deletions test/nepali_number_format_test.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:nepali_utils/nepali_utils.dart';
import 'package:nepali_utils/src/unicode_to_english.dart';
import 'package:test/test.dart';

void main() {
Expand Down Expand Up @@ -164,6 +165,25 @@ void main() {
),
);
});
group('text converter ', () {
final englishText =
"ka baaTa kachuwaa kha kharaayo\nkharaayoko sing' chaina kataa haraayo";
final nepaliText = 'क बाट कछुवा ख खरायो\nखरायोको सिङं छैन कता हरायो';
test(
'conversion of english to nepali unicode',
() => expect(
NepaliUnicode.convert('$englishText'),
'$nepaliText',
),
);
test(
'conversion of nepali to english unicode',
() => expect(
UnicodeToEnglish.convert('$nepaliText'),
'$englishText',
),
);
});
Comment on lines +168 to +186
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The tests do not belong in this file.

}

String _format(number) => NepaliNumberFormat().format(number);