import sys, re, string, os
import nl
second_order = [
  re.compile('^\(NX The/DT/the statement/NN/statement NX\) "/"/" .*? "/"/" \(VX is/VBZ/be VX\) true/JJ/true because/IN/because (.*)'),
  re.compile('^\(NX Another/DT/another way/NN/way NX\) to/TO/to \(VX say/VB/say VX\)'),
  re.compile('^\(NX spatial/JJ/spatial concept/NN/concept'),
  re.compile('^To/TO/to \(VX understand/VB/understand VX\) \(NX the/DT/the event/NN/event NX\) "/"/" .*? "/"/" ,/,/, \(NX it/PRP/it NX\) \(VX is/VBZ/be VX\) important/JJ/important to/TO/to \(VX know/VB/know VX\) that/IN/that (.*)'),
  re.compile('^\(NX The/DT/the story/NN/story'),
  re.compile('^\(NX The/DT/the common/JJ/common name/NN/name'),
  re.compile('^\(NX The/DT/the fact/NN/fact NX'),
  re.compile('^\(NX Difference/NNP/difference NX\) between/IN/between'),
  re.compile('^\(NX Similarity/NNP/similarity NX\) between/IN/between'),
  re.compile('^\(NX The/DT/the statement/NN/statement'),
  re.compile('^\(NX picture/NN/picture description/NN/description')
]
def pairs(lst):

  result = []
  for x in lst:
    for y in lst:
      if x != y:
        result.append((x, y))
        result.append((y, x))
  if result == []:
    return [(lst[0], lst[0])]
  else:
    return result
extraction_patterns = [
  [ re.compile('^\(NX Something/NN/something NX\) \(NX you/PRP/you NX\) \(VX find/VBP/find VX\) (.*?) \(VX is/VBZ/be VX\) \(NX (.*?) NX\)'),
    lambda x, y: '(LocationOf "%s" "%s")' % (y, x)
  ],
  [ re.compile('^\(NX Somewhere/NN/something NX\) \(NX you/PRP/you NX\) \(VX find/VBP/find VX\) (.*?) \(VX is/VBZ/be VX\) .*?/IN/.*? \(NX (.*?) NX\)'),
    lambda x, y: '(LocationOf "%s" "%s")' % (x, y)
  ],
  [ re.compile('^Somewhere/RB/somewhere \(NX (.*?) NX\) \(VX can/MD/can be/VB/be VX\) \(VX is/VBZ/be VX\) .*?/IN/.*? \(NX (.*?) NX\)'),
    lambda x, y: '(LocationOf "%s" "%s")' % (x, y)
  ],
  [ re.compile('^\(NX You/PRP/you NX\) \(VX are/VBP/be VX\) likely/JJ/likely to/TO/to \(VX find/VB/find VX\) \(NX (.*?) NX\) (.*)'),
    lambda x, y: '(LocationOf "%s" "%s")' % (x, y)
  ],
  [ re.compile('^(.*?) \(VX made/VBN/make VX\) out/IN/out of/IN/of (.*)'),
    lambda x, y: '(MadeOf "%s" "%s")' % (x, y)
  ],
  [ re.compile('^(.*?) \(VX .*?/VBZ/be made/VBN/make VX\) from/IN/from (.*)'),
    lambda x, y: '(MadeOf "%s" "%s")' % (x, y)
  ],
  [ re.compile('^(.*?) \(VX .*?/VBP/be made/VBN/make VX\) of/IN/of (.*)'),
    lambda x, y: '(MadeOf "%s" "%s")' % (x, y)
  ],
  [ re.compile('^\(NX (.*?) NX\) \(VX want/VBP/want VX\) to/TO/to \(VX (.*)'),
    lambda x, y: '(DesiresEvent "%s" "%s")' % (x, y)
  ],
  [ re.compile('^\(NX (.*?) NX\) \(VX does/VBZ/do not/RB/not want/VBP/want VX\) to/TO/to \(VX (.*)'),
    lambda x, y: '(DesiresNotEvent "%s" "%s")' % (x, y)
  ],
  [ re.compile('^\(NX Things/NNS/things NX\) that/WDT/that \(VX are/VBP/be VX\) often/RB/often \(VX found/VBN/found VX\) together/RB/together \(VX are/VBP/be VX\) \:/\:/\: (.*?),/,/, (.*?),/,/, (.*?),/,/, (.*?),/,/, (.*)'),
    lambda a, b, c, d, e: reduce(lambda p, q: p + '\n' + q, map(lambda y: '(OftenNear "%s" "%s")' % (y[0], y[1]), pairs([a, b, c, d, e])))
  ],
  [ re.compile('^\(NX Things/NNS/things NX\) that/WDT/that \(VX are/VBP/be VX\) often/RB/often \(VX found/VBN/found VX\) together/RB/together \(VX are/VBP/be VX\) \:/\:/\: (.*?),/,/, (.*?),/,/, (.*?),/,/, (.*)'),
    lambda a, b, c, d: reduce(lambda p, q: p + '\n' + q, map(lambda y: '(OftenNear "%s" "%s")' % (y[0], y[1]), pairs([a, b, c, d])))
  ],
  [ re.compile('^\(NX Things/NNS/things NX\) that/WDT/that \(VX are/VBP/be VX\) often/RB/often \(VX found/VBN/found VX\) together/RB/together \(VX are/VBP/be VX\) \:/\:/\: (.*?),/,/, (.*?),/,/, (.*)'),
    lambda a, b, c: reduce(lambda p, q: p + '\n' + q, map(lambda y: '(OftenNear "%s" "%s")' % (y[0], y[1]), pairs([a, b, c])))
  ],
  [ re.compile('^\(NX You/PRP/you NX\) \(VX would/MD/would (.*?) because/IN/because \(NX you/PRP/you NX\) \(VX want/VBP/want VX\) \(NX (.*)'),
    lambda x, y: '(EventForGoalState "%s" "%s")' % (y, x)
  ],
  [ re.compile('^\(NX You/PRP/you NX\) \(VX would/MD/would (.*?) because/IN/because \(NX you/PRP/you NX\) \(VX want/VBP/want VX\) \(VX (.*)'),
    lambda x, y: '(EventForGoalEvent "%s" "%s")' % (y, x)
  ],
  [ re.compile('^If/IN/if \(NX you/PRP/you NX\) \(VX want/VBP/want VX\) to/TO/to (.*?) then/RB/then \(NX you/PRP/you NX\) \(VX should/MD/should (.*)'),
    lambda x, y: '(EventForGoalEvent "%s" "%s")' % (x, y)
  ],
  [ re.compile('^If/IN/if \(NX you/PRP/you NX\) \(VX want/VBP/want VX\) to/TO/to (.*?) \(NX you/PRP/you NX\) \(VX should/MD/should (.*)'),
    lambda x, y: '(EventForGoalEvent "%s" "%s")' % (x, y)
  ],
  [ re.compile('^If/IN/if \(NX you/PRP/you NX\) \(VX want/VBP/want VX\) to/TO/to (.*?) ,/,/, \(VX (.*)'),
    lambda x, y: '(EventForGoalEvent "%s" "%s")' % (x, y)
  ],
  [ re.compile('^\(NX The/DT/the effect/NN/effect NX\) of/IN/of \(VX (.*?) is/VBZ/be VX\) that/IN/that \(NX (.*?) NX\) \(VX (.*)'),
    lambda x, y, z: '(EffectOf "%s" "%s %s")' % (x, y, z)
  ],
  [ re.compile('^\(NX The/DT/the effect/NN/effect NX\) of/IN/of \(VX (.*?) is/VBZ/be VX\) \(NX (.*?) NX\) \(VX (.*)'),
    lambda x, y, z: '(EffectOf "%s" "%s %s")' % (x, y, z)
  ],
  [ re.compile('^\(NX The/DT/the effect/NN/effect NX\) of/IN/of \(VX (.*?) is/VBZ/be VX\) \(VX (.*)'),
    lambda x, y: '(EffectOf "%s" "%s")' % (x, y)
  ],
  [ re.compile('^\(NX The/DT/the consequence/NN/consequence NX\) of/IN/of \(VX (.*?) is/VBZ/be VX\) \(VX (.*)'),
    lambda x, y: '(EffectOf "%s" "%s")' % (x, y)
  ],
  [ re.compile('^\(NX Something/NN/something NX\) that/WDT/that \(VX might/MD/might happen/VB/happen VX\) as/IN/as \(NX a/DT/a consequence/NN/consequence NX\) of/IN/of (.*?) \(VX is/VBZ/be (.+?) VX\) (.*)'),
    lambda x, y, z: '(EffectOf "%s" "%s %s")' % (x, y, z)
  ],
  [ re.compile('\w* \(VX (.*?) \(VX causes/VBZ/cause VX\) \(VX (.*)'),
    lambda x, y: '(EffectOf "%s" "%s")' % (x, y)
  ],
  [ re.compile('^\(NX (.*?) \(VX would/MD/would make/VB/make VX\) \(NX you/PRP/you NX\) \(VX want/VB/want VX\) to/TO/to \(VX (.*?)'),
    lambda x, y: '(ObjectCausesGoal "%s" "%s")' % (x, y)
  ],
  [ re.compile('^\(NX The/DT/the effect/NN/effect NX\) of/IN/of \(VX (.*?) is/VBZ/be VX\) that/IN/that \(NX (.*?) NX\) \(NX (.*)'),
    lambda x, y, z: '(EffectOfIsState "%s" "%s %s")' % (x, y, z)
  ],
  [ re.compile('^\(NX Something/NN/something NX\) that/WDT/that \(VX might/MD/might happen/VB/happen VX\) as/IN/as \(NX a/DT/a consequence/NN/consequence NX\) of/IN/of (.*?) \(VX is/VBZ/be VX\) \(NX (.*)'),
    lambda x, y: '(EffectOfIsState "%s" "%s")' % (x, y)
  ],
  [ re.compile('^\(NX (.*?) NX\) \(VX .*?/VB./be VX\) \(NX part/NN/part NX\) of/IN/of \(NX (.*?) NX\)'),
    lambda x, y: '(PartOf "%s" "%s")' % (x, y)
  ],
  [ re.compile('^\(NX (.*?) NX\) \(VX .*?/VBP/have VX\) \(NX (.*?) NX\)'),
    lambda x, y: '(PartOf "%s" "%s")' % (x, y)
  ],
  [ re.compile('^\(NX One/CD/one NX\) of/IN/of \(NX the/DT/the things/NNS/things NX\) \(NX you/PRP/you NX\) \(VX do/VBP/do VX\) when/WRB/when \(NX you/PRP/you NX\) (.*?) \(VX is/VBZ/be VX\) (.*)'),
    lambda x, y: '(SubeventOf "%s" "%s")' % (x, y)
  ],
  [ re.compile('^\(NX Something/NN/something NX\) that/WDT/that \(VX might/MD/might happen/VB/happen VX\) when/WRB/when \(NX you/PRP/you NX\) (.*?) \(VX is/VBZ/be VX\) (.*)'),
    lambda x, y: '(SubeventOf "%s" "%s")' % (x, y)
  ],
  [ re.compile('^\(NX Something/NN/something NX\) \(NX you/PRP/you NX\) \(VX might/MD/might do/VB/do VX\) while/IN/while (.*?) \(VX is/VBZ/be VX\) \(VX (.*)'),
    lambda x, y: '(SubeventOf "%s" "%s")' % (x, y)
  ],
  [ re.compile('^\(NX Something/NN/something NX\) that/WDT/that \(VX might/MD/might happen/VB/happen VX\) while/IN/while (.*?) \(VX is/VBZ/be (.*)'),
    lambda x, y: '(SubeventOf "%s" "%s")' % (x, y)
  ],
  [ re.compile('When/WRB/when \(NX you/PRP/you NX\) "/"/" (.*?) "/"/" \(NX you/PRP/you NX\) \(VX do/VBP/do VX\) the/DT/the \(VX following/VBG/following VX\) :/:/: \(NX 1/CD/1 NX\) ././. (.*?) ,/,/, \(NX 2/CD/2 NX\) ././. (.*?) ,/,/, \(NX 3/CD/3 NX\) ././. (.*?) ,/,/, \(NX 4/CD/4 NX\) ././. (.*?) ,/,/, \(NX 5/CD/5 NX\) ././. (.*?)'),
    lambda a, b, c, d, e, f: '(SubeventOf "%s" "%s")\n' % (a, b) + '(SubeventOf "%s" "%s")\n' % (a, c) + '(SubeventOf "%s" "%s")\n' % (a, d) + '(SubeventOf "%s" "%s")\n' % (a, e) + '(SubeventOf "%s" "%s")' % (a, f)
  ],
  [ re.compile('^\(NX Something/NN/something NX\) \(NX you/PRP/you NX\) \(VX need/VBP/need VX\) to/TO/to \(VX do/VB/do VX\) before/IN/before \(NX you/PRP/you NX\) (.*?) \(VX is/VBZ/be (.*)VX\) \(VX (.*)'),
    lambda x, y, z: '(PostEventOf "%s" "%s %s")' % (x, y, z)
  ],
  [ re.compile('^\(VX (.*?) \(VX requires/VBZ/require VX\) \(NX (.*)'),
    lambda x, y: '(EventRequiresObject "%s" "%s")' % (x, y)
  ],
  [ re.compile('^\(NX The/DT/the first/JJ/first thing/NN/thing NX\) \(NX you/PRP/you NX\) \(VX do/VBP/do VX\) when/WRB/when \(NX you/PRP/you NX\) (.*?) \(VX is/VBZ/be VX\) (.*)'),
    lambda x, y: '(FirstSubeventOf "%s" "%s")' % (x, y)
  ],
  [ re.compile('^\(NX The/DT/the first/JJ/first thing/NN/thing NX\) \(NX you/PRP/you NX\) \(VX do/VBP/do VX\) when/WRB/when \(NX you/PRP/you NX\) (.*?) is/NNS/be \(VX (.*)'),
    lambda x, y: '(FirstSubeventOf "%s" "%s")' % (x, y)
  ],
  [ re.compile('^\(NX The/DT/the last/JJ/last thing/NN/thing NX\) \(NX you/PRP/you NX\) \(VX do/VBP/do VX\) when/WRB/when \(NX you/PRP/you NX\) (.*?) \(VX is/VBZ/be VX\) (.*)'),
    lambda x, y: '(LastSubeventOf "%s" "%s")' % (x, y)
  ],
  [ re.compile('^\(NX The/DT/the last/JJ/last thing/NN/thing NX\) \(NX you/PRP/you NX\) \(VX do/VBP/do VX\) when/WRB/when \(NX you/PRP/you NX\) (.*?) is/NNS/be \(VX (.*)'),
    lambda x, y: '(LastSubeventOf "%s" "%s")' % (x, y)
  ],
  [ re.compile('^\(NX .*?/DT/.*? (.*?) NX\) \(VX .*?/VB./be VX\) for/IN/for \(VX (.*)'),
    lambda x, y: '(UsedFor "%s" "%s")' % (x, y)
  ],
  [ re.compile('^\(NX .*?/DT/.*? (.*?) NX\) \(VX .*?/VB./be used/VBN/use VX\) for/IN/for \(VX (.*)'),
    lambda x, y: '(UsedFor "%s" "%s")' % (x, y)
  ],
  [ re.compile('^\(NX .*?/DT/.*? (.*?) NX\) \(VX(?: can/MD/can)? .*?/VB./be used/VBN/use VX\) to/TO/to \(VX (.*)'),
    lambda x, y: '(UsedFor "%s" "%s")' % (x, y)
  ],
  [ re.compile('^\(NX You/PRP/you NX\) \(VX can/MD/can use/VB/use VX\) \(NX (.*?) NX\) to/TO/to \(VX (.*)'),
    lambda x, y: '(UsedFor "%s" "%s")' % (x, y)
  ],
  [ re.compile('^\(NX An/DT/a activity/NN/activity NX\) \(NX (.*?) NX\) \(VX can/MD/can do/VB/do VX\) \(VX is/VBZ/be VX\) \(NX (.*)'),
    lambda x, y: '(CapableOfState "%s" "%s")' % (x, y)
  ],
  [ re.compile('^\(NX An/DT/a activity/NN/activity (.*?) NX\) \(VX can/MD/can do/VB/do VX\) \(VX is/VBZ/be VX\) \(NX (.*)'),
    lambda x, y: '(CapableOfState "%s" "%s")' % (x, y)
  ],
  [ re.compile('^\(NX Situation/NNP/situation NX\) :/:/: \(NX I/PRP/i NX\) \(VX am/VBP/be (.+?) VX\) (.*)'),
    lambda x, y: '(CapableOf "someone" "%s %s")' % (x, y)
  ],
  [ re.compile('^\(NX An/DT/a activity/NN/activity (.*?) NX\) \(VX can/MD/can do/VB/do VX\) \(VX is/VBZ/be VX\) \(VX (.*)'),
    lambda x, y: '(CapableOf "%s" "%s")' % (x, y)
  ],
  [ re.compile('^\(NX Situation/NNP/situation NX\) :/:/: (.*)'),
    lambda x: None
  ],
  [ re.compile('^\(NX An/DT/a activity/NN/activity (.*?)NX\) \(VX can/MD/can do/VB/do VX\) \(VX is/VBZ/be VX\) \(VX (.*)'),
    lambda x, y: '(CapableOf "%s" "%s")' % (x, y)
  ],
  [ re.compile('^\(NX (.*?) NX\) \(VX can/MD/can (.*)'),
    lambda x, y: '(CapableOf "%s" "%s")' % (x, y)
  ],
  [ re.compile('^\(NX (.*?) NX\) \(VX can/MD/can cause/VB/cause VX\) \(NX (.*?)'),
    lambda x, y: '(ObjectCausesObject "%s" "%s")' % (x, y)
  ],
  [ re.compile('^\(NX (.*?) NX\) \(VX .*?/VB./be VX\) \(NX a/DT/a kind/NN/kind NX\) of/IN/of \(NX (.*?) NX\)'),
    lambda x, y: '(IsA "%s" "%s")' % (x, y)
  ],
  [ re.compile('^\(NX .*?/DT/.*? (.*?) NX\) \(VX .*?/VB./be VX\) \(NX a/DT/a type/NN/type NX\) of/IN/of \(NX (.*?) NX\)'),
    lambda x, y: '(IsA "%s" "%s")' % (x, y)
  ],
  [ re.compile('^\(NX .*?/DT/.*? (.*?) NX\) \(VX .*?/VB./be VX\) \(NX .*?/DT/.*? (.*?) NX\)'),
    lambda x, y: '(IsA "%s" "%s")' % (x, y)
  ],
  [ re.compile('^\(NX (.*?) NX\) \(VX .*?/VB./be VX\) \(NX (.*?) NX\)'),
    lambda x, y: '(IsA "%s" "%s")' % (x, y)
  ],
  [ re.compile('^\(NX (.*?) NX\) \(VX .*?/VB./be VX\) (\w*/JJ/\w*)'),
    lambda x, y: '(PropertyOf "%s" "%s")' % (x, y)
  ],
  [ re.compile('^\(NX (.*?) NX\) \(VX have/VBP/have (.*)'),
    lambda x, y: '(PropertyOf "%s" "%s")' % (x, y)
  ],
  [ re.compile('^\(NX (.*?) NX\) \(VX .*?/VB./be'),
    lambda x: None
  ],
  [ re.compile('^\(NX (.*?) NX\) \(VX (.*)'),
    lambda x, y: '(Do "%s" "%s")' % (x, y)
  ],
]
tag1 = re.compile('^(.+?)/(.+?)/(.+?)$')
def filterexpr(expr):

  elts = string.split(expr)
  s = ''
  for elt in elts:
    if elt[0] == '(':
      continue
    if elt[-1] == ')':
      continue
    result = re.findall(tag1, elt)
    if not result:
      continue
    if result[0][1] == 'DT':
      continue
    if len(result[0][0]) == 1:
      continue
    s = s + result[0][2] + ' '
  return s.strip()
f = open('omcsraw-chunked.txt')
out = open('predicates.txt', 'w')
res = open('residue.txt', 'w')
while 1:
  line = f.readline()
  if line == '':
    break
  if line == '\n':
    continue
  for pat in second_order:
    result = re.findall(pat, line)
    if result == []:
      continue
    if len(result) > 0:
      line = result[0]
    else:
      line = ''
  if line == '':
    continue
  written = 0
  for pat in extraction_patterns:
    result = re.findall(pat[0], line)
    if result == []:
      continue
    result = map(filterexpr, result[0])
    if not reduce(lambda x, y: x and y, result):
      break
    if filter(lambda x: len(x) > 40, result) != []:
      break
    if filter(lambda x: nl.is_stopword(x), result):
      break
    expr = apply(pat[1], result)
    if expr:
      print >>out, expr
    written = 1
    break
  if not written:
    print >>res, filterexpr(line), line