-
Notifications
You must be signed in to change notification settings - Fork 0
/
pgn.R
65 lines (57 loc) · 1.73 KB
/
pgn.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
source("global.R")
variants <- list.files(global$path) %>%
str_subset(global$dump) %>%
str_extract("db_[[:alnum:]]*") %>%
str_remove_all("db_") %>%
unique()
pgn <- foreach(i = variants, .combine = rbind) %do% {
Sys.time() %>% paste("importing", i) %>% message()
list.files(global$path, full.names = T) %>%
str_subset(global$dump) %>%
str_subset("csv$") %>%
str_subset(i) %>%
read.csv() %>%
data.table()
}
pgn[is.na(Variant)]$Variant <- "standard"
pgn <-
mutate(
pgn,
id = str_extract(Site, "[[:alnum:]]{8}$"),
utc = paste(UTCDate, UTCTime) %>%
strptime(format = "%Y.%m.%d %H:%M:%S", tz = "UTC") %>% as.numeric(),
white_title = ifelse(WhiteTitle == "", "none", WhiteTitle) %>% tolower(),
black_title = ifelse(BlackTitle == "", "none", BlackTitle) %>% tolower(),
termination = str_extract(Termination, "[[:alpha:]]*$") %>% tolower(),
control = str_extract(Event, "Rated [[:alpha:]]*") %>%
str_remove("Rated ") %>% tolower(),
variant = ifelse(
is.na(Variant),
"standard",
str_remove_all(Variant, "[^[:alnum:]]")
) %>% tolower()
) %>%
select(
id,
utc,
white = White,
white_rating = WhiteElo,
white_title,
white_diff = WhiteRatingDiff,
black = Black,
black_rating = BlackElo,
black_title,
black_diff = BlackRatingDiff,
result = Result,
termination,
clock = TimeControl,
control,
variant
)
controls <- pgn[variant == "standard", c("control", "clock")] %>%
distinct()
controls <- distinct(pgn[, "clock"]) %>%
left_join(controls, by = "clock") %>%
mutate(control = ifelse(is.na(control), "classical", control))
pgn <- left_join(pgn[, -c("control")], controls, by = "clock")
save(pgn, file = "pgn.RData")