首页 » 网站建设 » php取csv繁体技巧_用R措辞进行中文处理分词和制作文字云

php取csv繁体技巧_用R措辞进行中文处理分词和制作文字云

访客 2024-11-19 0

扫一扫用手机浏览

文章目录 [+]

tm套件-文本挖掘

tm套件紧张用于数据清理和语料库导出。
可以打消空缺字符、数字、英文标点符号及与号、英文停用词、中文标点符号、中文停用词。

准备事情

php取csv繁体技巧_用R措辞进行中文处理分词和制作文字云

#现在D:\R\tm 建立Corpus和Corpus_OUT两个文件夹

php取csv繁体技巧_用R措辞进行中文处理分词和制作文字云
(图片来自网络侵删)

#在Corpus建立记事本(存档时选择ANSI编码)

#记事本1 tm_A.txt 本日是西元2020年3月4日.早上出门是暴雨,因此,全身湿哒哒的,连鞋 子也湿了!

#记事本2 tm_B.txt 本日是西元2020年3月5日. It's raining today outside, I'm going to the office.

#记事本3 tm_C.txt 本日是西元2020年3月6日. I'm so busy today.

#安装tm套件

install.packages("tm",destdir = "E:\\runtime\\Rlibrary")

library(tm)

### 载入资料 ###

#把文件夹内的记事本文件输入资料库

Corpus_A<-Corpus(DirSource("D:\\R\\tm\\Corpus"))

#检视资料库

inspect(Corpus_A)

数据清理

#转化函数tm_map

#打消/n

Corpus_B<-tm_map(Corpus_A,stripWhitespace)

inspect(Corpus_B)

#打消数字

Corpus_C<-tm_map(Corpus_B,removeNumbers)

inspect(Corpus_C)

#打消英文标点符号

Corpus_D<-tm_map(Corpus_B,removePunctuation)

inspect(Corpus_D)

#英文停用词(涌现频率非常高,但对文章没有实际意义,如“to","the","of"

Corpus_E<-tm_map(Corpus_B,removeWords,stopwords("english"))

inspect(Corpus_E)

#打消中文标点符号(函数gsub的取代功能)

Corpus_F<-tm_map(Corpus_B,function(word){gsub("!
","",word)})

inspect(Corpus_F)

#利用正则表达式

Corpus_G<-tm_map(Corpus_B,function(word){gsub("[.!0-9A-Za-z]","",word)})

inspect(Corpus_G)

#打消中文停用词(gsub函数的限定:一次只能一个)

Corpus_H<-tm_map(Corpus_B,function(word){gsub("是","",word)})

inspect(Corpus_H)

#利用gsub函数打消多规格字词,利用for循环

my_stopword<-c("是","因此","连","了")

for(i in my_stopword)

{

Corpus_B<-tm_map(Corpus_B,function(word){gsub(i,"",word)})

}

inspect(Corpus_B)

#处理结束后,将资料输出到文件

writeCorpus(Corpus_B,path="D:\\R\\tm\\Corpus_OUT")

tmcn套件-文本挖掘中文赞助

#install.packages("tmcn",destdir = "E:\\runtime\\Rlibrary")

library(tmcn)

#繁简体转换

word_A<-c("我們將學習R語言,把繁體轉換成簡體")

word_B<-c("我们将学习R措辞,把简体转换成繁体")

#查看转换前编码

Encoding(word_A)

#方法一 iconv函数转换编码

word_A<-iconv(word_A,"","UTF-8")

Encoding(word_A)

#方法二 enc2utf8函数转换编码

word_A<-enc2utf8(word_A)

Encoding(word_A)

#统一utf-8编码后,利用函数toTrad函数繁简体转换

toTrad(word_A,rev=T)

toTrad(word_B,rev=F)

##---stopwordsCN函数----##

#现在D:\R中,建立tmcn文件夹

#函数stopwordsCN是常用的处理资料库中中文停用词

stop<-stopwordsCN()

Encoding(stop)

stop_TW<-toTrad(stop,rev=T)

#资料导出

#将停用词处理成繁体保存,供后续利用

#函数write.csv,存成csv文件

#先转换成数据框格式

stop_data<-data.frame(stop_TW)

write.csv(stop_data,file="D:\\R\\tmcn\\stop_data.csv",row.names = FALSE)

#存成txt文档

#参数col.names=FALSE 删除行列名称

#参数quote=FALSE 字符串不用引号表示

write.table(stop_TW,file="D:\\R\\tmcn\\stop_data.txt",row.names=FALSE,col.names=F ALSE,quote=FALSE,fileEncoding = "UTF-8")

jiebaR套件-中文分词

#先把资料存成记事本(常日为ANSI或UTF-8格式)D:\R\jieba\pig_UTF8.txt

#从前,有一只胖胖的猪妈妈,她生了三只小猪。

#最大的小猪:猪大哥很贪睡,很

#第二个小猪:猪二哥很爱吃,他也很

#幸好最小的小猪:猪小弟是个勤恳的好孩子,常常努力的事情。

#利用readr包read.lines读取txt

library(readr)

#笔墨编码为utf-8或者ANSI的输入

readLines("D:\\R\\jieba\\pig_ANSI.txt")

pig<-readLines("D:\\R\\jieba\\pig_UTF8.txt",encoding = "UTF-8")

#中文断词

library(jiebaR)

#建立分词引擎 名为cutword

cutword<-worker()

cutword

#初步断词

cutword[pig]

#增加停用词 减少赘词

#利用tmcn套件中的stopwordsCN的停用词

cutword<-worker(stop_word ="D:/R/tmcn/stop_data.txt" )

cutword

#查看断词结果

cutword[pig]

#增加字词:如:名字、专有名词、形容词...

add<-c("猪妈妈","三只小猪","猪大哥","猪二哥","猪小弟")

new_user_word(cutword,add)

#查看断词结果

pig_A<-cutword[pig]

#删除字符数为1的无用词

#套件stringr,函数str_count()打算字符数

pig_B<-pig_A[str_count(pig_A)>1]

Wordcloud2套件-制作笔墨云

#完成断词后,制作笔墨云的准备事情

#table函数,打算名词涌现次数

table_A<-table(pig_B)

#sort函数,排顺序数

table_B<-sort(table_A,decreasing = T)

#转换为数据框

table_C<-data.frame(table_B)

#载入wordcloud2套件

library(wordcloud2)

wordcloud2(table_C)

标签:

相关文章

微信第三方登录便捷与安全的完美融合

社交平台已成为人们日常生活中不可或缺的一部分。微信作为我国最受欢迎的社交软件之一,拥有庞大的用户群体。为了方便用户在不同平台间切换...

网站建设 2025-02-18 阅读0 评论0

广东高速代码表解码高速公路管理智慧

高速公路作为国家交通动脉,连接着城市与城市,承载着巨大的物流和人流。广东作为我国经济大省,高速公路网络密布,交通流量巨大。为了更好...

网站建设 2025-02-18 阅读0 评论0