Maybe write an explanation of the solution approach here
# separate into rules and messagesr_m <- r_m %>%mutate(rule =str_detect(V1, ":"))# get those objectsrules <-filter(r_m, rule ==TRUE) %>%select(-rule)messages <-filter(r_m, rule ==FALSE) %>%select(-rule) # parse some rulesrule_sep <- rules %>%separate(V1, into =c("rule_num", "rule_rule"), sep =": ") %>%arrange(rule_num)# make a copyrule_sub <- rule_sep %>%mutate(done =FALSE)# apparently we need a function to apply the substitution# otherwise doesn't pick up boundaries properlyreplace_func <-function(my_list, patt, repl) {str_replace_all(my_list, patt, repl)}
loop
# run repeatedly belowwhile (any(rule_sub$done ==FALSE)) {# i think we want to replace when there are all letters in the rule# if there are numbers then that's not a good replacement rule_sub <- rule_sub %>%mutate(any_num =str_detect(rule_rule, "\\d"))# get the rules we want to change rr <- rule_sub %>%filter(any_num ==FALSE& done ==FALSE)# now make the changesfor (z inseq_len(nrow(rr))) {# brackets for the rules with OR pipe patt =paste0("\\b", rr$rule_num[[z]], "\\b") repl =ifelse(str_detect(rr$rule_rule[[z]], "\\|"), paste0("(", rr$rule_rule[[z]], ")"), rr$rule_rule[[z]])# apply the rules with map function and update done rule_sub <- rule_sub %>%mutate(rule_rule =map(rule_rule, replace_func, patt, repl), done = done |str_detect(rule_num, patt)) }}
next
rule_sub <- rule_sub %>%mutate(rule_rule =str_remove_all(rule_rule, " "), rule_rule =paste0("^", rule_rule, "$"))# part one: just check rule zerorule_0 <- rule_sub %>%filter(rule_num =="0") %>%select(rule_rule) %>%pull(rule_rule)# ah goodness it is huge
answer
# let the computer handle the matchespart_one_df <- messages %>%mutate(matcher =str_detect(V1, rule_0))sum(part_one_df$matcher)
[1] 198
part two
oh hey just totally mess with things and make sure it is weird
# same as part one# run repeatedly belowwhile (any(rule_sub$done ==FALSE)) {# i think we want to replace when there are all letters in the rule# if there are numbers then that's not a good replacement rule_sub <- rule_sub %>%mutate(any_num =str_detect(rule_rule, "\\d"))# get the rules we want to change rr <- rule_sub %>%filter(any_num ==FALSE& done ==FALSE)# now make the changesfor (z inseq_len(nrow(rr))) {# brackets for the rules with OR pipe patt =paste0("\\b", rr$rule_num[[z]], "\\b") repl =ifelse(str_detect(rr$rule_rule[[z]], "\\|"), paste0("(", rr$rule_rule[[z]], ")"), rr$rule_rule[[z]])# apply the rules with map function and update done rule_sub <- rule_sub %>%mutate(rule_rule =map(rule_rule, replace_func, patt, repl), done = done |str_detect(rule_num, patt)) }}
answer
rule_sub <- rule_sub %>%mutate(rule_rule =str_remove_all(rule_rule, " "), rule_rule =paste0("^", rule_rule, "$"))# get the rule we want to checkrule_0 <- rule_sub %>%filter(rule_num =="0") %>%select(rule_rule) %>%pull(rule_rule)# let the computer handle the matchespart_two_df <- messages %>%mutate(matcher =str_detect(V1, rule_0))sum(part_two_df$matcher)