介绍

可口可乐(Coca-Cola)和百事可乐(PepsiCo)是软饮料行业的知名品牌,两家公司均跻身《财富》500强。在竞争激烈的市场中拥有广泛产品线的公司彼此之间存在着激烈的竞争,并在随后的几乎所有垂直产品市场中不断争夺市场份额。

通过从每家公司的官方推特下载5000条推文来分析这两家公司的客户情绪,并在R中进行分析。在这一分析中,我们可以了解如何从品牌的社交媒体参与(在本例中为推特)中分析客户情绪。

目录

  • 涉及的软件包及其应用

  • 什么是情绪分析?

  • 清除文本

  • 词云

  • 在一天和一周内发布推文

  • 推特数据的情感评分

  • 客户推特的情感分析

  • 结论

R中使用的软件包

什么是情绪分析?

情感分析是一种文本挖掘技术,它为文本提供上下文,能够从主观抽象的源材料中理解信息,借助Facebook、Instagram等社交媒体平台上的在线对话,帮助理解对品牌产品或服务的社会情感,推特或电子邮件。众所周知,计算机不理解我们的通用语言,为了让他们理解自然语言,我们首先将单词转换成数字格式。接下来我们将尝试一步一步地去实现这一过程。

清除文本

我们已经从Twitter下载了数据集,由于推特的文本形式包含了链接、hashtags、推特er句柄名称和表情符号,为了删除它们,我们在R中编写了函数ions。删除这些无用信息后,所有文本都将转换为小写,删除英语中没有意义的停止词(如冠词、介词等)、标点符号和数字,然后再将它们转换为文档术语矩阵。

文档术语矩阵:是一个矩阵,包含每个单词在每个文档上出现的次数。

