patento 0.2.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.
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,18 @@
1
+ source 'http://rubygems.org'
2
+
3
+ gem 'nokogiri'
4
+
5
+ group :development do
6
+ gem 'rspec'
7
+ gem 'rdoc'
8
+ gem 'bundler'
9
+ gem 'jeweler'
10
+ end
11
+
12
+ gem 'rspec', group: ['test', 'development']
13
+
14
+ group :test do
15
+ gem 'ruby_gntp'
16
+ gem 'guard-rspec'
17
+ gem 'spork'
18
+ end
@@ -0,0 +1,54 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ diff-lcs (1.1.3)
5
+ ffi (1.0.11)
6
+ git (1.2.5)
7
+ guard (1.2.3)
8
+ listen (>= 0.4.2)
9
+ thor (>= 0.14.6)
10
+ guard-rspec (1.2.0)
11
+ guard (>= 1.1)
12
+ jeweler (1.8.4)
13
+ bundler (~> 1.0)
14
+ git (>= 1.2.5)
15
+ rake
16
+ rdoc
17
+ json (1.7.3)
18
+ listen (0.4.7)
19
+ rb-fchange (~> 0.0.5)
20
+ rb-fsevent (~> 0.9.1)
21
+ rb-inotify (~> 0.8.8)
22
+ nokogiri (1.5.5)
23
+ rake (0.9.2.2)
24
+ rb-fchange (0.0.5)
25
+ ffi
26
+ rb-fsevent (0.9.1)
27
+ rb-inotify (0.8.8)
28
+ ffi (>= 0.5.0)
29
+ rdoc (3.12)
30
+ json (~> 1.4)
31
+ rspec (2.11.0)
32
+ rspec-core (~> 2.11.0)
33
+ rspec-expectations (~> 2.11.0)
34
+ rspec-mocks (~> 2.11.0)
35
+ rspec-core (2.11.0)
36
+ rspec-expectations (2.11.1)
37
+ diff-lcs (~> 1.1.3)
38
+ rspec-mocks (2.11.1)
39
+ ruby_gntp (0.3.4)
40
+ spork (0.9.2)
41
+ thor (0.15.4)
42
+
43
+ PLATFORMS
44
+ ruby
45
+
46
+ DEPENDENCIES
47
+ bundler
48
+ guard-rspec
49
+ jeweler
50
+ nokogiri
51
+ rdoc
52
+ rspec
53
+ ruby_gntp
54
+ spork
@@ -0,0 +1,9 @@
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
3
+
4
+ guard 'rspec', :cli => '--tag ~slow --format nested', :version => 2 do
5
+ watch(%r{^spec/.+_spec\.rb$})
6
+ watch(%r{^lib/patento/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
7
+ watch('spec/spec_helper.rb') { "spec" }
8
+ end
9
+
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2012 George Zalepa
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.
@@ -0,0 +1,78 @@
1
+ # Patento
2
+
3
+ Patento is a simple gem for scraping Google Patents and formatting the visible data into objects and hashes.
4
+
5
+ ## Usage
6
+
7
+ Patento has two models of operation: local and remote. In local mode, you pass in the .html file saved from Google patents. In remote mode, you simply supply a patent number and Patento will pull the HTML from Google.
8
+
9
+ Note that pulling remote data will result in one HTTP request per patent requested. Thus, if doing large amounts of downloading, please consider respecting Google's server load. For your convenience, Google's terms of service can be found here: [http://www.google.com/intl/en/policies/terms/](http://www.google.com/intl/en/policies/terms/).
10
+
11
+ ### Examples:
12
+
13
+ Patent.new(6000000) # Retreives patent details remotely
14
+
15
+ Patent.new(6000000, local_path: "US6000000.html") # Parses ./US6000000.html and returns the same data as above
16
+
17
+ ### Data Retreived
18
+
19
+ Patento retrieves the following data:
20
+
21
+ 1. Patent Number (`String`): `patent.number`
22
+ 2. Title (`String`): `patent.title`
23
+ 3. Abstract (`String`): `patent.abstract`
24
+ 4. Inventors (`Array` of `String` objects): `patent.inventors`
25
+ 5. Assignee (`String`): `patent.assignee`
26
+ 6. U.S. Classifications (`Array` of `String` objects): `patent.us_classifications`
27
+ 7. International Classification (`String`): `patent.intl_classifications`
28
+ 8. Filing Date (`Date`): `patent.filing_date`
29
+ 9. Issue Date (`Date`): `patent.issue_date`
30
+ * ***Note***: Patento does not retrieve the publication date of published applications due to this data not being available at Google
31
+ 10. Claims (`Array` of `Claim` objects (see below)): `patent.claims`
32
+ 11. Forward Citations (`Array` of `Hash` objects): `patent.forward_citations`
33
+ 12. Backward Citations (`Array` of `Hash` objects): `patent.backward_citations`
34
+
35
+ ## Citations
36
+ Forward and backward citations are just `Hash` objects with the following keys: `number`, `filing`, `issue`, `assignee`, and `title`.
37
+
38
+ ## Claim Objects
39
+ Patento stores claim data in separate `Claim` objects. Claim objects have a `number`, a `type` (independent or dependent), a `preamble`, and a `body`. All attributes are public.
40
+
41
+ All claims are guaranteed to have a `preamble`. Shorter claims (one liners) may *only* have a preamble.
42
+
43
+ The `body` element is a nested array of elements corresponding to the indenting of the original claim. For example, the claim:
44
+
45
+ Preamble
46
+ Element 1
47
+ Sub-element 1
48
+ Sub-element 2
49
+ Element 2
50
+
51
+ will have a `body` represented as a nested array like so:
52
+
53
+ [
54
+ [
55
+ "Element 1",
56
+
57
+ [
58
+ "Sub-element 1",
59
+ "Sub-element 2"
60
+ ]
61
+ ],
62
+
63
+ "Element 2"
64
+ ]
65
+
66
+ ## Contributing to patento
67
+
68
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
69
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
70
+ * Fork the project.
71
+ * Start a feature/bugfix branch.
72
+ * Commit and push until you are happy with your contribution.
73
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
74
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
75
+
76
+ ## Copyright
77
+
78
+ Copyright (c) 2012 George Zalepa. See LICENSE.txt for further details.
@@ -0,0 +1,49 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "patento"
18
+ gem.homepage = "http://github.com/aosik/patento"
19
+ gem.license = "MIT"
20
+ gem.summary = %Q{Patento is a simple gem for scraping Google Patents and formatting the visible data into objects and hashes.}
21
+ gem.description = %Q{Patento is a simple gem for scraping Google Patents and formatting the visible data into objects and hashes. See the README for more details}
22
+ gem.email = "george.zalepa@gmail.com"
23
+ gem.authors = ["George Zalepa"]
24
+ # dependencies defined in Gemfile
25
+ end
26
+ Jeweler::RubygemsDotOrgTasks.new
27
+
28
+ require 'rspec/core'
29
+ require 'rspec/core/rake_task'
30
+ RSpec::Core::RakeTask.new(:spec) do |spec|
31
+ spec.pattern = FileList['spec/**/*_spec.rb']
32
+ end
33
+
34
+ RSpec::Core::RakeTask.new(:rcov) do |spec|
35
+ spec.pattern = 'spec/**/*_spec.rb'
36
+ spec.rcov = true
37
+ end
38
+
39
+ task :default => :spec
40
+
41
+ require 'rdoc/task'
42
+ Rake::RDocTask.new do |rdoc|
43
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
44
+
45
+ rdoc.rdoc_dir = 'rdoc'
46
+ rdoc.title = "patento #{version}"
47
+ rdoc.rdoc_files.include('README*')
48
+ rdoc.rdoc_files.include('lib/**/*.rb')
49
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.2.0
@@ -0,0 +1,30 @@
1
+ require 'nokogiri'
2
+ require 'net/http'
3
+ require 'date'
4
+
5
+ require 'patento/document'
6
+ require 'patento/patent'
7
+ require 'patento/publication'
8
+ require 'patento/claim'
9
+
10
+ module Patento
11
+ # class Downloader
12
+ def self.download_html(number)
13
+ Net::HTTP.get_response(URI.parse("http://www.google.com/patents/US#{number}")).body
14
+ end
15
+ # end
16
+ end
17
+
18
+ # Some quick string regex helpers
19
+ class String
20
+ # identifies if the text contains "N. ..." where N is the claim number
21
+ def is_preamble?
22
+ self.match(/^\d*\.\s/) ? true : false
23
+ end
24
+
25
+ # identifiers if the text has " claim N " in it
26
+ # note this is not foolproof since some claims aren't worded this way (few, but some)
27
+ def is_independent?
28
+ self.match(/\sclaim\s\d*\s/) ? true : false
29
+ end
30
+ end
@@ -0,0 +1,93 @@
1
+ require 'pp'
2
+
3
+ module Patento
4
+
5
+ class Claim
6
+ attr_accessor :number, :type, :preamble, :body
7
+
8
+ # Class Methods
9
+
10
+ def self.from_hash(hash)
11
+
12
+ claim = Claim.new
13
+
14
+ claim.number = hash[:number]
15
+ claim.type = hash[:type]
16
+ claim.preamble = hash[:preamble]
17
+ claim.body = hash[:body]
18
+
19
+ return claim
20
+ end
21
+
22
+ def self.extract_all(number, options ={})
23
+ if options[:local_path]
24
+ html = Nokogiri::HTML(File.read(options[:local_path]))
25
+ else
26
+ html = Nokogiri::HTML(Patento.download_html(number))
27
+ end
28
+
29
+ return parse_claims(html)
30
+
31
+ end
32
+
33
+ def self.parse_claims(html)
34
+ hash = parse_claims_from_html(html)
35
+
36
+ formatted_claims = []
37
+
38
+ hash.each do |hash|
39
+ formatted_claims << Claim.from_hash(hash)
40
+ end
41
+ return formatted_claims
42
+ end
43
+
44
+ def self.parse_claims_from_html(html)
45
+ elements = html.at_css('#patent_claims_v').children
46
+
47
+ claims = []
48
+ index = -1
49
+
50
+
51
+ elements.each do |element|
52
+ next if element.children.empty?
53
+
54
+ if element.text.is_preamble?
55
+ index += 1
56
+ claims[index] = {
57
+ number: index + 1,
58
+ type: (element.text.match(/\sclaim\s\d*\s/) ? :dependent : :independent),
59
+ preamble: element.text.gsub(/^\d*\.\s/,''),
60
+ body: []
61
+ }
62
+ else
63
+ claims[index][:body] = parse_dl(element)
64
+ end
65
+ end
66
+
67
+ return claims
68
+
69
+ end
70
+
71
+ def self.parse_dl(element)
72
+ elements = []
73
+
74
+ index = 0
75
+
76
+ element.children.each do |e|
77
+ unless e.css('dl').empty? # nested elements
78
+ elements[index-1] = [elements[index-1]]
79
+ elements[index-1] << parse_dl(e.css('dl')) # god i hate recursion
80
+ else # plain ol elements
81
+ elements << e.text
82
+ index += 1
83
+ end
84
+
85
+ end
86
+ elements
87
+ end
88
+
89
+ end
90
+
91
+
92
+
93
+ end
@@ -0,0 +1,95 @@
1
+ module Patento
2
+ class Document
3
+
4
+ attr_accessor :number, :html
5
+
6
+ # Note: attributes are lazily evaluated
7
+ def initialize(number, options = {})
8
+ @number = number.to_s
9
+ if options[:local_path]
10
+ @html = Nokogiri::HTML(File.read(options[:local_path]))
11
+ else
12
+ @html = Nokogiri::HTML(Patento.download_html(number))
13
+ end
14
+ end
15
+
16
+
17
+ # Attributes
18
+ def title
19
+ @title ||= @html.css('.main-title').text.split(' - ')[1]
20
+ end
21
+
22
+ def abstract
23
+ @abstract ||= @html.css('.patent_abstract_text').text
24
+ end
25
+
26
+ def inventors
27
+ @inventors ||= parse_bibdata_links_for_url_match('q=ininventor:')
28
+ end
29
+
30
+ def assignee
31
+ @assignee ||= parse_bibdata_links_for_url_match('q=inassignee:')
32
+ end
33
+
34
+ def us_classifications
35
+ @us_classifications ||= parse_bibdata_links_for_url_match('q=http://www.uspto.gov/web/patents/classification/')
36
+ end
37
+
38
+ def filing_date
39
+ @filing_date ||= parse_date(:filing)
40
+ end
41
+
42
+ def claims
43
+ @claims ||= Claim.parse_claims(@html)
44
+ end
45
+
46
+ def forward_citations
47
+ @forward_citations ||= parse_citations(:forward)
48
+ end
49
+
50
+ def independent_claims
51
+ independent_claims = []
52
+ claims.each do |c|
53
+ if c[:type] == :independent
54
+ independent_claims << c
55
+ end
56
+ end
57
+ return independent_claims
58
+ end
59
+
60
+ # Helpers
61
+
62
+ def parse_citations(type)
63
+ container = (type == :backward ? '#patent_citations_v tr' : '#patent_referenced_by_v tr')
64
+ trs = @html.css(container)
65
+ trs.shift
66
+ citations = []
67
+ trs.each do |row|
68
+ citations << {
69
+ number: row.css('td')[0].css('a').text.gsub(/^US/, ''),
70
+ filing: Date.parse(row.css('td')[1].text),
71
+ issue: Date.parse(row.css('td')[2].text),
72
+ assignee: row.css('td')[3].text,
73
+ title: row.css('td')[4].text
74
+ }
75
+ end
76
+ return citations
77
+ end
78
+
79
+
80
+ def parse_date(type)
81
+ nodes = @html.css('#metadata_v .patent_bibdata p').children
82
+ index = (type == :filing ? 4 : 7)
83
+ Date.parse(nodes[index].text)
84
+ end
85
+
86
+ def parse_bibdata_links_for_url_match(pattern)
87
+ @matches = @html.css('#summarytable div.patent_bibdata a').to_a
88
+ @matches.collect! { |link| link.text if link.attr('href').match(pattern) }
89
+ @matches.compact!
90
+ @matches = @matches.first if @matches.count == 1
91
+ return @matches
92
+ end
93
+
94
+ end
95
+ end