extractor.rb 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. # frozen_string_literal: true
  2. module Extractor
  3. MAX_DOMAIN_LENGTH = 253
  4. extend Twitter::TwitterText::Extractor
  5. module_function
  6. def extract_entities_with_indices(text, options = {}, &block)
  7. entities = extract_urls_with_indices(text, options) +
  8. extract_hashtags_with_indices(text, check_url_overlap: false) +
  9. extract_mentions_or_lists_with_indices(text) +
  10. extract_extra_uris_with_indices(text)
  11. return [] if entities.empty?
  12. entities = remove_overlapping_entities(entities)
  13. entities.each(&block) if block
  14. entities
  15. end
  16. def extract_mentions_or_lists_with_indices(text)
  17. return [] unless text && Twitter::TwitterText::Regex[:at_signs].match?(text)
  18. possible_entries = []
  19. text.scan(Account::MENTION_RE) do |screen_name, _|
  20. match_data = $LAST_MATCH_INFO
  21. after = ::Regexp.last_match.post_match
  22. unless Twitter::TwitterText::Regex[:end_mention_match].match?(after)
  23. _, domain = screen_name.split('@')
  24. next if domain.present? && domain.length > MAX_DOMAIN_LENGTH
  25. start_position = match_data.char_begin(1) - 1
  26. end_position = match_data.char_end(1)
  27. possible_entries << {
  28. screen_name: screen_name,
  29. indices: [start_position, end_position],
  30. }
  31. end
  32. end
  33. if block_given?
  34. possible_entries.each do |mention|
  35. yield mention[:screen_name], mention[:indices].first, mention[:indices].last
  36. end
  37. end
  38. possible_entries
  39. end
  40. def extract_hashtags_with_indices(text, _options = {})
  41. return [] unless text&.index('#')
  42. possible_entries = []
  43. text.scan(Tag::HASHTAG_RE) do |hash_text, _|
  44. match_data = $LAST_MATCH_INFO
  45. start_position = match_data.char_begin(1) - 1
  46. end_position = match_data.char_end(1)
  47. after = ::Regexp.last_match.post_match
  48. if after.start_with?('://')
  49. hash_text.match(/(.+)(https?\Z)/) do |matched|
  50. hash_text = matched[1]
  51. end_position -= matched[2].codepoint_length
  52. end
  53. end
  54. possible_entries << {
  55. hashtag: hash_text,
  56. indices: [start_position, end_position],
  57. }
  58. end
  59. if block_given?
  60. possible_entries.each do |tag|
  61. yield tag[:hashtag], tag[:indices].first, tag[:indices].last
  62. end
  63. end
  64. possible_entries
  65. end
  66. def extract_cashtags_with_indices(_text)
  67. []
  68. end
  69. def extract_extra_uris_with_indices(text)
  70. return [] unless text&.index(':')
  71. possible_entries = []
  72. text.scan(Twitter::TwitterText::Regex[:valid_extended_uri]) do
  73. valid_uri_match_data = $LAST_MATCH_INFO
  74. start_position = valid_uri_match_data.char_begin(3)
  75. end_position = valid_uri_match_data.char_end(3)
  76. possible_entries << {
  77. url: valid_uri_match_data[3],
  78. indices: [start_position, end_position],
  79. }
  80. end
  81. if block_given?
  82. possible_entries.each do |url|
  83. yield url[:url], url[:indices].first, url[:indices].last
  84. end
  85. end
  86. possible_entries
  87. end
  88. end