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 5 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
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,21 @@ 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"));
Copy link
Owner

Choose a reason for hiding this comment

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

Please format the code and add a trailing comma.

// सयौं थुँगा फूलका हामी, एउटै माला नेपाली
```

### 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
121 changes: 121 additions & 0 deletions lib/src/unicode_to_english.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
// 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 {
static String _text = '';

/// 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}) {
var splittedString = text.split('');
sarbagyastha marked this conversation as resolved.
Show resolved Hide resolved
var convertedString = <String>[];
sarbagyastha marked this conversation as resolved.
Show resolved Hide resolved
for (var i = 0; i < splittedString.length; i++) {
if (i < splittedString.length - 1 && splittedString[i + 1] == '\u094D') {
Copy link
Owner

Choose a reason for hiding this comment

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

final <is?> = splittedString[i + 1] == '\u094D'; // give the variable name so that the check you're trying to perform becomes self-explanatory.

var fullStr = _unicodeMap[splittedString[i]].toString();
Copy link
Owner

Choose a reason for hiding this comment

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

What do you mean by fullStr here ?

fullStr = fullStr.substring(0, fullStr.length - 1);
convertedString.add(fullStr);
} else if (splittedString[i] != '\u094D') {
convertedString
.add(_unicodeMap[splittedString[i]] ?? splittedString[i]);
Copy link
Owner

Choose a reason for hiding this comment

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

please add missing trailing comma

}
}
for (var i = 0; i < convertedString.length; i++) {
if (_symbolMap[convertedString[i]] != null) {
convertedString[i - 1] = convertedString[i - 1].toString();
if (convertedString[i - 1].endsWith('a')) {
Copy link
Owner

Choose a reason for hiding this comment

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

Why was the check needed, please add a comment.

convertedString[i - 1] = convertedString[i - 1]
.substring(0, convertedString[i - 1].length - 1);
Copy link
Owner

Choose a reason for hiding this comment

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

convertedString[i - 1] is being repeated multiple times, please extract it a variable.

}
convertedString[i] = _symbolMap[convertedString[i]] ?? '';
}
}
_text = convertedString.join('');
return _text;
Copy link
Owner

Choose a reason for hiding this comment

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

Can be return directly.

}
}

Map<String, String> get _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': ',',
};
Map<String, String> get _symbolMap => {
'\u093E': 'aa',
'\u093F': 'i',
'\u0940': 'ii',
'\u0941': 'u',
'\u0942': 'U',
'\u0947': 'e',
'\u0948': 'ai',
'\u094B': 'o',
'\u094C': 'au',

'\u0901': "''", //chandrabindu
'\u0902': "'", //shreebindu
};
sarbagyastha marked this conversation as resolved.
Show resolved Hide resolved
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);