About

This page demonstrates text file processing in R using regular expressions.

Problem Statement

A text file with chess tournament results is given. Generates a .CSV file with the following information for all of the players: Player’s Name, Player’s State, Total Number of Points, Player’s Pre-Rating, and Average Pre Chess Rating of Opponents

Solution Approach

Extract the required details from the text file using regular expression. Process the pair of rows that provides the details for a player and store the extracted detail into a row of dataframe. Then, construct the details required for the output CSV file using dataframe operations. Write the output file.

Solution

Load required libraries

library(RCurl)
## Loading required package: bitops
library(stringr)
library(knitr)

Function to parse pair of line

parse_lines=function(line1_string,line2_string){
  #Reg exp for (id)|(name)|(pts)|(game_string)
  line_one_pattern="(\\d+).+?\\|(.+?)\\|(\\d*\\.?\\d*).+?\\|(.+)"
  #Get required text using Reg exp
  parse1=str_match_all(line1_string,line_one_pattern)[[1]]
 
  #Convert and Store text in dataframe
  value=data.frame(id=as.integer(parse1[2]),name=str_trim(parse1[3]),pts=as.numeric(parse1[4]))
  game_string=parse1[5]
  #Pattern of game details  
  game_pattern="\\D +(\\d*)\\|"
  #Get the opponent list
  opponent_list=as.numeric(str_match_all(game_string,game_pattern)[[1]][,2])
  #Add each opponent as column
  value=cbind(value,matrix(unlist(opponent_list),nrow=1))
  #Name opponent columns properly
  colnames(value)[4:10]=paste0("opponent.",1:7)
  
  #Let's process second line
  #Second line pattern
  line_two_pattern=" +(\\D+).+?\\|.+?: +(\\d+).+"
  #Get required value using reg Exp
  parse2=str_match_all(line2_string,line_two_pattern)[[1]]
  #Add state and pre tournament point of the player
  value=cbind(value,state=parse2[2],pre_point=as.numeric(parse2[3]))
  #Done
  return(value)
}

Vectorize the function

parse_lines_all=Vectorize(parse_lines,SIMPLIFY = F)

Function that computes opponent score

avg_opp_rating=function(x_opp,all_x) round(mean(all_x$pre_point[all_x$id %in% x_opp],na.rm=T))

Main Process

Read the file and call our parse function

#Get file data from Github
file_content=getURL("https://raw.githubusercontent.com/mkds/MSDA/master/IS607/data/tournamentinfo.txt")
#Read file data into a dataframe
file_text=read.table(text=file_content,sep="\n",stringsAsFactors = F,skip=4)
#Process in block of three
block_start=seq(1,nrow(file_text),3)
#Pass pair of rows to our parse function and append return values
parsed_value=do.call("rbind",parse_lines_all(file_text[block_start,],file_text[block_start+1,]))
rownames(parsed_value)=NULL

#Compute opponent score
parsed_value$avg_opp_rating=apply(parsed_value[,4:10],1,avg_opp_rating,all_x=parsed_value)
#Select the columns 
output_file=parsed_value[c("name","state","pts","pre_point","avg_opp_rating")]
# Ta da...
kable(output_file)
name state pts pre_point avg_opp_rating
GARY HUA ON 6.0 1794 1605
DAKSHESH DARURI MI 6.0 1553 1469
ADITYA BAJAJ MI 6.0 1384 1564
PATRICK H SCHILLING MI 5.5 1716 1574
HANSHI ZUO MI 5.5 1655 1501
HANSEN SONG OH 5.0 1686 1519
GARY DEE SWATHELL MI 5.0 1649 1372
EZEKIEL HOUGHTON MI 5.0 1641 1468
STEFANO LEE ON 5.0 1411 1523
ANVIT RAO MI 5.0 1365 1554
CAMERON WILLIAM MC LEMAN MI 4.5 1712 1468
KENNETH J TACK MI 4.5 1663 1506
TORRANCE HENRY JR MI 4.5 1666 1498
BRADLEY SHAW MI 4.5 1610 1515
ZACHARY JAMES HOUGHTON MI 4.5 1220 1484
MIKE NIKITIN MI 4.0 1604 1386
RONALD GRZEGORCZYK MI 4.0 1629 1499
DAVID SUNDEEN MI 4.0 1600 1480
DIPANKAR ROY MI 4.0 1564 1426
JASON ZHENG MI 4.0 1595 1411
DINH DANG BUI ON 4.0 1563 1470
EUGENE L MCCLURE MI 4.0 1555 1300
ALAN BUI ON 4.0 1363 1214
MICHAEL R ALDRICH MI 4.0 1229 1357
LOREN SCHWIEBERT MI 3.5 1745 1363
MAX ZHU ON 3.5 1579 1507
GAURAV GIDWANI MI 3.5 1552 1222
SOFIA ADINA STANESCU-BELLU MI 3.5 1507 1522
CHIEDOZIE OKORIE MI 3.5 1602 1314
GEORGE AVERY JONES ON 3.5 1522 1144
RISHI SHETTY MI 3.5 1494 1260
JOSHUA PHILIP MATHEWS ON 3.5 1441 1379
JADE GE MI 3.5 1449 1277
MICHAEL JEFFERY THOMAS MI 3.5 1399 1375
JOSHUA DAVID LEE MI 3.5 1438 1150
SIDDHARTH JHA MI 3.5 1355 1388
AMIYATOSH PWNANANDAM MI 3.5 980 1385
BRIAN LIU MI 3.0 1423 1539
JOEL R HENDON MI 3.0 1436 1430
FOREST ZHANG MI 3.0 1348 1391
KYLE WILLIAM MURPHY MI 3.0 1403 1248
JARED GE MI 3.0 1332 1150
ROBERT GLEN VASEY MI 3.0 1283 1107
JUSTIN D SCHILLING MI 3.0 1199 1327
DEREK YAN MI 3.0 1242 1152
JACOB ALEXANDER LAVALLEY MI 3.0 377 1358
ERIC WRIGHT MI 2.5 1362 1392
DANIEL KHAIN MI 2.5 1382 1356
MICHAEL J MARTIN MI 2.5 1291 1286
SHIVAM JHA MI 2.5 1056 1296
TEJAS AYYAGARI MI 2.5 1011 1356
ETHAN GUO MI 2.5 935 1495
JOSE C YBARRA MI 2.0 1393 1345
LARRY HODGE MI 2.0 1270 1206
ALEX KONG MI 2.0 1186 1406
MARISA RICCI MI 2.0 1153 1414
MICHAEL LU MI 2.0 1092 1363
VIRAJ MOHILE MI 2.0 917 1391
SEAN M MC CORMICK MI 2.0 853 1319
JULIA SHEN MI 1.5 967 1330
JEZZEL FARKAS ON 1.5 955 1327
ASHWIN BALAJI MI 1.0 1530 1186
THOMAS JOSEPH HOSMER MI 1.0 1175 1350
BEN LI MI 1.0 1163 1263

Output File

Write to File. File Link https://raw.githubusercontent.com/mkds/MSDA/master/IS607/data/chess_output.csv

write.table(output_file,"chess_output.csv",col.names=F,row.names = F,sep=",")