Module: SemanticExtraction::Alchemy
- Includes:
- UtilityMethods
- Defined in:
- lib/semantic_extraction/extractors/alchemy.rb
Constant Summary collapse
- STARTER =
"http://access.alchemyapi.com/calls/"
Class Method Summary collapse
- .extract_text(text) ⇒ Object
- .find_entities(text) ⇒ Object
- .find_keywords(text) ⇒ Object
- .output_entities(raw) ⇒ Object
- .output_keywords(raw) ⇒ Object
- .output_text(raw) ⇒ Object
Methods included from UtilityMethods
Class Method Details
.extract_text(text) ⇒ Object
45 46 47 48 49 50 51 |
# File 'lib/semantic_extraction/extractors/alchemy.rb', line 45 def self.extract_text(text) prefix = is_url?(text) ? "url" : "html" endpoint = (prefix == "url" ? "URL" : "HTML") + "GetText" url = STARTER + prefix + "/" + endpoint raw = post(url, text, prefix) output_text(raw) end |
.find_entities(text) ⇒ Object
23 24 25 26 27 28 29 |
# File 'lib/semantic_extraction/extractors/alchemy.rb', line 23 def self.find_entities(text) prefix = is_url?(text) ? "url" : "text" endpoint = (prefix == "url" ? "URL" : "Text") + "GetRankedNamedEntities" url = STARTER + prefix + "/" + endpoint raw = post(url, text, prefix) output_entities(raw) end |
.find_keywords(text) ⇒ Object
6 7 8 9 10 11 12 |
# File 'lib/semantic_extraction/extractors/alchemy.rb', line 6 def self.find_keywords(text) prefix = is_url?(text) ? "url" : "text" endpoint = (prefix == "url" ? "URL" : "Text") + "GetKeywords" url = STARTER + prefix + "/" + endpoint raw = post(url, text, prefix) output_keywords(raw) end |
.output_entities(raw) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/semantic_extraction/extractors/alchemy.rb', line 31 def self.output_entities(raw) h = Nokogiri::XML(raw) entities = [] (h/"entities entity").each do |p| hashie = Hash.from_xml(p.to_s)["entity"] typer = hashie.delete("type") if typer hashie["entity_type"] = typer end entities << OpenStruct.new(hashie) end return entities end |
.output_keywords(raw) ⇒ Object
14 15 16 17 18 19 20 21 |
# File 'lib/semantic_extraction/extractors/alchemy.rb', line 14 def self.output_keywords(raw) h = Nokogiri::XML(raw) keywords = [] (h/"keywords keyword").each do |p| keywords << p.text end return keywords end |
.output_text(raw) ⇒ Object
53 54 55 56 |
# File 'lib/semantic_extraction/extractors/alchemy.rb', line 53 def self.output_text(raw) h = Nokogiri::XML(raw) return (h/"text").first.inner_html end |