提问人:Alberto Llamas 提问时间:2/23/2023 最后编辑:Alberto Llamas 更新时间:2/23/2023 访问量:41
在 R 中使用 tau 包中的 textcnt 函数保留撇号
keeping the apostrophe using the textcnt function from the tau package in R
问:
R 的 tau 包中的 textcnt 函数有一个 split 参数,它的默认值是 split = “[[:space:][:p unct:][:d igit:]]+” ç 这个 argumet 也使用撇号 ' 拆分成单词,我不想这样,我怎样才能修改参数,使其不使用撇号来拆分单词?
此代码:
'图书馆(TAU) text<-“我不希望函数使用'来拆分”
textcnt(text, split = “[[:空格:][:p unct:][:d igit:]]+”,method=“string”,n=1L)'
生成以下输出:
don function i split t the to use want
1 1 1 1 1 2 2 1 1
我不想有 don 1 和 t 1,而是保留 don't as 1 字
我曾尝试使用stringr中的str_replace_all来事先删除标点符号,然后省略textcnt中参数的标点符号部分,但是它没有使用所有类型的符号,例如&>或“来拆分,我试图修改拆分参数,但随后它根本不会拆分句子或保留符号
谢谢
答:
0赞
Wiktor Stribiżew
2/23/2023
#1
使用基于 PCRE 的功能,您需要使用
split = "(?:(?!')[[:space:][:punct:][:digit:]])+|'\\B|\\B'"
这里
(?:
- 容器非捕获组的开始:(?!')
- 如果下一个字符是字符,则匹配失败'
[[:space:][:punct:][:digit:]]
- 匹配空格、标点符号或数字字符)+
- 匹配一次或多次(连续)'\B
- 一个 char,后面跟着字符串的末尾或非单词 char'
|
-或\B'
- a 前面有字符串开头或非单词字符。'
使用函数,您可以使用stringr
split = "[[:space:][:punct:][:digit:]--[']]+|'\\B|\\B'"
在这里,匹配除字符之外的所有匹配字符。[[:space:][:punct:][:digit:]--[']]
[[:space:][:punct:][:digit:]]
'
stringr
ICU 正则表达式风格支持使用此表示法进行字符类减法。
评论
0赞
Alberto Llamas
2/23/2023
显示以下错误 FUN(X[[i]], ...) 中的错误:无效的正则表达式 '[[:space:][:p unct:][:d igit:]--[']]+' 此外: 警告消息:在 FUN(X[[i]], ...) 中:PCRE 模式编译错误“字符类中的范围无效”在“--[']]+'
0赞
Wiktor Stribiżew
2/23/2023
@AlbertoLlamas 然后使用 PCRE 版本。
0赞
Alberto Llamas
2/23/2023
这奏效了,thak you split = “(?:(?!')[[:空格:][:p unct:][:d igit:]])+"
0赞
Alberto Llamas
2/23/2023
在更复杂的情况下,它会在一个单词旁边加上 al ' 和 “”,你认为有没有一种方法可以只保留字母之间的 ',例如保留 Ihave 而不是 'I 或 “no” 等等,谢谢
0赞
Wiktor Stribiżew
2/23/2023
@AlbertoLlamas尝试"(?:(?!')[[:space:][:punct:][:digit:]])+|'\\B|\\B'"
上一个:FFmpeg 的撇号问题
评论
"[[:space:][:punct:][:digit:]]+"
'