<commit_before>/** Version: 0.7.1 **/
/**
 * Copyright 2008 <NAME>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
**/
/*
	Representation of the PGN format. Different meta information
	about the actual game(s) plus the moves and result of the game.
*/
function Pgn(pgn) {
	// properties of the game eg players, ELOs etc
	this.props = new Object();
	this.requiredProps = ['Result','Black','White','Date',
								'Round','Site','Event'];
	this.requiredLength = this.requiredProps.length;
	// the moves, one move contains the black and white move
	this.moves = new Array();
	// the current move in the game
	this.currentMove = 0;
	// for outputting white and black moves separately
	this.skip = 0;

	// strip newlines
	this.pgnOrig = pgn;
	pgn = this.normalize(pgn);
	
	this.pgn = pgn;
	this.pgnRaw = pgn;
	this.pgnStripped = this.stripComments(pgn);
	
	this.pgn = this.extractTags(this.pgn);
	
	this.pgn = this.stripComments(this.pgn,true);
	
	var gameOverre = new Array(
		/1\/2-1\/2/,
		/0-1/,
		/1-0/,
		/\*/
	);

	// the moves;
	var themoves = this.pgn.split(" ");
	var ply = new Array();
	ply[1] = null;
	var plyidx = 0;	//make this 1 if FEN and black to move
	if (this.props["FEN"]) {
		var fen = this.props['FEN'].split(/\/| /g);
		if (fen[8] == 'b') {
			plyidx = 1;
			this.skip = 1;
		}
	}

	if (themoves.length>0 && themoves[themoves.length-1] == "...") {
		 themoves = themoves.slice(0, themoves.length-1);
	}

	var sizeOfTheMoves = themoves.length;
	if (themoves.length>0) {
		for (var i=0;i<gameOverre.length;i++) {
			if (themoves[themoves.length-1].match(gameOverre[i])) {
				sizeOfTheMoves = themoves.length-1;
				continue;
			}
		}
	}


	for (var i=0;i<sizeOfTheMoves;i++) {	//don't handle game end bit
		if (!themoves[i]) {
			continue;
		}
		themoves[i] = themoves[i].trim();

		if (this.moveHasOrigination(themoves[i])) {
			var tmp2 = themoves[i].split("-");
			var newMove;
			if (tmp2[0].match(/[0-9]*?\.?([A-Z])/) != null) {
				 // we can just replace the - with nothing
				 newMove  = themoves[i].replace("-","");
			}
			else {
				var matches = tmp2[0].match(/[0-9]+\./);
				if (matches) {
					newMove = matches[0]+tmp2[1];
				}
				else {
					newMove = tmp2[1];
				}				
			}
			themoves[i] = newMove;
		}
		var c = themoves[i].charAt(0);
		if (c >= '1' && c <= '9') {	//move number
			c = themoves[i].charAt(themoves[i].length-1);
			if (c == '.') {	//ends with . so nothing but a move
				continue;
			}
			var found = false;
			for (var j=0;j<themoves[i].length;j++) {
				c = themoves[i].charAt(j);
				if (c >= '0' && c <= '9') {
					continue;
				}
				else {
					found = true;
					var idx = j;
					// 6.0-0 goes wrong as 0 is used for castling
					if (!(themoves[i].charAt(j) >= '0' && themoves[i].charAt(j)<='9')) {
						 idx = j+1;
				}
					themoves[i] = themoves[i].substring(idx);	//strip move number
					break;
				}
			}
			if (!found) {
				continue;
			}
		}
		ply[plyidx] = themoves[i];
		if (plyidx == 1) {	//black's move or last move
			var move = new Move(ply[0], ply[1]);
			this.moves[this.moves.length] = move;
			plyidx = 0;
			ply = new Array();
			ply[1] = null;
		}
		else {
			plyidx = 1;
		}
	}
	
	if (ply[0] || ply[1]) {
		var move = new Move(ply[0], ply[1]);
		this.moves[this.moves.length] = move;
	}
}
/**
 *	will return the pgn string without comments or with comments replaced by dashes.
 */
Pgn.prototype.stripComments = function(pgn, replace) {
	if (this.isBroken(pgn))
		return this.stripItBroken(pgn, replace);
	else
		return this.stripIt(pgn, replace);
}
/**
 *	Does the move have information on its origin as well as its destination
 */
Pgn.prototype.moveHasOrigination = function(move) {
	return move.indexOf("-") != -1
			 && !/[0|o]-[0|o]/i.test(move)
			 && !/[0|o]-[0|o]-[0|o]/i.test(move);
}
/**
 *	This will "normalize" the pgn file by removing line breaks, collapsing strings of
 *	spaces, and replacing the symbols for moves with the characters (!, ?, etc.)
 */
