Skip to content

Instantly share code, notes, and snippets.

@hyrious
Created June 15, 2019 02:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hyrious/36b6292565fe1c6f65834ec602b6b3f8 to your computer and use it in GitHub Desktop.
Save hyrious/36b6292565fe1c6f65834ec602b6b3f8 to your computer and use it in GitHub Desktop.
# coding: utf-8
Types = {}
Objects = {}
def enum part
part.split(/, | and /)
end
def scan part, type
parts = part.scan(/(\w+ is \w+|\w+(?:, \w+)+ are \w+(?:, \w+)+)/)
s = parts.reduce({}) { |s, a|
if m = a[0].match(/(\w+) is (\w+)/)
s[m[1]] = m[2]
else
s.merge! a[0].split(/ are /).map { |e| enum e }.reduce(&:zip).to_h
end
s
}
Types[type]&.each { |field| s[field] ||= nil }
s
end
def parse sentence
case sentence
when /a (\w+) is defined by (.+)/
Types[$1] = enum $2
when /there is a (\w+) called (\w+)(.+)/
Objects[$2] = scan $3, $1
when /the (.+?) is (?:called )?(.+)/
Objects[$1] = $2
end
end
DATA.read.downcase.split(/\.\s+/).map(&:strip).each { |s| parse s }
pp Objects
__END__
A rect is defined by x, y, width and height.
There is a rect called X, its x, y are 114, 514 and its width is 1919810.
The game title is called hello world.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment