【问题标题】:identify words within a phrase and code as 0 or 1将短语和代码中的单词识别为 0 或 1
【发布时间】:2021-04-29 18:53:40
【问题描述】:

我正在处理儿童所说的话语和陈述。从每个话语中,如果语句中的一个或多个单词与多个“核心”单词(可能是 300 个单词)的预定义列表匹配,那么我想在“核心”中输入“1”(如果没有,则输入“0”进入“核心”)。

同样,如果语句中有一个或多个单词匹配不同的预定义“边缘”单词列表(可能是 300 个边缘单词;同样与核心单词不同),那么我想输入“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

提前致谢。我搜索了档案,但很难找到与我的情况相对应的解决方案。

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))

【问题讨论】:

  • 你能显示sn-p的预期输出吗

标签: r text match


【解决方案1】:

tidyverse 选项可能是:

library(dplyr)
library(stringr)

coreWords <- c('small', 'bed')
fringeWords <- c('head', 'night')

df %>%
  mutate(Core = + str_detect(Utterance, str_c(coreWords, collapse = '|')),
         Fringe = + str_detect(Utterance, str_c(fringeWords, collapse = '|')))

#                               Utterance Core Fringe
# 1                                 small    1      0
# 2                                 small    1      0
# 3                       where's his bed    1      0
# 4                       there's his bed    1      0
# 5                       there's his bed    1      0
# 6                      is that a pillow    0      0
# 7              what is that on his head    0      1
# 8         hey he has his arm stuck here    0      0
# 9                      there there's it    0      0
# 10      now you're gonna go night_night    0      1
# 11 and that's the thing you can turn on    0      0
# 12           yeah where's the music+box    0      0

【讨论】:

  • 事实证明,我不会有一个边缘词列表。相反,如果有任何附加词不是核心词,我需要输入“1”作为边缘词。换句话说,我将有一个核心词列表,而不是边缘词列表——任何不是核心的都将被视为边缘词。如果只有核心词,则边缘将等于“0”。是否可以调整边缘的脚本来做到这一点?
【解决方案2】:

这里有可能解决您的问题的快速方法(尽管我确信还有更优雅的解决方案)...

df <- 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))

## Define object with all the core terms:

CorePatterns <- c("his", "music", "turn")

## Define value of `df$Core` as `1` if `df$Utterance` 
## contains one of the patterns in `CorePatterns`, 
## otherwise, define it as `0`:

df$Core <- ifelse(grepl(paste(CorePatterns, collapse = "|"), 
                        df$Utterance), 
                  1, 0)

df

                              Utterance Core Fringe
> 1                                 small    0     NA
> 2                                 small    0     NA
> 3                       where's his bed    1     NA
> 4                       there's his bed    1     NA
> 5                       there's his bed    1     NA
> 6                      is that a pillow    0     NA
> 7              what is that on his head    1     NA
> 8         hey he has his arm stuck here    1     NA
> 9                      there there's it    0     NA
> 10      now you're gonna go night_night    0     NA
> 11 and that's the thing you can turn on    1     NA
> 12           yeah where's the music+box    1     NA

您可以对 Fringe 数据执行相同操作。

【讨论】:

    猜你喜欢
    • 2021-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多