jebediah 1.0.3 → 1.0.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/bin/git-jeb +1 -1
- data/bin/jebcycle +1 -1
- data/lib/jebediah.rb +49 -4
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 65156594909c339c72e889552c4ab526ccb3cfc3
|
4
|
+
data.tar.gz: 1957eaecb4b60820b57390a086e670b64decb0a0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ea17b8636c130ddf20c474bbeef9f5a108748e7986f8f2d7fffde6524100cc1e12fd4fc448825f038d3b463e923665241870b6289cfdf459af77b561d3fbffb4
|
7
|
+
data.tar.gz: 9a97c11e631d2ef7eeaeff005f6b7e0bd9bf54f73b5d74b7137c15fc9445420b0ab115e057cf0a25ca169c7a7b587c65bc3cd75b6d48fb9ecd9aadfb2396b96c
|
data/bin/git-jeb
CHANGED
data/bin/jebcycle
CHANGED
data/lib/jebediah.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
class Jebediah
|
2
2
|
def self.version
|
3
|
-
return "1.0.
|
3
|
+
return "1.0.5"
|
4
4
|
end
|
5
5
|
|
6
6
|
def initialize(dictPaths=nil)
|
@@ -23,6 +23,20 @@ class Jebediah
|
|
23
23
|
return @dictionaries.length
|
24
24
|
end
|
25
25
|
|
26
|
+
# Maximum hexadecimal string length that can safely be addressed by the dictionary set
|
27
|
+
def maxStringLength
|
28
|
+
weight = @dictionaries.inject(1) { |x, d| x * d.length }
|
29
|
+
bits = (Math.log(weight)/Math.log(2)).floor
|
30
|
+
chars = bits/4
|
31
|
+
end
|
32
|
+
|
33
|
+
# Returns maximum number addressable in the dictionary space
|
34
|
+
def hashFromString(str)
|
35
|
+
str = str[2..-1] if str.start_with?("0x")
|
36
|
+
str = str.to_s[0..maxStringLength-1]
|
37
|
+
hash = Integer("0x" + str)
|
38
|
+
end
|
39
|
+
|
26
40
|
# Load in dictionaries from paths
|
27
41
|
def loadDictionaries(dictPaths)
|
28
42
|
@dictionaries = []
|
@@ -50,10 +64,18 @@ class Jebediah
|
|
50
64
|
str =~ /^[0-9a-fA-F]+$/
|
51
65
|
end
|
52
66
|
|
67
|
+
def isHyphenated?(str)
|
68
|
+
str =~ /^[\w-]+$/
|
69
|
+
end
|
70
|
+
|
53
71
|
# Processes arbitrary input formatted as a string
|
54
72
|
def processString(str)
|
55
73
|
if isHash?(str) then
|
56
74
|
return { :type => 'phrase', :result => phraseForHash(str) }
|
75
|
+
elsif(isHyphenated?(str)) then
|
76
|
+
terms = dehyphenatePhrase(str)
|
77
|
+
return { :type => 'hash', :result => hashForPhrase(terms) } unless terms.nil?
|
78
|
+
return { :type => 'unreadable' }
|
57
79
|
else
|
58
80
|
terms = str.split(' ')
|
59
81
|
return { :type => 'hash', :result => hashForPhrase(terms) } if phraseLength == terms.length
|
@@ -107,7 +129,13 @@ class Jebediah
|
|
107
129
|
weight = 1
|
108
130
|
hash = 0
|
109
131
|
|
110
|
-
|
132
|
+
if phrase.is_a?(String) then
|
133
|
+
if isHyphenated?(phrase) then
|
134
|
+
phrase = dehyphenatePhrase(phrase)
|
135
|
+
else
|
136
|
+
phrase = phrase.gsub(/\s+/m, ' ').strip.split(' ')
|
137
|
+
end
|
138
|
+
end
|
111
139
|
|
112
140
|
# If the phrase doesn't have the same number of words as our nomenclature requires, we can't convert
|
113
141
|
if phrase.length != @dictionaries.length then
|
@@ -130,6 +158,24 @@ class Jebediah
|
|
130
158
|
"%07x" % hash
|
131
159
|
end
|
132
160
|
|
161
|
+
def longestMatchInDictionary(words, dict)
|
162
|
+
words.count.times.map { |n| words[0..n].join("-") }.select { |phrase| dict.include?(phrase) }.last.split("-")
|
163
|
+
end
|
164
|
+
|
165
|
+
def dehyphenatePhrase(phrase)
|
166
|
+
phrase = phrase.split("-") if phrase.is_a?(String)
|
167
|
+
split = []
|
168
|
+
@dictionaries.each do |dict|
|
169
|
+
return nil if phrase.empty?
|
170
|
+
match = longestMatchInDictionary(phrase, dict)
|
171
|
+
split << match.join("-")
|
172
|
+
phrase = phrase[match.count .. -1]
|
173
|
+
end
|
174
|
+
|
175
|
+
return nil unless phrase.empty?
|
176
|
+
split
|
177
|
+
end
|
178
|
+
|
133
179
|
# Convert a hash into a phrase, e.g. "abc4321" -> "rightward succeeded seal"
|
134
180
|
# Hash can be supplied as an integer, or hexadecimal string.
|
135
181
|
#
|
@@ -157,9 +203,8 @@ class Jebediah
|
|
157
203
|
# S_n - s_n = S_(n-1),
|
158
204
|
|
159
205
|
begin
|
160
|
-
hash = "0x" + hash if hash.is_a?(String) and !hash.start_with?("0x")
|
161
206
|
weight = @dictionaries.inject(1) { |x, dict| dict.length * x } # L0 L1 L2 ... L_n
|
162
|
-
sum =
|
207
|
+
sum = hashFromString(hash)
|
163
208
|
lines = [ 0 ] * @dictionaries.length # We fill from the end backwards, so allocate the total size up front
|
164
209
|
|
165
210
|
(@dictionaries.length-1).downto(0) do |n|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: jebediah
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jonas Acres
|
@@ -46,7 +46,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
46
46
|
version: '0'
|
47
47
|
requirements: []
|
48
48
|
rubyforge_project:
|
49
|
-
rubygems_version: 2.0.
|
49
|
+
rubygems_version: 2.0.14
|
50
50
|
signing_key:
|
51
51
|
specification_version: 4
|
52
52
|
summary: Converts hashes to names, and names to hashes
|