language_detector.rb 953 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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.to_sym
  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. URI.extract(new_text).each do |url|
  28. new_text.gsub!(url, '')
  29. end
  30. new_text.gsub!(Account::MENTION_RE, '')
  31. new_text.gsub!(Tag::HASHTAG_RE, '')
  32. new_text.gsub!(/\s+/, ' ')
  33. end
  34. end
  35. def default_locale
  36. account&.user_locale || I18n.default_locale
  37. end
  38. end