Skip to content

Instantly share code, notes, and snippets.

@toshia
Created March 6, 2018 00:04
Show Gist options
  • Save toshia/9b93e52c0645371625a581955ee6d9f8 to your computer and use it in GitHub Desktop.
Save toshia/9b93e52c0645371625a581955ee6d9f8 to your computer and use it in GitHub Desktop.
Entityの実験
def get_entity(decorated)
start_index, entity, end_index = Enumerator.new { |y|
Plugin.filtering(:entity_filter, decorated, y)
}.min_by(&:first)
return [decorated] if !start_index
target_text = decorated.description
[
Plugin::Entity::TextEntity.new(
parent: decorated,
description: target_text[0...start_index]),
entity,
*get_entity(
Plugin::Entity::TextEntity.new(
parent: decorated,
description: target_text[end_index..target_text.size])
).each{|e| e.parent = decorated }
]
end
get_entity(Class.new(Diva::Model){ field.string :description }.new(description: "I'm at 珈琲 庵 in 千代田区, 東京都 w/ @toshi_a @akkiesoft https://t.co/C71XdZjWXA")).map(&:description)
["text entity(I'm at 珈琲 庵 in 千代田区, 東京都 w/ )",
"hyperlink entity(@toshi_a, href=https://twitter.com/toshi_a)",
"text entity( )",
"hyperlink entity(@akkiesoft, href=https://twitter.com/akkiesoft)",
"text entity( )",
"hyperlink entity(https://t.co/C71XdZjWXA, href=https://t.co/C71XdZjWXA)",
"text entity()"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment