language_detector.rb 914 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. # frozen_string_literal: true
  2. class LanguageDetector
  3. attr_reader :text, :account
  4. def initialize(text, account = nil)
  5. @text = text
  6. @account = account
  7. @identifier = CLD3::NNetLanguageIdentifier.new(1, 2048)
  8. end
  9. def to_iso_s
  10. detected_language_code || default_locale
  11. end
  12. def prepared_text
  13. simplified_text.strip
  14. end
  15. private
  16. def detected_language_code
  17. result.language.to_sym if detected_language_reliable?
  18. end
  19. def result
  20. @result ||= @identifier.find_language(prepared_text)
  21. end
  22. def detected_language_reliable?
  23. result.reliable?
  24. end
  25. def simplified_text
  26. text.dup.tap do |new_text|
  27. new_text.gsub!(FetchLinkCardService::URL_PATTERN, '')
  28. new_text.gsub!(Account::MENTION_RE, '')
  29. new_text.gsub!(Tag::HASHTAG_RE, '')
  30. new_text.gsub!(/\s+/, ' ')
  31. end
  32. end
  33. def default_locale
  34. account&.user_locale&.to_sym || nil
  35. end
  36. end