首页 > 解决方案 > R中的正则表达式将单词与杂乱语音数据中的语音注释进行匹配

问题描述

我有带有语音注释的相当混乱的语音数据,例如:

data <- c("hi Greg (1.57) dropped your €johnnies¿",
          "[ARF   ] what's prostitute?",
          "°wanna (get in) the €dri::ve°",
          "my €go::d I can't get out here",
          "Sco:::[       :tt↑   ]",
          "↑where have you bee:::n",
          "j's (.) lie there ((inhales sharply)) breathe",
          "well↓ i can't feel [   any?  ]")

我想提取那些包含某些音标的单词标记,特别是“€”、“°”、“↑”、“↓”、“:”和具有多个大写字母的标记。提取的“语法”是这样,但模式对我来说太复杂了,因为音标可以出现在单词之前、单词内部和单词之后。

pattern <- ""
extract <- function(x) unlist(regmatches(x, gregexpr(pattern, x, perl = T)))
result <- extract(data)

预期的结果是这样的:

> result
 [1] "€johnnies¿" "ARF" "°wanna" "€dri::ve°" "€go::d" "Sco::::tt↑" "↑where"  "bee:::n" "well↓" "feel↑"

有任何想法吗?

标签: rregex

解决方案


您可能会考虑在问题的第一部分使用类似的东西,尽管您可以首先进行预处理以删除 [ ] 中的空格,就像在这种情况下Sco:::[ :tt↑ ]

[\s"]([€°↑↓:\w]*[€°↑↓:]+[€°↑↓:\w¿]*)[\s"]

https://regex101.com/r/nQVddI/2

编辑:

对于匹配超过 2 个大写字母的使用([A-Z]{2,}),我看不到混合字母的示例,比如AsDfG它也应该匹配吗?

两种情况:https ://regex101.com/r/nQVddI/3


推荐阅读