Skip to content

Commit b0a20bf

Browse files
committed
edit learning
1 parent 52dcbf8 commit b0a20bf

File tree

17 files changed

+1040
-32
lines changed

17 files changed

+1040
-32
lines changed

example/lib/main.dart

-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import 'package:provider/provider.dart';
33

44
import 'nlp/entity_extraction.dart';
55
import 'nlp/language.dart';
6-
import 'nlp/smart_reply.dart';
76
import 'nlp/translate.dart';
87
import 'vision/barcode_scanning.dart';
98
import 'vision/digital_ink_recognition.dart';
@@ -32,7 +31,6 @@ class LearningApp extends StatelessWidget {
3231
providers: [
3332
ChangeNotifierProvider(create: (_) => LearningLanguageState()),
3433
ChangeNotifierProvider(create: (_) => LearningTranslateState()),
35-
ChangeNotifierProvider(create: (_) => LearningSmartReplyState()),
3634
ChangeNotifierProvider(
3735
create: (_) => LearningEntityExtractionState()),
3836
ChangeNotifierProvider(create: (_) => LearningTextRecognitionState()),

example/lib/nlp/language.dart

+115-2
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,129 @@
11
import 'package:flutter/material.dart';
22
import 'package:flutter/cupertino.dart';
3+
import 'package:provider/provider.dart';
4+
5+
import 'package:learning_input_image/learning_input_image.dart';
6+
import 'package:learning_language/learning_language.dart';
37

48
class LearningLanguage extends StatefulWidget {
59
@override
610
_LearningLanguageState createState() => _LearningLanguageState();
711
}
812

913
class _LearningLanguageState extends State<LearningLanguage> {
14+
TextEditingController _controller = TextEditingController();
15+
LanguageIdentifier _identifier = LanguageIdentifier();
16+
17+
LearningLanguageState get state => Provider.of(context, listen: false);
18+
19+
@override
20+
void dispose() {
21+
_identifier.dispose();
22+
super.dispose();
23+
}
24+
25+
Future<void> _startIdentifying() async {
26+
state.startProcessing();
27+
28+
String language = await _identifier.identify(_controller.text);
29+
state.data = language == 'und' ? 'Not Identified' : language.toUpperCase();
30+
state.stopProcessing();
31+
}
32+
33+
Future<void> _startIdentifyingPossibleLanguages() async {
34+
state.startProcessing();
35+
36+
List<IdentifiedLanguage> languages =
37+
await _identifier.idenfityPossibleLanguages(_controller.text);
38+
39+
String result = '';
40+
for (IdentifiedLanguage l in languages) {
41+
result += '${l.language.toUpperCase()} (${l.confidence.toString()})\n';
42+
}
43+
44+
state.data = result;
45+
state.stopProcessing();
46+
}
47+
1048
@override
1149
Widget build(BuildContext context) {
12-
return Container();
50+
return Scaffold(
51+
appBar: AppBar(
52+
title: Text('Language Identification'),
53+
),
54+
body: SingleChildScrollView(
55+
child: Column(
56+
crossAxisAlignment: CrossAxisAlignment.stretch,
57+
children: [
58+
SizedBox(height: 20),
59+
TextField(
60+
autofocus: true,
61+
controller: _controller,
62+
textAlign: TextAlign.center,
63+
textAlignVertical: TextAlignVertical.center,
64+
keyboardType: TextInputType.multiline,
65+
decoration: InputDecoration(
66+
contentPadding: EdgeInsets.symmetric(horizontal: 18),
67+
),
68+
minLines: 5,
69+
maxLines: 10,
70+
style: TextStyle(fontSize: 18, color: Colors.blueGrey[700]),
71+
onChanged: (_) => state.clear(),
72+
),
73+
SizedBox(height: 15),
74+
NormalBlueButton(
75+
text: 'Identify Language',
76+
onPressed: _startIdentifying,
77+
),
78+
SizedBox(height: 8),
79+
NormalBlueButton(
80+
text: 'Idenfity Possible Languages',
81+
onPressed: _startIdentifyingPossibleLanguages,
82+
),
83+
SizedBox(height: 25),
84+
Consumer<LearningLanguageState>(
85+
builder: (_, state, __) => Center(
86+
child: Text(
87+
state.isProcessing ? 'Identifying language...' : state.data,
88+
textAlign: TextAlign.center,
89+
style: TextStyle(
90+
fontWeight: FontWeight.bold,
91+
fontSize: 20,
92+
),
93+
),
94+
),
95+
),
96+
],
97+
),
98+
),
99+
);
13100
}
14101
}
15102

16-
class LearningLanguageState extends ChangeNotifier {}
103+
class LearningLanguageState extends ChangeNotifier {
104+
String _data = '';
105+
bool _isProcessing = false;
106+
107+
String get data => _data;
108+
bool get isProcessing => _isProcessing;
109+
110+
void startProcessing() {
111+
_isProcessing = true;
112+
notifyListeners();
113+
}
114+
115+
void stopProcessing() {
116+
_isProcessing = false;
117+
notifyListeners();
118+
}
119+
120+
set data(String data) {
121+
_data = data;
122+
notifyListeners();
123+
}
124+
125+
void clear() {
126+
_data = '';
127+
notifyListeners();
128+
}
129+
}

example/lib/nlp/smart_reply.dart

+46-3
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,59 @@
11
import 'package:flutter/material.dart';
22
import 'package:flutter/cupertino.dart';
33

4+
import 'package:learning_smart_reply/learning_smart_reply.dart';
5+
import 'package:uuid/uuid.dart';
6+
47
class LearningSmartReply extends StatefulWidget {
58
@override
69
_LearningSmartReplyState createState() => _LearningSmartReplyState();
710
}
811

912
class _LearningSmartReplyState extends State<LearningSmartReply> {
13+
SmartReplyGenerator _smartReply = SmartReplyGenerator();
14+
15+
@override
16+
void initState() {
17+
super.initState();
18+
WidgetsBinding.instance?.addPostFrameCallback((_) async {
19+
await testSmartReply();
20+
});
21+
}
22+
23+
@override
24+
void dispose() {
25+
_smartReply.dispose();
26+
super.dispose();
27+
}
28+
29+
Future<void> testSmartReply() async {
30+
print('testSmartReply...');
31+
32+
Uuid uuid = Uuid();
33+
String userId = uuid.v4();
34+
int now = DateTime.now().millisecondsSinceEpoch;
35+
36+
List<Message> history = [
37+
Message('Hi', user: userId, timestamp: now - (60 * 60 * 1000)),
38+
Message('How are you?', timestamp: now - (20 * 60 * 1000)),
39+
Message('I am fine. Thanks.',
40+
user: userId, timestamp: now - (10 * 60 * 1000)),
41+
];
42+
43+
_smartReply = SmartReplyGenerator();
44+
var result = await _smartReply.generateReplies(history);
45+
46+
print('testSmartReply Result:');
47+
print(result);
48+
}
49+
1050
@override
1151
Widget build(BuildContext context) {
12-
return Container();
52+
return Scaffold(
53+
appBar: AppBar(
54+
title: Text('Smart Reply'),
55+
),
56+
body: Container(),
57+
);
1358
}
1459
}
15-
16-
class LearningSmartReplyState extends ChangeNotifier {}

example/lib/nlp/translate.dart

+124-2
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,138 @@
11
import 'package:flutter/material.dart';
22
import 'package:flutter/cupertino.dart';
3+
import 'package:provider/provider.dart';
4+
5+
import 'package:learning_input_image/learning_input_image.dart';
6+
import 'package:learning_translate/learning_translate.dart';
37

48
class LearningTranslate extends StatefulWidget {
59
@override
610
_LearningTranslateState createState() => _LearningTranslateState();
711
}
812

913
class _LearningTranslateState extends State<LearningTranslate> {
14+
TextEditingController _controller = TextEditingController();
15+
Translator _translator = Translator(from: FRENCH, to: ENGLISH);
16+
17+
LearningTranslateState get state => Provider.of(context, listen: false);
18+
19+
@override
20+
void initState() {
21+
super.initState();
22+
23+
WidgetsBinding.instance?.addPostFrameCallback((_) async {
24+
await TranslationModelManager.download(FRENCH);
25+
});
26+
}
27+
28+
@override
29+
void dispose() {
30+
_translator.dispose();
31+
super.dispose();
32+
}
33+
34+
Future<void> _startTranslating() async {
35+
bool exist = await TranslationModelManager.check(FRENCH);
36+
37+
if (!exist) {
38+
state.startDownloading();
39+
await TranslationModelManager.download(FRENCH);
40+
state.stopDownloading();
41+
}
42+
43+
state.startProcessing();
44+
state.data = await _translator.translate(_controller.text);
45+
state.stopProcessing();
46+
}
47+
1048
@override
1149
Widget build(BuildContext context) {
12-
return Container();
50+
return Scaffold(
51+
appBar: AppBar(
52+
title: Text('On-Device Translation'),
53+
),
54+
body: SingleChildScrollView(
55+
child: Column(
56+
crossAxisAlignment: CrossAxisAlignment.stretch,
57+
children: [
58+
SizedBox(height: 20),
59+
TextField(
60+
autofocus: true,
61+
controller: _controller,
62+
textAlign: TextAlign.center,
63+
textAlignVertical: TextAlignVertical.center,
64+
keyboardType: TextInputType.multiline,
65+
decoration: InputDecoration(
66+
contentPadding: EdgeInsets.symmetric(horizontal: 18),
67+
),
68+
minLines: 5,
69+
maxLines: 10,
70+
style: TextStyle(fontSize: 18, color: Colors.blueGrey[700]),
71+
onChanged: (_) => state.clear(),
72+
),
73+
SizedBox(height: 15),
74+
NormalBlueButton(
75+
text: 'Translate from FR to EN',
76+
onPressed: _startTranslating,
77+
),
78+
SizedBox(height: 25),
79+
Consumer<LearningTranslateState>(
80+
builder: (_, state, __) => Center(
81+
child: Text(
82+
state.data,
83+
textAlign: TextAlign.center,
84+
style: TextStyle(
85+
fontWeight: FontWeight.bold,
86+
fontSize: 20,
87+
),
88+
),
89+
),
90+
),
91+
],
92+
),
93+
),
94+
);
1395
}
1496
}
1597

16-
class LearningTranslateState extends ChangeNotifier {}
98+
class LearningTranslateState extends ChangeNotifier {
99+
String _data = '';
100+
bool _isProcessing = false;
101+
bool _isDownloading = false;
102+
103+
String get data => _data;
104+
bool get isProcessing => _isProcessing;
105+
bool get isDownloading => _isDownloading;
106+
107+
void startProcessing() {
108+
_data = 'Translating...';
109+
_isProcessing = true;
110+
notifyListeners();
111+
}
112+
113+
void stopProcessing() {
114+
_isProcessing = false;
115+
notifyListeners();
116+
}
117+
118+
void startDownloading() {
119+
_data = 'Downloading translation model...';
120+
_isDownloading = true;
121+
notifyListeners();
122+
}
123+
124+
void stopDownloading() {
125+
_isDownloading = false;
126+
notifyListeners();
127+
}
128+
129+
set data(String data) {
130+
_data = data;
131+
notifyListeners();
132+
}
133+
134+
void clear() {
135+
_data = '';
136+
notifyListeners();
137+
}
138+
}

0 commit comments

Comments
 (0)