Skip to content

Commit

Permalink
dep: migrate to fastest-levenshtein
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Sep 8, 2020
1 parent 27d3468 commit 031293c
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 11 deletions.
2 changes: 1 addition & 1 deletion packages/koishi-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@
"dependencies": {
"@types/koa-bodyparser": "^4.3.0",
"@types/koa-router": "^7.4.1",
"fastest-levenshtein": "^1.0.12",
"koa": "^2.13.0",
"koa-bodyparser": "^4.3.0",
"koa-router": "^9.4.0",
"koishi-utils": "^3.1.4",
"leven": "^3.1.0",
"lru-cache": "^6.0.0"
}
}
12 changes: 6 additions & 6 deletions packages/koishi-core/src/plugins/suggest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Session } from '../session'
import { Message } from './message'
import { getCommands } from './help'
import { format } from 'util'
import leven from 'leven'
import { distance } from 'fastest-levenshtein'

declare module '../session' {
interface Session {
Expand Down Expand Up @@ -62,13 +62,13 @@ Session.prototype.$suggest = function $suggest(this: Session, options: SuggestOp

let suggestions: string[], minDistance = Infinity
for (const name of items) {
const distance = leven(name, target)
if (name.length <= 2 || distance > name.length * coefficient) continue
if (distance === minDistance) {
const dist = distance(name, target)
if (name.length <= 2 || dist > name.length * coefficient) continue
if (dist === minDistance) {
suggestions.push(name)
} else if (distance < minDistance) {
} else if (dist < minDistance) {
suggestions = [name]
minDistance = distance
minDistance = dist
}
}
if (!suggestions) return next(() => this.$send(prefix))
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-teach/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@
},
"dependencies": {
"axios": "^0.20.0",
"fastest-levenshtein": "^1.0.12",
"koishi-utils": "^3.1.4",
"leven": "^3.1.0",
"regexpp": "^3.1.0"
}
}
6 changes: 3 additions & 3 deletions packages/plugin-teach/src/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { RegExpValidator } from 'regexpp'
import { defineProperty } from 'koishi-utils'
import { formatQuestionAnswers } from './search'
import { format } from 'util'
import leven from 'leven'
import { distance } from 'fastest-levenshtein'

declare module 'koishi-core/dist/command' {
interface CommandConfig {
Expand Down Expand Up @@ -75,9 +75,9 @@ export default function apply(ctx: Context, config: Dialogue.Config) {

function maybeAnswer(question: string, dialogues: Dialogue[]) {
return dialogues.every(dialogue => {
const dist = leven(question, dialogue.answer)
const dist = distance(question, dialogue.answer)
return dist < dialogue.answer.length / 2
&& dist < leven(question, dialogue.question)
&& dist < distance(question, dialogue.question)
})
}

Expand Down

0 comments on commit 031293c

Please sign in to comment.