我有一些包含数字短语的文本,后面跟着一些符号。例如,我想提取数字,然后是百分比。使用quanteda包中的kwic函数似乎适用于作为正则表达式的数字(例如,"\\d{1,}")。尽管如此,我还是找不到如何用quanteda来提取后面的百分比符号。以下案文可作为文本示例:
187例难治性梭菌中13例(7%)在ICU-1,9例(36%)在ICU-2,3例(5.9%)在ICU-2。8例(32%)腹泻仅归因于艰难梭菌和/或毒素,其余17例(68%)无症状:无一例出现假膜性结肠炎。
发布于 2018-04-11 09:06:46
原因是当您直接对一个语料库或字符对象调用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 否则,需要将regex包装在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 这种行为可能需要一些习惯,但这是确保用户完全控制多令牌序列搜索的最佳方法,而不是在输入尚未被标记时实现确定多令牌序列的元素的单一方法。
发布于 2018-04-11 04:11:39
quanteda包处理正则表达式非常奇怪。我不知道这个解决方案为什么工作,但我认为它与kwic如何对待指定的模式有关。使用pattern函数包装phrase并添加空格将返回正确的结果:
s <- c("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(s, phrase("\\d+ %"), valuetype = "regex")我建议您联系包维护人员并指出这个问题。似乎有违直觉。
https://stackoverflow.com/questions/49764651
复制相似问题