如何将情感词典导入R进行Kickstarter的数据抓取

如何将情感词典导入R进行Kickstarter的数据抓取,r,screen-scraping,mining,emotion,lexicon,R,Screen Scraping,Mining,Emotion,Lexicon,我正试图创建一个模型,用R来测量文本中的情感。基本上,使用一个包含情感词的词典,我只想从大量URL中提取“p”(段落)。 我希望通过使用词典,根据预定义的情感指示词的存在情况,找到每个URL中每个情感的字数 我使用的数据是JSON格式的,来自Webrobots:(最新的一组) 任何帮助都将不胜感激,因为我真的非常渴望开始这项工作! 即使只是知道如何将其导入R和一个代码来计算单词,也会有很大的帮助 亲切问候,, 一个绝望的文盲女孩 更新: 数据文件被导入到R中。但是,我找不到一种方法来编写代码,测

我正试图创建一个模型,用R来测量文本中的情感。基本上,使用一个包含情感词的词典,我只想从大量URL中提取“p”(段落)。 我希望通过使用词典,根据预定义的情感指示词的存在情况,找到每个URL中每个情感的字数

我使用的数据是JSON格式的,来自Webrobots:(最新的一组)

任何帮助都将不胜感激,因为我真的非常渴望开始这项工作! 即使只是知道如何将其导入R和一个代码来计算单词,也会有很大的帮助

亲切问候,, 一个绝望的文盲女孩

更新: 数据文件被导入到R中。但是,我找不到一种方法来编写代码,测试是否存在针对数据运行的词典指示的单词。我试图用六种基本情绪(快乐、悲伤、愤怒、惊讶、恐惧、厌恶)的每个活动的计数来创建6个新变量,这六种情绪表示这些情绪的存在

我已经在文件中指出了“p”部分,请仔细查看。我只需要对它的内容进行分类

