---
title: "regex"
subtitle: "Ch. 15"
format: 
  html:
    code-fold: TRUE
toc: TRUE
theme: flatly
warning: FALSE
message: FALSE
---

### exercises with babynames and regular expressions

```{r}
library(babynames)
library(tidyverse)
library(scales)
babynames |> 
       mutate(
            first = str_sub(name, 1, 1),
             last = str_sub(name, -1, -1)
         ) |> 
  count(year, first, wt = n, sort = TRUE) |>
  filter(n > 2e+03) |>
      ggplot(aes(x = year, y = n, 
                 color = first, 
                 group = first)) + geom_line()   + 
  scale_y_continuous(labels = label_comma())  + 
  labs(title="Babynames: Distribution of First Letters of First")
```

```{r}
babynames |> 
  mutate(
    first = str_sub(name, 1, 1),
    last = str_sub(name, -1, -1)
  ) |> group_by(last, wt = n) |>
  mutate(r = row_number()) |> 
  slice_head(n = 2) |> 
  ggplot(aes(x = year, y = last)) + geom_hex() + 
  labs(title="Babynames: Distribution of First Letters of Last Name Over Time")
```


### What baby name has the most vowels? 
```{r}
babynames |> 
  mutate(
    vowels = str_count(name, "[^aeiouAEIOU]"), 
    .keep = "used") |> 
  arrange(-vowels)
```

### What name has the highest proportion of vowels? (Hint: what is the denominator?)
```{r}
babynames |> 
  mutate(
    vowels = str_count(name, "[^aeiouAEIOU]"), 
    length = str_length(name),
    prop = vowels/length,
    .keep = "used") |> 
  arrange(-vowels, -length, prop)
```

### Replace all forward slashes in "a/b/c/d/e" with backslashes. 
```{r}
s <- str_replace_all("a/b/c/d/e","/","\\\\")
print(s)
str_view(s)
```

### Puzzle

What happens if you attempt to undo the transformation by replacing all backslashes with forward slashes? (We’ll discuss the problem very soon.)

```{r}
str_replace_all(s,"\\\\","/")
```

#### Implement a simple version of str_to_lower() using str_replace_all().
```{r}
str_replace_all("JAY ENJOYS ISLANDS OF UBER","[A-Z]",tolower)
```

#### Create a regular expression that will match telephone numbers.

```{r}
a <- "423-687-4291"
str_view(a,"[0-9]?+-\\d+-\\d+-\\d+")
```

#### How would you match the literal string "'\? How about "$^$"?

#### Explain why each of these patterns don’t match a \: "\", "\\", "\\\".

#### Given the corpus of common words in stringr::words, create regular expressions that find all words that:

1. Start with “y”.
2. Don’t start with “y”.
3. End with “x”.
4. Are exactly three letters long. (Don’t cheat by using str_length()!)
5. Have seven letters or more.
6. Contain a vowel-consonant pair.
7. Contain at least two vowel-consonant pairs in a row.
8. Only consist of repeated vowel-consonant pairs.
9. Write a function that mixes up words into a bad poem.

```{r}
# so silly
w <- tibble(words)

a <- {}
l <- function(.x, .y){
  for (i in c(1:.y)){
  a[i] <- slice_sample(w,n=.x) |> pull() |> str_flatten_comma() |>
    str_replace_all(","," ") 
  a[i] <- str_c(a[i],"  \\n")
  }
  return(str_view(a))
}

l(4,4)

l(3)
```

A Haiku has 3 lines, of with a 5,7,5 syllable pattern.  Use regex to make a Haiku below.  

1. First build a syllable checker
```{r echo = FALSE}
# syllable checker
words |> sample(1) -> a; print(a); str_view(a,"[aeiouy]+[^aeiouy]+")
words |> sample(1) -> a; print(a); str_count(a,"[aeiouy]+[^aeiouy]+")
```

2. Then run your syllable checker on randomly selected words until you have 5,7,5 syllables.


```{r}
# Haiku
a <- {}
haiku <- function(){
  s <- c(5,7,5)
  for (i in c(1:3)){
  a[i] <- slice_sample(w,n=s[i]) |> pull() |> str_flatten_comma() |>
    str_replace_all(","," ") 
  a[i] <- str_c(a[i],"  \\n")
  }
  return(str_view(a))
}
```

