【问题标题】:r quanteda top features extraction returning modified wordsr quanteda 顶级特征提取返回修改后的单词
【发布时间】:2019-01-27 05:02:19
【问题描述】:

我曾尝试使用 quanteda 来提取顶级特征,但结果是经过修改的单词,即“faulti”而不是“faulty”。这应该是预期的结果吗?

我已尝试在原始数据集中搜索排名靠前的特征关键字,但没有达到预期的匹配。

编辑:如果我为函数 dfm() 设置了选项 stem=FALSE,则关键词恢复为普通词。

library(quanteda)    
corpus1 = corpus(as.character(training_data$Elec_rmk))
kwic(corpus1, 'faulty')

#[text25701, 4]              Convertible roof sometime | faulty | . SD card missing.               
#[text25701, 22]              unavailable). Pilot lamp | faulty | .  

dfm1 <- dfm(
  corpus1, 
  ngrams = 1, 
  remove = stopwords("english"),
  remove_punct = TRUE,
  remove_numbers = TRUE,
  stem = TRUE)
tf1 <- topfeatures(dfm1, n = 10)
tf1
# key words were modified/truncated words?
#faulti malfunct    light    damag     miss    cover     rear     loos     lamp    plate 
#   562      523      454      337      331      325      295      259      250      238 

library(stringr)
sum(str_detect(training_data$Elec_rmk, 'faulti')) # 0
sum(str_detect(training_data$Elec_rmk, 'faulty')) # 495

【问题讨论】:

    标签: r quanteda


    【解决方案1】:

    dfm 默认没有词干。但是您将 stem 选项设置为 TRUE hency "faulti"。但正如您在编辑备注中提到的,将此设置为 FALSE(或省略此设置)将返回无词干的单词。

    但您似乎误解了 str_detect 返回的内容和 topfeatures 返回的内容。 str_detect 只检测句子中是否存在搜索字符串,但不检测多少次。您的总和仅计算句子中单词 (495) 的出现次数。 topfeatures 计算一个单词在文本中实际出现的次数 (562)。

    看下面的例子来看看区别:

    # 1 line of text (paragraph)
    my_text <- "I have two examples of two words in this text. Isn't having two words fun?"
    
    topfeatures(dfm(my_text, remove = stopwords("english"), remove_punct = TRUE), n = 2)
      two words 
        3     2 
    sum(str_detect(my_text, "two"))
    [1] 1
    
    # 2 sentences.
    my_text2 <- c("I have two examples of two words in this text.", "Isn't having two words fun?")
    
    topfeatures(dfm(my_text2, remove = stopwords("english"), remove_punct = TRUE), n = 2)
      two words 
        3     2 
    sum(str_detect(my_text2, "two"))
    [1] 2
    

    对于第一个示例,topfeatures 为单词“二”返回 3,str_detect 只返回 1。对于 str_detect,只有 1 个向量/一段文本可供查看。

    对于第二个示例,topfeatures 再次为单词“two”返回 3。 str_detect 现在返回 2,向量中有 2 个值,因此它会在两个句子中检测到单词“two”,但仍然比实际应该的 3 少。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-29
      • 1970-01-01
      相关资源
      最近更新 更多