Skip to content

Commit

Permalink
Add Dart client (google#93)
Browse files Browse the repository at this point in the history
  • Loading branch information
seithuhtun committed Nov 13, 2021
1 parent 9697316 commit 91c2b57
Show file tree
Hide file tree
Showing 26 changed files with 2,228 additions and 1 deletion.
17 changes: 17 additions & 0 deletions .github/workflows/mynmr_tools_ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -207,3 +207,20 @@ jobs:
with:
xcode-version: '11.5'
- run: make client-swift-test

# Dart Client(Flutter)
dart:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: subosito/flutter-action@v1
with:
flutter-version: '2.5.3'
- name: Install dependencies
run: |
cd clients/dart
flutter pub get
- name: Run dart tests
run: |
cd clients/dart
flutter test
13 changes: 12 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ RAKE=rake
GO=go
PHPUNIT=./vendor/bin/phpunit
PYTHON=python
FLUTTER=flutter

# /usr/bin/swift is under macOS System Integrity Protection, which
# filters out environment variables like DYLD_LIBRARY_PATH which let
Expand Down Expand Up @@ -59,22 +60,28 @@ copy-resources:
cp training/src/main/resources/com/google/myanmartools/zawgyiUnicodeModel.dat clients/php/resources
cp training/src/main/resources/com/google/myanmartools/zawgyiUnicodeModel.dat clients/go/resources
cp training/src/main/resources/com/google/myanmartools/zawgyiUnicodeModel.dat clients/python/src/myanmartools/resources
cp training/src/main/resources/com/google/myanmartools/zawgyiUnicodeModel.dat clients/dart/resources
cp training/src/main/resources/com/google/myanmartools/compatibility.tsv clients/java/src/test/resources/com/google/myanmartools
cp training/src/main/resources/com/google/myanmartools/compatibility.tsv clients/cpp/resources
cp training/src/main/resources/com/google/myanmartools/compatibility.tsv clients/js/resources
cp training/src/main/resources/com/google/myanmartools/compatibility.tsv clients/ruby/lib/myanmar-tools/resources
cp training/src/main/resources/com/google/myanmartools/compatibility.tsv clients/php/resources
cp training/src/main/resources/com/google/myanmartools/compatibility.tsv clients/go/resources
cp training/src/main/resources/com/google/myanmartools/compatibility.tsv clients/python/src/myanmartools/resources
cp training/src/main/resources/com/google/myanmartools/compatibility.tsv clients/dart/resources

cp genconvert/input/mmgov_zawgyi_src.txt clients/java/src/test/resources/com/google/myanmartools
cp genconvert/input/mmgov_zawgyi_src.txt clients/js/resources
cp genconvert/input/mmgov_zawgyi_src.txt clients/dart/resources
cp genconvert/input/udhr_mya_unicode_src.txt clients/js/resources
cp genconvert/input/udhr_mya_unicode_src.txt clients/java/src/test/resources/com/google/myanmartools
cp genconvert/input/udhr_mya_unicode_src.txt clients/dart/resources
cp genconvert/output/mmgov_unicode_out.txt clients/java/src/test/resources/com/google/myanmartools
cp genconvert/output/mmgov_unicode_out.txt clients/js/resources
cp genconvert/output/mmgov_unicode_out.txt clients/dart/resources
cp genconvert/output/udhr_mya_zawgyi_out.txt clients/java/src/test/resources/com/google/myanmartools
cp genconvert/output/udhr_mya_zawgyi_out.txt clients/js/resources
cp genconvert/output/udhr_mya_zawgyi_out.txt clients/dart/resources

train: zawgyiUnicodeModel.dat compatibility.tsv testData.tsv copy-resources

Expand Down Expand Up @@ -123,6 +130,9 @@ client-go: $(wildcard clients/go/**/*)
client-python: $(wildcard clients/python/**/*)
cd clients/python && $(PYTHON) setup.py install

client-dart:$(wildcard clients/dart/**/*)
cd clients/dart && $(FLUTTER) pub get

# Until Swift 5.3 and SE-0272 are released, the Swift Package Manager
# does not fully support binary library dependencies.
#
Expand All @@ -148,11 +158,12 @@ client-swift: client-cpp $(wildcard clients/swift/**/*)
client-swift-test: client-swift
DYLD_LIBRARY_PATH="$(CURDIR)"/clients/cpp $(SWIFT) test -Xlinker -L"$(CURDIR)"/clients/cpp --package-path clients/swift

test: clients client-cpp client-js client-ruby client-php client-go client-python client-swift client-swift-test
test: clients client-cpp client-js client-ruby client-php client-go client-python client-swift client-swift-test client-dart
cd clients/cpp && $(MAKE) test
cd clients/java && $(MVN) test
cd clients/js && $(NPM) test
cd clients/ruby && $(RAKE) test
cd clients/go && $(GO) test
$(PHPUNIT) --configuration clients/php/phpunit.xml
cd clients/python && $(PYTHON) -m unittest
cd clients/dart && $(FLUTTER) test
75 changes: 75 additions & 0 deletions clients/dart/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# Miscellaneous
*.class
*.log
*.pyc
*.swp
.DS_Store
.atom/
.buildlog/
.history
.svn/

# IntelliJ related
*.iml
*.ipr
*.iws
.idea/

# The .vscode folder contains launch configuration and tasks you configure in
# VS Code which you may wish to be included in version control, so this line
# is commented out by default.
#.vscode/

# Flutter/Dart/Pub related
**/doc/api/
.dart_tool/
.flutter-plugins
.flutter-plugins-dependencies
.packages
.pub-cache/
.pub/
build/

# Android related
**/android/**/gradle-wrapper.jar
**/android/.gradle
**/android/captures/
**/android/gradlew
**/android/gradlew.bat
**/android/local.properties
**/android/**/GeneratedPluginRegistrant.java

# iOS/XCode related
**/ios/**/*.mode1v3
**/ios/**/*.mode2v3
**/ios/**/*.moved-aside
**/ios/**/*.pbxuser
**/ios/**/*.perspectivev3
**/ios/**/*sync/
**/ios/**/.sconsign.dblite
**/ios/**/.tags*
**/ios/**/.vagrant/
**/ios/**/DerivedData/
**/ios/**/Icon?
**/ios/**/Pods/
**/ios/**/.symlinks/
**/ios/**/profile
**/ios/**/xcuserdata
**/ios/.generated/
**/ios/Flutter/App.framework
**/ios/Flutter/Flutter.framework
**/ios/Flutter/Flutter.podspec
**/ios/Flutter/Generated.xcconfig
**/ios/Flutter/ephemeral
**/ios/Flutter/app.flx
**/ios/Flutter/app.zip
**/ios/Flutter/flutter_assets/
**/ios/Flutter/flutter_export_environment.sh
**/ios/ServiceDefinitions.json
**/ios/Runner/GeneratedPluginRegistrant.*

# Exceptions to above rules.
!**/ios/**/default.mode1v3
!**/ios/**/default.mode2v3
!**/ios/**/default.pbxuser
!**/ios/**/default.perspectivev3
13 changes: 13 additions & 0 deletions clients/dart/LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Copyright 2021 Sei Thu Htun

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
35 changes: 35 additions & 0 deletions clients/dart/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Myanmar Tools Dart(Flutter) Documentation

This documentation is for Dart(Flutter) specific usage of *Myanmar Tools*.
See https://github.com/google/myanmar-tools/ for more.

## Usage

To detect Zawgyi, create a final instance of ZawgyiDetector, and call `getZawgyiProbability` with your string.

```dart
final _detector = await ZawGyiDetector.create();
double score = _detector.getZawGyiProbability("မ္း");
// score is now 0.999772 (very likely Zawgyi)
```

To convert between Zawgyi and Unicode, use the classe `ZawGyiConverter` as shown below.

```dart
final converter= ZawGyiConverter.create();
// Zawgyi to Unicode
String uniOutput = converter.zawGyiToUnicode('မ္း');
// uniOutput is now 'မ်း'
// Unicode to Zawgyi
String zawOutput = converter.zawGyiToUnicode('မ်း');
// zawOutput is now 'မ္း'
```

## Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/google/myanmar-tools . This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.

## License

The package is available as open source under the terms of the [Apache-2.0 License](http://www.apache.org/licenses/LICENSE-2.0).
1 change: 1 addition & 0 deletions clients/dart/analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include: package:flutter_lints/flutter.yaml
4 changes: 4 additions & 0 deletions clients/dart/lib/myanmar_tools.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
library myanmar_tools;

export 'src/zawgyi_detector.dart';
export 'src/zawgyi_converter.dart';
89 changes: 89 additions & 0 deletions clients/dart/lib/src/markov/binary_markov.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import 'dart:typed_data';


/// A class that behaves as if it were two Markov chains, called Chain A and Chain B. Whereas a
/// normal Markov chain would expose the log probability of a transition, this class exposes the
/// difference between the log probabilities of the two Markov chains. When training, you can specify
/// which Markov chain a transition should be added to.
///
/// The reasoning behind this class is that it has a smaller data footprint than two separate
/// Markov chain objects.
///
class BinaryMarkov {


// Dart Web JavaScript doesn't have the concept of 64-bit integers, so split the tag into two 32-bit ints.
static const _binaryTagLead = 0x424D4152;
static const _binaryTagTrail =0x4B4F5620;

// Current serial format version number, used in association with the magic number.
static const _binaryVersion = 0;

late List<List<double>> logProbabilityDifferences=[];


/// Creates an instance from a binary data stream.
///
/// @throws Exception If there was a problem reading the data.
BinaryMarkov(ByteData stream,int offset){

final binaryTagLead = stream.getUint32(offset);
offset += 4;

if (binaryTagLead != _binaryTagLead) {
throw Exception('Unexpected magic number lead; expected 0x${_binaryTagLead.toRadixString(16)} but got 0x${binaryTagLead.toRadixString(16)}');
}
final binaryTagTrail = stream.getUint32(offset);
offset += 4;
if (binaryTagTrail != _binaryTagTrail) {
throw Exception('Unexpected magic number trail; expected 0x${_binaryTagTrail.toRadixString(16)} but got0x${binaryTagTrail.toRadixString(16)}');
}

final binaryVersion= stream.getUint32(offset);
offset+=4;

if (binaryVersion != _binaryVersion) {
throw Exception('Unexpected serial version number; expected ${_binaryVersion.toString()} but got ${binaryVersion.toString()}');
}
int size = stream.getInt16(offset);
offset += 2;
var logProbabilityDifferences = List.generate(size, (_) =>List.filled(size, 0.0) );
for (int i1 = 0; i1 < size; i1++) {
int entries = stream.getInt16(offset);
offset += 2;
double fallback=0;
if(entries!=0){
fallback = stream.getFloat32(offset);
offset += 4;
}
int next = -1;
for (int i2 = 0; i2 < size; i2++) {
if (entries > 0 && next < i2) {
next = stream.getInt16(offset);
offset += 2;
entries--;
}
if (next == i2) {
logProbabilityDifferences[i1][i2] = stream.getFloat32(offset);
offset += 4;
} else {
logProbabilityDifferences[i1][i2] = fallback;
}
}
}
this.logProbabilityDifferences = logProbabilityDifferences;
}


/// Gets the difference in log probabilities between chain A and chain B. This behaves as if you
/// had two Markov chains and called <code>
/// chainA.getLogProbability(i1, i2) - chainB.getLogProbability(i1, i2)</code>.
///
/// @param i1 The index of the source node to transition from.
/// @param i2 The index of the destination node to transition to.
/// @return The difference between A and B in log probability of transitioning from i1 to i2.
double getLogProbabilityDifference(int i1, int i2) {
return logProbabilityDifferences[i1][i2];
}

}
Loading

0 comments on commit 91c2b57

Please sign in to comment.