有没有人知道将数字的文本表示转换为实际数字的函数,例如20305年的"二万三千五".我在数据帧行中编写了数字,并希望将它们转换为数字.
在包qdap中,您可以用单词替换数字表示的数字(例如,1001变为一千),但不是相反:
library(qdap)
replace_number("I like 346457 ice cream cones.")
[1] "I like three hundred forty six thousand four hundred fifty seven ice cream cones."
Run Code Online (Sandbox Code Playgroud) 我的文件有超过4M的行,我需要一种更有效的方法将我的数据转换为语料库和文档术语矩阵,以便我可以将它传递给贝叶斯分类器.
请考虑以下代码:
library(tm)
GetCorpus <-function(textVector)
{
doc.corpus <- Corpus(VectorSource(textVector))
doc.corpus <- tm_map(doc.corpus, tolower)
doc.corpus <- tm_map(doc.corpus, removeNumbers)
doc.corpus <- tm_map(doc.corpus, removePunctuation)
doc.corpus <- tm_map(doc.corpus, removeWords, stopwords("english"))
doc.corpus <- tm_map(doc.corpus, stemDocument, "english")
doc.corpus <- tm_map(doc.corpus, stripWhitespace)
doc.corpus <- tm_map(doc.corpus, PlainTextDocument)
return(doc.corpus)
}
data <- data.frame(
c("Let the big dogs hunt","No holds barred","My child is an honor student"), stringsAsFactors = F)
corp <- GetCorpus(data[,1])
inspect(corp)
dtm <- DocumentTermMatrix(corp)
inspect(dtm)
Run Code Online (Sandbox Code Playgroud)
输出:
> inspect(corp)
<<VCorpus (documents: 3, metadata (corpus/indexed): 0/0)>>
[[1]]
<<PlainTextDocument (metadata: 7)>> …
Run Code Online (Sandbox Code Playgroud) 我有许多PDF文档,我已将其读入带库的语料库中tm
.如何将语料库分解成句子?
可以readLines
通过sentSplit
从包qdap
[*] 读取文件来完成.该功能需要数据帧.它还需要放弃语料库并单独阅读所有文件.
如何在语料库中传递函数sentSplit
{ qdap
} tm
?或者,还有更好的方法?.
注意:sentDetect
库中有一个函数,openNLP
现在是Maxent_Sent_Token_Annotator
- 同样的问题适用:如何将它与语料库[tm]结合起来?
我正在尝试在Azure ML中安装qdap软件包.其余的依赖包安装没有任何问题.说到qdapTools,我得到了这个错误,虽然我尝试安装的版本是1.3.1(从R包附带的Decription文件中验证了这一点)
package 'qdapTools' 1.1.0 was found, but >= 1.3.1 is required by 'qdap
Run Code Online (Sandbox Code Playgroud)
"执行R脚本"中的代码:
install.packages("src/qdapTools.zip", repos = NULL, verbose = TRUE)
install.packages("src/magrittr.zip", lib = ".", repos = NULL, verbose = TRUE)
install.packages("src/stringi.zip", lib = ".", repos = NULL, verbose = TRUE)
install.packages("src/stringr.zip", lib = ".", repos = NULL, verbose = TRUE)
install.packages("src/qdapDictionaries.zip", lib = ".", repos = NULL, verbose = TRUE)
install.packages("src/qdapRegex.zip", lib = ".", repos = NULL, verbose = TRUE)
install.packages("src/RColorBrewer.zip", lib = ".", repos = NULL, verbose …
Run Code Online (Sandbox Code Playgroud) 我有一个 400,000 行的文件,其中包含需要进行地理编码的手动输入地址。文件中的相同地址有很多不同的变体,因此多次对同一地址使用 API 调用似乎很浪费。
为了减少这种情况,我想减少这五行:
Address
1 Main Street, Country A, World
1 Main St, Country A, World
1 Maine St, Country A, World
2 Side Street, Country A, World
2 Side St. Country A, World
Run Code Online (Sandbox Code Playgroud)
下降到两个:
Address
1 Main Street, Country A, World
2 Side Street, Country A, World
Run Code Online (Sandbox Code Playgroud)
使用该stringdist
包,您可以将字符串的“单词”部分组合在一起,但字符串匹配算法不区分数字。这意味着它将同一街道上的两个不同房屋号码归为同一地址。
为了解决这个问题,我想出了两种方法:首先,尝试使用正则表达式将数字和地址手动分离到单独的列中,然后重新加入它们。这样做的问题是,有这么多手动输入的地址,似乎有数百种不同的边缘情况,而且它变得笨拙。
使用这个关于分组的答案和这个将单词转换为数字的答案,我有第二种方法来处理边缘情况,但在计算上非常昂贵。有没有更好的第三种方法来做到这一点?
library(gsubfn)
library(english)
library(qdap)
library(stringdist)
library(tidyverse)
similarGroups <- function(x, thresh = 0.8, method = "lv"){
grp <- integer(length(x))
Address …
Run Code Online (Sandbox Code Playgroud) 使用该qdap::polarity()
函数有时会遇到错误:
derive_pubkey(key)出错:RAW()只能应用于'raw'而不是'list'
我敢肯定这是由于qdap
的key
阶级冲突的与openssl
包装的key
类(因为derive_pubkey()
从openssl
错误味精是引用).
当openssl
出现在loaded via a namespace (and not attached):
部分中时发生错误sessionInfo()
,并且它似乎抛弃了密钥类的方法分派并导致错误.
我只能通过重新启动R会话来修复错误.有没有办法openssl
从会话中删除脚印以解决此问题?或者有没有另一种方法可以避免这个问题,而无需重新启动R?
> successful <- qdap::polarity("testing")
> load_openssl_ns <- body(openssl:::print.key)
> fails <- qdap::polarity("testing")
Error in derive_pubkey(key) :
RAW() can only be applied to a 'raw', not a 'list'
Run Code Online (Sandbox Code Playgroud) 我使用qdap包来确定特定应用程序的每个评论评论的情绪.我从CSV文件中读取了评论评论,并将其传递给qdap的极性函数.一切正常,我得到所有评论评论的极性,但问题是计算所有句子的极性需要7-8秒(CSV文件中存在的句子总数是779).我在下面粘贴我的代码.
temp_csv <- filePath()
attach(temp_csv)
text_data <- temp_csv[,c('Content')]
print(Sys.time())
polterms <- list(neg=c('wtf'))
POLKEY <- sentiment_frame(positives=c(positive.words),negatives=c(polterms[[1]],negative.words))
polarity <- polarity(sentences, polarity.frame = POLKEY)
print(Sys.time())
Run Code Online (Sandbox Code Playgroud)
所用时间如下:
[1]"2016-04-12 16:43:01 IST"
[1]"2016-04-12 16:43:09 IST"
如果我做错了什么,有人可以告诉我吗?如何提高性能?
我想将qdap
的polarity
函数应用于文档向量,每个文档可以包含多个句子,并为每个文档获取相应的极性。例如:
library(qdap)
polarity(DATA$state)$all$polarity
# Results:
[1] -0.8165 -0.4082 0.0000 -0.8944 0.0000 0.0000 0.0000 -0.5774 0.0000
[10] 0.4082 0.0000
Warning message:
In polarity(DATA$state) :
Some rows contain double punctuation. Suggested use of `sentSplit` function.
Run Code Online (Sandbox Code Playgroud)
这个警告不能忽略,因为它似乎添加了文档中每个句子的极性分数。这可能导致文档级极性分数超出 [-1, 1] 范围。
我知道首先运行sentSplit
然后在句子中求平均值的选项,可能按字数加权极性,但这是 (1) 低效的(大约是在带有警告的完整文档上运行的时间的 4 倍),并且( 2)不清楚如何给句子加权。这个选项看起来像这样:
DATA$id <- seq(nrow(DATA)) # For identifying and aggregating documents
sentences <- sentSplit(DATA, "state")
library(data.table) # For aggregation
pol.dt <- data.table(polarity(sentences$state)$all)
pol.dt[, id := sentences$id]
document.polarity <- pol.dt[, sum(polarity * wc) / …
Run Code Online (Sandbox Code Playgroud) 如何在角色向量中找到频繁的相邻单词对?例如,使用原油数据集,一些常见的货币对是"原油","石油市场"和"百万桶".
下面的小例子的代码试图识别频繁的术语,然后使用正向前瞻断言,计算频繁术语立即跟随这些频繁术语的次数.但是这次尝试坠毁并烧毁了.
任何指导都将被理解为如何创建在第一列("对")中显示公共对的数据帧以及在第二列("计数")中显示它们在文本中出现的次数.
library(qdap)
library(tm)
# from the crude data set, create a text file from the first three documents, then clean it
text <- c(crude[[1]][1], crude[[2]][1], crude[[3]][1])
text <- tolower(text)
text <- tm::removeNumbers(text)
text <- str_replace_all(text, " ", "") # replace double spaces with single space
text <- str_replace_all(text, pattern = "[[:punct:]]", " ")
text <- removeWords(text, stopwords(kind = "SMART"))
# pick the top 10 individual words by frequency, since they will likely form the most common pairs
freq.terms …
Run Code Online (Sandbox Code Playgroud) 我遇到了错误
Error in checkForRemoteErrors(val) :
one node produced an error: arguments imply differing number of rows: 3, 0
Run Code Online (Sandbox Code Playgroud)
尝试check_spelling
在qdap
包中使用时。提供的数字 3 和 0 与下面提供的数据相关,但这只是更大的拼写检查字符串的一个小样本,当我将更大的字符串传递给字符串以进行拼写检查和用作字典时,行号会有所不同。当它突然开始工作时,我偶尔会成功,但是一旦我尝试重复该过程,我就会再次遇到相同的错误。
当我使用该check_spelling_interactive()
功能时,我也遇到了同样的错误。
我的理解是,我想用作拼写检查和字典的单词都应该在字符向量中。
我已经更新了我的qdap
. 在 Windows 7 64、R Studio 版本 0.99.467、R 版本 3.2.1 上运行。
任何帮助将不胜感激,因为我正在为此脱发,而且我没有那么多多余的钱。
library(qdap)
spellcheckstring = "universal motor vlb"
mydictionary = c("brake", "starter", "shock", "pad", "kit", "bore", "toyota", "ford", "pump", "nissan", "gas", "alternator", "switch")
class(spellcheckstring) # character
class(mydictionary) # character
check_spelling(spellcheckstring, dictionary = mydictionary)
Run Code Online (Sandbox Code Playgroud) 之前(作为一个菜鸟)我把它作为一个R包bug提交,让我一起运行吧.我认为以下所有都是好的:
replace_number("123 0 boogie")
[1] "one hundred twenty three boogie"
replace_number("1;1 foo")
[1] "one;one foo"
replace_number("47 bar")
[1] "forty seven bar"
replace_number("0")
"zero"
Run Code Online (Sandbox Code Playgroud)
我认为以下所有内容都很糟糕,因为输出中缺少"零":
replace_number("1;0 foo")
[1] "one; foo"
replace_number("00 bar")
[1] "bar"
replace_number("0x")
[1] "x"
Run Code Online (Sandbox Code Playgroud)
基本上,我会说它replace_number()
无法处理包含数字0的字符串("0"除外).这是一个真正的错误吗?
我需要预处理强制对齐的语音记录.但是,我用文本表示替换年份有困难.例如,1984年需要被"1984年"取代.我尝试了qdap包的replace_number函数.包装很棒,但它取而代之的是1984年的"一千九百八十四".我可以尝试任何R包中的其他功能吗?谢谢!
我最近一直在尝试data.frame
使用该tm
包在 R 中的单个列中查找词频。虽然它data.frame
本身有许多基于数字和字符的列,但我只对纯文本的单个列感兴趣。虽然我在清理文本本身时没有遇到问题,但一旦我尝试使用findFreqTerms()
命令拉取词频,我就会收到以下错误:
Error: inherits(x, c("DocumentTermMatrix", "TermDocumentMatrix")) is not TRUE
Run Code Online (Sandbox Code Playgroud)
我认为这是说我需要将数据转换为 aDocumentTermMatrix
或 a TermDocumentMatrix
,但是由于我只有一个正在处理的列,因此我也无法创建。错误如下:
> Test <- DocumentTermMatrix(Types)
Error in UseMethod("TermDocumentMatrix", x) :
no applicable method for 'TermDocumentMatrix' applied to an object of class "c('PlainTextDocument', 'TextDocument')"
Run Code Online (Sandbox Code Playgroud)
有没有办法从单列中获取频率计数?我在下面粘贴了我的完整代码,并对我采取的每一步进行了解释。我很感激你们能给我的任何帮助。
> # extracting the single column I wish to analyse from the data frame
Types <-Expenses$Types
> # lower all cases
Types <- tolower(Types)
> # remove punctuation
Types <- removePunctuation(Types)
> …
Run Code Online (Sandbox Code Playgroud) qdap ×13
r ×13
tm ×3
nlp ×2
azure-machine-learning-studio ×1
corpus ×1
data.table ×1
dplyr ×1
numbers ×1
sentence ×1
shiny ×1
split ×1
stringdist ×1
text ×1
tidyverse ×1