map-berk-parses.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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 berekelyparses.py.
  6. ##
  7. ## Current version: 1.0
  8. import sys, optparse, berkeleyparses
  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: FTB-trained Berkeley to a common tag set over itself and frenchFactored-trained Stanford)", 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 vTree.strip('\n ') == '(())': # treating Berkely unparsed sentences
  34. print vTree.strip('\n')
  35. elif len(vTree.strip()) != 0:
  36. berkeleyparses.mapToCommonTagSet(vTree, opts.mapping, vChangeTopNode, opts.outToptag, True)
  37. vfInput.close()
  38. ##======================================================================
  39. ## calling main
  40. if __name__ == "__main__":
  41. sys.exit(main())