Pgn.prototype.normalize = function(pgn) {
	pgn = pgn.replace(/\n/g," ");<commit_msg>Refactored<commit_after>/** Version: 0.7.1 **/
/**
 * Copyright 2008 <NAME>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
**/
/*
	Representation of the PGN format. Different meta information
	about the actual game(s) plus the moves and result of the game.
*/
function Pgn(pgn) {
	// properties of the game eg players, ELOs etc
	this.props = new Object();
	this.requiredProps = ['Result','Black','White','Date',
								'Round','Site','Event'];
	this.requiredLength = this.requiredProps.length;
	// the moves, one move contains the black and white move
	this.moves = new Array();
	// the current move in the game
	this.currentMove = 0;
	// for outputting white and black moves separately
	this.skip = 0;

	// strip newlines
	this.pgnOrig = pgn;
	pgn = this.normalize(pgn);
	
	this.pgn = pgn;
	this.pgnRaw = pgn;
	this.pgnStripped = this.stripComments(pgn);
	
	this.pgn = this.extractTags(this.pgn);
	
	this.pgn = this.stripComments(this.pgn,true);
	
	var gameOverre = new Array(
		/1\/2-1\/2/,
		/0-1/,
		/1-0/,
		/\*/
	);

	// the moves;
	var themoves = this.pgn.split(" ");
	var ply = new Array();
	ply[1] = null;
	var plyidx = 0;	//make this 1 if FEN and black to move
	if (this.props["FEN"]) {
		var fen = this.props['FEN'].split(/\/| /g);
		if (fen[8] == 'b') {
			plyidx = 1;
			this.skip = 1;
		}
	}

	if (themoves.length>0 && themoves[themoves.length-1] == "...") {
		 themoves = themoves.slice(0, themoves.length-1);
	}

	var sizeOfTheMoves = themoves.length;
	if (themoves.length>0) {
		for (var i=0;i<gameOverre.length;i++) {
			if (themoves[themoves.length-1].match(gameOverre[i])) {
				sizeOfTheMoves = themoves.length-1;
				continue;
			}
		}
	}

	for (var i=0;i<sizeOfTheMoves;i++) {	//don't handle game end bit
		if (themoves[i]) {
			themoves[i] = themoves[i].trim();
	
			if (this.moveHasOrigination(themoves[i])) {
				var tmp2 = themoves[i].split("-");
				var newMove;
				if (tmp2[0].match(/[0-9]*?\.?([A-Z])/) != null) {
					 // we can just replace the - with nothing
					 newMove  = themoves[i].replace("-","");
				}
				else {
					var matches = tmp2[0].match(/[0-9]+\./);
					if (matches) {
						newMove = matches[0]+tmp2[1];
					}
					else {
						newMove = tmp2[1];
					}				
				}
				themoves[i] = newMove;
			}
			themoves[i] = this.removeMoveNumber(themoves[i])
			if( themoves[i].length > 0 ) {
				ply[plyidx] = themoves[i];
				if (plyidx == 1) {	//black's move or last move
					var move = new Move(ply[0], ply[1]);
					this.moves[this.moves.length] = move;
					plyidx = 0;
					ply = new Array();
					ply[1] = null;
				}
				else {
					plyidx = 1;
				}
			}
		}
	}
	
	if (ply[0] || ply[1]) {
		var move = new Move(ply[0], ply[1]);
		this.moves[this.moves.length] = move;
	}
}
/**
 *	If the move is prefaced by a move number, remove it.
 */
Pgn.prototype.removeMoveNumber = function(move) {
	return move.replace(/^[1-9][0-9]*\.?[ ]*/g,"")
}
/**
 *	will return the pgn string without comments or with comments replaced by dashes.
 */
Pgn.prototype.stripComments = function(pgn, replace) {
	if (this.isBroken(pgn))
		return this.stripItBroken(pgn, replace);
	else
		return this.stripIt(pgn, replace);
}
/**
 *	Does the move have information on its origin as well as its destination
 */
Pgn.prototype.moveHasOrigination = function(move) {
	return move.indexOf("-") != -1
			 && !/[0|o]-[0|o]/i.test(move)
			 && !/[0|o]-[0|o]-[0|o]/i.test(move);
}
/**
 *	This will "normalize" the pgn file by removing line breaks, collapsing strings of
 *	spaces, and replacing the symbols for moves with the characters (!, ?, etc.)
 */
Pgn.prototype.normalize = function(pgn) {
	pgn = pgn.replace(/\n/g," ");
{'fraction_non_alphanumeric': 0.1684295240285123, 'fraction_numerical': 0.017360312715566796, 'mean_word_length': 2.970333181195801, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1133713', 'n_tokens_mistral': 3638, 'n_tokens_neox': 3169, 'n_words': 856}