def isValidMoveText(text): ok = 1 try: tokens = PgnTokenizer.tokenize(text) for token in tokens: # 1) comments if re.match('^{(.*)}$', token): pass # 2) move numbers elif re.match(r'(\d+)\.', token): pass # 3) normal move (SAN) elif re.match(Common.regexSanChess, token): pass # 4) result elif re.match(Common.regexResults, token): pass # 5) nope else: print "token -%s- is not allowed" % token ok = 0 break except: pass return ok
def parsePgn(self, text): tokens = PgnTokenizer.tokenize(text) moveNum = 1 player = 'W' while tokens: token = tokens.pop(0) print "on token: -%s-" % token # tag tokens eg: [Event "May 2013 Tourney"] m = re.match(r'\[(.*?) "(.*?)"\]', token) if m: self.tags[m.group(1)] = m.group(2) continue # comment tokens eg: { good move! also consider Rxe8 } m = re.match('^{(.*)}$', token) if m: # if we're in the moves section, comment applies to a move if self.moves: self.moves[-1].addComment(m.group(1)) # else it applies to the match comments else: self.comments.append(m.group(1)) continue # result tokens eg: 0-1 m = re.match(Common.regexResults, token) if m: self.result = token if tokens: raise Exception("result token was not the final token! next is: " + tokens[0]) continue # move number token eg: 34. m = re.match(r'(\d+)\.', token) if m: if moveNum == int(m.group(1)) + 1: raise Exception("out of order move number: " + token) moveNum += 1; player = 'W' # normal move (SAN) m = re.match(Common.regexSanChess, token) if m: move = ChessMove.ChessMove() move.moveNum = moveNum move.player = player move.san = token self.moves.append(move) player = {'W':'B', 'B':'W'}[player] # calculate all board states self.states = [self.initState] for move in self.moves: # exceptions (repeated moves due to time forfeiture, etc.) just carry state along... if 'TIME_FORFEIT' in move.flags: self.states.append(self.states[-1]) continue currState = self.states[-1] nextState = x.nextState(self.states[-1], move.san) print "current state: " + currState print "next state: " + nextState self.states.append(nextState)
def parsePgn(self, text): tokens = PgnTokenizer.tokenize(text) moveNum = 1 player = 'W' while tokens: token = tokens.pop(0) print "on token: -%s-" % token # tag tokens eg: [Event "May 2013 Tourney"] m = re.match(r'\[(.*?) "(.*?)"\]', token) if m: self.tags[m.group(1)] = m.group(2) continue # comment tokens eg: { good move! also consider Rxe8 } m = re.match('^{(.*)}$', token) if m: # if we're in the moves section, comment applies to a move if self.moves: self.moves[-1].addComment(m.group(1)) # else it applies to the match comments else: self.comments.append(m.group(1)) continue # result tokens eg: 0-1 m = re.match(Common.regexResults, token) if m: self.result = token if tokens: raise Exception( "result token was not the final token! next is: " + tokens[0]) continue # move number token eg: 34. m = re.match(r'(\d+)\.', token) if m: if moveNum == int(m.group(1)) + 1: raise Exception("out of order move number: " + token) moveNum += 1 player = 'W' # normal move (SAN) m = re.match(Common.regexSanChess, token) if m: move = ChessMove.ChessMove() move.moveNum = moveNum move.player = player move.san = token self.moves.append(move) player = {'W': 'B', 'B': 'W'}[player] # calculate all board states self.states = [self.initState] for move in self.moves: # exceptions (repeated moves due to time forfeiture, etc.) just carry state along... if 'TIME_FORFEIT' in move.flags: self.states.append(self.states[-1]) continue currState = self.states[-1] nextState = x.nextState(self.states[-1], move.san) print "current state: " + currState print "next state: " + nextState self.states.append(nextState)
def parsePgn(self, text): tokens = PgnTokenizer.tokenize(text) currMoveNum = 0 player = 'W' while tokens: token = tokens.pop(0) #print "on token: -%s-" % token # tag tokens eg: [Event "May 2013 Tourney"] m = re.match(r'\[(.*?) "(.*?)"\]', token) if m: self.tags[m.group(1)] = m.group(2) continue # comment tokens eg: { good move! also consider Rxe8 } m = re.match('^{(.*)}$', token) if m: # if we're in the moves section, comment applies to a move if self.moves: self.moves[-1].addComment(m.group(1)) # else it applies to the match comments else: self.comments.append(m.group(1)) continue # result tokens eg: 0-1 m = re.match(Common.regexResults, token) if m: self.result = token if tokens: raise Exception("result token was not the final token! next is: " + tokens[0]) continue # move number token eg: 34. m = re.match(r'(\d+)\.', token) if m: if currMoveNum + 1 != int(m.group(1)): raise Exception("out of order move number: " + token) player = 'w' currMoveNum += 1 # normal move (SAN) m = re.match(Common.regexSanChess, token) if m: move = ChessMove.ChessMove() move.moveNum = currMoveNum move.player = player move.san = token self.moves.append(move) player = {'w':'b', 'b':'w'}[player] # calculate all board states # # initial state? or special state? (Fischer960, etc.) if 'SetUp' in self.tags and self.tags['SetUp'] == '1': if 'FEN' in self.tags: self.initState = ChessState(self.tags['FEN']) self.states = [self.initState] # loop over all moves... for move in self.moves: # exceptions (repeated moves due to time forfeiture, etc.) just carry state along... if 'TIME_FORFEIT' in move.flags: self.states.append(self.states[-1]) continue currState = self.states[-1] nextState = currState.transition(move) self.states.append(nextState)
fObj = open(outFile, 'w') for (i,card) in enumerate(cards): (fen, question, answer) = (card['fen'], card['question'], card['answer']) # detect result (Result tag must match end of movetext) result = '*' moveText = '' if not answer or re.match(r'^\s*$', answer): moveText = '{ null } *' elif isValidMoveText(answer): append = True try: ansToks = PgnTokenizer.tokenize(answer) if ansToks and re.match(Common.regexResults, ansToks[-1]): result = ansToks[-1] append = False except: print "answer: -%s- is not valid movetext, commenting it..." % answer pass if append: moveText = '%s %s' % (answer, result) else: moveText = answer else: moveText = '{ %s } *' % answer pgnEntry = ''