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