【问题标题】:How to identify illogical character strings/sentences如何识别不合逻辑的字符串/句子
【发布时间】:2021-04-17 04:30:00
【问题描述】:

假设我有一个数据集,其中每一行都包含一个句子,该句子源于一个非常大的调查(德语和法语)中的一个开放式问题。大多数句子(答案)都是合乎逻辑的;即有意义的单词组合。但也有少数人粗心大意,随便填了各种不合逻辑的字符串。

一个有用的第一步是识别不是单词的所有内容或以其他方式识别不合逻辑的字符串。是否存在可以促进这一点的软件包?如何解决这个问题?

例子:

df <- structure(list(sentence = c("Das ist ein deutscher Satz.", "Ein kürzerer Satz", "34t34 t444tt", "C'est une sentence francaise", ".-......", "---2r13 1r-2r2")), .Names = c("sentence"), row.names = c(NA,6L), class = "data.frame")

head(df)
                      sentence
1  Das ist ein deutscher Satz.
2            Ein kürzerer Satz
3                  34t34 t444tt
4 C'est une sentence francaise
5                     .-......
6                ---2r13 1r-2r2

【问题讨论】:

    标签: r string text


    【解决方案1】:

    您可以使用 a-z 和 A-Z 对所有字符的份额以及单词的数量来检测句子。

    df$nonCharShare <- nchar(gsub("[[:alpha:] ]", "", df$sentence)) / nchar(df$sentence)
    df$words <- lengths(strsplit(df$sentence, " ", TRUE))
    df
    #                      sentence nonCharShare words
    #1  Das ist ein deutscher Satz.   0.03703704     5
    #2            Ein kürzerer Satz   0.00000000     3
    #3                  34t34t444tt   0.63636364     1
    #4 C'est une sentence francaise   0.03571429     4
    #5                     .-......   1.00000000     1
    #6                ---2r131r-2r2   0.76923077     1
    

    【讨论】:

    • 很好,但是这对我个人的废话输入有问题,就像“sdfksdf asdfjsda asdfjsdaf asfd”。
    • 是的,确定它不是字典,无法检测到这些输入。
    • 不幸的是,数据还包含无意义的字符串,如所描述的 jay.sf。但这仍然是一个有用的起点!
    【解决方案2】:

    假设你有一本字典作为向量。

    dict <- c("das", "deutscher", "ein", "est", "francaise", "ist", "kürzerer", 
              "satz", "sentence", "une")
    

    您可以将其粘贴到模式中

    dict.pat <- paste(dict, collapse="|")
    

    使用它来使用grepl 标记不需要的条目。

    df <- transform(df, flag=sapply(df$sentence, function(x) +(!grepl(dict.pat, x))))
    df
    #                       sentence flag
    # 1  Das ist ein deutscher Satz.    0
    # 2            Ein kürzerer Satz    0
    # 3                  34t34t444tt    1
    # 4 C'est une sentence francaise    0
    # 5                     .-......    1
    # 6                ---2r131r-2r2    1
    

    这是一个开始。当然,您需要考虑什么是“不受欢迎的”。

    您可能还想查看quanteda 包。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-25
      • 2020-04-30
      • 2019-11-20
      • 1970-01-01
      相关资源
      最近更新 更多