over_h3sentence 1.1.0 → 1.2.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,7 +1,7 @@
1
1
  require 'json'
2
2
 
3
3
  class OverH3sentence
4
- LIST_OF_WORDS = JSON.parse(File.read(File.join(File.dirname(__FILE__), './list_of_words_v1.json')))
4
+ LIST_OF_WORDS = JSON.parse(File.read(File.join(File.dirname(__FILE__), './list_of_words_v2.json')))
5
5
  COMBINATIONS_VOCAB = ['000','001','010','011','100','101','110','111','002','012','020','021','022','102','112','120','121','122','200','201','202','210','211','212','220','221','222']
6
6
 
7
7
  def self.sentence(hex_id)
@@ -11,6 +11,13 @@ class OverH3sentence
11
11
  words.join('.')
12
12
  end
13
13
 
14
+ def self.hex_id(sentence)
15
+ raise ArgumentError, 'Sentence must be 3 words long' if sentence.split('.').length != 3
16
+
17
+ h3_address = form_words_to_h3(sentence.split('.'))
18
+ h3_address.to_s(16)
19
+ end
20
+
14
21
  def self.form_h3_to_words(h3_address)
15
22
  binary = h3_address.to_s(2)
16
23
 
@@ -33,4 +40,48 @@ class OverH3sentence
33
40
 
34
41
  [first_word, second_word, third_word]
35
42
  end
43
+
44
+ def self.form_words_to_h3(words)
45
+ h3_invariant_head = '10001100'
46
+ h3_invariant_tail = '111111111'
47
+
48
+ triplet_adj = []
49
+ str_value = ''
50
+
51
+ for i in 0..words.length-1 do
52
+ for j in 0..LIST_OF_WORDS.length do
53
+ if (LIST_OF_WORDS[j] === words[i])
54
+ str_value = j.to_s
55
+ end
56
+ end
57
+ length_string = str_value.length
58
+ if (length_string < 5)
59
+ for n in 0..5-length_string-1 do
60
+ str_value = "0#{str_value}"
61
+ end
62
+ end
63
+
64
+ triplet_adj.push(str_value)
65
+ end
66
+
67
+
68
+ first_trinary_code = triplet_adj[0][0] + triplet_adj[1][0] + triplet_adj[2][0]
69
+ first_integer_value = 0
70
+
71
+ COMBINATIONS_VOCAB.each_with_index do |value, index|
72
+ if (value === first_trinary_code.to_s)
73
+ first_integer_value = index
74
+ end
75
+ end
76
+ full_integer = (first_integer_value).to_s + triplet_adj[0][-4..-1].to_s + triplet_adj[1][-4..-1].to_s + triplet_adj[2][-4..-1].to_s
77
+ binary_full_integer = full_integer.to_i.to_s(2)
78
+ binary_full_integer_length = binary_full_integer.length
79
+ for i in 0..43-binary_full_integer_length-1 do
80
+ binary_full_integer = "0#{binary_full_integer}"
81
+ end
82
+
83
+ whole_binary = h3_invariant_head + binary_full_integer + h3_invariant_tail
84
+
85
+ whole_binary.to_i(2)
86
+ end
36
87
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: over_h3sentence
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gregorio Galante
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-05-23 00:00:00.000000000 Z
11
+ date: 2024-07-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json
@@ -33,6 +33,7 @@ extra_rdoc_files: []
33
33
  files:
34
34
  - bin/over_h3sentence
35
35
  - lib/list_of_words_v1.json
36
+ - lib/list_of_words_v2.json
36
37
  - lib/over_h3sentence.rb
37
38
  homepage: https://github.com/gregogalante/over_h3sentence
38
39
  licenses:
@@ -55,7 +56,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
55
56
  - !ruby/object:Gem::Version
56
57
  version: '0'
57
58
  requirements: []
58
- rubygems_version: 3.5.6
59
+ rubygems_version: 3.5.15
59
60
  signing_key:
60
61
  specification_version: 4
61
62
  summary: Generate sentence from Over land hex id (H3 address)