removeURL <- function(x) gsub(“(f|ht)tp(s?)://\S+”, “”, x, perl=T)removeHashTags <- function(x) gsub(“#\S+”, “”, x)removeTwitterHandles <- function(x) gsub(“@\S+”, “”, x)removeSlash <- function(x) gsub(“n”,” “, x)removeEmoticons <- function(x) gsub(“[^x01-x7F]”, “”, x)data_pepsi$text <- iconv(data_pepsi$text, to = “utf-8”)pepsi_corpus <- Corpus(VectorSource(data_pepsi$text))pepsi_corpus <- tm_map(pepsi_corpus,tolower)pepsi_corpus <- tm_map(pepsi_corpus,removeWords,stopwords(“en”))pepsi_corpus <- tm_map(pepsi_corpus,content_transformer(removeHashTags))pepsi_corpus <- tm_map(pepsi_corpus,content_transformer(removeTwitterHandles))pepsi_corpus <- tm_map(pepsi_corpus,content_transformer(removeURL))pepsi_corpus <- tm_map(pepsi_corpus,content_transformer(removeSlash))pepsi_corpus <- tm_map(pepsi_corpus,removePunctuation)pepsi_corpus <- tm_map(pepsi_corpus,removeNumbers)pepsi_corpus <- tm_map(pepsi_corpus,content_transformer(removeEmoticons))pepsi_corpus <- tm_map(pepsi_corpus,stripWhitespace)pepsi_clean_df <- data.frame(text = get(“content”, pepsi_corpus))dtm_pepsi <- DocumentTermMatrix(pepsi_corpus)dtm_pepsi <- removeSparseTerms(dtm_pepsi,0.999)pepsi_df <- as.data.frame(as.matrix(dtm_pepsi))data_cola$text <- iconv(data_cola$text, to = “utf-8”)cola_corpus <- Corpus(VectorSource(data_cola$text))cola_corpus <- tm_map(cola_corpus,tolower)cola_corpus <- tm_map(cola_corpus,removeWords,stopwords(“en”))cola_corpus <- tm_map(cola_corpus,content_transformer(removeHashTags))cola_corpus <- tm_map(cola_corpus,content_transformer(removeTwitterHandles))cola_corpus <- tm_map(cola_corpus,content_transformer(removeURL))cola_corpus <- tm_map(cola_corpus,content_transformer(removeSlash))cola_corpus <- tm_map(cola_corpus,removePunctuation)cola_corpus <- tm_map(cola_corpus,removeNumbers)cola_corpus <- tm_map(cola_corpus,content_transformer(removeEmoticons))cola_corpus <- tm_map(cola_corpus,stripWhitespace)cola_clean_df <- data.frame(text = get(“content”, cola_corpus))dtm_cola <- DocumentTermMatrix(cola_corpus)dtm_cola <- removeSparseTerms(dtm_cola,0.999)cola_df <- as.data.frame(as.matrix(dtm_cola))

词云

wordcloud是测试数据的一种表示形式,它通过增加测试数据的大小来突出显示最常用的单词,该技术用于将文本可视化为图像,是单词或标签的集合。在R中,可以使用worldcloud2包来实现,以下是它的输出代码。

word_pepsi_df <- data.frame(names(pepsi_df),colSums(pepsi_df))names(word_pepsi_df) <- c(“words”,”freq”)word_pepsi_df <- subset(word_pepsi_df, word_pepsi_df$freq > 0)wordcloud2(data = word_pepsi_df,size = 1.5,color = “random-light”,backgroundColor = “dark”)word_cola_df <- data.frame(names(cola_df),colSums(cola_df))names(word_cola_df) <- c(“words”,”freq”)word_cola_df <- subset(word_cola_df, word_cola_df$freq > 0)wordcloud2(data = word_cola_df,size = 1.5,color = “random-light”,backgroundColor = “dark”)

百事可乐和可口可乐的推特数据的词云

正如我们所知,词云中的词大小取决于其在推特中的频率,因此词会不断变化, just, native, right, racism很多出现在百事可乐客户的推特中,而get和support等词更多地出现在可口可乐客户的推特中。

在一天和一周内发布推文

由于推特收集的时间跨度超过一周,因此我们可以分析大多数用户活跃或用户在该品牌上发布最多推文的时间和工作日,这可以通过使用ggplot2库的折线图来可视化。下面是与输出一起使用的函数

data_pepsi$Date <- as.Date(data_pepsi$created_at)data_pepsi$hour <- hour(data_pepsi$created_at)data_pepsi$weekday<-factor(weekdays(data_pepsi$Date),levels=c(“Monday”,”Tuesday”,”Wednesday”,”Thursday”,”Friday”,”Saturday”,”Sunday”))ggplot(data_pepsi,aes(x= hour)) + geom_density() + theme_minimal() + ggtitle(“Pepsi”)ggplot(data_pepsi,aes(x= weekday)) + geom_bar(color = “#CC79A7”, fill = “#CC79A7”) + theme_minimal() +ggtitle(“Pepsi”) + ylim(0,1800)data_cola$Date <- as.Date(data_cola$created_at)data_cola$Day <- day(data_cola$created_at)data_cola$hour <- hour(data_cola$created_at)data_cola$weekday<-factor(weekdays(as.Date(data_cola$Date)),levels=c(“Monday”,”Tuesday”,”Wednesday”,”Thursday”,”Friday”,”Saturday”,”Sunday”))ggplot(data_cola,aes(x= hour)) + geom_density() + theme_minimal() + ggtitle(“Coca-Cola”)ggplot(data_cola,aes(x=
weekday)) + geom_bar(color = “#CC79A7”, fill = “#CC79A7”) + theme_minimal()

从上面的图表中,我们可以看到百事可乐和可口可乐在下午3-4点和凌晨1点左右都出现了峰值,因为人们喜欢在工作无聊或深夜使用社交媒体,这在我们的工作中是显而易见的。

一周内推特的分布情况

当每日推文显示在条形图上时,对于百事来说,周四是推特数量最多的一天,这是因为他们发布了季度报告,但就可口可乐而言,周二我们看到的推特数量最少。

推特数据的情感评分

在本节中,我们把推特数据分为积极的、消极的和中立的,这可以通过使用sendimentR包来实现,该软件包为每个词典单词分配一个从-1到+1的情感评分,并取推特中每个单词的平均值,得到每个推特的最终情感评分。

sentiments <- sentiment_by(get_sentences(pepsi_clean_df$text))data$sentiment_score <- round(sentiments$ave_sentiment,2)data$sentiment_score[data_pepsi$sentiment_score > 0] <- “Positive”data$sentiment_score[data_pepsi$sentiment_score < 0] <- “Negative”data$sentiment_score[data_pepsi$sentiment_score == 0] <- “Neutral”data$sentiment_score <- as.factor(data$sentiment_score)ggplot(data,aes(x = sentiment_score)) + geom_bar(color = “steelblue”, fill = “steelblue”) + theme_minimal()

几乎75%的推特用户都持肯定态度,因为这两个品牌在他们的客户中相当受欢迎。

顾客推特的情感分析

推特的情绪是由Syuzhet软件包执行的,该软件包根据十个情绪指数对每个词典单词进行评分,包括愤怒、预期、厌恶、恐惧、喜悦、悲伤、惊讶、信任、消极和积极。

如果我们把索引上每个词的值加起来,所有推特的情绪都可以用条形图表示。

cols <- c(“red”,”pink”,”green”,”orange”,”yellow”,”skyblue”,”purple”,”blue”,”black”,”grey”)pepsi_sentimentsdf <- get_nrc_sentiment(names(pepsi_df))barplot(colSums(pepsi_sentimentsdf),main = “Pepsi”,col = cols,space = 0.05,horiz = F,angle = 45,cex.axis = 0.75,las = 2,srt = 60,border = NA)cola_sentimentsdf <- get_nrc_sentiment(names(cola_df))barplot(colSums(cola_sentimentsdf),main = “Coca-Cola”,col = cols,space = 0.05,horiz = F,angle = 45,cex.axis = 0.75,las = 2,srt = 60,border = NA)

上面的输出是所有情绪在条形图上的显示,因为从条形图可以很清楚地看出,积极性对两家公司都起主导作用,这进一步加强了我们的上述假设。继续跟踪图表中的变化可以作为对新产品或广告的反馈。

最常用词

word_pepsi_df$words <- factor(word_pepsi_df$words, levels = word_pepsi_df$words[order(word_pepsi_df$freq)])word_cola_df$words <- factor(word_cola_df$words, levels = word_cola_df$words[order(word_cola_df$freq)])ggplot(word_pepsi_df[1:15,],aes(x = freq, y = words)) + geom_bar(stat = “identity”, color = “#C4961A”,fill = “#C4961A”) + theme_minimal() + ggtitle(“Pepsi”)ggplot(word_cola_df[1:15,],aes(x = freq, y = words)) + geom_bar(stat = “identity”, color = “#C4961A”,fill = “#C4961A”) + theme_minimal() + ggtitle(“Coca-Cola”)createNgram <-function(stringVector, ngramSize){ngram <- data.table()ng <- textcnt(stringVector, method = “string”, n=ngramSize, tolower = FALSE)if(ngramSize==1){ngram <- data.table(w1 = names(ng), freq = unclass(ng), length=nchar(names(ng)))}else {ngram <- data.table(w1w2 = names(ng), freq = unclass(ng), length=nchar(names(ng)))}return(ngram)}pepsi_bigrams_df <- createNgram(pepsi_clean_df$text,2)cola_bigrams_df <- createNgram(cola_clean_df$text,2)pepsi_bigrams_df$w1w2 <- factor(pepsi_bigrams_df$w1w2,levels = pepsi_bigrams_df$w1w2[order(pepsi_bigrams_df$freq)])cola_bigrams_df$w1w2 <- factor(cola_bigrams_df$w1w2,levels = cola_bigrams_df$w1w2[order(cola_bigrams_df$freq)])names(pepsi_bigrams_df) <- c(“words”, “freq”, “length”)names(cola_bigrams_df) <- c(“words”, “freq”, “length”)ggplot(pepsi_bigrams_df[1:15,],aes(x = freq, y = words)) + geom_bar(stat = “identity”, color = “#00AFBB”,fill = “#00AFBB”) + theme_minimal() + ggtitle(“Pepsi”)ggplot(cola_bigrams_df[1:15,],aes(x = freq, y = words)) + geom_bar(stat = “identity”, color = “#00AFBB”,fill = “#00AFBB”) + theme_minimal() + ggtitle(“Coca-Cola”)

二元语法

二元语法是一对字词,当句子被拆分成两个字词时产生的。获取单词的上下文是有用的,因为单个单词通常不提供任何上下文。

结论

我们可以看到,从现有的社交媒体参与度来看,公司可以分析客户的情绪,并据此制定业务战略,来用于制定公司决策(例如启动产品线)。

☆ END ☆

如果看到这里,说明你喜欢这篇文章,请转发、点赞。微信搜索「uncle_pn」,欢迎添加小编微信「 mthler」,每日朋友圈更新一篇高质量博文。

扫描二维码添加小编↓

使Twitter数据对百事可乐和可口可乐进行客户情感分析相关推荐

  1. 使用大数据去挖掘每个用户的客户价值-RFM

    絮叨两句: 博主是一名数据分析实习生,利用博客记录自己所学的知识,也希望能帮助到正在学习的同学们 人的一生中会遇到各种各样的困难和折磨,逃避是解决不了问题的,唯有以乐观的精神去迎接生活的挑战 少年易老 ...

  2. 大数据毕设选题 - 大数据网络舆情热点数据分析系统(情感分析 python)

    文章目录 0 前言 1 课题背景 2 实现效果 3 文本情感分析 3 Django 4 爬虫 5 最后 0 前言

  3. Twitter情感分析及其可视化

    向AI转型的程序员都关注了这个号??? 大数据挖掘DT数据分析  公众号: datadw 本文 github 地址 在公众号 datadw 里 回复 推特 即可获取. 主要是基于twitter的内容有 ...

  4. 情感分析论文中涉及的数据

    基于三支决策的多粒度文本情感分类模型–张越兵.苗夺谦. 张志飞. 中文语料库选用谭松波在携程(http://www.ctrip.com)上采集的酒店评论ChnSentiCorp-Htl-ba-600, ...

  5. 基于Python实现的论坛帖子文本情感分析完整代码+数据 可直接运行 毕业设计

    完整代码:https://download.csdn.net/download/qq_38735017/87425721 一.课程项目 文本分类分析 二.项目类容 爬取川大匿名社区SCUinfo在一段 ...

  6. Twitter教程:如何下载Twitter数据副本?

    Twitterrific Mac版是一款非常现代化优秀的桌面Twitter客户端,这款软件不仅看起来惊人,它的表现也是非常棒的,本文给大家的带来了如何下载Twitter数据副本的教程.教程如下 在 i ...

  7. 什么是大数据公司面临的问题以及如何使用大数据解决

    Heyy EveryOne ..!! 大家好.. !! In this article, you will come to know- what is Hadoop, Big Data & D ...

  8. 蠎周刊418 ~Pandas DataFrame: 使处理数据令人愉快

    原文: PyCoder's Weekly - Issue #418 200429 Zoom.Quiet(大妈) 用时 42 分钟 完成快译 200429 Zoom.Quiet(大妈) 用时 37 分钟 ...

  9. python收集数据程序_用Python挖掘Twitter数据:数据采集

    原标题:用Python挖掘Twitter数据:数据采集 作者:Marco Bonzanini 翻译:数盟 这是7部系列中的第1部分,注重挖掘Twitter数据以用于各种案例.这是第一篇文章,专注于数据 ...

最新文章

  1. GitHub/GitLab/Gitee中项目互拷贝后仍保留历史提交记录的方法
  2. 剑指offer青蛙跳台阶问题
  3. ASP.NET前台JS与后台CS函数如何互相调用
  4. Gradle Goodness: Set Java Compiler Encoding--转载
  5. Java 学习网站汇总贴
  6. php用switch编写车费的输出,PHP Switch语句在实际代码中的应用
  7. 【Python】cv2.error: ... (-215:Assertion failed) ssize.empty() in function ‘cv::resize’ 的解决方法
  8. 怎么获取codeforces的数据_飞瓜数据5大功能盘点,帮你抓住2019抖音新一波涨粉红利期!...
  9. 学习型php空间,php开源学习型框架CMVC v1.2.0
  10. hbase1.3版本启动流程及优化
  11. 进入docker容器中查看文件夹_理解 docker 容器中的 uid 和 gid
  12. 阿里云 maven 镜像地址
  13. Python用户画像词云图生成并集成到django网站
  14. python复制网页文字_我用Python在网上复制文字的几种实用方法
  15. ADSL拨号代理服务器实现HTTP代理的搭建过程
  16. python图片提取文字
  17. java 高效列转行,java 列转行
  18. Things3 3.13.13 一款优秀的GTD任务管理工具
  19. 第104篇 Compound 中的 cTokens
  20. JOS学习笔记(一)

热门文章

  1. TCP三次握手和四次挥手最通俗解释说明
  2. 【备忘】 modbusTcp4个寄存器电力遥信遥控对应
  3. manjaro docker安装使用
  4. npm install安装报错 npm ERR! code Z_BUF_ERROR 问题解决
  5. 为什么引入非线性激励函数
  6. 基于vi构建强大的IDE
  7. 显卡显存测试u盘 mats_影驰RTX 2080 Ti HOF Plus显卡评测:披坚执锐的性能王冠守护者...
  8. 推荐 7 个牛哄哄的电商项目
  9. 【数字IC验证快速入门】11、Verilog TestBench(VTB)入门
  10. VS2017MFC发布打包