[英]Extract matches in a song in R
I'm using R to extract sentences that contain specific words ("everything", "along", "wrote") from the lyrics of a song and here is the song: Yellow- Coldplay我正在使用 R 从一首歌的歌词中提取包含特定单词(“一切”、“沿”、“写”)的句子,这是这首歌:Yellow-Coldplay
Look at the stars Look how they shine for you And everything you do Yeah, they were all yellow I came along I wrote a song for you And all the things you do And it was called Yellow So, then I took my turn What a thing to've done And it was all yellow Your skin Oh yeah, your skin and bones Turn in to something beautiful Do you know You know I love you so You know I love you so I swam across I jumped across for you What a thing to do 'Cause you were all yellow I drew a line看星星 看它们为你发光 你所做的一切 是的,它们都是黄色的 我来了 我为你写了一首歌 你所做的所有事情 它被称为黄色 所以,然后轮到我了 什么东西完成 全是黄色 你的皮肤 哦,是的,你的皮肤和骨头 变成美丽的东西 你知道吗 你知道我爱你 所以你知道我爱你 所以我游过去 我为你跳过去因为你都是黄色的我画了一条线
Create a vector with the letter, but it does not compile用字母创建一个向量,但它不编译
Here's an option using tidyverse
.这是使用
tidyverse
的选项。 It's not perfect and you'll have to adapt to your specific use case:它并不完美,您必须适应您的特定用例:
lyrics <- data.frame(yellow = "Look at the stars Look how they shine for you And everything you do Yeah, they were all yellow I came along I wrote a song for you And all the things you do And it was called Yellow So, then I took my turn What a thing to've done And it was all yellow Your skin Oh yeah, your skin and bones Turn in to something beautiful Do you know You know I love you so You know I love you so I swam across I jumped across for you What a thing to do 'Cause you were all yellow I drew a line")
library(tidyverse)
lyrics %>%
mutate(yellow = gsub('([[:upper:]])', '<>\\1', yellow)) %>%
separate_rows(yellow, sep = "<>") %>%
mutate(flag = str_detect(yellow, "everything|along|wrote")) %>%
filter(flag == T)
This gives us:这给了我们:
# A tibble: 3 x 2
yellow flag
<chr> <lgl>
1 "And everything you do " TRUE
2 "I came along " TRUE
3 "I wrote a song for you " TRUE
You have to figure out: What constitutes a sentence?你必须弄清楚:什么是句子? I counted a new sentence when there was capitalization.
有大写的时候我数了一个新的句子。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.