map-stanf-parses.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #! /usr/bin/python
  2. ## This script changes the syntactic tags in the input syntax trees to
  3. ## tags from another tag set according to the specified mapping.
  4. ##
  5. ## Mappings are defined in stanfordparses.py.
  6. ##
  7. ## Current version: 1.0
  8. import sys, optparse, stanfordparses
  9. ##======================================================================
  10. ## main
  11. def main(argv=None):
  12. if argv is None:
  13. argv = sys.argv
  14. parser = optparse.OptionParser(usage="%prog <INPUT PARSE FILE>" +
  15. "\nChanges the syntactic tags in the input syntax trees to tags from another tag set according to the specified mapping.", version="%prog 1.0")
  16. parser.add_option("-m", "--mapping", help="mapping to be used (1: frenchFactored-trained Stanford to a common tag set over itself and FTB-trained Berkeley)", metavar="MAPPING", dest="mapping", default='1', action="store")
  17. parser.add_option("-t", "--toptag", help="top node tag to be used in output (unchanged if not specified)", metavar="OUTPUT TOP NODE TAG", dest="outToptag", action="store")
  18. # processing input arguments
  19. (opts, posArgs) = parser.parse_args()
  20. if len(posArgs) < 1:
  21. parser.error("At least 1 arguments are required")
  22. # opening input parse file
  23. try:
  24. vfInput = open(posArgs[0])
  25. except IOError:
  26. sys.exit('Can\'t open input parse file: ' + posArgs[0])
  27. # processing options
  28. if opts.outToptag == None:
  29. vChangeTopNode = False
  30. else:
  31. vChangeTopNode = True
  32. for vTree in vfInput:
  33. if len(vTree.strip()) != 0:
  34. stanfordparses.mapToCommonTagSet(vTree, opts.mapping, vChangeTopNode, opts.outToptag, True)
  35. vfInput.close()
  36. ##======================================================================
  37. ## calling main
  38. if __name__ == "__main__":
  39. sys.exit(main())