计算通配符在文本中出现的次数(在 R 中)
Count number of times a word-wildcard appears in text (in R)
我有一个由常规词 ("activated") 或通配符词 ("activat*") 组成的向量。我想:
1) 计算每个单词在给定文本中出现的次数(即,如果 "activated" 在文本中出现,"activated" 频率将为 1)。
2) 计算每个单词通配符在文本中出现的次数(即,如果 "activated" 和 "activation" 在文本中出现,"activat*" 频率将为 2)。
我能够实现 (1),但无法实现 (2)。有人可以帮忙吗?谢谢。
library(tm)
library(qdap)
text <- "activation has begun. system activated"
text <- Corpus(VectorSource(text))
words <- c("activation", "activated", "activat*")
# Using termco to search for the words in the text
apply_as_df(text, termco, match.list=words)
# Result:
# docs word.count activation activated activat*
# 1 doc 1 5 1(20.00%) 1(20.00%) 0
这是否可能与版本有关?我 运行 完全相同的代码(见下文)并得到了你所期望的
> text <- "activation has begunm system activated"
> text <- Corpus(VectorSource(text))
> words <- c("activation", "activated", "activat")
> apply_as_df(text, termco, match.list=words)
docs word.count activation activated activat
1 doc 1 5 1(20.00%) 1(20.00%) 2(40.00%)
下面是我运行R.version()
时的输出。我 运行 在 Windows 10 上的 RStudio 版本 0.99.491 中 运行 宁此。
> R.Version()
$platform
[1] "x86_64-w64-mingw32"
$arch
[1] "x86_64"
$os
[1] "mingw32"
$system
[1] "x86_64, mingw32"
$status
[1] ""
$major
[1] "3"
$minor
[1] "2.3"
$year
[1] "2015"
$month
[1] "12"
$day
[1] "10"
$`svn rev`
[1] "69752"
$language
[1] "R"
$version.string
[1] "R version 3.2.3 (2015-12-10)"
$nickname
[1] "Wooden Christmas-Tree"
希望对您有所帮助
也许考虑使用库的不同方法 stringi
?
text <- "activation has begun. system activated"
words <- c("activation", "activated", "activat*")
library(stringi)
counts <- unlist(lapply(words,function(word)
{
newWord <- stri_replace_all_fixed(word,"*", "\p{L}")
stri_count_regex(text, newWord)
}))
ratios <- counts/stri_count_words(text)
names(ratios) <- words
ratios
结果是:
activation activated activat*
0.2 0.2 0.4
在代码中,我将 *
转换为 \p{L} ,这意味着正则表达式模式中的任何字母。之后我计算发现的正则表达式出现次数。
我有一个由常规词 ("activated") 或通配符词 ("activat*") 组成的向量。我想:
1) 计算每个单词在给定文本中出现的次数(即,如果 "activated" 在文本中出现,"activated" 频率将为 1)。
2) 计算每个单词通配符在文本中出现的次数(即,如果 "activated" 和 "activation" 在文本中出现,"activat*" 频率将为 2)。
我能够实现 (1),但无法实现 (2)。有人可以帮忙吗?谢谢。
library(tm)
library(qdap)
text <- "activation has begun. system activated"
text <- Corpus(VectorSource(text))
words <- c("activation", "activated", "activat*")
# Using termco to search for the words in the text
apply_as_df(text, termco, match.list=words)
# Result:
# docs word.count activation activated activat*
# 1 doc 1 5 1(20.00%) 1(20.00%) 0
这是否可能与版本有关?我 运行 完全相同的代码(见下文)并得到了你所期望的
> text <- "activation has begunm system activated"
> text <- Corpus(VectorSource(text))
> words <- c("activation", "activated", "activat")
> apply_as_df(text, termco, match.list=words)
docs word.count activation activated activat
1 doc 1 5 1(20.00%) 1(20.00%) 2(40.00%)
下面是我运行R.version()
时的输出。我 运行 在 Windows 10 上的 RStudio 版本 0.99.491 中 运行 宁此。
> R.Version()
$platform
[1] "x86_64-w64-mingw32"
$arch
[1] "x86_64"
$os
[1] "mingw32"
$system
[1] "x86_64, mingw32"
$status
[1] ""
$major
[1] "3"
$minor
[1] "2.3"
$year
[1] "2015"
$month
[1] "12"
$day
[1] "10"
$`svn rev`
[1] "69752"
$language
[1] "R"
$version.string
[1] "R version 3.2.3 (2015-12-10)"
$nickname
[1] "Wooden Christmas-Tree"
希望对您有所帮助
也许考虑使用库的不同方法 stringi
?
text <- "activation has begun. system activated"
words <- c("activation", "activated", "activat*")
library(stringi)
counts <- unlist(lapply(words,function(word)
{
newWord <- stri_replace_all_fixed(word,"*", "\p{L}")
stri_count_regex(text, newWord)
}))
ratios <- counts/stri_count_words(text)
names(ratios) <- words
ratios
结果是:
activation activated activat*
0.2 0.2 0.4
在代码中,我将 *
转换为 \p{L} ,这意味着正则表达式模式中的任何字母。之后我计算发现的正则表达式出现次数。