2017-05-05 13:48:22 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Extractor
|
2022-03-30 08:46:03 -04:00
|
|
|
MAX_DOMAIN_LENGTH = 253
|
|
|
|
|
2021-03-02 06:02:56 -05:00
|
|
|
extend Twitter::TwitterText::Extractor
|
2017-05-05 13:48:22 -04:00
|
|
|
|
|
|
|
module_function
|
|
|
|
|
2022-03-25 21:53:34 -04:00
|
|
|
def extract_entities_with_indices(text, options = {}, &block)
|
2023-02-18 17:09:40 -05:00
|
|
|
entities = extract_urls_with_indices(text, options) +
|
|
|
|
extract_hashtags_with_indices(text, check_url_overlap: false) +
|
|
|
|
extract_mentions_or_lists_with_indices(text) +
|
|
|
|
extract_extra_uris_with_indices(text)
|
2022-03-25 21:53:34 -04:00
|
|
|
|
|
|
|
return [] if entities.empty?
|
|
|
|
|
|
|
|
entities = remove_overlapping_entities(entities)
|
2023-02-08 04:36:23 -05:00
|
|
|
entities.each(&block) if block
|
2022-03-25 21:53:34 -04:00
|
|
|
entities
|
|
|
|
end
|
|
|
|
|
2017-11-16 20:06:26 -05:00
|
|
|
def extract_mentions_or_lists_with_indices(text)
|
2022-03-25 21:53:34 -04:00
|
|
|
return [] unless text && Twitter::TwitterText::Regex[:at_signs].match?(text)
|
2017-05-05 13:48:22 -04:00
|
|
|
|
|
|
|
possible_entries = []
|
|
|
|
|
2022-03-25 21:53:34 -04:00
|
|
|
text.scan(Account::MENTION_RE) do |screen_name, _|
|
2017-05-05 13:48:22 -04:00
|
|
|
match_data = $LAST_MATCH_INFO
|
2023-02-18 00:20:20 -05:00
|
|
|
after = ::Regexp.last_match.post_match
|
2022-03-25 21:53:34 -04:00
|
|
|
|
2021-03-02 06:02:56 -05:00
|
|
|
unless Twitter::TwitterText::Regex[:end_mention_match].match?(after)
|
2022-03-30 08:46:03 -04:00
|
|
|
_, domain = screen_name.split('@')
|
|
|
|
|
|
|
|
next if domain.present? && domain.length > MAX_DOMAIN_LENGTH
|
|
|
|
|
2017-05-05 13:48:22 -04:00
|
|
|
start_position = match_data.char_begin(1) - 1
|
2022-03-25 21:53:34 -04:00
|
|
|
end_position = match_data.char_end(1)
|
|
|
|
|
2017-05-05 13:48:22 -04:00
|
|
|
possible_entries << {
|
|
|
|
screen_name: screen_name,
|
|
|
|
indices: [start_position, end_position],
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if block_given?
|
|
|
|
possible_entries.each do |mention|
|
|
|
|
yield mention[:screen_name], mention[:indices].first, mention[:indices].last
|
|
|
|
end
|
|
|
|
end
|
2022-03-25 21:53:34 -04:00
|
|
|
|
2017-05-05 13:48:22 -04:00
|
|
|
possible_entries
|
|
|
|
end
|
2017-05-19 14:19:14 -04:00
|
|
|
|
2022-03-25 21:53:34 -04:00
|
|
|
def extract_hashtags_with_indices(text, _options = {})
|
|
|
|
return [] unless text&.index('#')
|
|
|
|
|
|
|
|
possible_entries = []
|
2017-05-19 14:19:14 -04:00
|
|
|
|
|
|
|
text.scan(Tag::HASHTAG_RE) do |hash_text, _|
|
2022-03-25 21:53:34 -04:00
|
|
|
match_data = $LAST_MATCH_INFO
|
2017-05-19 14:19:14 -04:00
|
|
|
start_position = match_data.char_begin(1) - 1
|
2022-03-25 21:53:34 -04:00
|
|
|
end_position = match_data.char_end(1)
|
2023-02-18 00:20:20 -05:00
|
|
|
after = ::Regexp.last_match.post_match
|
2022-03-25 21:53:34 -04:00
|
|
|
|
2023-05-23 04:16:50 -04:00
|
|
|
if after.start_with?('://')
|
2017-05-19 14:19:14 -04:00
|
|
|
hash_text.match(/(.+)(https?\Z)/) do |matched|
|
2022-03-25 21:53:34 -04:00
|
|
|
hash_text = matched[1]
|
2021-03-02 06:02:56 -05:00
|
|
|
end_position -= matched[2].codepoint_length
|
2017-05-19 14:19:14 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-03-25 21:53:34 -04:00
|
|
|
possible_entries << {
|
2017-05-19 14:19:14 -04:00
|
|
|
hashtag: hash_text,
|
|
|
|
indices: [start_position, end_position],
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2022-03-25 21:53:34 -04:00
|
|
|
if block_given?
|
|
|
|
possible_entries.each do |tag|
|
|
|
|
yield tag[:hashtag], tag[:indices].first, tag[:indices].last
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
possible_entries
|
2017-05-19 14:19:14 -04:00
|
|
|
end
|
2017-05-24 08:32:53 -04:00
|
|
|
|
|
|
|
def extract_cashtags_with_indices(_text)
|
2022-03-25 21:53:34 -04:00
|
|
|
[]
|
|
|
|
end
|
|
|
|
|
|
|
|
def extract_extra_uris_with_indices(text)
|
|
|
|
return [] unless text&.index(':')
|
|
|
|
|
|
|
|
possible_entries = []
|
|
|
|
|
|
|
|
text.scan(Twitter::TwitterText::Regex[:valid_extended_uri]) do
|
|
|
|
valid_uri_match_data = $LAST_MATCH_INFO
|
|
|
|
|
|
|
|
start_position = valid_uri_match_data.char_begin(3)
|
|
|
|
end_position = valid_uri_match_data.char_end(3)
|
|
|
|
|
|
|
|
possible_entries << {
|
|
|
|
url: valid_uri_match_data[3],
|
|
|
|
indices: [start_position, end_position],
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
if block_given?
|
|
|
|
possible_entries.each do |url|
|
|
|
|
yield url[:url], url[:indices].first, url[:indices].last
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
possible_entries
|
2017-05-24 08:32:53 -04:00
|
|
|
end
|
2017-05-05 13:48:22 -04:00
|
|
|
end
|