fork(2) download
  1. h='able to fly'
  2. i="(not )?(#{h}|\\w+)"
  3. o=->s{n=Regexp.new(i+" (is also|are) "+i).match s
  4. [[n[2],!n[1]],[n[5],!n[4]]]}
  5. c=e=!z=[]
  6. w=->r{z.member?(r)||(z<<(a,b=r)
  7. c|=a[0]==b[0]&&a[1]!=b[1]
  8. w[[[b[0],!b[1]],[a[0],!a[1]]]]
  9. z.map{|q|q[1]==r[0]&&w[[q[0],r[1]]]})}
  10. y=->x{z.member?([[p='Pigs',!e],[h,x]])}
  11. f=->x{x.split(?.).map{|s|w[o[s]]}
  12. c|y[!e]?'Yes':y[e]?'No':'Maybe'}
  13.  
  14.  
  15. def assert_equal(x, y)
  16. fail "Not equal: <#{x.inspect}> != <#{y.inspect}>" if x != y
  17. puts "OK."
  18. end
  19. test=->(text, exp){
  20. c=!z=[] # (re) initialization needed for multiple test runs
  21. assert_equal(f[text], exp)
  22. }
  23.  
  24. test['Pigs are green. Everything that is green is also intelligent. Everything that is able to fly is also not intelligent.', 'No']
  25. test['Pigs are old. Everything that is not able to fly is also not old.', 'Yes']
  26. test['Everything that is sweet is also not old. Everything that is intelligent is also blue.', 'Maybe']
  27. test['Pigs are not able to fly. Everything that is red is also sweet. Everything that is sweet is also not red.', 'Yes']
  28. test['Pigs are very smart. Pigs are able to fly.', 'Yes'] # <-- any answer accepted here
  29.  
Success #stdin #stdout 0.02s 7484KB
stdin
Standard input is empty
stdout
OK.
OK.
OK.
OK.
OK.