词汇表下载
  • 第一步是从该链接手动下载(简单的复制和粘贴)词典列表,并将其保存为.csv格式:
  • 然后你需要把这个列表分解成4个独立的部分,每个部分应该有一个影响。这将产生4.csv文件,如下所示:

    anger_list = w.csv
    fear_list  = x.csv
    joy_list   = y.csv
    sad_list   = z.csv
    
    如果您不想手动执行此操作,则有一个替代词典列表,其中数据可直接下载到单独的文件中:

    文本数据下载
  • 您共享的另一个链接()现在似乎既有JSON文件也有csv文件,将其读入R似乎非常简单
  • 清除用于文本提取的URL
  • 我不确定您感兴趣分析的列/字段;因为我在2019年2月下载的数据集没有字段“p”
  • 由于您提到了URL的存在,我也分享了一个可能的编辑或清理URL的简短代码。这将帮助您从URL中获取清晰的文本数据:

    replacePunctuation <- function(x)
    {
    
      # Lowercase all words for convenience
      x <- tolower(x)
    
      # Remove words with multiple consecutive digits in them (3 in this case) 
      x <- gsub("[a-zA-Z]*([0-9]{3,})[a-zA-Z0-9]* ?", " ", x)
    
      # Remove extra punctuation
      x <- gsub("[.]+[ ]"," ",x) # full stop
      x <- gsub("[:]+[ ]"," ",x) # Colon
      x <- gsub("[?]"," ",x)     # Question Marks
      x <- gsub("[!]"," ",x)     # Exclamation Marks
      x <- gsub("[;]"," ",x)     # Semi colon
      x <- gsub("[,]"," ",x)     # Comma
      x <- gsub("[']"," ",x)     # Apostrophe
      x <- gsub("[-]"," ",x)     # Hyphen
      x <- gsub("[#]"," ",x)     
    
      # Remove all newline characters
      x <- gsub("[\r\n]", " ", x)
    
      # Regex pattern for removing stop words
      stop_pattern <- paste0("\\b(", paste0(stopwords("en"), collapse="|"), ")\\b")
      x <- gsub(stop_pattern, " ", x)
    
      # Replace whitespace longer than 1 space with a single space
      x <- gsub(" {2,}", " ", x)
    
      x
    }
    
    现在,为这四个影响中的每一个向该数据框添加额外的列

    df$ANGER   = 0
    df$FEAR    = 0
    df$JOY     = 0
    df$SADNESS = 0
    
    然后,您只需循环遍历df的每一行,根据空格将文本p分解为单词。然后你从你的词典列表中寻找特定词汇出现在你得到的精简单词中。然后为每个影响分配分数,如下所示:

    for (i in 1:nrow(df))
    {
      # counter initialization
      angry = 0
      feared = 0
      joyful = 0
      sad = 0
    
    # for df, let's say the text 'p' is at first column place  
    words <- strsplit(df[i,1], " ")[[1]]  
      for (j in 1:length(words))
      {
        if (words[j] %in% anger_list[,1])
          angry = angry + 1
        else {
          if (words[j] %in% fear_list[,1])   
            feared = feared + 1
          else { 
            if (words[j] %in% joy_list[,1])
              joyful = joyful + 1
            else
              sad = sad + 1
          } #else 2
        } #else 1
      } #for 2
    
      df[i,2] <- angry
      df[i,3] <- feared
      df[i,4] <- joyful
      df[i,5] <- sad
    
    }#for 1
    
    for(1中的i:nrow(df))
    {
    #计数器初始化
    愤怒=0
    恐惧=0
    快乐=0
    sad=0
    #对于df,假设文本“p”位于第一列的位置
    词汇表下载
    
  • 第一步是从该链接手动下载(简单的复制和粘贴)词典列表,并将其保存为.csv格式:
  • 然后您需要将此列表分解为4个单独的部分,每个部分应有一个影响。这将生成4个.csv文件,如下所示:

    anger_list = w.csv
    fear_list  = x.csv
    joy_list   = y.csv
    sad_list   = z.csv
    
    如果您不想手动执行此操作,则有一个替代词典列表,其中数据可直接下载到单独的文件中:

    文本数据下载
  • 您共享的另一个链接()现在似乎既有JSON文件也有csv文件,将其读入R似乎非常简单
  • 清除用于文本提取的URL
  • 我不确定您感兴趣分析的列/字段;因为我在2019年2月下载的数据集没有字段“p”
  • 由于您提到了URL的存在,我也分享了一个可能编辑或清理URL的简短代码。这将帮助您从URL中获得干净的文本数据:

    replacePunctuation <- function(x)
    {
    
      # Lowercase all words for convenience
      x <- tolower(x)
    
      # Remove words with multiple consecutive digits in them (3 in this case) 
      x <- gsub("[a-zA-Z]*([0-9]{3,})[a-zA-Z0-9]* ?", " ", x)
    
      # Remove extra punctuation
      x <- gsub("[.]+[ ]"," ",x) # full stop
      x <- gsub("[:]+[ ]"," ",x) # Colon
      x <- gsub("[?]"," ",x)     # Question Marks
      x <- gsub("[!]"," ",x)     # Exclamation Marks
      x <- gsub("[;]"," ",x)     # Semi colon
      x <- gsub("[,]"," ",x)     # Comma
      x <- gsub("[']"," ",x)     # Apostrophe
      x <- gsub("[-]"," ",x)     # Hyphen
      x <- gsub("[#]"," ",x)     
    
      # Remove all newline characters
      x <- gsub("[\r\n]", " ", x)
    
      # Regex pattern for removing stop words
      stop_pattern <- paste0("\\b(", paste0(stopwords("en"), collapse="|"), ")\\b")
      x <- gsub(stop_pattern, " ", x)
    
      # Replace whitespace longer than 1 space with a single space
      x <- gsub(" {2,}", " ", x)
    
      x
    }
    
    现在,为这四个影响中的每一个向该数据框添加额外的列

    df$ANGER   = 0
    df$FEAR    = 0
    df$JOY     = 0
    df$SADNESS = 0
    
    然后,您只需在df的每一行中循环,将文本p分解为基于空白的单词。然后,您可以从词典列表中查找特定词汇出现的情况,并将其添加到精简的单词中。然后,您为每个影响分配分数,如下所示:

    for (i in 1:nrow(df))
    {
      # counter initialization
      angry = 0
      feared = 0
      joyful = 0
      sad = 0
    
    # for df, let's say the text 'p' is at first column place  
    words <- strsplit(df[i,1], " ")[[1]]  
      for (j in 1:length(words))
      {
        if (words[j] %in% anger_list[,1])
          angry = angry + 1
        else {
          if (words[j] %in% fear_list[,1])   
            feared = feared + 1
          else { 
            if (words[j] %in% joy_list[,1])
              joyful = joyful + 1
            else
              sad = sad + 1
          } #else 2
        } #else 1
      } #for 2
    
      df[i,2] <- angry
      df[i,3] <- feared
      df[i,4] <- joyful
      df[i,5] <- sad
    
    }#for 1
    
    for(1中的i:nrow(df))
    {
    #计数器初始化
    愤怒=0
    恐惧=0
    快乐=0
    sad=0
    #对于df,假设文本“p”位于第一列的位置
    
    欢迎来到SO,提问时请更具体一点:您尝试了什么,您期望什么,等等。请参阅欢迎来到SO,提问时请更具体一点:您尝试了什么,您期望什么,等等。请参阅