alchemy_api 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.document +5 -0
- data/.gitignore +24 -0
- data/LICENSE +20 -0
- data/README.markdown +36 -0
- data/Rakefile +47 -0
- data/VERSION +1 -0
- data/alchemy_api.gemspec +92 -0
- data/lib/alchemy_api.rb +29 -0
- data/lib/alchemy_api/base.rb +37 -0
- data/lib/alchemy_api/categorization.rb +47 -0
- data/lib/alchemy_api/language_detection.rb +51 -0
- data/lib/alchemy_api/term_extraction.rb +56 -0
- data/lib/alchemy_api/text_extraction.rb +85 -0
- data/spec/alchemy_api/base_spec.rb +62 -0
- data/spec/alchemy_api/categorization_spec.rb +54 -0
- data/spec/alchemy_api/language_detection_spec.rb +72 -0
- data/spec/alchemy_api/term_extraction_spec.rb +70 -0
- data/spec/alchemy_api/text_extraction_spec.rb +94 -0
- data/spec/alchemy_api_spec.rb +10 -0
- data/spec/cache/categorization/get_categorization_from_html/ddc3cf50efe5bd5c2159abfb49121cfa2314ca88.cache +29 -0
- data/spec/cache/categorization/get_categorization_from_text/8b476a3b532afd2da646b145e9dde07570c27352.cache +29 -0
- data/spec/cache/categorization/get_categorization_from_url/7536a34e1d54a95d8ee07d2a98036362761e1621.cache +27 -0
- data/spec/cache/language_detection/get_language_from_html/0faf7be978647b611d9c59e1efa497dd76e542f5.cache +33 -0
- data/spec/cache/language_detection/get_language_from_text/1ad3f50c1fda37000e24c196f12212ea9d536cb4.cache +33 -0
- data/spec/cache/language_detection/get_language_from_url/d077a95e60be0876bb7650ad213f5f43e83454d4.cache +31 -0
- data/spec/cache/term_extraction/get_ranked_keywords_from_html/7718a0fbd03739e4213a4e66c32a79a10c3499c3.cache +50 -0
- data/spec/cache/term_extraction/get_ranked_keywords_from_text/6f49e68ee4a9150368e671e70b632dbdc40860bb.cache +51 -0
- data/spec/cache/term_extraction/get_ranked_keywords_from_url/b9c291523159563d2224d676ec43b7b79a902d21.cache +48 -0
- data/spec/cache/text_extraction/get_raw_text_from_html/9db19f848a798db1f9a8c6cce9074d03cf2637a8.cache +27 -0
- data/spec/cache/text_extraction/get_raw_text_from_html/e9c236b6e861b57d238c810bb3c307cada170cad.cache +17 -0
- data/spec/cache/text_extraction/get_raw_text_from_url/8f5dff27211163e41ea5e7c3c534acf7b87d2098.cache +25 -0
- data/spec/cache/text_extraction/get_text_from_html/e7e6dba4c8570a41dbcb05233793018fc5ae4e1e.cache +27 -0
- data/spec/cache/text_extraction/get_text_from_url/13facbfeae029d936c7dc18ecaff5d2764b94618.cache +25 -0
- data/spec/cache/text_extraction/get_title_from_html/2a526348db23f992fee293d34f94c087e77290c5.cache +27 -0
- data/spec/cache/text_extraction/get_title_from_url/e84c0c7c67668706ae0cf3eefcd88c0911cd2b65.cache +25 -0
- data/spec/fixtures/article.txt +9 -0
- data/spec/fixtures/bp_spill.html +929 -0
- data/spec/spec.opts +1 -0
- data/spec/spec_helper.rb +20 -0
- metadata +136 -0
data/.document
ADDED
data/.gitignore
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
## MAC OS
|
2
|
+
.DS_Store
|
3
|
+
|
4
|
+
## TEXTMATE
|
5
|
+
*.tmproj
|
6
|
+
tmtags
|
7
|
+
|
8
|
+
## EMACS
|
9
|
+
*~
|
10
|
+
\#*
|
11
|
+
.\#*
|
12
|
+
|
13
|
+
## VIM
|
14
|
+
*.swp
|
15
|
+
|
16
|
+
## PROJECT::GENERAL
|
17
|
+
coverage
|
18
|
+
rdoc
|
19
|
+
pkg
|
20
|
+
.yardoc
|
21
|
+
doc
|
22
|
+
|
23
|
+
## PROJECT::SPECIFIC
|
24
|
+
README.markdown.html
|
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 David Balatero
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.markdown
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
alchemy_api
|
2
|
+
===========
|
3
|
+
|
4
|
+
This is a library implementing the AlchemyAPI. It's built on top of [monster_mash](http://github.com/dbalatero/monster_mash), which means you can hit Alchemy with your choice of serial or parallel requests.
|
5
|
+
|
6
|
+
Supported
|
7
|
+
---------
|
8
|
+
* Text extraction from text/html/URL
|
9
|
+
* Language detection from text/html/URL
|
10
|
+
* Keyword extraction from text/html/URL
|
11
|
+
* Text extraction from text/html/URL
|
12
|
+
|
13
|
+
Not supported
|
14
|
+
-------------
|
15
|
+
Currently the following APIs are not supported yet:
|
16
|
+
|
17
|
+
* Entity extraction (in progress on branch entities)
|
18
|
+
* Content scraping
|
19
|
+
* Microformats
|
20
|
+
* RSS/ATOM feed detecion
|
21
|
+
|
22
|
+
Note on Patches/Pull Requests
|
23
|
+
-----------------------------
|
24
|
+
|
25
|
+
* Fork the project.
|
26
|
+
* Make your feature addition or bug fix.
|
27
|
+
* Add tests for it. This is important so I don't break it in a
|
28
|
+
future version unintentionally.
|
29
|
+
* Commit, do not mess with rakefile, version, or history.
|
30
|
+
(if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
|
31
|
+
* Send me a pull request. Bonus points for topic branches.
|
32
|
+
|
33
|
+
Copyright
|
34
|
+
---------
|
35
|
+
|
36
|
+
Copyright (c) 2010 David Balatero. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "alchemy_api"
|
8
|
+
gem.summary = %Q{Provides a client API library for AlchemyAPI's awesome NLP services. Allows you to make parallel or serial requests.}
|
9
|
+
gem.description = %Q{Provides a client API library for AlchemyAPI's awesome NLP services. Allows you to make parallel or serial requests.}
|
10
|
+
gem.email = "dbalatero@gmail.com"
|
11
|
+
gem.homepage = "http://github.com/dbalatero/alchemy_api"
|
12
|
+
gem.authors = ["David Balatero"]
|
13
|
+
gem.add_development_dependency "rspec", ">= 1.2.9"
|
14
|
+
gem.add_dependency "monster_mash", ">= 0.1.1"
|
15
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
16
|
+
end
|
17
|
+
Jeweler::GemcutterTasks.new
|
18
|
+
rescue LoadError
|
19
|
+
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
20
|
+
end
|
21
|
+
|
22
|
+
require 'spec/rake/spectask'
|
23
|
+
Spec::Rake::SpecTask.new(:spec) do |spec|
|
24
|
+
spec.libs << 'lib' << 'spec'
|
25
|
+
spec.spec_files = FileList['spec/**/*_spec.rb']
|
26
|
+
end
|
27
|
+
|
28
|
+
Spec::Rake::SpecTask.new(:rcov) do |spec|
|
29
|
+
spec.libs << 'lib' << 'spec'
|
30
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
31
|
+
spec.rcov = true
|
32
|
+
spec.rcov_opts = ['--exclude', 'spec']
|
33
|
+
end
|
34
|
+
|
35
|
+
task :spec => :check_dependencies
|
36
|
+
|
37
|
+
task :default => :spec
|
38
|
+
|
39
|
+
require 'rake/rdoctask'
|
40
|
+
Rake::RDocTask.new do |rdoc|
|
41
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
42
|
+
|
43
|
+
rdoc.rdoc_dir = 'rdoc'
|
44
|
+
rdoc.title = "alchemy_api #{version}"
|
45
|
+
rdoc.rdoc_files.include('README*')
|
46
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
47
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
data/alchemy_api.gemspec
ADDED
@@ -0,0 +1,92 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{alchemy_api}
|
8
|
+
s.version = "0.1.0"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["David Balatero"]
|
12
|
+
s.date = %q{2010-05-17}
|
13
|
+
s.description = %q{Provides a client API library for AlchemyAPI's awesome NLP services. Allows you to make parallel or serial requests.}
|
14
|
+
s.email = %q{dbalatero@gmail.com}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE",
|
17
|
+
"README.markdown",
|
18
|
+
"README.markdown.html"
|
19
|
+
]
|
20
|
+
s.files = [
|
21
|
+
".document",
|
22
|
+
".gitignore",
|
23
|
+
"LICENSE",
|
24
|
+
"README.markdown",
|
25
|
+
"Rakefile",
|
26
|
+
"VERSION",
|
27
|
+
"alchemy_api.gemspec",
|
28
|
+
"lib/alchemy_api.rb",
|
29
|
+
"lib/alchemy_api/base.rb",
|
30
|
+
"lib/alchemy_api/categorization.rb",
|
31
|
+
"lib/alchemy_api/language_detection.rb",
|
32
|
+
"lib/alchemy_api/term_extraction.rb",
|
33
|
+
"lib/alchemy_api/text_extraction.rb",
|
34
|
+
"spec/alchemy_api/base_spec.rb",
|
35
|
+
"spec/alchemy_api/categorization_spec.rb",
|
36
|
+
"spec/alchemy_api/language_detection_spec.rb",
|
37
|
+
"spec/alchemy_api/term_extraction_spec.rb",
|
38
|
+
"spec/alchemy_api/text_extraction_spec.rb",
|
39
|
+
"spec/alchemy_api_spec.rb",
|
40
|
+
"spec/cache/categorization/get_categorization_from_html/ddc3cf50efe5bd5c2159abfb49121cfa2314ca88.cache",
|
41
|
+
"spec/cache/categorization/get_categorization_from_text/8b476a3b532afd2da646b145e9dde07570c27352.cache",
|
42
|
+
"spec/cache/categorization/get_categorization_from_url/7536a34e1d54a95d8ee07d2a98036362761e1621.cache",
|
43
|
+
"spec/cache/language_detection/get_language_from_html/0faf7be978647b611d9c59e1efa497dd76e542f5.cache",
|
44
|
+
"spec/cache/language_detection/get_language_from_text/1ad3f50c1fda37000e24c196f12212ea9d536cb4.cache",
|
45
|
+
"spec/cache/language_detection/get_language_from_url/d077a95e60be0876bb7650ad213f5f43e83454d4.cache",
|
46
|
+
"spec/cache/term_extraction/get_ranked_keywords_from_html/7718a0fbd03739e4213a4e66c32a79a10c3499c3.cache",
|
47
|
+
"spec/cache/term_extraction/get_ranked_keywords_from_text/6f49e68ee4a9150368e671e70b632dbdc40860bb.cache",
|
48
|
+
"spec/cache/term_extraction/get_ranked_keywords_from_url/b9c291523159563d2224d676ec43b7b79a902d21.cache",
|
49
|
+
"spec/cache/text_extraction/get_raw_text_from_html/9db19f848a798db1f9a8c6cce9074d03cf2637a8.cache",
|
50
|
+
"spec/cache/text_extraction/get_raw_text_from_html/e9c236b6e861b57d238c810bb3c307cada170cad.cache",
|
51
|
+
"spec/cache/text_extraction/get_raw_text_from_url/8f5dff27211163e41ea5e7c3c534acf7b87d2098.cache",
|
52
|
+
"spec/cache/text_extraction/get_text_from_html/e7e6dba4c8570a41dbcb05233793018fc5ae4e1e.cache",
|
53
|
+
"spec/cache/text_extraction/get_text_from_url/13facbfeae029d936c7dc18ecaff5d2764b94618.cache",
|
54
|
+
"spec/cache/text_extraction/get_title_from_html/2a526348db23f992fee293d34f94c087e77290c5.cache",
|
55
|
+
"spec/cache/text_extraction/get_title_from_url/e84c0c7c67668706ae0cf3eefcd88c0911cd2b65.cache",
|
56
|
+
"spec/fixtures/article.txt",
|
57
|
+
"spec/fixtures/bp_spill.html",
|
58
|
+
"spec/spec.opts",
|
59
|
+
"spec/spec_helper.rb"
|
60
|
+
]
|
61
|
+
s.homepage = %q{http://github.com/dbalatero/alchemy_api}
|
62
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
63
|
+
s.require_paths = ["lib"]
|
64
|
+
s.rubygems_version = %q{1.3.6}
|
65
|
+
s.summary = %q{Provides a client API library for AlchemyAPI's awesome NLP services. Allows you to make parallel or serial requests.}
|
66
|
+
s.test_files = [
|
67
|
+
"spec/alchemy_api/base_spec.rb",
|
68
|
+
"spec/alchemy_api/categorization_spec.rb",
|
69
|
+
"spec/alchemy_api/language_detection_spec.rb",
|
70
|
+
"spec/alchemy_api/term_extraction_spec.rb",
|
71
|
+
"spec/alchemy_api/text_extraction_spec.rb",
|
72
|
+
"spec/alchemy_api_spec.rb",
|
73
|
+
"spec/spec_helper.rb"
|
74
|
+
]
|
75
|
+
|
76
|
+
if s.respond_to? :specification_version then
|
77
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
78
|
+
s.specification_version = 3
|
79
|
+
|
80
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
81
|
+
s.add_development_dependency(%q<rspec>, [">= 1.2.9"])
|
82
|
+
s.add_runtime_dependency(%q<monster_mash>, [">= 0.1.1"])
|
83
|
+
else
|
84
|
+
s.add_dependency(%q<rspec>, [">= 1.2.9"])
|
85
|
+
s.add_dependency(%q<monster_mash>, [">= 0.1.1"])
|
86
|
+
end
|
87
|
+
else
|
88
|
+
s.add_dependency(%q<rspec>, [">= 1.2.9"])
|
89
|
+
s.add_dependency(%q<monster_mash>, [">= 0.1.1"])
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
data/lib/alchemy_api.rb
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'ostruct'
|
2
|
+
require 'json'
|
3
|
+
require 'monster_mash'
|
4
|
+
|
5
|
+
Dir.glob(File.dirname(__FILE__) + "/**/*.rb").each do |f|
|
6
|
+
require f
|
7
|
+
end
|
8
|
+
|
9
|
+
module AlchemyApi
|
10
|
+
@api_key = nil
|
11
|
+
@base_uri = "http://access.alchemyapi.com/calls/url"
|
12
|
+
@base_html_uri = "http://access.alchemyapi.com/calls/html"
|
13
|
+
@base_text_uri = "http://access.alchemyapi.com/calls/text"
|
14
|
+
|
15
|
+
class << self
|
16
|
+
attr_accessor :api_key
|
17
|
+
attr_accessor :base_uri
|
18
|
+
attr_accessor :base_html_uri
|
19
|
+
attr_accessor :base_text_uri
|
20
|
+
end
|
21
|
+
|
22
|
+
class UnknownError < StandardError; end
|
23
|
+
class InvalidApiKeyError < StandardError; end
|
24
|
+
class CannotRetrieveUrlError < StandardError; end
|
25
|
+
class RedirectionLimitError < CannotRetrieveUrlError; end
|
26
|
+
class PageIsNotValidHtmlError < StandardError; end
|
27
|
+
class ContentExceedsMaxLimitError < StandardError; end
|
28
|
+
class InvalidHtmlError < StandardError; end
|
29
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
module AlchemyApi
|
2
|
+
class Base < MonsterMash::Base
|
3
|
+
defaults do
|
4
|
+
cache_timeout 999999
|
5
|
+
user_agent 'Ruby AlchemyApi'
|
6
|
+
params :apikey => AlchemyApi.api_key,
|
7
|
+
:outputMode => 'json'
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.get_json(response)
|
11
|
+
json = JSON.parse(response.body)
|
12
|
+
check_json_for_errors_and_raise!(json)
|
13
|
+
json
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.check_json_for_errors_and_raise!(json)
|
17
|
+
if json['status'] == 'ERROR'
|
18
|
+
case json['statusInfo']
|
19
|
+
when 'invalid-api-key'
|
20
|
+
raise InvalidApiKeyError, "The API key you sent (#{AlchemyApi.api_key.inspect}) is invalid! Please set AlchemyApi.api_key!"
|
21
|
+
when 'cannot-retrieve'
|
22
|
+
raise CannotRetrieveUrlError, "The URL (#{json['url']}) could not be retrieved."
|
23
|
+
when 'cannot-retrieve:http-redirect-limit'
|
24
|
+
raise RedirectionLimitError, "The URL (#{json['url']}) could not be retrieved, as it reached a redirect limit."
|
25
|
+
when 'page-is-not-html'
|
26
|
+
raise PageIsNotValidHtmlError, "The page at #{json['url']} is not valid HTML!"
|
27
|
+
when 'content-exceeds-size-limit'
|
28
|
+
raise ContentExceedsMaxLimitError, "The page at #{json['url']} is larger than 600KB!"
|
29
|
+
when 'invalid-html'
|
30
|
+
raise InvalidHtmlError, "The HTML sent was invalid!"
|
31
|
+
else
|
32
|
+
raise UnknownError, "Got an unknown error: #{json['statusInfo']}"
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
module AlchemyApi
|
2
|
+
Category = Struct.new(:url, :name, :score)
|
3
|
+
|
4
|
+
class Categorization < Base
|
5
|
+
post(:get_categorization_from_text) do |text, *args|
|
6
|
+
options = args.first || {}
|
7
|
+
uri "#{AlchemyApi.base_text_uri}/TextGetCategory"
|
8
|
+
params :text => text,
|
9
|
+
:url => options[:url] || ''
|
10
|
+
handler do |response|
|
11
|
+
AlchemyApi::Categorization.get_categorization_handler(response)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
post(:get_categorization_from_url) do |url, *args|
|
16
|
+
options = args.first || {}
|
17
|
+
uri "#{AlchemyApi.base_uri}/URLGetCategory"
|
18
|
+
params :url => url,
|
19
|
+
:cquery => options[:cquery] || '',
|
20
|
+
:sourceText => options[:source_text] || 'cleaned_or_raw',
|
21
|
+
:xpath => options[:xpath] || ''
|
22
|
+
handler do |response|
|
23
|
+
AlchemyApi::Categorization.get_categorization_handler(response)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
|
28
|
+
post(:get_categorization_from_html) do |html, *args|
|
29
|
+
options = args.first || {}
|
30
|
+
uri "#{AlchemyApi.base_html_uri}/HTMLGetCategory"
|
31
|
+
params :html => html,
|
32
|
+
:url => options[:url] || '',
|
33
|
+
:cquery => options[:cquery] || '',
|
34
|
+
:sourceText => options[:source_text] || 'cleaned_or_raw',
|
35
|
+
:xpath => options[:xpath] || ''
|
36
|
+
handler do |response|
|
37
|
+
AlchemyApi::Categorization.get_categorization_handler(response)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def self.get_categorization_handler(response)
|
42
|
+
json = get_json(response)
|
43
|
+
Category.new(json['url'], json['category'],
|
44
|
+
json['score'].to_f)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
module AlchemyApi
|
2
|
+
LanguageResult = Struct.new(:url, :language, :iso_639_1, :iso_639_2,
|
3
|
+
:iso_639_3, :ethnologue_url,
|
4
|
+
:native_speakers, :wikipedia_url)
|
5
|
+
|
6
|
+
class LanguageDetection < Base
|
7
|
+
post(:get_language_from_url) do |url, *args|
|
8
|
+
options = args.first || {}
|
9
|
+
uri "#{AlchemyApi.base_uri}/URLGetLanguage"
|
10
|
+
params :url => url,
|
11
|
+
:sourceText => options[:source_text] || 'cleaned_or_raw',
|
12
|
+
:cquery => options[:cquery],
|
13
|
+
:xpath => options[:xpath]
|
14
|
+
handler do |response|
|
15
|
+
AlchemyApi::LanguageDetection.get_language_handler(response)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
post(:get_language_from_html) do |html, *args|
|
20
|
+
options = args.first || {}
|
21
|
+
uri "#{AlchemyApi.base_html_uri}/HTMLGetLanguage"
|
22
|
+
params :html => html,
|
23
|
+
:url => options[:url],
|
24
|
+
:sourceText => options[:source_text] || 'cleaned_or_raw',
|
25
|
+
:cquery => options[:cquery],
|
26
|
+
:xpath => options[:xpath]
|
27
|
+
handler do |response|
|
28
|
+
AlchemyApi::LanguageDetection.get_language_handler(response)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
post(:get_language_from_text) do |text, *args|
|
33
|
+
options = args.first || {}
|
34
|
+
uri "#{AlchemyApi.base_text_uri}/TextGetLanguage"
|
35
|
+
params :text => text,
|
36
|
+
:url => options[:url]
|
37
|
+
handler do |response|
|
38
|
+
AlchemyApi::LanguageDetection.get_language_handler(response)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def self.get_language_handler(response)
|
43
|
+
json = get_json(response)
|
44
|
+
LanguageResult.new(json['url'], json['language'], json['iso-639-1'],
|
45
|
+
json['iso-639-2'], json['iso-639-3'],
|
46
|
+
json['ethnologue'],
|
47
|
+
json['native-speakers'],
|
48
|
+
json['wikipedia'])
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
module AlchemyApi
|
2
|
+
TermExtractionResult = Struct.new(:keywords, :language, :url, :source_text)
|
3
|
+
Keyword = Struct.new(:text, :relevance)
|
4
|
+
|
5
|
+
class TermExtraction < Base
|
6
|
+
post(:get_ranked_keywords_from_html) do |html, *args|
|
7
|
+
options = args.first || {}
|
8
|
+
uri "#{AlchemyApi.base_html_uri}/HTMLGetRankedKeywords"
|
9
|
+
params :html => html,
|
10
|
+
:url => options[:url],
|
11
|
+
:maxRetrieve => options[:max_retrieve] || 10,
|
12
|
+
:showSourceText => options[:show_source_text] ? 1 : 0,
|
13
|
+
:sourceText => options[:source_text] || 'cleaned_or_raw',
|
14
|
+
:cquery => options[:cquery],
|
15
|
+
:xpath => options[:xpath]
|
16
|
+
handler do |response|
|
17
|
+
AlchemyApi::TermExtraction.get_ranked_keywords_handler(response)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
post(:get_ranked_keywords_from_url) do |url, *args|
|
22
|
+
options = args.first || {}
|
23
|
+
uri "#{AlchemyApi.base_uri}/URLGetRankedKeywords"
|
24
|
+
params :url => url,
|
25
|
+
:maxRetrieve => options[:max_retrieve] || 10,
|
26
|
+
:showSourceText => options[:show_source_text] ? 1 : 0,
|
27
|
+
:sourceText => options[:source_text] || 'cleaned_or_raw',
|
28
|
+
:cquery => options[:cquery],
|
29
|
+
:xpath => options[:xpath]
|
30
|
+
handler do |response|
|
31
|
+
AlchemyApi::TermExtraction.get_ranked_keywords_handler(response)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
post(:get_ranked_keywords_from_text) do |text, *args|
|
36
|
+
options = args.first || {}
|
37
|
+
uri "#{AlchemyApi.base_text_uri}/TextGetRankedKeywords"
|
38
|
+
params :text => text,
|
39
|
+
:url => options[:url],
|
40
|
+
:maxRetrieve => options[:max_retrieve] || 10,
|
41
|
+
:showSourceText => options[:show_source_text] ? 1 : 0
|
42
|
+
handler do |response|
|
43
|
+
AlchemyApi::TermExtraction.get_ranked_keywords_handler(response)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def self.get_ranked_keywords_handler(response)
|
48
|
+
json = get_json(response)
|
49
|
+
keywords = json['keywords'].map do |kw|
|
50
|
+
Keyword.new(kw['text'], kw['relevance'].to_f)
|
51
|
+
end
|
52
|
+
TermExtractionResult.new(keywords, json['language'],
|
53
|
+
json['url'], json['text'])
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|