Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save brunaw/bc9c5786e5634b35d393d3daaf74aa18 to your computer and use it in GitHub Desktop.
Save brunaw/bc9c5786e5634b35d393d3daaf74aa18 to your computer and use it in GitHub Desktop.
eliminate_words <- function(first_sentence, second_sentence){
if(is.na(second_sentence)) return(first_sentence)
first_word <- word(second_sentence, 1)
characters_first_word <- strsplit(first_word, split = "")[[1]]
# Get all possible words that could have remained in the previous
# sentence
words_list <- vector()
for(i in 1:length(characters_first_word)) {
words_list[i] <- paste(characters_first_word[1:i], collapse = "")
}
# Eliminate them
first_sentence <- str_split(first_sentence, " ")[[1]]
if(first_sentence[length(first_sentence)] %in% words_list){
first_sentence <- first_sentence[-length(first_sentence)]
}
first_sentence <- paste0(first_sentence, collapse = " ")
return(first_sentence)
}
chords.net %>%
mutate(
second_sentence = lead(lyric, n = 1)) %>%
rowwise() %>%
mutate(
new_lyric =
eliminate_words(first_sentence = lyric,
second_sentence = second_sentence)) %>%
select(-second_sentence) %>%
View()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment