ul-wukong 4.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (261) hide show
  1. checksums.yaml +15 -0
  2. data/.gitignore +60 -0
  3. data/.gitmodules +6 -0
  4. data/.rspec +2 -0
  5. data/.travis.yml +19 -0
  6. data/.yardopts +6 -0
  7. data/CHANGELOG.md +7 -0
  8. data/Gemfile +17 -0
  9. data/Guardfile +12 -0
  10. data/LICENSE.md +95 -0
  11. data/NOTES-travis.md +31 -0
  12. data/README-old.md +422 -0
  13. data/README.md +1308 -0
  14. data/Rakefile +28 -0
  15. data/TODO.md +99 -0
  16. data/bin/cutc +30 -0
  17. data/bin/cuttab +5 -0
  18. data/bin/greptrue +6 -0
  19. data/bin/md5sort +20 -0
  20. data/bin/setcat +11 -0
  21. data/bin/tabchar +5 -0
  22. data/bin/uniq-ord +59 -0
  23. data/bin/uniqc +3 -0
  24. data/bin/wu +34 -0
  25. data/bin/wu-clean-encoding +31 -0
  26. data/bin/wu-date +13 -0
  27. data/bin/wu-datetime +13 -0
  28. data/bin/wu-hist +3 -0
  29. data/bin/wu-lign +186 -0
  30. data/bin/wu-local +4 -0
  31. data/bin/wu-plus +9 -0
  32. data/bin/wu-source +5 -0
  33. data/bin/wu-sum +31 -0
  34. data/diagrams/wu_local.dot +39 -0
  35. data/diagrams/wu_local.dot.png +0 -0
  36. data/examples/Gemfile +38 -0
  37. data/examples/README.md +9 -0
  38. data/examples/basic/string_reverser.rb +23 -0
  39. data/examples/basic/tiny_count.rb +8 -0
  40. data/examples/basic/word_count/accumulator.rb +26 -0
  41. data/examples/basic/word_count/tokenizer.rb +13 -0
  42. data/examples/basic/word_count/word_count.rb +6 -0
  43. data/examples/dataflow/scraper_macro_flow.rb +28 -0
  44. data/examples/deploy_pack/Gemfile +6 -0
  45. data/examples/deploy_pack/README.md +6 -0
  46. data/examples/deploy_pack/a/b/c/.gitkeep +0 -0
  47. data/examples/deploy_pack/app/processors/string_reverser.rb +5 -0
  48. data/examples/deploy_pack/config/environment.rb +1 -0
  49. data/examples/dsl/dataflow/fibonacci_series.rb +101 -0
  50. data/examples/dsl/dataflow/scraper_macro_flow.rb +28 -0
  51. data/examples/dsl/dataflow/simple.rb +12 -0
  52. data/examples/dsl/dataflow/telegram.rb +45 -0
  53. data/examples/dsl/workflow/cherry_pie.dot +97 -0
  54. data/examples/dsl/workflow/cherry_pie.md +104 -0
  55. data/examples/dsl/workflow/cherry_pie.png +0 -0
  56. data/examples/dsl/workflow/cherry_pie.rb +101 -0
  57. data/examples/empty/.gitkeep +0 -0
  58. data/examples/examples_helper.rb +9 -0
  59. data/examples/geo.rb +4 -0
  60. data/examples/geo/geo_grids.numbers +0 -0
  61. data/examples/geo/geolocated.rb +331 -0
  62. data/examples/geo/quadtile.rb +69 -0
  63. data/examples/geo/spec/geolocated_spec.rb +247 -0
  64. data/examples/geo/tile_fetcher.rb +77 -0
  65. data/examples/graph/implied_geolocation/README.md +63 -0
  66. data/examples/graph/minimum_spanning_tree/airfares_graphviz.rb +73 -0
  67. data/examples/improver/tweet_summary.rb +73 -0
  68. data/examples/loadable.rb +2 -0
  69. data/examples/munging/airline_flights/airline_flights.rake +83 -0
  70. data/examples/munging/airline_flights/airplane.rb +0 -0
  71. data/examples/munging/airline_flights/airport_id_unification.rb +129 -0
  72. data/examples/munging/airline_flights/airport_ok_chars.rb +4 -0
  73. data/examples/munging/airline_flights/indexable.rb +75 -0
  74. data/examples/munging/airline_flights/indexable_spec.rb +90 -0
  75. data/examples/munging/airline_flights/reconcile_airports.rb +142 -0
  76. data/examples/munging/airline_flights/tasks.rake +83 -0
  77. data/examples/munging/airline_flights/topcities.rb +167 -0
  78. data/examples/munging/geo/geo_json.rb +54 -0
  79. data/examples/munging/geo/geo_models.rb +69 -0
  80. data/examples/munging/geo/geonames_models.rb +107 -0
  81. data/examples/munging/geo/iso_codes.rb +172 -0
  82. data/examples/munging/geo/reconcile_countries.rb +124 -0
  83. data/examples/munging/geo/tasks.rake +71 -0
  84. data/examples/munging/wikipedia/articles/extract_articles-parsed.rb +79 -0
  85. data/examples/munging/wikipedia/articles/extract_articles-templated.rb +136 -0
  86. data/examples/munging/wikipedia/articles/textualize_articles.rb +54 -0
  87. data/examples/munging/wikipedia/articles/verify_structure.rb +43 -0
  88. data/examples/munging/wikipedia/articles/wp2txt-LICENSE.txt +22 -0
  89. data/examples/munging/wikipedia/articles/wp2txt_article.rb +259 -0
  90. data/examples/munging/wikipedia/articles/wp2txt_utils.rb +452 -0
  91. data/examples/munging/wikipedia/dbpedia/dbpedia_common.rb +5 -0
  92. data/examples/munging/wikipedia/dbpedia/dbpedia_extract_geocoordinates.rb +78 -0
  93. data/examples/munging/wikipedia/dbpedia/extract_links-cruft.rb +66 -0
  94. data/examples/munging/wikipedia/dbpedia/extract_links.rb +260 -0
  95. data/examples/munging/wikipedia/dbpedia/sameas_extractor.rb +20 -0
  96. data/examples/rake_helper.rb +97 -0
  97. data/examples/ruby_project/Gemfile +6 -0
  98. data/examples/ruby_project/README.md +6 -0
  99. data/examples/ruby_project/a/b/c/.gitkeep +0 -0
  100. data/examples/server_logs/geo_ip_mapping/munge_geolite.rb +82 -0
  101. data/examples/server_logs/logline.rb +95 -0
  102. data/examples/server_logs/models.rb +66 -0
  103. data/examples/server_logs/page_counts.pig +48 -0
  104. data/examples/server_logs/server_logs-01-parse-script.rb +13 -0
  105. data/examples/server_logs/server_logs-02-histograms-full.rb +33 -0
  106. data/examples/server_logs/server_logs-02-histograms-mapper.rb +14 -0
  107. data/examples/server_logs/server_logs-03-breadcrumbs-full.rb +71 -0
  108. data/examples/server_logs/server_logs-04-page_page_edges-full.rb +40 -0
  109. data/examples/serverlogs/geo_ip_mapping/munge_geolite.rb +82 -0
  110. data/examples/serverlogs/models/logline.rb +102 -0
  111. data/examples/serverlogs/parser/apache_parser_widget.rb +46 -0
  112. data/examples/serverlogs/visit_paths/common.rb +4 -0
  113. data/examples/serverlogs/visit_paths/page_counts.pig +48 -0
  114. data/examples/serverlogs/visit_paths/serverlogs-01-parse-script.rb +11 -0
  115. data/examples/serverlogs/visit_paths/serverlogs-02-histograms-full.rb +31 -0
  116. data/examples/serverlogs/visit_paths/serverlogs-02-histograms-mapper.rb +12 -0
  117. data/examples/serverlogs/visit_paths/serverlogs-03-breadcrumbs-full.rb +67 -0
  118. data/examples/serverlogs/visit_paths/serverlogs-04-page_page_edges-full.rb +38 -0
  119. data/examples/splitter.rb +94 -0
  120. data/examples/string_reverser.rb +7 -0
  121. data/examples/text/pig_latin/pig_latinizer.rb +35 -0
  122. data/examples/text/pig_latin/pig_latinizer_widget.rb +16 -0
  123. data/examples/text/regional_flavor/README.md +14 -0
  124. data/examples/text/regional_flavor/article_wordbags.pig +39 -0
  125. data/examples/text/regional_flavor/j01-article_wordbags.rb +4 -0
  126. data/examples/text/regional_flavor/simple_pig_script.pig +27 -0
  127. data/examples/twitter.rb +5 -0
  128. data/lib/hanuman.rb +36 -0
  129. data/lib/hanuman/graph.rb +97 -0
  130. data/lib/hanuman/graphvizzer.rb +206 -0
  131. data/lib/hanuman/graphvizzer/gv_models.rb +161 -0
  132. data/lib/hanuman/graphvizzer/gv_presenter.rb +97 -0
  133. data/lib/hanuman/link.rb +35 -0
  134. data/lib/hanuman/registry.rb +46 -0
  135. data/lib/hanuman/stage.rb +128 -0
  136. data/lib/hanuman/tree.rb +67 -0
  137. data/lib/wu/geo.rb +4 -0
  138. data/lib/wu/geo/geo_grids.numbers +0 -0
  139. data/lib/wu/geo/geolocated.rb +331 -0
  140. data/lib/wu/geo/quadtile.rb +69 -0
  141. data/lib/wu/graph/union_find.rb +62 -0
  142. data/lib/wu/model/reconcilable.rb +63 -0
  143. data/lib/wu/munging.rb +71 -0
  144. data/lib/wu/social/models/twitter.rb +31 -0
  145. data/lib/wu/wikipedia/models.rb +20 -0
  146. data/lib/wukong.rb +54 -0
  147. data/lib/wukong/dataflow.rb +43 -0
  148. data/lib/wukong/doc_helpers.rb +14 -0
  149. data/lib/wukong/doc_helpers/dataflow_handler.rb +29 -0
  150. data/lib/wukong/doc_helpers/field_handler.rb +91 -0
  151. data/lib/wukong/doc_helpers/processor_handler.rb +29 -0
  152. data/lib/wukong/driver.rb +214 -0
  153. data/lib/wukong/driver/event_machine_driver.rb +15 -0
  154. data/lib/wukong/driver/wiring.rb +68 -0
  155. data/lib/wukong/local.rb +42 -0
  156. data/lib/wukong/local/runner.rb +96 -0
  157. data/lib/wukong/local/stdio_driver.rb +104 -0
  158. data/lib/wukong/logger.rb +102 -0
  159. data/lib/wukong/model/faker.rb +136 -0
  160. data/lib/wukong/model/flatpack_parser/flat.rb +60 -0
  161. data/lib/wukong/model/flatpack_parser/flatpack.rb +4 -0
  162. data/lib/wukong/model/flatpack_parser/lang.rb +46 -0
  163. data/lib/wukong/model/flatpack_parser/parser.rb +55 -0
  164. data/lib/wukong/model/flatpack_parser/tokens.rb +130 -0
  165. data/lib/wukong/plugin.rb +48 -0
  166. data/lib/wukong/processor.rb +110 -0
  167. data/lib/wukong/rake_helper.rb +6 -0
  168. data/lib/wukong/runner.rb +169 -0
  169. data/lib/wukong/runner/boot_sequence.rb +123 -0
  170. data/lib/wukong/runner/code_loader.rb +52 -0
  171. data/lib/wukong/runner/command_runner.rb +44 -0
  172. data/lib/wukong/runner/deploy_pack_loader.rb +75 -0
  173. data/lib/wukong/runner/help_message.rb +42 -0
  174. data/lib/wukong/source.rb +33 -0
  175. data/lib/wukong/source/source_driver.rb +74 -0
  176. data/lib/wukong/source/source_runner.rb +38 -0
  177. data/lib/wukong/spec_helpers.rb +74 -0
  178. data/lib/wukong/spec_helpers/integration_tests.rb +150 -0
  179. data/lib/wukong/spec_helpers/integration_tests/integration_test_matchers.rb +207 -0
  180. data/lib/wukong/spec_helpers/integration_tests/integration_test_runner.rb +97 -0
  181. data/lib/wukong/spec_helpers/shared_examples.rb +22 -0
  182. data/lib/wukong/spec_helpers/unit_tests.rb +135 -0
  183. data/lib/wukong/spec_helpers/unit_tests/unit_test_driver.rb +132 -0
  184. data/lib/wukong/spec_helpers/unit_tests/unit_test_matchers.rb +169 -0
  185. data/lib/wukong/spec_helpers/unit_tests/unit_test_runner.rb +60 -0
  186. data/lib/wukong/version.rb +3 -0
  187. data/lib/wukong/widget/echo.rb +55 -0
  188. data/lib/wukong/widget/extract.rb +122 -0
  189. data/lib/wukong/widget/filters.rb +452 -0
  190. data/lib/wukong/widget/logger.rb +56 -0
  191. data/lib/wukong/widget/operators.rb +82 -0
  192. data/lib/wukong/widget/reducers.rb +10 -0
  193. data/lib/wukong/widget/reducers/accumulator.rb +73 -0
  194. data/lib/wukong/widget/reducers/bin.rb +368 -0
  195. data/lib/wukong/widget/reducers/count.rb +73 -0
  196. data/lib/wukong/widget/reducers/group.rb +128 -0
  197. data/lib/wukong/widget/reducers/group_concat.rb +98 -0
  198. data/lib/wukong/widget/reducers/improver.rb +71 -0
  199. data/lib/wukong/widget/reducers/join_xml.rb +37 -0
  200. data/lib/wukong/widget/reducers/moments.rb +72 -0
  201. data/lib/wukong/widget/reducers/sort.rb +180 -0
  202. data/lib/wukong/widget/reducers/uniq.rb +91 -0
  203. data/lib/wukong/widget/serializers.rb +317 -0
  204. data/lib/wukong/widget/utils.rb +46 -0
  205. data/lib/wukong/widgets.rb +7 -0
  206. data/spec/examples/dataflow/fibonacci_series_spec.rb +18 -0
  207. data/spec/examples/dataflow/parse_apache_logs_spec.rb +8 -0
  208. data/spec/examples/dataflow/parsing_spec.rb +14 -0
  209. data/spec/examples/dataflow/simple_spec.rb +34 -0
  210. data/spec/examples/dataflow/telegram_spec.rb +43 -0
  211. data/spec/examples/graph/minimum_spanning_tree_spec.rb +34 -0
  212. data/spec/examples/munging/airline_flights/identifiers_spec.rb +16 -0
  213. data/spec/examples/munging/airline_flights_spec.rb +202 -0
  214. data/spec/examples/text/pig_latin_spec.rb +18 -0
  215. data/spec/examples/workflow/cherry_pie_spec.rb +36 -0
  216. data/spec/hanuman/graph_spec.rb +119 -0
  217. data/spec/hanuman/hanuman_spec.rb +10 -0
  218. data/spec/hanuman/registry_spec.rb +123 -0
  219. data/spec/hanuman/stage_spec.rb +81 -0
  220. data/spec/hanuman/tree_spec.rb +119 -0
  221. data/spec/spec.opts +1 -0
  222. data/spec/spec_helper.rb +43 -0
  223. data/spec/support/example_test_helpers.rb +95 -0
  224. data/spec/support/hanuman_test_helpers.rb +92 -0
  225. data/spec/support/integration_helper.rb +38 -0
  226. data/spec/support/model_test_helpers.rb +115 -0
  227. data/spec/support/shared_context_for_graphs.rb +57 -0
  228. data/spec/support/shared_context_for_reducers.rb +37 -0
  229. data/spec/support/shared_examples_for_builders.rb +94 -0
  230. data/spec/support/shared_examples_for_shortcuts.rb +57 -0
  231. data/spec/wu/model/reconcilable_spec.rb +152 -0
  232. data/spec/wukong/dataflow_spec.rb +87 -0
  233. data/spec/wukong/driver_spec.rb +154 -0
  234. data/spec/wukong/local/runner_spec.rb +29 -0
  235. data/spec/wukong/local/stdio_driver_spec.rb +73 -0
  236. data/spec/wukong/local_spec.rb +6 -0
  237. data/spec/wukong/logger_spec.rb +49 -0
  238. data/spec/wukong/model/faker_spec.rb +132 -0
  239. data/spec/wukong/processor_spec.rb +21 -0
  240. data/spec/wukong/runner_spec.rb +132 -0
  241. data/spec/wukong/source_spec.rb +6 -0
  242. data/spec/wukong/widget/extract_spec.rb +101 -0
  243. data/spec/wukong/widget/filters_spec.rb +79 -0
  244. data/spec/wukong/widget/logger_spec.rb +23 -0
  245. data/spec/wukong/widget/operators_spec.rb +25 -0
  246. data/spec/wukong/widget/reducers/bin_spec.rb +92 -0
  247. data/spec/wukong/widget/reducers/count_spec.rb +11 -0
  248. data/spec/wukong/widget/reducers/group_spec.rb +21 -0
  249. data/spec/wukong/widget/reducers/join_xml_spec.rb +25 -0
  250. data/spec/wukong/widget/reducers/moments_spec.rb +36 -0
  251. data/spec/wukong/widget/reducers/sort_spec.rb +26 -0
  252. data/spec/wukong/widget/reducers/uniq_spec.rb +14 -0
  253. data/spec/wukong/widget/serializers_spec.rb +114 -0
  254. data/spec/wukong/widget/sink_spec.rb +19 -0
  255. data/spec/wukong/widget/source_spec.rb +65 -0
  256. data/spec/wukong/wu-local_spec.rb +109 -0
  257. data/spec/wukong/wu-source_spec.rb +32 -0
  258. data/spec/wukong/wu_spec.rb +14 -0
  259. data/spec/wukong/wukong_spec.rb +10 -0
  260. data/wukong.gemspec +35 -0
  261. metadata +465 -0
