store suggestions as language:word instead of word

avoid words like 'constructor' from accessing properties of
suggestions object.
This commit is contained in:
Brian 2015-03-11 14:50:24 +00:00
parent 9bb534707f
commit 220f82a395

View file

@ -11,9 +11,10 @@ class ASpellRunner
@runAspellOnWords language, words, (error, output) => @runAspellOnWords language, words, (error, output) =>
return callback(error) if error? return callback(error) if error?
#output = @removeAspellHeader(output) #output = @removeAspellHeader(output)
suggestions = @getSuggestions(output) suggestions = @getSuggestions(language, output)
results = [] results = []
hits = 0 hits = 0
addToCache = {}
for word, i in words for word, i in words
key = language + ':' + word key = language + ':' + word
cached = cache.get(key) cached = cache.get(key)
@ -25,16 +26,22 @@ class ASpellRunner
else else
results.push index: i, suggestions: cached results.push index: i, suggestions: cached
else else
if suggestions[word]? if suggestions[key]?
cache.set(key, suggestions[word]) addToCache[key] = suggestions[key]
results.push index: i, suggestions: suggestions[word] results.push index: i, suggestions: suggestions[key]
else else
# a valid word, but uncached # a valid word, but uncached
cache.set(key, true) addToCache[key] = true
# update the cache after processing all words, to avoid cache
# changing while we use it
for k, v in addToCache
cache.set(k, v)
logger.log hits: hits, total: words.length, hitrate: hits/words.length, "cache hit rate" logger.log hits: hits, total: words.length, hitrate: hits/words.length, "cache hit rate"
callback null, results callback null, results
getSuggestions: (output) -> getSuggestions: (language, output) ->
lines = output.split("\n") lines = output.split("\n")
suggestions = {} suggestions = {}
for line in lines for line in lines
@ -43,12 +50,12 @@ class ASpellRunner
if parts.length > 1 if parts.length > 1
word = parts[1] word = parts[1]
suggestionsString = line.slice(line.indexOf(":") + 2) suggestionsString = line.slice(line.indexOf(":") + 2)
suggestions[word] = suggestionsString.split(", ") suggestions[language + ":" + word] = suggestionsString.split(", ")
else if line[0] == "#" # No suggestions else if line[0] == "#" # No suggestions
parts = line.split(" ") parts = line.split(" ")
if parts.length > 1 if parts.length > 1
word = parts[1] word = parts[1]
suggestions[word] = [] suggestions[language + ":" + word] = []
return suggestions return suggestions
#removeAspellHeader: (output) -> output.slice(1) #removeAspellHeader: (output) -> output.slice(1)