docbrown/app/labor/language_detector.rb
rhymes e588fa7ece Code cleanups (#659)
* Initial automatic cleanup with rubocop

* Fix syntax error introduced by rubocop

* Cleanup seeds file

* Cleanup lib folder

* Exclude bin folder because it contains auto generated files

* Make Rubocop a little bit more chatty

* Block length should not include comments in the count

* Cleanup config folder

* Cleanup specs

* Updated Rubocop version and generated a todo file

* Fix broken ArticlesApi spec

* Fix tests

* Restored rubocop pre-commit hook
2018-08-07 11:00:13 -04:00

29 lines
786 B
Ruby

class LanguageDetector
def initialize(article)
@article = article
end
def detect
response = get_language
response["result"] if response["confidence"] > 0.8
rescue StandardError
nil
end
def get_language
return { "result" => "en", "confidence" => 0.9 } unless Rails.env.production?
client = Algorithmia.client(ApplicationConfig["ALGORITHMIA_KEY"])
algo = client.algo("miguelher/LanguageDetector/0.1.0")
algo.pipe(text).result
end
def text
@article.title + "\n" +
non_default_description.to_s +
FrontMatterParser::Parser.new(:md).call(@article.body_markdown).content.split("`")[0]
end
def non_default_description
@article.description + "\n" unless @article.description.include? "From the DEV community"
end
end