oorb 0.1.3 → 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.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/lib/oorb.rb +41 -44
  3. data/lib/oorb/version.rb +1 -1
  4. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a529e26f1cb9849fe55c4bedb9e94fae3682d27f
4
- data.tar.gz: 548619f352415c0d3e49fe387da551e3921ddd58
3
+ metadata.gz: 5d8b52cb222bebd999ebf895296e15562ebc867c
4
+ data.tar.gz: 645d055d078fe7f092d1b1a946fee00dd1a8b126
5
5
  SHA512:
6
- metadata.gz: 5cbaf0f53ca1fcb6a889fda04fe70f239b2be5c779355f735b6c1e425c6a871d3f2851a0c9eb2b16ca858808deb90f95466057a315db0fb029af93e007990b11
7
- data.tar.gz: a9592c656d19a230acab76c6373392060df806c210ee7fe3e6f507aa2bb78579138ea7e3b0e9fad456ff8446e102a2ef3711d9a2323515dfa6d73fbf20f91d1f
6
+ metadata.gz: 9a0234421e5e323ec0ff88802f6c57a330e712da7551293fbea0015d7ae9f0676eac0965528ee415e1625f7ed12120d466c29dbed8ccc4b19d2215d91f98fe63
7
+ data.tar.gz: 291386c5a0f9ba5b74087edc83f3837f2733ad49e3dd86cb201ddd037d1f7516f24bba3882e210b7fb6d47123da2ce54d272d079bc0d129291329db72d71baa8
data/lib/oorb.rb CHANGED
@@ -1,74 +1,71 @@
1
1
  require "oorb/version"
2
- LETTERS = {'s' => %w(f l j i 3 8 5),
3
- 'h' => %w(b),
4
- 'b' => %w(h),
5
- 'y' => %w(v j 7),
6
- 'c' => %w(e f d o 6),
7
- 'i' => %w(l 1),
8
- 'e' => %w(6 c d f 4 3),
9
- 'o' => %w(c 6 0 3 d),
10
- 't' => %w(i l 4),
11
- 'a' => %w(9),
12
- 'l' => %w(1 i t),
13
- 'v' => %w(y),
14
- 'f' => %w(c s),
15
- 'd' => %w(3 0 o),
16
- 'z' => %w(2),
17
- 'g' => %w(9 8),
18
- 'j' => %w(y),
19
- }
20
2
 
21
3
  ##
22
4
  # OCR Optimized Regex Builder
23
5
  class OORB
24
- def initialize
25
- @letters = LETTERS
26
- end
27
6
 
28
- attr_reader :letters
7
+ ##
8
+ # Letters that regularly are mistaken in OCR and their common replacements
9
+ LETTERS = {'s' => %w(f l j i 3 8 5),
10
+ 'h' => %w(b),
11
+ 'b' => %w(h),
12
+ 'y' => %w(v j 7),
13
+ 'c' => %w(e f d o 6),
14
+ 'i' => %w(l 1),
15
+ 'e' => %w(6 c d f 4 3),
16
+ 'o' => %w(c 6 0 3 d),
17
+ 't' => %w(i l 4),
18
+ 'a' => %w(9),
19
+ 'l' => %w(1 i t),
20
+ 'v' => %w(y),
21
+ 'f' => %w(c s),
22
+ 'd' => %w(3 0 o),
23
+ 'z' => %w(2),
24
+ 'g' => %w(9 8),
25
+ 'j' => %w(y),
26
+ }
29
27
 
30
28
  ##
31
29
  # Runs the application from the command line
32
30
  def run
33
31
  puts "Waiting for a statement."
34
32
  user_input = gets.chomp
35
- puts build_regex(user_input)
33
+ combined = combine_whitespace(user_input)
34
+ puts build_regex(combined)
36
35
  run
37
36
  end
38
37
 
39
38
  ##
40
39
  # Builds an OCR optimized regular expression from a string
41
40
  def build_regex(input)
42
- if input =~ /[\[\]]/
43
- raise ArgumentError, "Square braces? Really!? Fuck off."
44
- end
45
- statement = input.downcase.chars.map do |l|
46
- LETTERS.has_key?(l) ? build_collection(l) : l
41
+ input.downcase.chars.map do |char|
42
+ LETTERS.has_key?(char) ? build_collection(char) : escape(char)
47
43
  end.join
48
- escapes(statement)
44
+ end
45
+
46
+ ##
47
+ # Combines whitespace characters to make " " into " "
48
+ def combine_whitespace(string)
49
+ string.gsub(/\s+/, "\s")
49
50
  end
50
51
 
51
52
  ##
52
53
  # Builds a group match from an input letter
53
54
  # Raises an argument error if the letter isn't from the LETTERS hash
54
- def build_collection(letter)
55
- unless LETTERS[letter]
56
- raise ArgumentError, "Valid arguments are #{LETTERS.keys.join(", ")}."
55
+ def build_collection(character)
56
+ unless LETTERS[character]
57
+ raise ArgumentError, "Valid arguments are a single character from #{LETTERS.keys.join(", ")}."
57
58
  end
58
- LETTERS[letter].each { |x| letter << x }
59
- "[#{letter}]"
59
+ LETTERS[character].each { |x| character << x }
60
+ "[#{character}]"
60
61
  end
61
62
 
62
63
  ##
63
- # Escapes backslashes, whitespace, punctuation, parens and curly braces
64
- # Square brackets are forbidden
65
- def escapes(string)
66
- string.gsub(/\\/, "\\\\\\") # escape backslashes
67
- .gsub(/\s+/, "\\s") # make whitespace \s
68
- .gsub(/[.,;:'`]/, "[.,;:'`\\s]?") # make punction optional
69
- .gsub(/\(/, "\\(") # escape open parens
70
- .gsub(/\)/, "\\)") # escape close parens
71
- .gsub(/\{/, "\\{") # escape open curly braces
72
- .gsub(/\}/, "\\}") # escape open curly braces
64
+ # Escapes a single-character string and makes \s optional \\s?
65
+ def escape(character)
66
+ if character.length > 1
67
+ raise ArgumentError, "Argument must be a single character string"
68
+ end
69
+ character == "\s" ? "\\s?" : Regexp.escape(character)
73
70
  end
74
71
  end
data/lib/oorb/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Oorb
2
- VERSION = "0.1.3"
2
+ VERSION = "0.2.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: oorb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Calvyn82