gpt4 book ai didi

r - quanteda kwic 提取数字后跟百分比

转载 作者:行者123 更新时间:2023-12-05 03:06:12 25 4
gpt4 key购买 nike

我有一些文本包含包含数字的短语,后跟一些符号。我想提取它们,例如,数字后跟百分比。使用 quanteda 包中的 kwic 函数似乎适用于数字作为正则表达式(例如 "\\d{1,}")。尽管如此,我没有找到如何使用 quanteda 提取它后跟一个百分号。以下文本可作为文本示例:

Thirteen (7%) of 187 patients acquired C. difficile in ICU-1, 9 (36%) of 25 on ICU-2 and 3 (5.9%) of 51 patients in BU. Eight (32%) developed diarrhoea attributable only to C. difficile and/ or toxin, and the remaining 17 (68%) were asymptomat- ic: none had pseudomembranous colitis.

最佳答案

原因是,当您直接在语料库或字符对象上调用 kwic() 时,它会将一些参数传递给 tokens(),这会影响标记化的发生方式,在上下文中的关键字分析之前。 (这记录在 ?kwic... 参数中。)

quanteda 中的默认标记化使用 stringi 词边界定义,因此:

tokens("Thirteen (7%) of 187")
# tokens from 1 document.
# text1 :
# [1] "Thirteen" "(" "7" "%" ")" "of" "187"

如果您想使用更简单的空白标记器,可以使用:

tokens("Thirteen (7%) of 187", what = "fasterword")
# tokens from 1 document.
# text1 :
# [1] "Thirteen" "(7%)" "of" "187"

因此,在 kwic() 中使用它的方法是:

kwic(s, "\\d+%", valuetype = "regex", what = "fasterword")

# [text1, 2] Thirteen | (7%) | of 187 patients acquired C.
# [text1, 12] C. difficile in ICU-1, 9 | (36%) | of 25 on ICU-2 and
# [text1, 19] 25 on ICU-2 and 3 | (5.9%) | of 51 patients in BU.
# [text1, 26] 51 patients in BU. Eight | (32%) | developed diarrhoea attributable only to
# [text1, 41] toxin, and the remaining 17 | (68%) | were asymptomat- ic: none had

否则,您需要将正则表达式包装在 phrase() 函数中,并用空格分隔元素:

kwic(s, phrase("\\d+ %"), valuetype = "regex")

# [text1, 3:4] Thirteen( | 7 % | ) of 187 patients acquired
# [text1, 18:19] in ICU-1, 9( | 36 % | ) of 25 on ICU-2
# [text1, 28:29] on ICU-2 and 3( | 5.9 % | ) of 51 patients in
# [text1, 39:40] in BU. Eight( | 32 % | ) developed diarrhoea attributable only
# [text1, 60:61] and the remaining 17( | 68 % | ) were asymptomat- ic

这种行为可能需要一些时间来适应,但这是确保用户完全控制搜索多 token 序列的最佳方式,而不是实现一种确定多 token 元素应该是什么的单一方法输入尚未被标记化时的顺序。

关于r - quanteda kwic 提取数字后跟百分比,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49764651/

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