A more powerful script for comparing two strings, based on string-similarity, which can allow you to get the situation of the accuracy of every single word.
- Usage
- API
- Acknowledgements
Import js:
<script src="proxy.php?url=https%3A%2F%2Fgithub.com%2Fstring-similarity-ex.js"></script>
In your code:
var key = 'Hello, world!';
var answer = 'Hello, word';
// main usage
var situation = stringSimilarityEx.getSentenceSituation(key, answer);
console.log(situation);
Returns an array, which includes all words that are the same as those of the correct sentence.
- mainString (string): correct sentence
- targetString (string): user's sentence
Order must not be reversed.
(array): every item is a word with punctuation.
var key = 'Hello, world!';
var answer = 'Hello, word';
var match = stringSimilarityEx.getBestMatch(key, answer);
console.log(match);
// ['hello, ']
Returns an array, in which every item is an object with two properties - "word" and "replaces".
- formatString (string): the format of the fault tolerance string
Refer to getSentenceSituation to get definition.
(array): every item is an object like "{word: 'soul', replaces: ['sole', 'so']}".
var parse = stringSimilarityEx.parseFaultTolerance('soul:sole,so');
console.log(parse);
// [{"word":"soul","replaces":["sole","so"]}]
Returns an array including all possibility of replacing words.
- formatString (string): the format of the fault tolerance string
Refer to getSentenceSituation to get definition.
(array): every item is an object for replacing words in the sentence.
var replacements = stringSimilarityEx.getSentenceReplacements('soul:sole,so');
console.log(replacements);
// [{"soul":"sole"},{"soul":"so"}]
Returns an array including all similar sentences based on the format main string.
- mainString (string): correct sentence
- formatString (string): the format of the fault tolerance string
Refer to getSentenceSituation to get definition.
(array): every item is a string.
var similarities = stringSimilarityEx.getSimilarSentences('Hello, world!', 'world:word');
console.log(similarities);
// ["Hello, word!"]
Main function of "stringSimilarityEx", allow you to get the situation of the accuracy of every single word.
- mainString (string): correct sentence
- targetString (string): user's sentence
- formatString (string|undefined): the format of the fault tolerance string
Definition of the format string: search1:replace1|search2:replace2,replace3
- example 1: soul:sole
- example 2: soul:sole,so
- example 3: soul:sole,so|well:will
- example 4: soul:sole,so|well:will|too:two,to
(array): every item is an object with two properties - "word" and "correct". Property "correct" equals 0 indicates the word is not correct. It can be obtained that property "correct" equals 1 indicates the word is correct.
var key = 'Hello, world!';
var answer = 'Hello, word';
var format = 'world:word';
// without tolerance: [{"word":"Hello,","correct":1},{"word":"world!","correct":0}]
var situation1 = stringSimilarityEx.getSentenceSituation(key, answer);
console.log(situation1);
// with tolerance: [{"word":"Hello,","correct":1},{"word":"world!","correct":1}]
var situation2 = stringSimilarityEx.getSentenceSituation(key, answer, format);
console.log(situation2);
Thank Winson for his idea of how to get the best match between two strings and correcting my grammar mistakes.