美文网首页
R文本分析(二)

R文本分析(二)

作者: 小豆角lch | 来源:发表于2017-07-20 14:40 被阅读0次

文本预处理

1.设置路径

setwd("e://r语言学习//r代码")

2.加载词典

installDict("D:\\R\\sources\\Dictionaries\\news.scel",dictname = "news1")

installDict("D:\\R\\sources\\Dictionaries\\news2.scel",dictname = "news2")

listDict()

3.加载文档

data <-readLines("d:\\R\\RWorkspace\\fhnews.txt",encoding ="UTF-8")

4.去除特殊词

dataTemp <- gsub("[0-90123456789 < > ~]","",data)

5.分词

dataTemp <- segmentCN(dataTemp)

dataTemp[1:2]

6.去除停用词

stopwords<- unlist(read.table("D:\\R\\RWorkspace\\StopWords.txt",stringsAsFactors=F))

stopwords[50:100]

removeStopWords <- function(x,stopwords) {

temp <- character(0)

index <- 1

xLen <- length(x)

while (index <= xLen) {

if (length(stopwords[stopwords==x[index]]) <1)

temp<- c(temp,x[index])

index <- index +1

}

temp

}

> dataTemp2 <-lapply(dataTemp,removeStopWords,stopwords)

> dataTemp2[1:2]

文本分类

通过词频的余弦相似度做文本分类

1.加载语料库

library("tm")

reuters =VCorpus(VectorSource(doc_CN))

reuters <- tm_map(reuters, stripWhitespace)

2.删除停用词

data_stw<- unlist (read.table("E:\\text mining\\stopword\\中文停用词.txt",stringsAsFactors=F))

#head(data_stw,n=10)

reuters=tm_map(reuters,removeWords,data_stw)

3.生成TF-IDF特征

control=list(removePunctuation=T,minDocFreq=5,wordLengths = c(1, Inf),weighting = weightTfIdf)

doc.tdm=TermDocumentMatrix(reuters,control)

length(doc.tdm$dimnames$Terms)

tdm_removed=removeSparseTerms(doc.tdm, 0.97)

length(tdm_removed$dimnames$Terms)

mat = as.matrix(tdm_removed)####转换成文档矩阵

classifier = naiveBayes(mat[1:x,], as.factor(data$标题[1:x]) )##贝叶斯分类器,训练

predicted = predict(classifier, mat[z:y,]);#预测

A=table(data$标题[z:y], predicted)#预测交叉矩阵

predicted财经 禅道 军事 科技

财经  10  28    34    1

禅道    0  41    4    0

军事    0  10    25    0

科技    4  21    18  11

b1=length(which(predicted==data$标题[z:y]))/length(predicted)#计算召回率

b1[1] 0.4202899

补充:其它机器学习分类算法

library(RTextTools)

container = create_container(mat[1:y,], as.factor(data$标题[1:y]) ,

trainSize=1:x, testSize=1:y,virgin=TRUE)

models = train_models(container, algorithms=c("BAGGING" ,  "MAXENT" ,  "NNET" ,    "RF"    ,    "SVM" ,    "TREE" ))

results = classify_models(container, models)

#How about the accuracy?

# recall accuracy

森林=recall_accuracy(as.numeric(as.factor(data$标题[z:y])), results[,"FORESTS_LABEL"])

最大熵=recall_accuracy(as.numeric(as.factor(data$标题[z:y])), results[,"MAXENTROPY_LABEL"])

决策树=recall_accuracy(as.numeric(as.factor(data$标题[z:y])), results[,"TREE_LABEL"])

袋袋=recall_accuracy(as.numeric(as.factor(data$标题[z:y])), results[,"BAGGING_LABEL"])

向量机=recall_accuracy(as.numeric(as.factor(data$标题[z:y])), results[,"SVM_LABEL"])

神经网络=recall_accuracy(as.numeric(as.factor(data$标题[z:y])), results[,"NNETWORK_LABEL"])

a=c()

c=c()

e=c()

a=cbind( 随机森林=as.vector(results[,"FORESTS_LABEL"]), 决策树=as.vector(results[,"TREE_LABEL"]) , 支持向量机=as.vector(results[,"SVM_LABEL"]),贝叶斯=as.vector(predicted), 最大熵=as.vector(results[,"MAXENTROPY_LABEL"]),袋袋=as.vector(results[,"BAGGING_LABEL"]),神经网络=as.vector( results[,"NNETWORK_LABEL"]))

for(i in 1:length(results[,"FORESTS_LABEL"][z:y]))

{

b=table(a[i,])

c[i]<-names(which(b==max(table(a[i,]))))

}

模型预测=cbind(a,组合模型=c)

A=table(data$标题[z:y],c)

b=length(which(c==data$标题[z:y]))/length(c)

组合模型=b

e=c(贝叶斯=b1,森林=森林,最大熵=最大熵,决策树=决策树,袋袋=袋袋,向量机=向量机,神经网络=神经网络,组合投票=组合模型)

##结果该满意了吧!!!

e  贝叶斯      森林    最大熵    决策树      袋袋    向量机  神经网络  组合投票

0.4202899 1.0000000 1.0000000 0.5893720 1.0000000 0.3526570 0.9033816 1.0000000

文本聚类

文本聚类就没什么技术含量了,主要原因是其实非监督学习,效果一般不是很好。

data=t(mat[,1:50])

data.scale <- scale(data)

d <- dist(data.scale, method = "euclidean")

fit <- hclust(d, method="ward.D")

plot(fit,main="文本聚类")

相关文章

  • R文本分析(二)

    文本预处理 1.设置路径 setwd("e://r语言学习//r代码") 2.加载词典 installDict("...

  • R文本分析(一)

    Part1安装依赖包 http://blog.csdn.net/cl1143015961/article/deta...

  • R文本分析(三)

    主题模型训练 ############################################ libra...

  • Python读写本地文件

    func、arguments open() 函数 r : 读文本rb : 读二进制w : 写文本wb : 写二进制...

  • QQ文本分析—R语言

    1、参考文献 设置超链接:时间分布和成员分析;QQ聊天内容;recharts安装包官方介绍 2、读入QQ聊天记录 ...

  • R语言 数据挖掘-文本分析(1)

    刚接触R语言一周,和matab不同R作用于数据挖掘的库很多,详解见 R语言数据挖掘包,下面简介文本分析经常使用到的...

  • R文本挖掘:文本主题分析topic analysis

    对于海量未知内容文本的挖掘,主题分析是一个常见的技巧,在主题模型中,主题表示一个概念、一个方面,表现为一系列相关的...

  • R语言爬虫与文本分析

    原文链接:R语言爬虫与文本分析 微信公众号:机器学习养成记 搜索添加微信公众号:chenchenwings 之前...

  • 基于R的文本分析01

    第一步:载入分词包 第二步:装载需要分析的文本 第三步:分词 第四步:建立语料库 第五步:内容分析(聚类分析) 附图:

  • R文本挖掘:社会网络分析

    社会网络分析是综合运用图论、数学模型来研究行动者与行动者、行动者与其所处社会网络、以及一个社会网络与另一社会网络之...

网友评论

      本文标题:R文本分析(二)

      本文链接:https://www.haomeiwen.com/subject/hwodkxtx.html