【发布时间】:2021-08-05 10:49:55
【问题描述】:
注意:关于此的更新/新问题始于 =====================
原帖:我正在处理儿童所说的话语和陈述。从每个话语中,如果语句中的一个或多个单词与多个“核心”单词(可能是 300 个单词)的预定义列表匹配,那么我想在“核心”中输入“1”(如果没有,则输入“0”进入“核心”)。
如果语句中有一个或多个词不是核心词,那么我想在'Fringe'中输入'1'(如果只有核心词而没有额外的,那么在'中输入'0'边缘')。
基本上,现在我只有话语,从这些话语中,我需要确定是否有任何单词与核心单词匹配,如果有任何额外的单词,则将它们识别为边缘。这是我的数据的 sn-p。
Core Fringe Utterance
1 NA NA small
2 NA NA small
3 NA NA where's his bed
4 NA NA there's his bed
5 NA NA there's his bed
6 NA NA is that a pillow
感谢来自原始帖子的 rjen,下面的代码将使我能够识别核心词和边缘词——假设我知道边缘词(这是我最初预期的)。然而,挑战已经改变,所以现在任何不是核心的东西都将被视为边缘。所以基本上我需要保持从列表中检测单词并将其定义为核心的能力,但我还需要能够搜索话语,如果话语中有任何不是核心的单词,将其识别为“1”因为我不会列出边缘词。
library(dplyr)
library(stringr)
coreWords <- c('small', 'bed')
fringeWords <- c('head', 'his')
CFdataNew <- CFdata %>%
mutate(Core = + str_detect(Utterance, str_c(coreWords, collapse = '|')),
Fringe = + str_detect(Utterance, str_c(fringeWords, collapse = '|')))
dput() 代码是:
structure(list(Utterance = c("small", "small", "where's his bed", "there's his bed", "there's his bed", "is that a pillow", "what is that on his head", "hey he has his arm stuck here", "there there's it", "now you're gonna go night_night", "and that's the thing you can turn on", "yeah where's the music+box"), Core = c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA), Fringe = c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA)), class = "data.frame", row.names = c(NA, -12L))
====================
更新 rjen 的响应对示例数据非常有效。然而,我现在有一个核心单词和话语的“真实”列表,并不是所有额外的单词都被认为是边缘单词,即使它们不是,也有一些单词被识别为核心单词。我将这些词用双引号括起来,看看这是否有助于解决问题,以便核心词是明确的,但它没有。
coreWords <-c("I", "no", "nah", "nope", "yes", "yeah", "yea", "mmhmm", "yah",
"ya", "un-huh", "uhhuh", "my", "the", "want", "is", "it", "that",
"a", "go", "mine", "you", "what", "on", "in", "here", "more",
"out", "off", "some", "help", "all done", "finished")
Df1 <- df %>%
mutate(id = row_number()) %>%
separate_rows(Utterance, sep = ' ') %>%
mutate(Core = + str_detect(Utterance, str_c(coreWords, collapse = '|')),
Fringe = + !Core) %>%
group_by(id) %>%
mutate(Core = + (sum(Core) > 0),
Fringe = + (sum(Fringe) > 0)) %>%
slice(1) %>%
select(-Utterance) %>%
left_join(df) %>%
ungroup() %>%
select(Utterance, Core, Fringe, id)
上面脚本的输出和更长的核心词列表看起来像这样。
# A tibble: 98 x 4
Utterance Core Fringe id
<chr> <int> <int> <int>
1 a baby 1 0 1
2 small 1 0 2
3 yes 1 0 3
4 where's his bed 1 1 4
5 there's his bed 1 1 5
6 where's his pillow 1 1 6
7 what is that on his head 1 0 7
8 hey he has his arm stuck here 1 1 8
9 there there's it 1 0 9
10 now you're gonna go night-night 1 1 10
# ... with 88 more rows
例如,在第 1 行中,“a”是核心词,因此“1”代表核心是正确的。但是,“婴儿”应该被选为边缘,所以边缘应该是“1”,而不是“0”。第 7 行和第 9 行也有应该被识别为边缘但不是的单词。
此外,如果话语中包含核心词的一部分,它似乎会被计算在内。例如,“small”被识别为核心词,即使它不是(但“all done”是一个核心词)。 “他的床在哪里”被识别为核心和边缘,尽管没有一个词是核心。非常感谢您对正在发生的事情以及如何纠正它提出任何建议。
【问题讨论】: