【问题标题】:Creating a function to remove only specific word in a list (R)创建一个函数以仅删除列表中的特定单词(R)
【发布时间】:2018-10-10 18:51:21
【问题描述】:

我有一个不受欢迎的词(西班牙语)列表,这些词没有意义,但它们也存在于另一个词中。我只想在它们是一个词时删除它,而不是当它们是另一个词的一部分时。

例如:“la”是一篇西班牙语文章,但如果我使用函数将其删除,也会将一个有用的术语分解成两个词,如“relacion”(表示关系)

我的第一选择是创建一个函数来删除这些术语。

bdtidy$tweet <- #here are tweets
fix.useless <- function(doc) {   
function(doc) {
doc <- gsub("la", ".", doc)
doc <- gsub("las", ".", doc)
doc <- gsub("el", ".", doc)
doc <- gsub("ellos", ".", doc)
doc <- gsub("ellas", ".", doc)
return(doc)
 }

bdtidy$tweet <- sapply(bdtidy$tweet, fix.useless)

我的第二个选择是使用列表,然后在 df 中使用过滤器

nousar <- c("rt", "pero", "para"...)
new df %>% bdtidy %>%
 filter(!tweet $in$ nousar))

但结果总是删除所有这些单词并将术语分解为两个单词,这使我的分析毫无用处。 谢谢。

【问题讨论】:

  • 如果你先gsub("el", ".", doc) 然后"ellos" 变成".los" 和下面的gsub("ellos", ".", doc) 什么都不做。

标签: r dplyr filtering tidyverse sentiment-analysis


【解决方案1】:

从字符串中删除单个单词的一种方法是在单词的两侧使用空格,例如以下示例:

# sample input
x <- c("Get rid of la but not lala")
# pattern with spaces flanking target word
y <- gsub(" la ", " ", x)
# output
> y
[1] "Get rid of but not lala"

【讨论】:

    【解决方案2】:

    您可以标记单词。也就是说,提取单个单词。提取它们后,您可以检查令牌是否匹配并删除它们。 stringr 包可以在这里为您提供帮助

    #sample text
    text <- "hola, me llamo struggles. El package 'stringr' puede resolver la pregunta."
    
    #normalize text by making everything lowercase
    lower_text <- stringr::str_to_lower(text)
    
    #split text at anything that isn't a number or a letter
    tokens <- stringr::str_split(lower_text, "[^[:alnum:]]+")
    
    #create a list of stop words
    stop_words <- c('la', 'las', 'el', 'ellos')
    
    #remove words that are in the stop words vector
    tokens[[1]][!tokens[[1]] %in% stop_words]
    

    由于您可能会通过大量推文来执行此操作,因此我建议您还可以查看tidytext 包并阅读教程https://www.tidytextmining.com/

    df <- data.frame(
      tweet = text,
      tweet_id = 1234,
      user = 'struggles',
      stringsAsFactors = F
    )
    
    twitter_tokens <- tidytext::unnest_tokens(df, word, tweet)
    
    clean_twitter_tokens <- dplyr::filter(twitter_tokens, !word %in% stop_words)
    

    这会给你类似的东西

      tweet_id      user      word
    1     1234 struggles      hola
    2     1234 struggles        me
    3     1234 struggles     llamo
    4     1234 struggles struggles
    5     1234 struggles   package
    6     1234 struggles   stringr
    7     1234 struggles     puede
    8     1234 struggles  resolver
    9     1234 struggles  pregunta
    

    如果你想把它放在一个句子中,那么以下内容将把它带回来:

    lean_twitter_tokens %>%
      dplyr::group_by(tweet_id, user) %>%
      dplyr::summarize(tweet = stringr::str_c(word, collapse = ' '))
    

    给你

      tweet_id user      tweet                                                          
         <dbl> <chr>     <chr>                                                          
    1     1234 struggles hola me llamo struggles package stringr puede resolver pregunta
    

    【讨论】:

      猜你喜欢
      • 2019-08-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-07-06
      • 2021-03-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多