【发布时间】:2022-01-10 06:21:40
【问题描述】:
到目前为止,我已经在 R 代码中完成了以下操作。我正在执行 Twitter 情绪分析(正面/负面),我需要使用分类模型,例如:逻辑回归、SVM 等。截至目前,我已经删除了空格、网址、表情符号等。创建一个新列“整洁的推文”和对其进行标记。然后我在条形图上绘制最常见的单词。现在,我想实现 Word2Vec 技术来标记推文并在我的模型中使用它(在 R 中)。但我不知道该怎么做。有人可以帮我吗? csv文件链接:https://drive.google.com/file/d/1ARqEt75G1UcUpfdBtae1yEvurydeE2vr/view?usp=sharing
谢谢!
library(xgboost)
library(readr)
library(stringr)
library(caret)
library(car)
library(tidytext)
library(stringr)
library(tidyr)
library(dplyr)
set.seed(123)
twitter_train<-read.csv("/Users/R/Final Training Data Set-twitter.csv")
text<-twitter_train$tweet
text <- tolower(text)
# Remove mentions, urls, emojis, numbers, punctuations, etc.
text <- gsub("@\\w+", "", text)
text <- gsub("https?://.+", "", text)
text <- gsub("\\d+\\w*\\d*", "", text)
text <- gsub("#\\w+", "", text)
text <- gsub("[^\x01-\x7F]", "", text)
text <- gsub("[[:punct:]]", " ", text)
# Remove spaces and newlines
text <- gsub("\n", " ", text)
text <- gsub("^\\s+", "", text)
text <- gsub("\\s+$", "", text)
text <- gsub("[ |\t]+", " ", text)
#Create new column to store cleaned tweets
twitter_train["fix_text"] <- text
head(twitter_train$fix_text, 10)
# Convert to tidy format
tidy_text <- twitter_train %>%
select(id,label,fix_text) %>%
#Tokenize the word from the tweets
unnest_tokens(input = fix_text, output = word) %>%
# Remove stop words
anti_join(stop_words, by="word")
#Plotting most common words in corpus
tidy_text %>% # gives you a bar chart of the most frequent words found in the tweets
count(word, sort = TRUE) %>%
top_n(30) %>%
mutate(word = reorder(word, n)) %>%
ggplot(aes(x = word, y = n)) +
geom_col() +
xlab(NULL) +
coord_flip() +
labs(y = "Count",
x = "Unique words",
title = "Most frequent words found in the dataset",
subtitle = "Stop words removed from the list")
【问题讨论】:
-
请修剪您的代码,以便更容易找到您的问题。请按照以下指南创建minimal reproducible example。