@@ -50,6 +50,7 @@ class Global with ChangeNotifier {
5050 await prefs.setString ("wordData" , jsonEncode ({"Words" : [], "Classes" : {}}));
5151 wordData = DictData (words: [], classes: []);
5252 logger.info ("首次启动: 配置表初始化完成" );
53+ globalFSRS = FSRS ()..init (outerPrefs: prefs);
5354 await postInit ();
5455 } else {
5556 await conveySetting ();
@@ -230,11 +231,20 @@ class Global with ChangeNotifier {
230231 // }
231232 DictData dataFormater (Map <String , dynamic > data, DictData exData, String sourceName) {
232233 logger.info ("开始词汇格式化" );
233- List <String > wordList = [];
234- for (WordItem x in exData.words) {
235- wordList.add (x.arabic);
234+
235+ // Use Maps for O(1) lookup speed instead of O(N) List.indexOf
236+ Map <String , int > rawWordMap = {};
237+ Map <String , int > pureWordMap = {};
238+ List <String > chineseList = [];
239+
240+ for (int i = 0 ; i < exData.words.length; i++ ) {
241+ WordItem x = exData.words[i];
242+ rawWordMap[x.arabic] = i;
243+ pureWordMap[x.arabic.removeAracicExtensionPart ().trim ()] = i;
244+ chineseList.add (x.chinese); // Keep list for indexing since it maps 1:1 with word id
236245 }
237- int counter = wordList.length;
246+
247+ int counter = exData.words.length;
238248
239249 SourceItem ? exSource;
240250 // 查找已有数据中是否有同名的源数据组
@@ -249,9 +259,28 @@ class Global with ChangeNotifier {
249259 for (var className in data.keys){
250260 ClassItem exClass = ClassItem (className: className, wordIndexs: []);
251261 for (var word in data[className]){
252- if (wordList.contains (word["arabic" ])){
262+ String newRaw = word["arabic" ];
263+ String newPure = newRaw.removeAracicExtensionPart ().trim ();
264+ int existingIndex = - 1 ;
265+
266+ if (rawWordMap.containsKey (newRaw)) {
267+ existingIndex = rawWordMap[newRaw]! ;
268+ } else if (pureWordMap.containsKey (newPure)) {
269+ int potentialIndex = pureWordMap[newPure]! ;
270+ // Pure arabic is the same, but different vowels. Are they the same meaning?
271+ if (chineseList[potentialIndex].hasSimilarMeaning (word["chinese" ])) {
272+ existingIndex = potentialIndex;
273+ }
274+ }
275+
276+ if (existingIndex != - 1 ) {
277+ // If it already exists globally, just add it to this class
278+ if (! exClass.wordIndexs.contains (existingIndex)) {
279+ exClass.wordIndexs.add (existingIndex);
280+ }
253281 continue ;
254282 }
283+
255284 exClass.wordIndexs.add (counter);
256285 exData.words.add (
257286 WordItem (
@@ -262,7 +291,9 @@ class Global with ChangeNotifier {
262291 id: counter
263292 )
264293 );
265- wordList.add (word["arabic" ]);
294+ rawWordMap[newRaw] = counter;
295+ pureWordMap[newPure] = counter;
296+ chineseList.add (word["chinese" ]);
266297 counter ++ ;
267298 }
268299 exSource.subClasses.add (exClass);
0 commit comments