【问题标题】:How to distinguish bigrams and merge them into one CSV file in R Studio如何区分二元组并将它们合并到 R Studio 中的一个 CSV 文件中
【发布时间】:2014-07-27 21:30:58
【问题描述】:

好的,所以我正在尝试让 R 读取句子,提取二元组,并将所有这些二元组合并到一个 csv 中。现在我有代码可以提取一个句子的二元组:

sentence=gsub('[[:punct:]]','', sentence)
    sentence=gsub('[[:cntrl:]]','', sentence)
    sentence=gsub('\\d+','', sentence)
    sentence=tolower(sentence)
    words<- strsplit(sentence, "\\s+")[[1]]
    New=NULL
    for(i in 1:length(words)-1){ 
      New[i]=paste(words[i],words[i+1])     
  }
New=as.matrix(New)
colnames(New)<-"Bigrams"

但是,我希望能够导入一个包含不同句子的 csv,并让前一行代码为每个句子提取二元组,然后将它们合并到一个 csv 文件中。我开始编写代码(如下),但它不正确。我将非常感谢我能得到的任何帮助。 R 中的自然语言处理相当新。

library(tm)
library(plyr)
library(stringr)
data<-read.csv("file.csv")
sentences=as.vector(data$text)

bigrams<-function(sentences){

bigrams2<-mlply(sentences,function(sentence){
    sentence=gsub('[[:punct:]]','', sentence)
    sentence=gsub('[[:cntrl:]]','', sentence)
    sentence=gsub('\\d+','', sentence)
    sentence=tolower(sentence)
    words<- strsplit(sentence, "\\s+")[[1]]
    New=NULL
    for(i in 1:length(words)-1){ 
      New[i]=paste(words[i],words[i+1])     
   }
New=as.matrix(New)
colnames(New)<-"Bigrams"
New
})
merge(bigrams2,all=TRUE)

} 

谢谢!

【问题讨论】:

    标签: r csv nlp rstudio


    【解决方案1】:

    不是一个直接的答案,但您可能会发现为此使用 tmRWeka 的内置功能更简单:

    library(RWeka)   # for NGramTokenizer(...)
    library(tm)
    # sample data
    data <- data.frame(text=c("This is some text.",
                              "This is some other text.",
                              "This is some punctuation; and some more, and more...",
                              "These are some numbers: 1,2,3,4, five."))
    
    doc  <- PlainTextDocument(data$text)
    doc  <- removeNumbers(doc)
    BigramTokenizer <- function(x) NGramTokenizer(x, Weka_control(min = 2, max = 2))
    tdm <- TermDocumentMatrix(Corpus(VectorSource(doc)), 
                              control = list(tokenize = BigramTokenizer))
    result <- rownames(tdm)
    result
    #  [1] "and more"         "and some"         "are some"         "is some"         
    #  [5] "more and"         "numbers five"     "other text"       "punctuation and" 
    #  [9] "some more"        "some numbers"     "some other"       "some punctuation"
    # [13] "some text"        "these are"        "this is"         
    

    编辑回应 OP 的评论。

    所以这里有一个不使用RWeka 中的NGramTokenizer 的方法。使用bigrams(...) 函数here 的修改版本。请注意,您必须明确删除标点符号。

    bigrams <- function(text){
      word.vec <- strsplit(text, "\\s+")[[1]]
      sapply(1:(length(word.vec)-1), function(x)paste(word.vec[x], word.vec[x+1]))
    }
    doc  <- PlainTextDocument(data$text)
    doc  <- removeNumbers(doc)
    doc  <- removePunctuation(doc)
    tdm <- TermDocumentMatrix(Corpus(VectorSource(doc)), 
                              control = list(tokenize = bigrams))
    result.2 <- rownames(tdm)
    
    identical(result,result.2)
    # [1] TRUE
    

    【讨论】:

    • 感谢您的回复。我看到有些人在网上这样做,但是,我无法让 RWeka 在我的电脑上工作。你知道为什么会这样吗?这是我得到的错误消息:错误:.onLoad failed in loadNamespace() for 'rJava', details: call: fun(libname, pkgname) error: JAVA_HOME cannot be determined from the Registry Error: package or namespace load failed for ' RWeka'
    • 查看我的编辑以了解不使用RWeka 的方法。至于安装这个包,我的建议是确保你首先安装了最新版本的 R 和最新版本的 Java 运行时。看起来安装程序在您的系统上根本找不到 Java 运行时??
    猜你喜欢
    • 2021-10-28
    • 2019-07-22
    • 2020-12-21
    • 2017-02-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多