@@ -0,0 +1,54 @@
1
+ # -*- coding: utf-8 -*-
2
+ module Wukong
3
+ module Data
4
+ class GeoJson ; include Gorillib::Model ; end
5
+ class GeoJson::Geometry ; include Gorillib::Model ; end
6
+
7
+ class GeoJson
8
+ include Gorillib::Model::LoadFromJson
9
+ include Gorillib::Model::Indexable
10
+ field :type, String
11
+ field :id, String
12
+ field :geometry, GeoJson::Geometry
13
+ field :properties, GenericModel
14
+
15
+ def self.load(*args)
16
+ load_json(*args) do |val|
17
+ p val.properties
18
+ p val.properties.to_place
19
+ end
20
+ end
21
+
22
+ end
23
+
24
+ class GeoJson::Geometry
25
+ field :type, String
26
+ field :coordinates, Array
27
+
28
+ def point?
29
+ type == 'Point'
30
+ end
31
+
32
+ def longitude
33
+ return nil if coordinates.blank?
34
+ raise "Longitude only available for Point objects" unless point?
35
+ coordinates[0]
36
+ end
37
+ def latitude
38
+ return nil if coordinates.blank?
39
+ raise "Latitude only available for Point objects" unless point?
40
+ coordinates[1]
41
+ end
42
+ end
43
+
44
+ class GeonamesGeoJson < GeoJson
45
+ def receive_properties(hsh)
46
+ if hsh.respond_to?(:merge)
47
+ super(hsh.merge(geo_json_id: id, longitude: geometry.longitude, latitude: geometry.latitude))
48
+ else
49
+ super
50
+ end
51
+ end
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,69 @@
1
+ module Geo
2
+
3
+ class Place
4
+ include Gorillib::Model
5
+ include Gorillib::Model::Indexable
6
+
7
+ field :geonames_id, String
8
+ field :country_id, String, doc: "ISO 3166 2-letter alphanumeric id ('us', 'mx', etc). Must be lowercase"
9
+ field :admin1_id, String
10
+ field :feature_cat, String
11
+ field :feature_subcat, String
12
+ #
13
+ field :name, String
14
+ #
15
+ field :timezone, String
16
+ field :elevation, Float
17
+ field :longitude, Float
18
+ field :latitude, Float
19
+ #
20
+ field :alternate_names, String, default: ""
21
+
22
+ def names
23
+ ([name] + alternate_names.split("|")).compact_blank
24
+ end
25
+
26
+ def coordinates
27
+ { longitude: longitude, latitude: latitude, elevation: elevation }.compact
28
+ end
29
+
30
+ def self.slugify_name(val)
31
+ val.downcase.
32
+ gsub(/(?:\s+and\s+|\s+-\s+|[^[:alpha:]\-]+)/, '-').
33
+ gsub(/\A-*(.+?)-*\z/, '\1')
34
+ end
35
+ end
36
+
37
+ class AdministrativeArea < Place
38
+ field :population, Integer
39
+ field :official_name, String
40
+ def names ; super.tap{|arr| arr.insert(1, official_name) }.uniq.compact_blank ; end
41
+ end
42
+
43
+ class Country < AdministrativeArea
44
+ field :country_al3id, String, identifier: true, doc: "ISO 3166 3-letter alphanumeric id ('usa', 'mex', etc). Must be lowercase."
45
+ field :country_numid, Integer, identifier: true, doc: "ISO 3166 numeric identifier ('usa' = 840)"
46
+ field :tld_id, String, doc: "TLD (top-level domain) identifier"
47
+ end
48
+
49
+ class CountryNameLookup
50
+ include Gorillib::Model
51
+ include Gorillib::Model::Indexable
52
+ include Gorillib::Model::LoadFromTsv
53
+ index_on :slug
54
+
55
+ field :country_id, String
56
+ field :country_al3id, String
57
+ field :country_numid, Integer
58
+ field :tld_id, String
59
+ field :geonames_id, String
60
+ field :name, String
61
+ field :slug, String
62
+ field :alt_name, String
63
+
64
+ def self.load(filename=nil)
65
+ filename ||= :country_name_lookup
66
+ @values = load_tsv(filename)
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,107 @@
1
+ # -*- coding: utf-8 -*-
2
+ # {"type":"Feature",
3
+ # "id":"3cc54602f2d69c1111dc35f0aaa92240",
4
+ # "geometry":{"type":"Point","coordinates":[42.5,11.5]},
5
+ # "properties":{
6
+ # "geonameid":"223816","country_code":"DJ","admin1_code":"00",
7
+ # "feature_code":"PCLI","feature_class":"A",
8
+ # "asciiname":"Republic of Djibouti","name":"Republic of Djibouti","alternatenames":"Cîbûtî,...",
9
+ # "modification_date":"2011-07-09",
10
+ # "timezone":"Africa/Djibouti","gtopo30":"668","population":"740528"}}
11
+
12
+
13
+ # {"type":"Feature","id":"5b66ac7270763facfe1e9ab9c1bf99f8",
14
+ # "geometry":{"type":"Point","coordinates":[-98.5,39.76]},
15
+ # "properties":{
16
+ # "modification_date":"2011-04-27","_type":"geo/geonames_country",
17
+ # "asciiname":"United States","name":"United States","gtopo30":"537","geonameid":"6252001",
18
+ # "feature_code":"PCLI","country_code":"US","feature_class":"A",
19
+ # "alternatenames":"...","admin1_code":"00","population":"310232863"}}
20
+
21
+ module Geo
22
+
23
+ class GeonamesPlace
24
+ include Gorillib::Model
25
+ class_attribute :place_klass ; self.place_klass = ::Geo::Place
26
+
27
+ field :name, String
28
+ field :asciiname, String
29
+ field :geonameid, String
30
+ field :country_code, String
31
+ field :admin1_code, String, blankish: [0, "0", "00", nil, ""]
32
+ field :feature_code, String
33
+ field :feature_class, String
34
+ #
35
+ field :modification_date, String
36
+ field :timezone, String
37
+ #
38
+ field :gtopo30, Float, blankish: ["-9999", -9999, nil, ""], doc: "Elevation in the [GTOPO30](http://en.wikipedia.org/wiki/GTOPO30) model"
39
+ field :longitude, Float
40
+ field :latitude, Float
41
+ #
42
+ field :population, Integer, blankish: [0, "0", nil, ""]
43
+ field :alternatenames, String
44
+
45
+ # because 'Saint Helena, Ascension and Tristan da Cunha' is an official
46
+ # country name (and others like it
47
+ def alternate_names_with_pipes
48
+ # comma ',' with no spaces separates names; comma space ', ' is internal.
49
+ an = alternatenames.gsub(/,/, '|').gsub(/\| /, ', ')
50
+ ([name, asciiname] + an.split('|')).uniq.join("|")
51
+ end
52
+
53
+ def to_place
54
+ attrs = {
55
+ name: asciiname,
56
+ official_name: name,
57
+ geonames_id: "gn:#{geonameid}",
58
+ country_id: country_code.downcase,
59
+ admin1_id: admin1_code,
60
+ feature_cat: feature_class,
61
+ feature_subcat: feature_code,
62
+ alternate_names: alternate_names_with_pipes,
63
+ updated_at: modification_date,
64
+ timezone: timezone,
65
+ elevation: gtopo30,
66
+ longitude: longitude,
67
+ latitude: latitude,
68
+ population: population,
69
+ }
70
+ place_klass.receive(attrs)
71
+ end
72
+ end
73
+
74
+ # Stub class: Geonames JSON elements have :_type = geo/geonames_country
75
+ class GeonamesCountry < GeonamesPlace
76
+ self.place_klass = Geo::Country
77
+ end
78
+
79
+ # http://download.geonames.org/export/zip/
80
+ #
81
+ # country code : iso country code, 2 characters
82
+ # postal code : varchar(20)
83
+ # place name : varchar(180)
84
+ # admin name1 : 1. order subdivision (state) varchar(100)
85
+ # admin code1 : 1. order subdivision (state) varchar(20)
86
+ # admin name2 : 2. order subdivision (county/province) varchar(100)
87
+ # admin code2 : 2. order subdivision (county/province) varchar(20)
88
+ # admin name3 : 3. order subdivision (community) varchar(100)
89
+ # admin code3 : 3. order subdivision (community) varchar(20)
90
+ # latitude : estimated latitude (wgs84)
91
+ # longitude : estimated longitude (wgs84)
92
+ # accuracy : accuracy of lat/lng from 1=estimated to 6=centroid
93
+ class GeonamesPostal
94
+ field :country_id, String, doc: "iso country code, 2 characters"
95
+ field :postal_id, String, doc: "varchar(20)"
96
+ field :name, String, doc: "varchar(180)"
97
+ field :admin1_name, String, doc: "1. order subdivision (state) varchar(100)"
98
+ field :admin1_id, String, doc: "1. order subdivision (state) varchar(20)"
99
+ field :admin2_name, String, doc: "2. order subdivision (county/province) varchar(100)"
100
+ field :admin2_id, String, doc: "2. order subdivision (county/province) varchar(20)"
101
+ field :admin3_name, String, doc: "3. order subdivision (community) varchar(100)"
102
+ field :admin3_id, String, doc: "3. order subdivision (community) varchar(20)"
103
+ field :latitude, String, doc: "estimated latitude (wgs84)"
104
+ field :longitude, String, doc: "estimated longitude (wgs84)"
105
+ field :accuracy, String, doc: "accuracy of lat/lng from 1=estimated to 6=centroid"
106
+ end
107
+ end
@@ -0,0 +1,172 @@
1
+ require 'active_support/lazy_load_hooks'
2
+ require 'active_support/i18n'
3
+ require 'active_support/inflector/transliterate'
4
+
5
+ module Wukong
6
+
7
+ module Data
8
+
9
+ # These classes use data from the
10
+ # [isocodes](http://pkg-isocodes.alioth.debian.org/) debian project. That
11
+ # package provides lists of various ISO standards (e.g. country, language,
12
+ # language scripts, and currency names) in one place, rather than repeated in
13
+ # many programs throughout the system.
14
+ #
15
+ class IsoCode
16
+ include Gorillib::Model
17
+ include Gorillib::Model::LoadFromTsv
18
+ include Gorillib::Model::Indexable
19
+
20
+ class_attribute :handle, instance_writer: false
21
+ def self.load(filename=nil)
22
+ filename ||= [:geo_data, 'iso_codes', "iso_3166.tsv"]
23
+ @values = load_tsv(filename, num_fields: 4..6)
24
+ end
25
+ end
26
+
27
+ #
28
+ # ISO 3166 Country code
29
+ #
30
+ # Lists the 2-letter country code and "short" country name. The official ISO
31
+ # 3166 maintenance agency is ISO. The gettext domain is
32
+ # "iso_3166". [origin](http://www.iso.org/iso/country_codes)
33
+ #
34
+ class CountryCode < IsoCode
35
+ include ActiveSupport::Inflector
36
+
37
+ self.handle = :iso_3166
38
+ index_on :alpha_2_code, :alpha_3_code, :country_numid, :name, :common_name, :official_name
39
+ field :alpha_2_code, String, identifier: true
40
+ field :alpha_3_code, String, identifier: true
41
+ field :country_numid, Integer, identifier: true
42
+ field :name, String
43
+ field :official_name, String, blankish: ["", nil]
44
+ field :common_name, String, blankish: ["", nil]
45
+
46
+ def names
47
+ [common_name, name, official_name].compact_blank
48
+ end
49
+ def self.for_any_name(val)
50
+ for_name(val){ for_common_name(val){ for_official_name(val) } }
51
+ end
52
+
53
+ def to_place
54
+ attrs = {
55
+ name: transliterate(names.first),
56
+ official_name: names.last,
57
+ country_id: alpha_2_code.downcase,
58
+ alternate_names: names.join('|'),
59
+ country_al3id: alpha_3_code.downcase,
60
+ country_numid: country_numid,
61
+ }
62
+ Geo::Country.receive(attrs.compact_blank)
63
+ end
64
+ end
65
+
66
+ class CountryCode < IsoCode
67
+ self.handle = :iso_3166_3
68
+ field :alpha_3_code, String, identifier: true
69
+ field :alpha_4_code, String, identifier: true
70
+ field :country_numid, Integer, identifier: true
71
+ field :country_names, String
72
+ field :comment, String
73
+ field :date_withdrawn, String
74
+ end
75
+
76
+ #
77
+ # ISO 3166-2 Country Subdivision (Admin 1: state, region, etc) Code
78
+ #
79
+ # The ISO 3166 standard includes a "Country Subdivision Code", giving a code
80
+ # for the names of the principal administrative subdivisions of the
81
+ # countries coded in ISO 3166. The official ISO 3166-2 maintenance agency is
82
+ # ISO. The gettext domain is "iso_3166_2".
83
+ # <http://www.iso.org/iso/country_codes/background_on_iso_3166/iso_3166-2.htm>
84
+ #
85
+ class RegionCode < IsoCode
86
+ self.handle = :iso_3166_2
87
+ field :region_code, String, identifier: true
88
+ field :country_code, String
89
+ field :parent_region, String
90
+ field :region_kind, String
91
+ field :name, String
92
+ alias_method :state_code, :region_code
93
+ end
94
+
95
+ #
96
+ # ISO 639 Language Code
97
+ #
98
+ # This lists the 2-letter and 3-letter language codes and language
99
+ # names. The official ISO 639 maintenance agency is the Library of
100
+ # Congress. The gettext domain is "iso_639".
101
+ # [origin](http://www.loc.gov/standards/iso639-2/)
102
+ #
103
+ class BasicLanguageCode < IsoCode
104
+ self.handle = :iso_639
105
+ field :iso_639_1_code, String, identifier: true
106
+ field :iso_639_2B_code, String, identifier: true
107
+ field :iso_639_2T_code, String, identifier: true
108
+ field :name, String, identifier: true
109
+ end
110
+
111
+ # ISO 639-3
112
+ #
113
+ # This is a further development of ISO 639-2, see above. All codes of ISO
114
+ # 639-2 are included in ISO 639-3. ISO 639-3 attempts to provide as complete
115
+ # an enumeration of languages as possible, including living, extinct,
116
+ # ancient, and constructed languages, whether major or minor, written or
117
+ # unwritten. The gettext domain is "iso_639_3". The official ISO 639-3
118
+ # maintenance agency is SIL International.
119
+ # [origin](http://www.sil.org/iso639-3/)
120
+ #
121
+ class LanguageCode < BasicLanguageCode
122
+ self.handle = :iso_639_3
123
+ field :language_id, String, identifier: true
124
+ field :part1_code, String
125
+ field :part2_code, String
126
+ field :scope, String
127
+ field :status, String
128
+ field :language_kind, String
129
+ field :name, String
130
+ field :inverted_name, String
131
+ field :reference_name, String
132
+ end
133
+
134
+ #
135
+ # ISO 15924 Language Scripts (alphabet) names
136
+ #
137
+ # This lists the language scripts names. The official ISO 15924 maintenance
138
+ # agency is the Unicode Consortium. The gettext domain is "iso_15924".
139
+ # [origin](http://unicode.org/iso15924/)
140
+ #
141
+ class LanguageScriptCode < IsoCode
142
+ self.handle = :iso_15924
143
+ field :alpha_4_code, String, identifier: true
144
+ field :script_numid, Integer, identifier: true
145
+ field :name, String
146
+ end
147
+
148
+ #
149
+ # ISO 4217 Currency Code
150
+ #
151
+ # This lists the currency codes and names. The official ISO 4217 maintenance
152
+ # agency is the British Standards Institution. The gettext domain is
153
+ # "iso_4217".
154
+ # [origin](http://www.bsi-global.com/en/Standards-and-Publications/Industry-Sectors/Services/BSI-Currency-Code-Service/)
155
+ #
156
+ class CurrencyCode < IsoCode
157
+ self.handle = :iso_4217
158
+ field :currency_code, String, identifier: true
159
+ field :currency_numid, Integer, identifier: true
160
+ field :name, String
161
+ end
162
+
163
+ #
164
+ # Historic Currency Code
165
+ #
166
+ class HistoricCurrencyCode < CurrencyCode
167
+ self.handle = :historic_iso_4217
168
+ field :date_withdrawn, String
169
+ end
170
+
171
+ end
172
+ end
@@ -0,0 +1,124 @@
1
+ require 'gorillib/model/reconcilable'
2
+ # require_relative('./geo_models')
3
+ # require_relative('./geo_json')
4
+
5
+ module Geo
6
+
7
+ Place.class_eval do
8
+ include Gorillib::Model::Reconcilable
9
+
10
+ def adopt_alternate_names(that_val, _)
11
+ return true if that_val.blank?
12
+ names = "#{alternate_names}|#{that_val}".split("|")
13
+ names.uniq!
14
+ names.delete(name)
15
+ write_attribute :alternate_names, names.compact_blank.join("|")
16
+ true
17
+ end
18
+
19
+ def conflicting_attribute!(attr, this_val, that_val)
20
+ case attr
21
+ when :name, :official_name then return :pass
22
+ end
23
+ super
24
+ end
25
+
26
+ end
27
+
28
+ Country.class_eval do
29
+ index_on :country_id
30
+ field :iso_3166_active, :boolean
31
+ end
32
+
33
+
34
+ class FullIso3166
35
+ include Gorillib::Model
36
+ include Gorillib::Model::Reconcilable
37
+ include Gorillib::Model::LoadFromTsv
38
+ self.tsv_options = self.tsv_options.merge(num_fields: 6..8, pop_headers: true)
39
+
40
+ field :country_id, String
41
+ field :tld_id, String
42
+ field :iso_3166_3, String
43
+ field :name, String
44
+ field :code_status, String
45
+ field :iso_3166_active, :boolean, blankish: ['N', false, nil, '']
46
+ field :year_granted, String
47
+ field :notes, String
48
+
49
+ def active?
50
+ iso_3166_active == "Y"
51
+ end
52
+
53
+ def to_place
54
+ Geo::Country.receive({
55
+ country_id: country_id,
56
+ name: name,
57
+ tld_id: tld_id,
58
+ iso_3166_active: iso_3166_active,
59
+ })
60
+ end
61
+ end
62
+
63
+ end
64
+
65
+ # cd Congo (Kinshasa)
66
+ # um Baker Island
67
+ # um Howland Island
68
+ # um Jarvis Island
69
+ # um Johnston Atoll
70
+ # um Kingman Reef
71
+ # um Midway Islands
72
+ # um Navassa Island
73
+ # um Palmyra Atoll
74
+ # um Wake Island
75
+ # mi Midway Islands
76
+ # na Netherlands Antilles
77
+ # gs South Georgia and the Islands
78
+ # sj Svalbard
79
+ # wk Wake Island
80
+ # ps West Bank
81
+ # ps West Bank and the Gaza Strip
82
+ # ps Gaza Strip
83
+
84
+ class CountryReconciler
85
+
86
+ def self.load_reconciled_countries
87
+
88
+ Geo::FullIso3166.load_tsv([:geo_data, 'iso_codes/full_iso_3166.tsv']) do |raw_country|
89
+ Geo::Country.values << raw_country.to_place
90
+ end
91
+
92
+ Wukong::Data::CountryCode.load
93
+ Wukong::Data::CountryCode.values.each do |raw_country|
94
+ iso_country = raw_country.to_place
95
+ country = Geo::Country.for_country_id(iso_country.country_id){ Geo::Country.new }
96
+ country.adopt(iso_country)
97
+ end
98
+
99
+ Wukong::Data::GeonamesGeoJson.load_json(:geonames_countries) do |raw_feature|
100
+ gn_country = raw_feature.properties.to_place
101
+ country = Geo::Country.for_country_id(gn_country.country_id){ Geo::Country.new }
102
+ country.adopt(gn_country)
103
+ end
104
+
105
+ Geo::Country.values.sort_by!(&:country_id)
106
+ end
107
+ end
108
+
109
+
110
+
111
+ # {
112
+ # :xx => { :name => 'Iran' },
113
+ # :xx => 'Tanzania, United Republic',
114
+ # :xx => 'Palestinian Territory, Occupied',
115
+ # :xx =>
116
+ # }
117
+ # :kp => "North Korea" Korea, Democratic People's Republic
118
+ # :kr => "South Korea" Korea, Republic of
119
+ # :bn => "Brunei"
120
+ # :bq => "Caribbean Netherlands"
121
+ # Lao People's Democratic Republic
122
+ #
123
+ # :va Holy See (Vatican City State)
124
+ # :vi Virgin Islands, U.S.