Last active
March 18, 2021 07:45
-
-
Save azu/2841dc090d45702e6848c0fd1ac752f4 to your computer and use it in GitHub Desktop.
typo finder for \w
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const lodash = require("lodash"); | |
const levenshtein = require("js-levenshtein"); | |
const { JSDOM } = require("jsdom") | |
class Typo { | |
constructor(strings) { | |
this.strings = strings; | |
this.groupByKey = lodash.groupBy(strings); | |
} | |
keys() { | |
return Object.keys(this.groupByKey); | |
} | |
countOfKey(key) { | |
if (key in this.groupByKey) { | |
return this.groupByKey[key].length; | |
} | |
return 0; | |
} | |
getSimilarKey(key, { distance, minKeyLength }) { | |
const allKeys = this.keys(); | |
return allKeys.map(targetKey => { | |
const distance = levenshtein(key, targetKey); | |
return { | |
key: targetKey, | |
length: targetKey.length, | |
distance | |
}; | |
}).filter(keyInfo => { | |
return keyInfo.distance === distance && keyInfo.length >= minKeyLength; | |
}); | |
} | |
preferKey(keyA, keyB) { | |
return this.countOfKey(keyA) - this.countOfKey(keyB); | |
} | |
} | |
(async function (){ | |
const dom = await JSDOM.fromURL("https://qiita.com/YankeeDeltaBravo225/items/9f08c0eccd48f00b9f9e"); | |
const contentElement = dom.window.document.querySelector(".p-items_main"); | |
const words = [...contentElement.textContent.matchAll(/[\w\d'-]+/g)].map(word => { | |
return word[0].toLowerCase() | |
}) | |
const typo = new Typo(words); | |
typo.keys().forEach(key => { | |
const matches = typo.getSimilarKey(key, { | |
distance: 1, | |
minKeyLength: 3 | |
}); | |
if (matches.length === 0) { | |
return; | |
} | |
const preferResults = matches.filter(match => { | |
return typo.preferKey(key, match.key) < 0; | |
}); | |
if (preferResults.length === 0) { | |
return; | |
} | |
const print = (results) => { | |
return results.map(result => { | |
return `${result.key} is used ${typo.countOfKey(result.key)} times. Distance is ${result.distance}`; | |
}).join("\n"); | |
}; | |
console.log(` | |
----------------------- | |
${key} may be typo. It is used at ${typo.countOfKey(key)} times. | |
Alternatives: | |
${print(preferResults)} | |
----------------------- | |
`); | |
}); | |
})() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment