应用定性数据分析包RQDA(Qualitative Data Analysis)和文挖掘框架包tm结合进行文本挖掘

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 <- RQDA2tm( "记者" , mf = FALSE)
> 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、根据编码“网友”分析:

gg <- RQDA2tm( "网友" , mf = TRUE)
> findFreqTerms( dtm, 5)
[ 1] "驾驶"   "身份证" "问题" 

----------------------------------------------------------------------------
2、根据编码“公安部回应”分析:

gg <- RQDA2tm( "公安部回应" , mf = TRUE)
> findFreqTerms( dtm, 5)
[ 1] "办理"   "部门"   "公安"   "管理"   "规定"   "机动车" "机关"   "交通"
[ 9] "安全"   "不得"   "车辆"   "道路"   "驾驶"   "驾驶证" "汽车"   "实施"
[ 17] "使用"   "小型"   "营运"   "载货"   "载客"   "证明"   "工作"   "法律"
[ 25] "公民"   "居民"   "社会"   "身份"   "身份证" "条件"   "相关"   "行为"
[ 33] "证件"   "措施"   "违法"   "应当"   "公安部" "信息" 

**************************************************************
二、找出与“驾驶”相关达70%以上的词条
----------------------------------------------------------------------------
1、根据编码“网友”分析:

> findAssocs( dtm, "驾驶", 0.7)
  驾驶 公安部   能否   规定   驾照   汽车
  1.00   0.87   0.80   0.79   0.78   0.72
----------------------------------------------------------------------------
2、根据编码“公安部回应”分析:

> findAssocs( dtm, "驾驶", 0.7)
  驾驶   需要   期限   证明   小型   法律   使用   中型 驾驶证   依法   检查
  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

对上面的数据改为将每条回应为研究对象进行文档聚类分析,结果如下:

综合上面两种聚类分析可以判断:公安部负责对人民网网民进行回应的工作人员有两名,因为每个人的写作用词习惯是比较固定的。

 

  1. 对三位房地产大佬在搜房网博客近期文章的分析:
  2. 搜房网博客链接:
    潘石屹
    http://blog.soufun.com/blog_132261.htm
    王石
    http://blog.soufun.com/blog_1525150.htm
    任志强
    http://blog.soufun.com/blog_1796106.htm
  3. 标题:
  4. > 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
    >
  5.  
  6. 结果:
  7. > ##  任志强最喜欢的用词:
    > 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
  8.  
  9. 由此看来王石仍然在到处玩儿,怪不得从万科A到万科B,再到万科债券08G1、08G2都跌的一塌糊涂。
    任志强的兴趣仍在研究国家政策,忧国忧民啊!
    老潘的工作重点已经转到上海的商业地产了。
  10. 1 年 前回复 # 回复
  1. bensonwu

    新手上路
    注册于: 2009/05/21
    发帖数: 11

    好吧,现在再用支持向量机的方法对上面的数据建模,看是否能分辨出某些话是谁说的:

    > 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
           cost1
          gamma0.01666667
         coef.00
    Number of Support Vectors 10
    ( 4 4 2 )

    Number of Classes3
    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

    训练集中有一个错的,但预测集中全中。
    *******************************************************
    哪么随机找两段他们的话看能判断出吗?

    >
    > test <- c( "昨天休息了一天,驻地村子现在很大,二年前很小,自首位成功登上珠穆朗玛峰的探险家埃德蒙.希拉里组织义工建立学校医疗诊所,当地条件开始显著改善,目前这里三分之一的当地人已经移民国外,境外一些人士建立了一些家庭小旅馆,卫生条件不错,新西兰狮子会建立了一家小卫生所,以改善当地的医疗条件。这些让我体会到登山这项运动对当地生活条件改善的有益影响。")
    >
    ………………
    > tt1[, c( test_h)] <- test_tt[, c( test_h)]
    > tt1[ is.na( tt1)]<- 0
    > predict( model, tt1)
       1
    王石
    Levels: 潘石屹 任志强 王石
    >
    对了,是《珠峰零公里口述之四 (2010-4-9 16:06:22)》中的一段话。
    ++++++++++++++++++++++++++++++++++++++++
    > test <- c( "2007年出台的“第二套住房的信贷”新政,让中国的房地产市场调头直下,在“两防”的政策推力和美国的金融危机双重作用之下,让中国的宏观经济也随之调头直下,GDP快速下降到6.3%,从而让中国政府不得不用四万亿元投资、放量的货币信贷和解封“第二套住房信贷”改为0.7倍信贷降息和对改善性住房(实际的第二套)信贷给以支持,才让中国的房地产和宏观经济从谷底翻转恢复到保八之上。
    + "
    )
    ……………………
    > tt1[, c( test_h)] <- test_tt[, c( test_h)]
    > tt1[ is.na( tt1)]<- 0
    > predict( model, tt1)
         1
    任志强
    Levels: 潘石屹 任志强 王石
    >
    也对了,是《何需分清几套房 (2010-4-29 9:38:52) 》中的一段话

    1 年 前回复 # 回复
  1. bensonwu

    新手上路
    注册于: 2009/05/21
    发帖数: 11

    那么下面这段文字是谁写的呢?

    test <- c( "经常有人问我第一桶金怎么来的,从哪里得到的,有多少。其实每次有人问我这个问题时,我都想说,人的第一桶金是自信。即使你没钱也不要怕,自信就是你的资本。也有人在自信前面加了一个不好的修饰语,叫盲目自信,我不太爱听。我说过很多次自我的害处,但我认为与自我有点关系的不多的好东西之一,就是自信。自信当然有自我意识,还有信,相信的信。相信,是正面的、健康的。要相信自己。一个相信自己的人才会相信他人,相信未来。")

    > predict( model, tt1)
         1
    潘石屹
    Levels: 潘石屹 任志强 王石

    太神奇了!潘石屹——《自信是人生第一桶金》的一段。见下面的链接:

    http://www.pinggu.org/bbs/thread-863705-1-1.html

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
分享产生价值! A valuable new edition of a standard reference "A 'must-have' book for anyone expecting to do research and/or applications in categorical data analysis." –Statistics in Medicine on Categorical Data Analysis, First Edition The use of statistical methods for categorical data has increased dramatically, particularly for applications in the biomedical and social sciences. Responding to new developments in the field as well as to the needs of a new generation of professionals and students, this new edition of the classic Categorical Data Analysis offers a comprehensive introduction to the most important methods for categorical data analysis. Designed for statisticians and biostatisticians as well as scientists and graduate students practicing statistics, Categorical Data Analysis, Second Edition summarizes the latest methods for univariate and correlated multivariate categorical responses. Readers will find a unified generalized linear models approach that connects logistic regression and Poisson and negative binomial regression for discrete data with normal regression for continuous data. Adding to the value in the new edition is coverage of: Three new chapters on methods for repeated measurement and other forms of clustered categorical data, including marginal models and associated generalized estimating equations (GEE) methods, and mixed models with random effects Stronger emphasis on logistic regression modeling of binary and multicategory data An appendix showing the use of SAS for conducting nearly all analyses in the book Prescriptions for how ordinal variables should be treated differently than nominal variables Discussion of exact small-sample procedures More than 100 analyses of real data sets to illustrate application of the methods, and more than 600 exercises An Instructor's Manual presenting detailed solutions to all the problems in the book is available from the Wiley editorial department.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值