http://cos.name/cn/topic/102130
应用定性数据分析包RQDA(Qualitative Data Analysis)和文挖掘框架包tm结合进行文本挖掘。
在对访谈内容或剧本、小说部分内容进行文本挖掘时,如果用不断的剪粘保存的方法非常繁琐而且容易漏掉一些内容。好在黄荣贵开发的RQDA包可以进行文档管理和内容编码及提取,大大方便了利用tm包进行文本挖掘,既提高了效率又提高了准确性,下面举一个小例子:
1、安装RQDA包、tm包和中文分词软件(分词软件见下面链接);
http://www.pinggu.org/bbs/thread-853290-1-1.html
2、装载RQDA包并建立一个新的工程项目;
3、输入相关文本文件;
4、进行编码和作标记;
5、双击想要提取的编码即可提取相关文本;
6、运行下面下载的程序进行文本提取、转换、分词、文本挖掘工作。
> gg
A corpus with 55 text documents
>
------------------------------------------------
> ### 去掉多余空格 ####
> reuters <- tm_map( gg, stripWhitespace)
> reuters[[ 1]]
这个也是临时改的?这儿应该放一个那样的桌子。
>
------------------------------------------------
> ## 全文搜索 ##
> searchFullText( gg[[ 1]], "是临[时]?改")
[ 1] TRUE
>
------------------------------------------------
> ### 查找以某字开头、结尾等的词条 ###
> stemCompletion( gg, c( "财", "政", "部"))
财 政 部
"财政部就是替政府花钱的" "" ""
------------------------------------------------
> ### 元数据管理 ###
> DublinCore( reuters[[ 2]], "title") <- "建国60周年"
> meta( reuters[[ 2]])
Available meta data pairs are:
Author :
DateTimeStamp: 2010- 07- 15 02: 06: 27
Description :
Heading : 建国 60周年
ID : 2
Language : eng
Origin :
>
------------------------------------------------
> ### 创建词条-文件矩阵
>
> dtm <- DocumentTermMatrix( reuters, control = list( minWordLength= 2)) ##最短词两个字
> inspect( dtm[ 1: 2, 3: 6])
A document- term matrix ( 2 documents, 4 terms)
Non-/ sparse entries: 0/ 8
Sparsity : 100%
Maximal term length: 5
Weighting : term frequency ( tf)
Terms
Docs 10000 12 120 1966
1 0 0 0 0
2 0 0 0 0
------------------------------------------------
> ## 操作词条-文件矩阵 ##
> ## 1、找出最少出现过3次的词条 ##
> findFreqTerms( dtm, 3)
[ 1] "政策"
------------------------------------------------
> ## 2、找出与"应该"相关度到少达0.6的词条 ###
> findAssocs( dtm, "应该", 0.6)
0.11 应该 桌子 临时
1.0 1.0 1.0 0.7
>
民网 >> 时政 >> 时政专题 >> 网友进言
http://politics.people.com.cn/GB/8198/138817/index.html
MetaID fname fid
1 0 公安部答复本网网友关于轻微交通违法处罚等4问题 1
2 0 公安部答复本网网友关于驾龄计算、异地购车上牌、老人驾车等8问题 2
3 0 公安部答复本网网友关于如何转回农业户口等3问题 3
4 0 公安部回复本网网友关于驾驶证年检被注销等3问题 4
5 0 公安部回复人民网网友关于异地缴交通罚款等4问题 5
6 0 公安部回复人民网网友关于身份证重号错号等4问题 6
一、出现5次以上的词条
----------------------------------------------------------------------------
1、根据编码“网友”分析:
> findFreqTerms( dtm, 5)
[ 1] "驾驶" "身份证" "问题"
----------------------------------------------------------------------------
2、根据编码“公安部回应”分析:
> findFreqTerms( dtm, 5)
[ 1] "办理" "部门" "公安" "管理" "规定" "机动车" "机关" "交通"
[ 9] "安全" "不得" "车辆" "道路" "驾驶" "驾驶证" "汽车" "实施"
[ 17] "使用" "小型" "营运" "载货" "载客" "证明" "工作" "法律"
[ 25] "公民" "居民" "社会" "身份" "身份证" "条件" "相关" "行为"
[ 33] "证件" "措施" "违法" "应当" "公安部" "信息"
**************************************************************
二、找出与“驾驶”相关达70%以上的词条
----------------------------------------------------------------------------
1、根据编码“网友”分析:
驾驶 公安部 能否 规定 驾照 汽车
1.00 0.87 0.80 0.79 0.78 0.72
2、根据编码“公安部回应”分析:
驾驶 需要 期限 证明 小型 法律 使用 中型 驾驶证 依法 检查
1.00 0.95 0.90 0.86 0.84 0.83 0.80 0.79 0.77 0.77 0.75
有效 超过
0.75 0.72
MetaID fname fid
1 0 公安部答复本网网友关于轻微交通违法处罚等4问题 1
2 0 公安部答复本网网友关于驾龄计算、异地购车上牌、老人驾车等8问题 2
3 0 公安部答复本网网友关于如何转回农业户口等3问题 3
4 0 公安部回复本网网友关于驾驶证年检被注销等3问题 4
5 0 公安部回复人民网网友关于异地缴交通罚款等4问题 5
6 0 公安部回复人民网网友关于身份证重号错号等4问题 6
对上面的数据改为将每条回应为研究对象进行文档聚类分析,结果如下:
综合上面两种聚类分析可以判断:公安部负责对人民网网民进行回应的工作人员有两名,因为每个人的写作用词习惯是比较固定的。
-
对三位房地产大佬在搜房网博客近期文章的分析:
-
标题:
-
> txt
MetaID fname fid ID
1 0 穿越“鬼门关”登山者视界 4(王) 3 1
2 0 美丽的建筑网友交流 42(王) 7 2
3 0 如何了解日本民族网上交流 43(王) 8 3
> txt
MetaID fname fid ID
1 0 从发改委的文件看政策的变化(任) 4 1
2 0 该不该降价(任) 6 2
3 0 听老柳侃管理(任) 11 3
4 0 稳定、明确的政策预期更为重要(任) 12 4
5 0 先拆还是先建,这是一门艺术(任) 13 5
6 0 幸福指数——再次写给八零后(任) 16 6
> txt
MetaID fname fid ID
1 0 IPAD现象预示了社会结构的变化(潘) 1 1
2 0 SOHO中国进驻上海外滩(潘) 2 2
3 0 房地产业要做受人尊重的行业(潘) 5 3
4 0 商业地产与住房是两个完全不同的市场(潘) 9 4
5 0 世界正经历分娩之阵痛(潘) 10 5
6 0 现在中国房地产市场上“尖叫”声一片(潘) 14 6
7 0 信仰改变了我的生命(潘) 15 7
8 0 银河 SOHO和光华路 SOHO2通过 LEED预认证(潘) 17 8
9 0 银河 SOHO开盘三天销售 46.75亿元(潘) 18 9
10 0 致《酥油》作者: 你的爱和我们的感受构成世界的力量(潘) 19 10
> -
-
结果:
-
> ## 任志强最喜欢的用词:
> inspect( dtm_rzq[, j])
A document- term matrix ( 6 documents, 10 terms)
Non-/ sparse entries: 41/ 19
Sparsity : 32%
Maximal term length: 2
Weighting : term frequency ( tf)
Terms
Docs 价格 企业 租赁 发展 改革 没有 社会 一代 知道 中国
1 0 1 5 4 7 0 3 0 0 6
2 31 2 0 3 1 7 4 0 4 15
3 0 42 0 4 0 5 4 0 0 2
4 9 1 0 0 0 7 4 0 3 4
5 1 0 25 3 0 6 4 0 2 4
6 1 5 0 25 48 30 55 41 35 51
> ## 潘石屹最喜欢的用词:
> inspect( dtm_psy[, j])
A document- term matrix ( 10 documents, 9 terms)
Non-/ sparse entries: 49/ 41
Sparsity : 46%
Maximal term length: 4
Weighting : term frequency ( tf)
Terms
Docs 一个 商业 上海 外滩 项目 房地产 市场 土地 soho
1 17 12 0 0 0 4 4 1 1
2 6 17 15 27 21 1 2 2 6
3 8 0 0 0 2 11 2 10 0
4 6 16 0 0 2 0 5 0 13
5 10 2 0 0 6 0 0 0 9
6 5 4 3 0 1 17 37 15 0
7 5 0 0 0 1 0 1 0 0
8 1 0 0 0 8 0 0 0 7
9 1 15 0 0 4 0 9 0 18
10 7 0 0 0 0 0 0 1 0
> ## 王石最喜欢的用词
> inspect( dtm_ws[, j])
A document- term matrix ( 3 documents, 16 terms)
Non-/ sparse entries: 17/ 31
Sparsity : 65%
Maximal term length: 2
Weighting : term frequency ( tf)
Terms
Docs 冰川 穿越 攀登 融化 珠峰 处理 东京 焚烧 垃圾 等级 了解 日本 喜欢 相扑
1 18 6 4 8 4 0 0 0 0 0 0 0 0 0
2 0 0 0 0 0 4 4 8 11 0 0 1 0 0
3 0 0 0 0 0 0 0 0 0 4 4 7 4 10
Terms
Docs 运动 秩序
1 0 0
2 0 0
3 4 6 -
-
由此看来王石仍然在到处玩儿,怪不得从万科A到万科B,再到万科债券08G1、08G2都跌的一塌糊涂。
任志强的兴趣仍在研究国家政策,忧国忧民啊!
老潘的工作重点已经转到上海的商业地产了。
-
好吧,现在再用支持向量机的方法对上面的数据建模,看是否能分辨出某些话是谁说的:
> library( e1071)
> ## 生成训练用的数据 ##
………………
> tt <- rbind( tt, tt1)
>
> tt[ is.na( tt)]<- 0
> tt[, "作者"] <- factor( tt[, "作者"])
> model <- svm(作者 ~ ., data = tt[ c( 1: 4, 7: 14, 17: 18),], kernel = "sigmoid")
> summary( model)
Call:
svm( formula = 作者 ~ ., data = tt[ c( 1: 4, 7: 14, 17: 18), ], kernel = "sigmoid")
Parameters:
SVM- Type: C- classification
SVM- Kernel: sigmoid
cost: 1
gamma: 0.01666667
coef.0: 0
Number of Support Vectors: 10
( 4 4 2 )
Number of Classes: 3
Levels:
潘石屹 任志强 王石
> ## 模型拟合测试 ##
> ## 训练集(样本内)拟合 ##
> pred <- predict( model, tt[ c( 1: 4, 7: 14, 17: 18), 1: length( hh)])
> table( pred, tt[ c( 1: 4, 7: 14, 17: 18), "作者"])
pred 潘石屹 任志强 王石
潘石屹 7 0 0
任志强 1 4 0
王石 0 0 2
> ## 测试集(样本外)预测 ##
> pred <- predict( model, tt[ c( 5: 6, 15: 16, 19), 1: length( hh)])
> table( pred, tt[ c( 5: 6, 15: 16, 19), "作者"])
pred 潘石屹 任志强 王石
潘石屹 2 0 0
任志强 0 2 0
王石 0 0 1训练集中有一个错的,但预测集中全中。
*******************************************************
哪么随机找两段他们的话看能判断出吗?
>对了,是《珠峰零公里口述之四 (2010-4-9 16:06:22)》中的一段话。
> test <- c( "昨天休息了一天,驻地村子现在很大,二年前很小,自首位成功登上珠穆朗玛峰的探险家埃德蒙.希拉里组织义工建立学校医疗诊所,当地条件开始显著改善,目前这里三分之一的当地人已经移民国外,境外一些人士建立了一些家庭小旅馆,卫生条件不错,新西兰狮子会建立了一家小卫生所,以改善当地的医疗条件。这些让我体会到登山这项运动对当地生活条件改善的有益影响。")
>
………………
> tt1[, c( test_h)] <- test_tt[, c( test_h)]
> tt1[ is.na( tt1)]<- 0
> predict( model, tt1)
1
王石
Levels: 潘石屹 任志强 王石
>
++++++++++++++++++++++++++++++++++++++++
> test <- c( "2007年出台的“第二套住房的信贷”新政,让中国的房地产市场调头直下,在“两防”的政策推力和美国的金融危机双重作用之下,让中国的宏观经济也随之调头直下,GDP快速下降到6.3%,从而让中国政府不得不用四万亿元投资、放量的货币信贷和解封“第二套住房信贷”改为0.7倍信贷降息和对改善性住房(实际的第二套)信贷给以支持,才让中国的房地产和宏观经济从谷底翻转恢复到保八之上。也对了,是《何需分清几套房 (2010-4-29 9:38:52) 》中的一段话
+ ")
……………………
> tt1[, c( test_h)] <- test_tt[, c( test_h)]
> tt1[ is.na( tt1)]<- 0
> predict( model, tt1)
1
任志强
Levels: 潘石屹 任志强 王石
>
-
那么下面这段文字是谁写的呢?
test <- c( "经常有人问我第一桶金怎么来的,从哪里得到的,有多少。其实每次有人问我这个问题时,我都想说,人的第一桶金是自信。即使你没钱也不要怕,自信就是你的资本。也有人在自信前面加了一个不好的修饰语,叫盲目自信,我不太爱听。我说过很多次自我的害处,但我认为与自我有点关系的不多的好东西之一,就是自信。自信当然有自我意识,还有信,相信的信。相信,是正面的、健康的。要相信自己。一个相信自己的人才会相信他人,相信未来。")
> predict( model, tt1)
1
潘石屹
Levels: 潘石屹 任志强 王石太神奇了!潘石屹——《自信是人生第一桶金》的一段。见下面的链接: