【问题标题】:How to keep apostrophes when creating bigrams in R?在 R 中创建二元组时如何保留撇号?
【发布时间】:2017-08-10 17:17:45
【问题描述】:

我有一个现有代码用于创建文档中所有二元组的表,但它删除了撇号。如何调整此代码以将“我已经”之类的词视为一个词?

 text1 = scan(file.choose(), what="character",sep="\n")
 text1 <- tolower(text1)
 tokens <- unlist(strsplit(text1, "[^a-z]+"))
 tokens <- mytable[tokens != ""]
 tokens2 <- c(tokens[-1], ".")
 bigrams <- paste(tokens, tokens2)
 freq <- sort(table(bigrams), decreasing=T)
 write.csv(file = "bigram count.csv" , x=freq, row.names = FALSE)

例如,短语“I've had fun”会输出'i've had'和'had fun'

【问题讨论】:

    标签: r text text-analysis


    【解决方案1】:

    您可以使用诸如“tm”或“ngram”之类的包。例如,使用 ngram 来获取具有 bigram 及其频率的 data.frame。

    require(ngram)
    TMP <-  "I've had fun tonight" 
    TMP1 <- ngram(str = TMP,n = 2)
    TMP2 <- as.data.frame(get.phrasetable(TMP1))
    TMP2
    
            ngrams freq      prop
    1     had fun     1 0.3333333
    2    I've had     1 0.3333333
    3 fun tonight     1 0.3333333
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-06-29
      • 1970-01-01
      • 2016-09-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-27
      • 2017-11-24
      相关资源
      最近更新 更多