From 248359bb094bb8c9219de069d0d16adb913ff385 Mon Sep 17 00:00:00 2001 From: Yasuaki Uechi Date: Thu, 7 Oct 2021 12:44:01 +0900 Subject: [PATCH] fix: better suggestion --- src/components/Suggestion.tsx | 21 +++++++-------------- src/util/array.ts | 32 ++++++++++++++++++-------------- 2 files changed, 25 insertions(+), 28 deletions(-) diff --git a/src/components/Suggestion.tsx b/src/components/Suggestion.tsx index f11bb46..d241d15 100644 --- a/src/components/Suggestion.tsx +++ b/src/components/Suggestion.tsx @@ -1,6 +1,5 @@ import fetch from 'cross-fetch'; import { motion } from 'framer-motion'; -import MersenneTwister from 'mersennetwister'; import React, { useEffect, useRef, useState } from 'react'; import { useTranslation } from 'react-i18next'; import { TiArrowSync } from 'react-icons/ti'; @@ -9,7 +8,7 @@ import { sendAcceptSuggestionEvent, sendShuffleSuggestionEvent, } from '../util/analytics'; -import { fillArray, sampleFromArray } from '../util/array'; +import { sample, sampleMany, times } from '../util/array'; import { mobile, slideUp } from '../util/css'; import { capitalize, @@ -23,7 +22,6 @@ import { type Modifier = (word: string) => string; -const maximumCount = 3; const modifiers: Modifier[] = [ (word): string => `${capitalize(germanify(word))}`, (word): string => `${capitalize(word)}`, @@ -166,17 +164,13 @@ const fontFamilies = [ const fontWeight = [300, 600, 900]; -const mt = new MersenneTwister(); - -function sample(arr: T[]): T { - return arr[Math.floor(mt.random() * arr.length)]; -} +const numSuggestion = 3; function modifyWord(word: string): string { return sample(modifiers)(word); } -async function findSynonyms(word: string): Promise { +async function getSynonyms(word: string): Promise { try { const response = await fetch( `https://translate.googleapis.com/translate_a/single?client=gtx&sl=auto&dt=ss&ie=UTF-8&oe=UTF-8&dj=1&q=${encodeURIComponent( @@ -211,10 +205,9 @@ const Suggestion: React.FC<{ const [bestWords, setBestWords] = useState([]); function shuffle(): void { - const best = fillArray( - sampleFromArray(synonymRef.current, maximumCount), - query, - maximumCount + const best = sampleMany( + [...synonymRef.current.filter((s) => s.length < 8), ...times(query, 10)], + numSuggestion ).map((word) => modifyWord(word)); setBestWords(best); } @@ -233,7 +226,7 @@ const Suggestion: React.FC<{ let isEffective = true; const fn = async (): Promise => { if (query && query.length > 0) { - const synonyms = await findSynonyms(query); + const synonyms = await getSynonyms(query); if (!isEffective) { return; } diff --git a/src/util/array.ts b/src/util/array.ts index a5e5469..d4fa265 100644 --- a/src/util/array.ts +++ b/src/util/array.ts @@ -1,6 +1,22 @@ -export function shuffleArray(array: T[]): T[] { +import MersenneTwister from 'mersennetwister'; + +const mt = new MersenneTwister(); + +export function times(i: T, times: number): T[] { + return Array.from({ length: times }, () => i); +} + +export function sample(arr: T[]): T { + return arr[Math.floor(mt.random() * arr.length)]; +} + +export function sampleMany(array: T[], maximum: number = 1): T[] { + return shuffle(array).slice(0, maximum); +} + +export function shuffle(array: T[]): T[] { for (let i = array.length - 1; i > 0; i--) { - const j = Math.floor(Math.random() * (i + 1)); + const j = Math.floor(mt.random() * (i + 1)); const temp = array[i]; array[i] = array[j]; array[j] = temp; @@ -8,18 +24,6 @@ export function shuffleArray(array: T[]): T[] { return array; } -export function sampleFromArray(array: T[], maximum: number): T[] { - return shuffleArray(array).slice(0, maximum); -} - -export function fillArray(array: T[], filler: string, maximum: number): T[] { - const deficit = maximum - array.length; - if (deficit > 0) { - array = [...array, ...Array(deficit).fill(filler)]; - } - return array; -} - export function compose(arg: T, ...fn: ((arg: T) => T)[]): T { return fn.reduce((arg, f) => f(arg), arg); }