gpt4 book ai didi

r - 文本挖掘 R 包和正则表达式来处理替换智能 curl 引号

转载 作者:行者123 更新时间:2023-12-04 11:05:58 26 4
gpt4 key购买 nike

我有一堆像下面这样的文本,带有不同的智能引号 - 用于单引号和双引号。我所知道的包最终只能删除这些字符,但我希望将它们替换为普通引号。

textclean::replace_non_ascii("You don‘t get “your” money’s worth")

收到输出: "You dont get your moneys worth"
预期输出: "You don't get "your" money's worth"
如果有人能用正则表达式一次性替换所有这样的引号,也将不胜感激。

谢谢!

最佳答案

用两个 gsub操作:1)替换双引号,2)替换单引号:

> gsub("[“”]", "\"", gsub("[‘’]", "'", text))
[1] "You don't get \"your\" money's worth"

online R demo .在 Linux 和 Windows 上都经过测试,并且工作原理相同。
[“”]构造为正 character class匹配类中定义的任何单个字符。

要规范化类似于双引号的所有字符,您可能需要使用
> sngl_quot_rx = "[ʻʼʽ٬‘’‚‛՚︐]"
> dbl_quot_rx = "[«»““”„‟≪≫《》〝〞〟\"″‶]"
> res = gsub(dbl_quot_rx, "\"", gsub(sngl_quot_rx, "'", `Encoding<-`(text, "UTF8")))
> cat(res, sep="\n")
You don't get "your" money's worth

在这里, [«»““”„‟≪≫《》〝〞〟"″‶]火柴
«   00AB  LEFT-POINTING DOUBLE ANGLE QUOTATION MARK
» 00BB RIGHT-POINTING DOUBLE ANGLE QUOTATION MARK
“ 05F4 HEBREW PUNCTUATION GERSHAYIM
“ 201C LEFT DOUBLE QUOTATION MARK
” 201D RIGHT DOUBLE QUOTATION MARK
„ 201E DOUBLE LOW-9 QUOTATION MARK
‟ 201F DOUBLE HIGH-REVERSED-9 QUOTATION MARK
≪ 226A MUCH LESS-THAN
≫ 226B MUCH GREATER-THAN
《 300A LEFT DOUBLE ANGLE BRACKET
》 300B RIGHT DOUBLE ANGLE BRACKET
〝 301D REVERSED DOUBLE PRIME QUOTATION MARK
〞 301E DOUBLE PRIME QUOTATION MARK
〟 301F LOW DOUBLE PRIME QUOTATION MARK
" FF02 FULLWIDTH QUOTATION MARK
″ 2033 DOUBLE PRIME
‶ 2036 REVERSED DOUBLE PRIME
[ʻʼʽ٬‘’‚‛՚︐]用于规范一些类似于单引号的字符:
ʻ  02BB  MODIFIER LETTER TURNED COMMA
ʼ 02BC MODIFIER LETTER APOSTROPHE
ʽ 02BD MODIFIER LETTER REVERSED COMMA
٬ 066C ARABIC THOUSANDS SEPARATOR
‘ 2018 LEFT SINGLE QUOTATION MARK
’ 2019 RIGHT SINGLE QUOTATION MARK
‚ 201A SINGLE LOW-9 QUOTATION MARK
‛ 201B SINGLE HIGH-REVERSED-9 QUOTATION MARK
՚ 055A ARMENIAN APOSTROPHE
︐ FE10 PRESENTATION FORM FOR VERTICAL COMMA

关于r - 文本挖掘 R 包和正则表达式来处理替换智能 curl 引号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47173557/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com