overleaf/services/spelling/app/coffee/SpellingAPIManager.coffee

52 lines
1.4 KiB
CoffeeScript
Raw Normal View History

2014-08-15 07:13:35 -04:00
ASpell = require './ASpell'
LearnedWordsManager = require './LearnedWordsManager'
async = require 'async'
module.exports = SpellingAPIManager =
whitelist: [
'ShareLaTeX',
'sharelatex',
'LaTeX',
'http',
'https',
'www'
]
2014-08-15 07:13:35 -04:00
runRequest: (token, request, callback = (error, result) ->) ->
if !request.words?
return callback(new Error("malformed JSON"))
2014-08-15 07:13:35 -04:00
lang = request.language || "en"
check = (words, callback) ->
ASpell.checkWords lang, words, (error, misspellings) ->
callback error, misspellings: misspellings
wordsToCheck = request.words || []
2014-08-15 07:13:35 -04:00
if token?
LearnedWordsManager.getLearnedWords token, (error, learnedWords) ->
return callback(error) if error?
words = (wordsToCheck).slice(0,10000)
2014-08-15 07:13:35 -04:00
check words, (error, result) ->
return callback error if error?
result.misspellings = result.misspellings.filter (m) ->
word = words[m.index]
learnedWords.indexOf(word) == -1 and SpellingAPIManager.whitelist.indexOf(word) == -1
2014-08-15 07:13:35 -04:00
callback error, result
else
check(wordsToCheck, callback)
2014-08-15 07:13:35 -04:00
learnWord: (token, request, callback = (error) ->) ->
if !request.word?
return callback(new Error("malformed JSON"))
if !token?
return callback(new Error("no token provided"))
LearnedWordsManager.learnWord token, request.word, callback
2017-10-30 12:57:34 -04:00
deleteDic: (token, callback)->
LearnedWordsManager.deleteUsersLearnedWords token, callback
2014-08-15 07:13:35 -04:00