enhance(frontend): 絵文字のオートコンプリートのアルゴリズムの改善 (MisskeyIO#261)
* 実際は同じ絵文字なら重複してサジェストに出ないように * エイリアスではない絵文字>前方一致>部分一致>あいまい検索順で表示されるようになるように
This commit is contained in:
		| @@ -265,7 +265,7 @@ function emojiAutoComplete(query: string | null, emojiDb: EmojiDef[], max = 30): | |||||||
| 	// 前方一致(エイリアスなし) | 	// 前方一致(エイリアスなし) | ||||||
| 	emojiDb.some(x => { | 	emojiDb.some(x => { | ||||||
| 		if (x.name.startsWith(query) && !x.aliasOf) { | 		if (x.name.startsWith(query) && !x.aliasOf) { | ||||||
| 			matched.set(x.name, { emoji: x, score: query.length }); | 			matched.set(x.name, { emoji: x, score: query.length + 1 }); | ||||||
| 		} | 		} | ||||||
| 		return matched.size === max; | 		return matched.size === max; | ||||||
| 	}); | 	}); | ||||||
| @@ -273,8 +273,8 @@ function emojiAutoComplete(query: string | null, emojiDb: EmojiDef[], max = 30): | |||||||
| 	// 前方一致(エイリアス込み) | 	// 前方一致(エイリアス込み) | ||||||
| 	if (matched.size < max) { | 	if (matched.size < max) { | ||||||
| 		emojiDb.some(x => { | 		emojiDb.some(x => { | ||||||
| 			if (x.name.startsWith(query)) { | 			if (x.name.startsWith(query) && !matched.has(x.aliasOf ?? x.name)) { | ||||||
| 				matched.set(x.name, { emoji: x, score: query.length }); | 				matched.set(x.aliasOf ?? x.name, { emoji: x, score: query.length }); | ||||||
| 			} | 			} | ||||||
| 			return matched.size === max; | 			return matched.size === max; | ||||||
| 		}); | 		}); | ||||||
| @@ -283,36 +283,32 @@ function emojiAutoComplete(query: string | null, emojiDb: EmojiDef[], max = 30): | |||||||
| 	// 部分一致(エイリアス込み) | 	// 部分一致(エイリアス込み) | ||||||
| 	if (matched.size < max) { | 	if (matched.size < max) { | ||||||
| 		emojiDb.some(x => { | 		emojiDb.some(x => { | ||||||
| 			if (x.name.includes(query)) { | 			if (x.name.includes(query) && !matched.has(x.aliasOf ?? x.name)) { | ||||||
| 				matched.set(x.name, { emoji: x, score: query.length }); | 				matched.set(x.aliasOf ?? x.name, { emoji: x, score: query.length - 1 }); | ||||||
| 			} | 			} | ||||||
| 			return matched.size === max; | 			return matched.size === max; | ||||||
| 		}); | 		}); | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	// 簡易あいまい検索 | 	// 簡易あいまい検索(3文字以上) | ||||||
| 	if (matched.size < max) { | 	if (matched.size < max && query.length > 3) { | ||||||
| 		const queryChars = [...query]; | 		const queryChars = [...query]; | ||||||
| 		const hitEmojis = new Map<string, EmojiScore>(); | 		const hitEmojis = new Map<string, EmojiScore>(); | ||||||
|  |  | ||||||
| 		for (const x of emojiDb) { | 		for (const x of emojiDb) { | ||||||
| 			// クエリ文字列の1文字単位で絵文字名にヒットするかを見る | 			// 文字列の位置を進めながら、クエリの文字を順番に探す | ||||||
| 			// ただし、過剰に検出されるのを防ぐためクエリ文字列に登場する順番で絵文字名を走査する |  | ||||||
|  |  | ||||||
| 			let queryCharHitPos = 0; | 			let pos = 0; | ||||||
| 			let queryCharHitCount = 0; | 			let hit = 0; | ||||||
| 			for (let idx = 0; idx < queryChars.length; idx++) { | 			for (const c of queryChars) { | ||||||
| 				queryCharHitPos = x.name.indexOf(queryChars[idx], queryCharHitPos); | 				pos = x.name.indexOf(c, pos); | ||||||
| 				if (queryCharHitPos <= -1) { | 				if (pos <= -1) break; | ||||||
| 					break; | 				hit++; | ||||||
| 				} |  | ||||||
|  |  | ||||||
| 				queryCharHitCount++; |  | ||||||
| 			} | 			} | ||||||
|  |  | ||||||
| 			// ヒット数が少なすぎると検索結果が汚れるので調節する | 			// 半分以上の文字が含まれていればヒットとする | ||||||
| 			if (queryCharHitCount > 2) { | 			if (hit > Math.ceil(queryChars.length / 2) && hit - 2 > (matched.get(x.aliasOf ?? x.name)?.score ?? 0)) { | ||||||
| 				hitEmojis.set(x.name, { emoji: x, score: queryCharHitCount }); | 				hitEmojis.set(x.aliasOf ?? x.name, { emoji: x, score: hit - 2 }); | ||||||
| 			} | 			} | ||||||
| 		} | 		} | ||||||
|  |  | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 まっちゃとーにゅ
					まっちゃとーにゅ