detect_foul.R 1.25 KB
Newer Older
SuperTikuwa's avatar
SuperTikuwa committed
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
detect_foul <- function(path) {
  rcl <- path |> readr::read_lines()

  foul_actions <- c()

  count <- 1

  for (i in rcl) {
    str <- stringr::str_extract(i, "foul_\\w+_(l|r)")
    if (!is.na(str)) {
      foul_actions <- c(foul_actions, rcl[count - 1])
    }
    count <- count + 1
  }

  foul_actions <- foul_actions |>
    tibble::as_tibble() |>
    dplyr::mutate(
      step = value |> stringr::str_extract("\\d+") |> as.numeric(),
      agent = value |> stringr::str_extract("\\w+_([0-9]{1,2}|Coach)"),
      team = agent |> stringr::str_remove("_([0-9]{1,2}|Coach)"),
      unum = agent |> stringr::str_extract("([0-9]{1,2}|Coach)$"),
      commands = value |>
        stringr::str_extract("\\(.+\\)$") |>
        purrr::map(~ .x |>
          stringr::str_split("\\(|\\)", simplify = TRUE) |>
          stringr::str_trim() |>
          purrr::discard(~ .x == "")),
    ) |>
    tidyr::unnest(commands) |>
    dplyr::mutate(
      commands = commands |> stringr::str_split("\\ ", n = 2),
      command  = commands |> purrr::map_chr(1),
      args     = commands |> purrr::map(~ .x[-1]),
    ) |>
    dplyr::select(
      step,
      team,
      unum,
      command,
      args,
      # line = value,
    ) |>
    dplyr::filter(command == "tackle")

  return(foul_actions)
}