reality 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,106 @@
1
+ module Reality
2
+ module Wikidata
3
+ module Query
4
+ PREFIX = %Q{
5
+ PREFIX wikibase: <http://wikiba.se/ontology#>
6
+ PREFIX wd: <http://www.wikidata.org/entity/>
7
+ PREFIX wdt: <http://www.wikidata.org/prop/direct/>
8
+ PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
9
+ PREFIX p: <http://www.wikidata.org/prop/>
10
+ PREFIX v: <http://www.wikidata.org/prop/statement/>
11
+ PREFIX schema: <http://schema.org/>
12
+ }
13
+
14
+ # selecting
15
+ # ?entity wdt:* ?value
16
+ # ?entity rdfs:label ?label@en
17
+ # ?enwikiarticle schema:about ?entity
18
+ PREDICATES = %Q{
19
+ {
20
+ ?s ?p ?o .
21
+ FILTER(
22
+ STRSTARTS(STR(?p), "http://www.wikidata.org/prop/direct/") ||
23
+ (?p = rdfs:label && langMatches(lang(?o), "EN"))
24
+ )
25
+ } union {
26
+ bind(schema:about as ?p) .
27
+ ?o schema:about ?s .
28
+ filter(strstarts(str(?o), "https://en.wikipedia.org/wiki/"))
29
+ }
30
+ }
31
+ SERVICES = %Q{
32
+ SERVICE wikibase:label {
33
+ bd:serviceParam wikibase:language "en" .
34
+ }
35
+ }
36
+
37
+ QUERY = %Q{
38
+ #{PREFIX}
39
+
40
+ SELECT ?s ?p ?o ?oLabel WHERE {
41
+ %{selectors} .
42
+ #{PREDICATES} .
43
+ #{SERVICES}
44
+ }
45
+ }
46
+
47
+ SELECTORS = {
48
+ wikipedia: '<https://en.wikipedia.org/wiki/%{title}> schema:about ?s',
49
+ id: 'BIND(wd:%{id} as ?s)',
50
+ label: '?s rdfs:label "%{label}"@en'
51
+ }
52
+
53
+ UNSAFE = Regexp.union(URI::UNSAFE, /[,()':!]/)
54
+
55
+ using Refinements
56
+
57
+ module_function
58
+
59
+ def by_wikititle(*titles)
60
+ fetch(:en_wikipage, titles.
61
+ map{|t| SELECTORS[:wikipedia] % {title: URI.escape(t, UNSAFE)}}
62
+ )
63
+ end
64
+
65
+ def by_id(*ids)
66
+ fetch(:id, ids.map{|id| SELECTORS[:id] % {id: id}} )
67
+ end
68
+
69
+ def by_label(*labels)
70
+ fetch(:label, labels.map{|l| SELECTORS[:label] % {label: l}} )
71
+ end
72
+
73
+ private
74
+
75
+ module_function
76
+
77
+ MAX_SLICE = 20
78
+
79
+ def fetch(key, selectors)
80
+ selectors.each_slice(MAX_SLICE).map{|chunk|
81
+ fetch_sublist(chunk)
82
+ }.flatten(1).
83
+ group_by(&key).
84
+ # of several entities with same label, we always prefer first-by-id
85
+ map{|key, group| group.sort_by(&:id_i).first}.
86
+ map{|entity| [entity.send(key), entity]}.to_h
87
+ end
88
+
89
+ def fetch_sublist(selectors)
90
+ make_query(selectors).
91
+ derp{|query| faraday.get('', query: query, format: :json)}.
92
+ derp{|res| Wikidata::Entity.from_sparql(res.body)}
93
+ end
94
+
95
+ def faraday
96
+ @faraday ||= Faraday.new(url: 'https://query.wikidata.org/sparql'){|f|
97
+ f.adapter Faraday.default_adapter
98
+ }
99
+ end
100
+
101
+ def make_query(selectors)
102
+ QUERY % {selectors: selectors.map{|sel| "{#{sel}}"}.join(" UNION ")}
103
+ end
104
+ end
105
+ end
106
+ end
@@ -30,17 +30,15 @@ Gem::Specification.new do |s|
30
30
  s.bindir = 'bin'
31
31
  s.executables << 'reality'
32
32
 
33
- s.add_dependency 'infoboxer', '>= 0.2.3'
33
+ s.required_ruby_version = '>= 2.1.0'
34
+
35
+ s.add_dependency 'infoboxer', '>= 0.2.4'
34
36
  s.add_dependency 'hashie'
35
37
  s.add_dependency 'open-weather'
36
- s.add_dependency 'timezone'
38
+ s.add_dependency 'quandl'
39
+ s.add_dependency 'timezone', '>= 0.99.0'
37
40
  s.add_dependency 'geokit'
38
41
  s.add_dependency 'tzinfo'
39
42
  s.add_dependency 'ruby-sun-times'
40
43
  s.add_dependency 'time_boots'
41
-
42
- s.add_development_dependency 'rspec', '~> 3'
43
- s.add_development_dependency 'rspec-its', '~> 1'
44
- s.add_development_dependency 'vcr'
45
- s.add_development_dependency 'webmock'
46
44
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: reality
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Victor Shepelev and contributors
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-03-02 00:00:00.000000000 Z
11
+ date: 2016-04-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: infoboxer
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: 0.2.3
19
+ version: 0.2.4
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: 0.2.3
26
+ version: 0.2.4
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: hashie
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -53,7 +53,7 @@ dependencies:
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
55
  - !ruby/object:Gem::Dependency
56
- name: timezone
56
+ name: quandl
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
59
  - - ">="
@@ -67,21 +67,21 @@ dependencies:
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
69
  - !ruby/object:Gem::Dependency
70
- name: geokit
70
+ name: timezone
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
73
  - - ">="
74
74
  - !ruby/object:Gem::Version
75
- version: '0'
75
+ version: 0.99.0
76
76
  type: :runtime
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
80
  - - ">="
81
81
  - !ruby/object:Gem::Version
82
- version: '0'
82
+ version: 0.99.0
83
83
  - !ruby/object:Gem::Dependency
84
- name: tzinfo
84
+ name: geokit
85
85
  requirement: !ruby/object:Gem::Requirement
86
86
  requirements:
87
87
  - - ">="
@@ -95,7 +95,7 @@ dependencies:
95
95
  - !ruby/object:Gem::Version
96
96
  version: '0'
97
97
  - !ruby/object:Gem::Dependency
98
- name: ruby-sun-times
98
+ name: tzinfo
99
99
  requirement: !ruby/object:Gem::Requirement
100
100
  requirements:
101
101
  - - ">="
@@ -109,7 +109,7 @@ dependencies:
109
109
  - !ruby/object:Gem::Version
110
110
  version: '0'
111
111
  - !ruby/object:Gem::Dependency
112
- name: time_boots
112
+ name: ruby-sun-times
113
113
  requirement: !ruby/object:Gem::Requirement
114
114
  requirements:
115
115
  - - ">="
@@ -123,55 +123,13 @@ dependencies:
123
123
  - !ruby/object:Gem::Version
124
124
  version: '0'
125
125
  - !ruby/object:Gem::Dependency
126
- name: rspec
127
- requirement: !ruby/object:Gem::Requirement
128
- requirements:
129
- - - "~>"
130
- - !ruby/object:Gem::Version
131
- version: '3'
132
- type: :development
133
- prerelease: false
134
- version_requirements: !ruby/object:Gem::Requirement
135
- requirements:
136
- - - "~>"
137
- - !ruby/object:Gem::Version
138
- version: '3'
139
- - !ruby/object:Gem::Dependency
140
- name: rspec-its
141
- requirement: !ruby/object:Gem::Requirement
142
- requirements:
143
- - - "~>"
144
- - !ruby/object:Gem::Version
145
- version: '1'
146
- type: :development
147
- prerelease: false
148
- version_requirements: !ruby/object:Gem::Requirement
149
- requirements:
150
- - - "~>"
151
- - !ruby/object:Gem::Version
152
- version: '1'
153
- - !ruby/object:Gem::Dependency
154
- name: vcr
155
- requirement: !ruby/object:Gem::Requirement
156
- requirements:
157
- - - ">="
158
- - !ruby/object:Gem::Version
159
- version: '0'
160
- type: :development
161
- prerelease: false
162
- version_requirements: !ruby/object:Gem::Requirement
163
- requirements:
164
- - - ">="
165
- - !ruby/object:Gem::Version
166
- version: '0'
167
- - !ruby/object:Gem::Dependency
168
- name: webmock
126
+ name: time_boots
169
127
  requirement: !ruby/object:Gem::Requirement
170
128
  requirements:
171
129
  - - ">="
172
130
  - !ruby/object:Gem::Version
173
131
  version: '0'
174
- type: :development
132
+ type: :runtime
175
133
  prerelease: false
176
134
  version_requirements: !ruby/object:Gem::Requirement
177
135
  requirements:
@@ -189,6 +147,8 @@ extensions: []
189
147
  extra_rdoc_files: []
190
148
  files:
191
149
  - ".dokaz"
150
+ - ".yardopts"
151
+ - CHANGELOG.md
192
152
  - LICENSE.txt
193
153
  - README.md
194
154
  - bin/reality
@@ -202,6 +162,7 @@ files:
202
162
  - data/wikidata-predicates.json
203
163
  - data/wikidata-predicates.yaml
204
164
  - lib/reality.rb
165
+ - lib/reality/command_line.rb
205
166
  - lib/reality/config.rb
206
167
  - lib/reality/definitions/dictionaries.rb
207
168
  - lib/reality/definitions/helpers.rb
@@ -218,12 +179,14 @@ files:
218
179
  - lib/reality/entity/wikipedia_type.rb
219
180
  - lib/reality/extras/geonames.rb
220
181
  - lib/reality/extras/open_weather_map.rb
182
+ - lib/reality/extras/quandl.rb
221
183
  - lib/reality/geo.rb
222
184
  - lib/reality/infoboxer_templates.rb
223
185
  - lib/reality/list.rb
224
186
  - lib/reality/measure.rb
225
187
  - lib/reality/measure/unit.rb
226
188
  - lib/reality/methods.rb
189
+ - lib/reality/names.rb
227
190
  - lib/reality/pretty_inspect.rb
228
191
  - lib/reality/refinements.rb
229
192
  - lib/reality/shortcuts.rb
@@ -232,6 +195,7 @@ files:
232
195
  - lib/reality/util/parsers.rb
233
196
  - lib/reality/version.rb
234
197
  - lib/reality/wikidata.rb
198
+ - lib/reality/wikidata/query.rb
235
199
  - reality.gemspec
236
200
  - script/extract_all_infoboxes.rb
237
201
  - script/extract_countries.rb
@@ -253,7 +217,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
253
217
  requirements:
254
218
  - - ">="
255
219
  - !ruby/object:Gem::Version
256
- version: '0'
220
+ version: 2.1.0
257
221
  required_rubygems_version: !ruby/object:Gem::Requirement
258
222
  requirements:
259
223
  - - ">="