【问题标题】:Plotting a word-cloud by date for a twitter search result? (using R)为 twitter 搜索结果按日期绘制词云? (使用 R)
【发布时间】:2011-02-27 00:39:25
【问题描述】:

我希望在 twitter 上搜索一个单词(比如#google),然后能够生成 twitts 中使用的单词的标签云,但要根据日期(例如,有一个小时的移动窗口,每次移动 10 分钟,向我展示不同的词是如何在一天中更频繁地使用的)。

我将不胜感激有关如何执行此操作的任何帮助:信息资源、编程代码(R 是我擅长使用的唯一语言)和可视化想法。问题:

  1. 我如何获取信息?

    在 R 中,我发现 twitteR 包有 searchTwitter 命令。但我不知道我能从中得到多大的“n”。此外,它不会返回 twitt 的来源日期。

    我看到here 可以得到 1500 条推文,但这需要我手动进行解析(这导致我进入第 2 步)。另外,为了我的目的,我需要成千上万的推特。甚至有可能让他们回想起来吗? (例如,每次都通过 API URL 询问较早的帖子?)如果没有,还有一个更普遍的问题,即如何在您的家用计算机上创建个人推特存储? (这个问题最好留给另一个 SO 线程 - 尽管这里人们的任何见解对我来说都会非常有趣)

  2. 如何解析信息(在 R 中)?我知道 R 具有可以从 rcurl 和 twitteR 包中获得帮助的功能。但我不知道是哪个,或者如何使用它们。任何建议都会有所帮助。

  3. 如何分析?如何删除所有“不感兴趣”的词?我发现R中的“tm”包有this example

    reuters

    这能解决问题吗?我应该做点别的/更多吗?

    另外,我想我想在根据时间切割我的数据集之后这样做(这将需要一些类似 posix 的函数(我不确定这里需要哪些函数,或者如何使用它)。

  4. 最后,还有可视化的问题。如何创建单词的标签云?我找到了a solution for this here,还有其他建议/建议吗?

我相信我在这里提出了一个很大的问题,但我试图将其分解为尽可能多的直截了当的问题。欢迎任何帮助!

最好的,

塔尔

【问题讨论】:

  • 对于它的价值,我认为这应该分解成单独的问题......
  • 谢谢Shane,你认为我应该把它分成4个不同的问题,或者以不同的方式? (感谢您的意见)
  • 我同意 Shane 的观点。至于问题的数量,寻找技术和一般概念,并将它们分别放在一个问题中。例如如何在 R 中创建词云?或者在为关键词解析文本时,哪些方法和包有助于筛选出关键词?
  • #2 关于解析:用给出的数据样本提出这个问题。任何不包含真实数据的解析问题都像是在问“我怎么上床?”你会得到建议,但它在现实生活中帮助你的可能性几乎为零。
  • #1 - 你会遇到一些叫“JD LONG”的人,他们会询问你是否阅读过 twitteR 的代码以及是否阅读过 Twitter API 文档。您应该先阅读这些内容,然后再提出具体问题。例如:返回超过 1500 条推文的服务如何获得这些结果,因为 Twitter 将 API 限制在 1500? (我不知道 1500 是否是正确的数字)

标签: database r visualization twitter


【解决方案1】:

【讨论】:

    【解决方案2】:

    至于绘图:我在这里做了一个词云:http://trends.techcrunch.com/2009/09/25/describe-yourself-in-3-or-4-words/ 使用 sn-ps 包,我的代码在那里。我手动提取了某些单词。如果您有更具体的问题,请查看并告诉我。

    【讨论】:

      【解决方案3】:

      我注意到这是一个老问题,通过网络搜索可以找到多种解决方案,但这里有一个答案(通过http://blog.ouseful.info/2012/02/15/generating-twitter-wordclouds-in-r-prompted-by-an-open-learning-blogpost/):

      require(twitteR)
      searchTerm='#dev8d'
      #Grab the tweets
      rdmTweets <- searchTwitter(searchTerm, n=500)
      #Use a handy helper function to put the tweets into a dataframe
      tw.df=twListToDF(rdmTweets)
      
      ##Note: there are some handy, basic Twitter related functions here:
      ##https://github.com/matteoredaelli/twitter-r-utils
      #For example:
      RemoveAtPeople <- function(tweet) {
        gsub("@\\w+", "", tweet)
      }
      #Then for example, remove @d names
      tweets <- as.vector(sapply(tw.df$text, RemoveAtPeople))
      
      ##Wordcloud - scripts available from various sources; I used:
      #http://rdatamining.wordpress.com/2011/11/09/using-text-mining-to-find-out-what-rdatamining-tweets-are-about/
      #Call with eg: tw.c=generateCorpus(tw.df$text)
      generateCorpus= function(df,my.stopwords=c()){
        #Install the textmining library
        require(tm)
        #The following is cribbed and seems to do what it says on the can
        tw.corpus= Corpus(VectorSource(df))
        # remove punctuation
        tw.corpus = tm_map(tw.corpus, removePunctuation)
        #normalise case
        tw.corpus = tm_map(tw.corpus, tolower)
        # remove stopwords
        tw.corpus = tm_map(tw.corpus, removeWords, stopwords('english'))
        tw.corpus = tm_map(tw.corpus, removeWords, my.stopwords)
      
        tw.corpus
      }
      
      wordcloud.generate=function(corpus,min.freq=3){
        require(wordcloud)
        doc.m = TermDocumentMatrix(corpus, control = list(minWordLength = 1))
        dm = as.matrix(doc.m)
        # calculate the frequency of words
        v = sort(rowSums(dm), decreasing=TRUE)
        d = data.frame(word=names(v), freq=v)
        #Generate the wordcloud
        wc=wordcloud(d$word, d$freq, min.freq=min.freq)
        wc
      }
      
      print(wordcloud.generate(generateCorpus(tweets,'dev8d'),7))
      
      ##Generate an image file of the wordcloud
      png('test.png', width=600,height=600)
      wordcloud.generate(generateCorpus(tweets,'dev8d'),7)
      dev.off()
      
      #We could make it even easier if we hide away the tweet grabbing code. eg:
      tweets.grabber=function(searchTerm,num=500){
        require(twitteR)
        rdmTweets = searchTwitter(searchTerm, n=num)
        tw.df=twListToDF(rdmTweets)
        as.vector(sapply(tw.df$text, RemoveAtPeople))
      }
      #Then we could do something like:
      tweets=tweets.grabber('ukgc12')
      wordcloud.generate(generateCorpus(tweets),3)
      

      【讨论】:

        【解决方案4】:

        我想回答你制作大词云的问题。 我做的是

        1. 使用 s0.tweet THIS。

        2. 通过这个命令组合它们:

        rdmTweets = c(s0.tweet,s1.tweet,s2.tweet,s3.tweet,s4.tweet,s5.tweet,s6.tweet,s7.tweet)

        结果:

        这个 Square Cloud 包含大约 9000 条推文。

        来源:People voice about Lynas Malaysia through Twitter Analysis with R CloudStat

        希望对您有所帮助!

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2017-07-01
          • 1970-01-01
          • 1970-01-01
          • 2015-08-01
          • 2018-08-21
          • 2023-03-30
          • 2010-09-26
          • 1970-01-01
          相关资源
          最近更新 更多