Просмотр исходного кода

按照长度和数量计算权重

visuddhinanda 2 лет назад
Родитель
Сommit
20ee215c9b
1 измененных файлов с 18 добавлено и 3 удалено
  1. 18 3
      dashboard/src/components/dict/SearchVocabulary.tsx

+ 18 - 3
dashboard/src/components/dict/SearchVocabulary.tsx

@@ -101,9 +101,24 @@ const SearchVocabularyWidget = ({
 
     get<IVocabularyListResponse>(`/v2/${api}?view=key&key=${value}`)
       .then((json) => {
-        const words: ValueType[] = json.data.rows.map((item) => {
-          return renderItem(item.word, item.count, item.meaning);
-        });
+        const words: ValueType[] = json.data.rows
+          .map((item) => {
+            let weight = item.count / item.strlen;
+            if (item.word.length === value.length) {
+              weight = 100;
+            }
+            return {
+              word: item.word,
+              count: item.count,
+              meaning: item.meaning,
+              weight: weight,
+            };
+          })
+          .sort((a, b) => b.weight - a.weight)
+          .map((item) => {
+            return renderItem(item.word, item.count, item.meaning);
+          });
+
         setOptions(words);
       })
       .finally(() => {