-
-
Save leejarvis/f2e1777cb8c75026efb0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
class Scanner | |
KEYWORDS = { | |
:direction => %w( north south east west etc ), | |
:verb => %w( go stop kill eat ), | |
:stop => %w( the in of from at it ), | |
:noun => %w( door bear princess cabinet ) | |
} | |
Token = Struct.new(:type, :word) | |
def self.scan(string) | |
string.split.map do |word| | |
if key = KEYWORDS.find { |k, v| v.include?(word) } | |
Token.new key.shift, word | |
end | |
end | |
end | |
end | |
p Scanner.scan 'south' #=> [#<struct Scanner::Token type=:direction, word="south">] | |
p Scanner.scan 'door' #=> [#<struct Scanner::Token type=:noun, word="door">] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment