extractor.rb 3.0 KB

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