Index: graphspell-js/str_transform.js ================================================================== --- graphspell-js/str_transform.js +++ graphspell-js/str_transform.js @@ -5,10 +5,18 @@ // Note: 48 is the ASCII code for "0" var str_transform = { + + getNgrams: function (sWord, n=2) { + let lNgrams = []; + for (let i=0; i <= sWord.length - n; i++) { + lNgrams.push(sWord.slice(i, i+n)); + } + return lNgrams; + }, longestCommonSubstring: function (string1, string2) { // https://en.wikibooks.org/wiki/Algorithm_Implementation/Strings/Longest_common_substring // untested Index: graphspell/str_transform.py ================================================================== --- graphspell/str_transform.py +++ graphspell/str_transform.py @@ -1,12 +1,17 @@ -#!python3 - """ Operations on strings: - calculate distance between two strings - transform strings with transformation codes """ + +#### Ngrams + +def getNgrams (sWord, n=2): + "return a list of Ngrams strings" + return [ sWord[i:i+n] for i in range(len(sWord)-n+1) ] + #### DISTANCE CALCULATIONS def longestCommonSubstring (s1, s2):