#写在前面的话:此教程主要是用R连接了DB2数据库,并进行文本分析,制作了词图

#教程为markdown编写

---
title: "网站留言分析"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo=FALSE,error=FALSE,warning=FALSE,message = F,comment="")
```

```{r}
#连接数据库并读取数据
library(RODBC)
options(scipen=200)
conn = odbcConnect(dsn="DB2",uid="tjfx","1")
myfile<-sqlQuery(conn,"select * from T_from_last_year_liuyan")
myfile<-myfile[,4]
#head(myfile)

#调入分词的库

library("rJava")

library("Rwordseg")

#调入绘制词云的库

library("RColorBrewer")

library("wordcloud")

#预处理,这步可以将读入的文本转换为可以分词的字符,没有这步不能分词

myfile.res <- myfile[myfile!=" "]
```
```{r,echo=F,error=FALSE,warning=FALSE,message = F,comment="",results='hide'}
#载入语料库,搞好词库后,用下面的语句就可以装入内存:
installDict("ciku.txt",dictname = "my dict")
```
```{r}
#查看装载的词典:

#listDict()

#删除安装的词典:

#as.charecter
myfile.res<-as.character(myfile.res)
#分词,并将分词结果转换为向量

myfile.words <- unlist(lapply(X = myfile.res,FUN = segmentCN))

#剔除URL等各种不需要的字符,还需要删除什么特殊的字符可以依样画葫芦在下面增加gsub的语句

myfile.words <- gsub(pattern="http:[a-zA-Z\\/\\.0-9]+","",myfile.words)

myfile.words <- gsub("\n","",myfile.words)

myfile.words <- gsub(" ","",myfile.words)

#去掉停用词

data_stw=read.table(file="mystopword.txt",colClasses="character")

stopwords_CN=c(NULL)

for(i in 1:dim(data_stw)[1]){

stopwords_CN=c(stopwords_CN,data_stw[i,1])

}

for(j in 1:length(stopwords_CN)){

myfile.words <- subset(myfile.words,myfile.words!=stopwords_CN[j])

}

#过滤掉1个字的词

myfile.words <- subset(myfile.words, nchar(as.character(myfile.words))>1)
#统计词频

myfile.freq <- table(unlist(myfile.words))

myfile.freq <- rev(sort(myfile.freq))

myfile.freq <- data.frame(word=names(myfile.freq), freq=myfile.freq)

#按词频过滤词,过滤掉只出现过一次的词,这里可以根据需要调整过滤的词频数

myfile.freq2=subset(myfile.freq, myfile.freq$freq>=5)

#绘制词云

#设置一个颜色系:

mycolors <- brewer.pal(8,"Dark2")

#设置字体

windowsFonts(myFont=windowsFont("微软雅黑"))

#画图
set.seed(123)

wordcloud(myfile.freq2$word,myfile.freq2$freq,random.order=FALSE,

random.color=FALSE,colors=mycolors,family="myFont")

write.table(myfile.freq2,row.names=F,quote = FALSE )

#结果图:

输出的词频文档删除了行号,详见代码:

R 连接DB2数据库,并制作词图的更多相关文章

  1. R 连接DB2数据库

    1.odbc文件下载 教程: http://dasapp.oregon.gov/datamart/files/IBM_DB2_9.7_Run_Time_client_Notes.pdf 驱动地址: h ...

  2. 连接db2数据库时NumberFormatException异常的解决方式

    连接db2数据库时报异常:java.lang.NumberFormatException: For input string: "A" from a DB2 JDBC(JCC) j ...

  3. c#连接db2数据库

    .net项目要连接db2数据库,是要安装客户端的,否则是连接不上的: 若出现“未在本地计算机上注册‘ibmdadb2’提供程序” 解决办法: 1.先找到安装后的ibmdadb2.dll文件复制到c:\ ...

  4. 远程连接db2数据库

    在db2数据库中,需要理解catalog(编目)这个概念,理解前先说下db2数据库的体系结构:由系统(节点)也就是主机,下面是实例,实例下面是数据库,然后是表空间,再是数据库对象.现在假设你有一个数据 ...

  5. R连接mysql数据库方法详解

    Warning messages: 1: In odbcDriverConnect("DSN=Rdata;UID=root") : [RODBC] ERROR: state IM0 ...

  6. 使用开源数据库客户端DBeaver连接DB2数据库

    下载安装 首先进入 官网 选择对应的版本进行安装. 下载下来后,一直惦记next即可完成安装(期间包括选择文件安装路径等操作,可按需修改). 连接db2 打开DBeaver,新建连接-->DBe ...

  7. jsp连接MYSQL数据库教程(文字+图)

    步骤: 1.在mysql官网下载JDBC驱动程序.网址:https://dev.mysql.com/downloads/connector/j/ 2.把里面的jar包(mysql-connector- ...

  8. R 连接mysql数据库

    一.配置RODBC 1.R下载RODBC包,安装好.2.在http://dev.mysql.com/downloads/connector/odbc下载mySQL ODBC,安装好.3.windows ...

  9. 连接db2数据库出现No buffer space available (maximum connections reached?)

    Caused by: javax.naming.NamingException: [jcc][t4][2043][11550][3.57.82] 异常 java.net.SocketException ...

随机推荐

  1. 移动端自动化测试-WTF Appium

    手机App分为两大类,原生App(Native App)和混合APP(Hybrid App) 原生App(Native App) 原生App实际就是我们所常见的传统App开发模式,云端数据存储+App ...

  2. Ubuntu安装RTX2080显卡驱动

    安装RTX2080显卡驱动 近日新购了一台DELL服务器,用于TensorFlow,由于显卡是另加的,需要安装显卡驱动. 服务器配置 服务器型号:DELL PowerEdge R730 CPU:2*I ...

  3. php第八节课

    加载 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.o ...

  4. lamp平台搭建论坛网站(Discuz论坛)

    1. 安装Apache 1) 安装apr [root@www lamp]# yum install zlib-devel gcc gcc-c++ openssl-devel pcre-devel -y ...

  5. Linux思维导图之rpm、yum、编译

    yum安装失败: 1.yum client 路径指向不正确:2.yum server 缓存未清理(yum clean all;yum makecache):3.网络不连通

  6. 关于 多个git用户或多个git管理工具切换时出现的问题总结

    在这几天遇到了个比较头痛的问题 因为在同时使用多个git工具(gitlab,github.gitee)由于账户的问题和这个仓库指定地址,导致拉代码和推代码不能正常运行 问题解决: 对于多个git直接的 ...

  7. php中的empty()方法

    empty() 判断一个变量是否为“空”,isset() 判断一个变量是否已经设置.empty还会检测变量是否为空.为零.当一个变量值为0,empty() 认为这个变量同等于空,即相当于没有设置.例如 ...

  8. webpack 配置 Vue 多页应用 —— 从入门到放弃

    webpack 配置 Vue 多页应用 -- 从入门到放弃 一直以来,前端享有无需配置,一个浏览器足矣的优势,直到一大堆构建工具的出现,其中 webpack 就是其中最复杂的一个,因此出现了一个新兴职 ...

  9. django-celery-win10

    setting初始化: 定时任务 增加tasks 启动:

  10. (28)SpringBoot启动时的Banner设置【从零开始学Spring Boot】

    对于使用过Spring Boot的开发者来说,程序启动的时候输出的由字符组成的Spring符号并不陌生.这个是Spring Boot为自己设计的Banner: 1.    .   ____       ...