123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 |
- #! /usr/bin/python
- ## This script checks for mismatched brackets in input parse trees.
- ## The following types of mismatches are supported:
- ## - total opening/closing bracket number conflict
- ## - early exceeding closing brackets
- ##
- ## This script is built upon no well-known bracketing mishmatch in
- ## parsing context.
- ##
- ## To do: upgarde to support them
- ##
- ## Version 1.0
- import sys, optparse
- ##======================================================================
- ## main
- def main(argv=None):
- if argv is None:
- argv = sys.argv
-
- parser = optparse.OptionParser(usage="%prog <PARSE FILE>" +
- "\nChecks the input parse trees and outputs the line numbers of trees containing mismatched brackets.", version="%prog 1.0")
- (opts, posArgs) = parser.parse_args()
-
- if len(posArgs) < 1:
- parser.error("At least 1 argument is needed!")
- ## opening input parse file
- vParseFileName = posArgs[0]
- if vParseFileName == None:
- print 'Weka pasre file is not provided!'
- sys.exit(2)
- else:
- try:
- vParseFile = open(vParseFileName, 'r')
- except IOError:
- print 'Can\'t open parse file: ' + vParseFileName
- sys.exit(2)
-
- vTreeCntr = 1
- for vTree in vParseFile:
- vBracketCntr = 0
- for vChr in vTree:
- if vChr == '(':
- vBracketCntr += 1
- elif vChr == ')':
- vBracketCntr -= 1
- ## cheking for exceeding number of closing brackets throughout the tree
- ## It may happen that at some points in a tree number of closing brackets
- ## exceed that of opening brackets but finally the total number of opening
- ## and closing brackets in tree be equal (e.g. ())(() which is wrong)
- if vBracketCntr < 0:
- print vTreeCntr
- continue
-
- # final check for the equality of numbers of openning and closing brackets
- if vBracketCntr != 0:
- print vTreeCntr
-
- vTreeCntr += 1
- ##======================================================================
- ## calling main
- if __name__ == "__main__":
- sys.exit(main())
|