-
Notifications
You must be signed in to change notification settings - Fork 1
/
MLKitEntityExtraction.js
161 lines (151 loc) · 3.71 KB
/
MLKitEntityExtraction.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import { NativeModules, Platform } from "react-native";
const { MLKitEntityExtraction: NativeMLKitEntityExtraction } = NativeModules;
const isAndroid = Platform.OS === 'android';
export const ENTITY_LANG_TAGS = {
ARABIC: "arabic",
GERMAN: "german",
ENGLISH: "english",
SPANISH: "spanish",
FRENCH: "french",
ITALIAN: "italian",
JAPANESE: "japanese",
KOREAN: "korean",
DUTCH: "dutch",
POLISH: "polish",
PORTUGUESE: "portuguese",
RUSSIAN: "russian",
THAI: "thai",
TURKISH: "turkish",
CHINESE: "chinese",
}
export const LANG_TAGS = {
AFRIKAANS: "af",
ALBANIAN: "sq",
ARABIC: "ar",
BELARUSIAN: "be",
BULGARIAN: "bg",
BENGALI: "bn",
CATALAN: "ca",
CHINESE: "zh",
CROATIAN: "hr",
CZECH: "cs",
DANISH: "da",
DUTCH: "nl",
ENGLISH: "en",
ESPERANTO: "eo",
ESTONIAN: "et",
FINNISH: "fi",
FRENCH: "fr",
GALICIAN: "gl",
GEORGIAN: "ka",
GERMAN: "de",
GREEK: "el",
GUJARATI: "gu",
HAITIAN_CREOLE: "ht",
HEBREW: "he",
HINDI: "hi",
HUNGARIAN: "hu",
ICELANDIC: "is",
INDONESIAN: "id",
IRISH: "ga",
ITALIAN: "it",
JAPANESE: "ja",
KANNADA: "kn",
KOREAN: "ko",
LITHUANIAN: "lt",
LATVIAN: "lv",
MACEDONIAN: "mk",
MARATHI: "mr",
MALAY: "ms",
MALTESE: "mt",
NORWEGIAN: "no",
PERSIAN: "fa",
POLISH: "pl",
PORTUGUESE: "pt",
ROMANIAN: "ro",
RUSSIAN: "ru",
SLOVAK: "sk",
SLOVENIAN: "sl",
SPANISH: "es",
SWEDISH: "sv",
SWAHILI: "sw",
TAGALOG: "tl",
TAMIL: "ta",
TELUGU: "te",
THAI: "th",
TURKISH: "tr",
UKRAINIAN: "uk",
URDU: "ur",
VIETNAMESE: "vi",
WELSH: "cy",
}
export const ENTITY_TYPES = {
TYPE_ADDRESS: 1,
TYPE_DATE_TIME: 2,
TYPE_EMAIL: 3,
TYPE_FLIGHT_NUMBER: 4,
TYPE_IBAN: 5,
TYPE_ISBN: 6,
TYPE_PAYMENT_CARD: 7,
TYPE_PHONE: 8,
TYPE_TRACKING_NUMBER: 9,
TYPE_URL: 10,
TYPE_MONEY: 11,
}
const annotate = (text, lang, types) => {
return new Promise((resolver, rejecter) => {
let mappedTypes = [];
for (let tp of types) {
if (typeof(ENTITY_TYPES[tp]) === 'number') mappedTypes.push(ENTITY_TYPES[tp]);
}
NativeMLKitEntityExtraction.annotate(
text,
isAndroid ? ENTITY_LANG_TAGS[lang] : LANG_TAGS[lang],
mappedTypes,
(v) => {
resolver(v);
},
(e) => {
rejecter(e);
});
});
}
const isModelDownloaded = (language) => {
return new Promise((resolver, rejecter) => {
NativeMLKitEntityExtraction.isModelDownloaded(
isAndroid ? ENTITY_LANG_TAGS[language] : LANG_TAGS[language],
(v) => {
if (Platform.OS === 'ios') {
resolver(v === 1 ? true : false);
} else {
resolver(v);
}
}
);
});
}
const deleteDownloadedModel = (language) => {
return new Promise((resolver, rejecter) => {
NativeMLKitEntityExtraction.deleteDownloadedModel(
isAndroid ? ENTITY_LANG_TAGS[language] : LANG_TAGS[language],
(v) => { resolver(v); },
(e) => { rejecter(e); }
);
});
}
const downloadModel = (language) => {
return new Promise((resolver, rejecter) => {
NativeMLKitEntityExtraction.downloadModel(
isAndroid ? ENTITY_LANG_TAGS[language] : LANG_TAGS[language],
(v) => { resolver(v); },
(e) => { rejecter(e); }
);
});
}
const MLKitEntityExtraction = {
annotate,
isModelDownloaded,
deleteDownloadedModel,
downloadModel
}
export default MLKitEntityExtraction;