Created
July 4, 2018 13:09
-
-
Save rasmusab/aee350a217d742204707f7afd5ef9ed4 to your computer and use it in GitHub Desktop.
A barely tested R script that takes a backgammon match in any format gnubg can read, analyzes the match and returns a data frame with the analysis
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# A barely tested R script that takes a backgammon match in any format | |
# gnubg can read, analyzes the match and returns a data frame with the analysis | |
# It requires that gnubg is readily available on the command line. | |
library(tidyverse) | |
library(jsonlite) | |
library(glue) | |
analyze_bg_match <- function(match_fname, match_format = "auto") { | |
gnubg_analysis_fname = tempfile() | |
python_analyze_match <- glue(trim(" | |
import json | |
import gnubg | |
import sys | |
gnubg.command('import {match_format} \"{match_fname}\"') | |
gnubg.command('analyze match') | |
match = gnubg.match() | |
outfile = open('{gnubg_analysis_fname}', 'w') | |
json.dump(match, outfile) | |
#json.dump(match, outfile, sort_keys=True, indent=4, separators=(',', ': ')) | |
outfile.close() | |
gnubg.command('show analysis')")) | |
temp_python_code_fname = tempfile() | |
write_file(python_analyze_match, temp_python_code_fname) | |
gnubg_stdout <- system2("gnubg", glue('-t -q --python="{temp_python_code_fname}"'), stdout = TRUE) | |
gnubg_stdout <- paste(gnubg_stdout, collapse = "\n") | |
match <- fromJSON(gnubg_analysis_fname, simplifyMatrix = FALSE, simplifyVector = TRUE, simplifyDataFrame = TRUE, flatten = TRUE) | |
match$gnubg_stdout <- gnubg_stdout | |
match | |
} | |
# Example usage: | |
path_to_bg_match_file <- "Razz - XG-Champion 5 point match Jun 26 2018.txt" | |
match <- analyze_bg_match(path_to_bg_match_file) | |
# match is now a deeply nested list with all the analysis from gnubg |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment