Showing posts with label Word-Cloud. Show all posts
Showing posts with label Word-Cloud. Show all posts
Sunday, September 20, 2015
A Word Cloud with Spatial Meaning
..Some time ago I did a word cloud for representing a Google Scholar search result. Tal Galili pointed me at a post by Drew Conway that expanded on the topic of word clouds lacking spatial meaning. In fact the spatial ordering of words in a word cloud is arbitrary and meaningless..
As I am an ecologist, I soon came to the idea that text could be treated as a multivariate data set - assuming that words can be treated as species and sentences being similar to samples. So, presuming that it makes sense to put sentences and words in a cross-table as I similarly would do with a species / samples matrix, it may also be sensible to analyze such a matrix by ordination-methods for multivariate data, mostly used by ecologist recently. I chose NMDS ordination, as it is robust and quite easy to compute with R-package {vegan}.
In a NMDS ordination plot the distances between Species/Words that often co-occurre within sentences or/and within groups of sentences (say, sentences said by you vs. sentences said by me) are minimized. That is, words associated with each other or with words within levels of a grouping-factor are plotted closer to each other as comapred to words with low association.
In my simple example two texts are compared, each with five sentences. One with sentences I said about you (denoted by red "Is") and sentences said by you about yourself (the red "Ys"). Words used by both of us are in the intersection. Whereas, e.g., words said exclusively by me are far away from the centroid of sentences said by you, and vice versa. I will not annoy you with the nitty-gritty stuff of ordination methods or NMDS, you will have to check this yourself.
Word frequencies are represented by size of the plotted text, as in the usual word clouds..
So, to all linguists out there, what do you think??
The stand-alone code to produce this word cloud can be found HERE.
Read more »
As I am an ecologist, I soon came to the idea that text could be treated as a multivariate data set - assuming that words can be treated as species and sentences being similar to samples. So, presuming that it makes sense to put sentences and words in a cross-table as I similarly would do with a species / samples matrix, it may also be sensible to analyze such a matrix by ordination-methods for multivariate data, mostly used by ecologist recently. I chose NMDS ordination, as it is robust and quite easy to compute with R-package {vegan}.
In a NMDS ordination plot the distances between Species/Words that often co-occurre within sentences or/and within groups of sentences (say, sentences said by you vs. sentences said by me) are minimized. That is, words associated with each other or with words within levels of a grouping-factor are plotted closer to each other as comapred to words with low association.
In my simple example two texts are compared, each with five sentences. One with sentences I said about you (denoted by red "Is") and sentences said by you about yourself (the red "Ys"). Words used by both of us are in the intersection. Whereas, e.g., words said exclusively by me are far away from the centroid of sentences said by you, and vice versa. I will not annoy you with the nitty-gritty stuff of ordination methods or NMDS, you will have to check this yourself.
Word frequencies are represented by size of the plotted text, as in the usual word clouds..
So, to all linguists out there, what do you think??
The stand-alone code to produce this word cloud can be found HERE.
R-Function GScholarScraper to Webscrape Google Scholar Search Result
NOTE: You'll find the update HERE and HERE.
NOTE: The script is currently not working because the code of the Google-Scholar site has changed...
I'll see for this as soon as I find some spare time for it!
NOTE: If you try to access GoogleScholar programatically consider this words of caution:
http://stackoverflow.com/questions/7523961/google-scholar-with-matlab/7587994#7587994
...
Based on my previous post on Web Scraping I coded and uploaded the Function "GScholarScraper" HERE for testing!
The function will pull all (!) results, processing pages in chunks of 100 results/titles, and return a file with all titles, links, etc. It will also produce a word cloud using the words in the publication titles.
Please try your own search strings and report errors, etc.!
Build and run properly under:
R version 2.13.0 (2011-04-13) and R version R-2.13.2 (2011-09-30)
Platform: i386-pc-mingw32/i386 (32-bit) locale:
[1] LC_COLLATE=English_United States.1252
[2] LC_CTYPE=English_United States.1252
[3] LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C
[5] LC_TIME=English_United States.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] stringr_0.5 tm_0.5-6 wordcloud_1.2 Rcpp_0.9.7
loaded via a namespace (and not attached):
[1] plyr_1.5.1 slam_0.1-23
PS: Errors reported lately (see comments) were resolved, the source code was updated..
Read more »
NOTE: The script is currently not working because the code of the Google-Scholar site has changed...
I'll see for this as soon as I find some spare time for it!
NOTE: If you try to access GoogleScholar programatically consider this words of caution:
http://stackoverflow.com/questions/7523961/google-scholar-with-matlab/7587994#7587994
...
Based on my previous post on Web Scraping I coded and uploaded the Function "GScholarScraper" HERE for testing!
The function will pull all (!) results, processing pages in chunks of 100 results/titles, and return a file with all titles, links, etc. It will also produce a word cloud using the words in the publication titles.
Please try your own search strings and report errors, etc.!
Build and run properly under:
R version 2.13.0 (2011-04-13) and R version R-2.13.2 (2011-09-30)
Platform: i386-pc-mingw32/i386 (32-bit) locale:
[1] LC_COLLATE=English_United States.1252
[2] LC_CTYPE=English_United States.1252
[3] LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C
[5] LC_TIME=English_United States.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] stringr_0.5 tm_0.5-6 wordcloud_1.2 Rcpp_0.9.7
loaded via a namespace (and not attached):
[1] plyr_1.5.1 slam_0.1-23
PS: Errors reported lately (see comments) were resolved, the source code was updated..
Webscraping Google Scholar & Show Result as Word Cloud Using R
NOTE: Please see the update HERE and HERE!
...When reading Scott Chemberlain's last post about web-scraping I felt it was time to pick up and complete an idea that I was brooding over for some time now:
When a scientist aims out for a new project the first thing to do is to evaluate if other people already have come along to answer the very questions he is about to work on. I.e., I was interested if there has been done any research regarding amphibian diversity at regional/geographical scales correlated to environmental/landscape parameters. Usually I would got to Google-Scholar and search something like - intitle:amphibians AND intitle:richness OR intitle:diversity AND environment OR landscape - and then browse thru the results. But, this is often tedious and a way for a quick visual examination would be of great benefit.
The code I present will solve this task. It may be awkward in places and there might be a more effective way to yield the same result - but it may serve as a starter and I would very much appreciate people more literate than me picking up the torch...
For my example-search it is shown that there has not been very much going on regarding amphibian diversity correlated to environment and landscape...
See code HERE.
PS: I'd be happy about collaboration / tips / editing - so feel free to contact me and I will add you to the list of editors - you then could edit / comment / add to the script on Google Docs.
...some drawbacks need to be considered:
Read more »
...When reading Scott Chemberlain's last post about web-scraping I felt it was time to pick up and complete an idea that I was brooding over for some time now:
When a scientist aims out for a new project the first thing to do is to evaluate if other people already have come along to answer the very questions he is about to work on. I.e., I was interested if there has been done any research regarding amphibian diversity at regional/geographical scales correlated to environmental/landscape parameters. Usually I would got to Google-Scholar and search something like - intitle:amphibians AND intitle:richness OR intitle:diversity AND environment OR landscape - and then browse thru the results. But, this is often tedious and a way for a quick visual examination would be of great benefit.
The code I present will solve this task. It may be awkward in places and there might be a more effective way to yield the same result - but it may serve as a starter and I would very much appreciate people more literate than me picking up the torch...
For my example-search it is shown that there has not been very much going on regarding amphibian diversity correlated to environment and landscape...
See code HERE.
PS: I'd be happy about collaboration / tips / editing - so feel free to contact me and I will add you to the list of editors - you then could edit / comment / add to the script on Google Docs.
...some drawbacks need to be considered:
- Maximum no. of search results = 100
- Only titles are considered. Additionally considering abstracts may yield more representative results.. but abstracts are truncated in the search result and I don't know if it is possible to retrieve the full abstracts.
- Also, long titles may be truncated...
- A more illustrative result would be achieved if one could get rid of all other words than nouns, verbs and adjectives - don't know how to do this, but I am sure this is possible.
- more drawbacks? you tell..
Saturday, September 19, 2015
Follow-Up: Making a Word Cloud for a Search Result from GScholar_Scraper_3.1
Here's a short follow-up on how to produce a word cloud for a search result from GScholarScraper_3.1:# File-Name: GScholarScraper_3.1.R
# Date: 2012-08-22
# Author: Kay Cichini
# Email: kay.cichini@gmail.com
# Purpose: Scrape Google Scholar search result
# Packages used: XML
# Licence: CC BY-SA-NC
#
# Arguments:
# (1) input:
# A search string as used in Google Scholar search dialog
#
# (2) write:
# Logical, should a table be writen to user default directory?
# if TRUE ("T") a CSV-file with hyperlinks to the publications will be created.
#
# Difference to version 3:
# (3) added "since" argument - define year since when publications should be returned..
# defaults to 1900..
#
# (4) added "citation" argument - logical, if "0" citations are included
# defaults to "1" and no citations will be included..
# added field "YEAR" to output
#
# Caveat: if a submitted search string gives more than 1000 hits there seem
# to be some problems (I guess I'm being stopped by Google for roboting the site..)
#
# And, there is an issue with this error message:
# > Error in htmlParse(URL):
# > error in creating parser for http://scholar.google.com/scholar?q
# I haven't figured out his one yet.. most likely also a Google blocking mechanism..
# Reconnecting / new IP-address helps..
GScholar_Scraper <- function(input, since = 1900, write = F, citation = 1) {
require(XML)
# putting together the search-URL:
URL <- paste("http://scholar.google.com/scholar?q=", input, "&as_sdt=1,5&as_vis=",
citation, "&as_ylo=", since, sep = "")
cat("\nThe URL used is: ", "\n----\n", paste("* ", "http://scholar.google.com/scholar?q=", input, "&as_sdt=1,5&as_vis=",
citation, "&as_ylo=", since, " *", sep = ""))
# get content and parse it:
doc <- htmlParse(URL)
# number of hits:
h1 <- xpathSApply(doc, "//div[@id='gs_ab_md']", xmlValue)
h2 <- strsplit(h1, " ")[[1]][2]
num <- as.integer(sub("[[:punct:]]", "", h2))
cat("\n\nNumber of hits: ", num, "\n----\n", "If this number is far from the returned results\nsomething might have gone wrong..\n\n", sep = "")
# If there are no results, stop and throw an error message:
if (num == 0 | is.na(num)) {
stop("\n\n...There is no result for the submitted search string!")
}
pages.max <- ceiling(num/100)
# 'start' as used in URL:
start <- 100 * 1:pages.max - 100
# Collect URLs as list:
URLs <- paste("http://scholar.google.com/scholar?start=", start, "&q=", input,
"&num=100&as_sdt=1,5&as_vis=", citation, "&as_ylo=", since, sep = "")
scraper_internal <- function(x) {
doc <- htmlParse(x, encoding="UTF-8")
# titles:
tit <- xpathSApply(doc, "//h3[@class='gs_rt']", xmlValue)
# publication:
pub <- xpathSApply(doc, "//div[@class='gs_a']", xmlValue)
# links:
lin <- xpathSApply(doc, "//h3[@class='gs_rt']/a", xmlAttrs)
# summaries are truncated, and thus wont be used..
# abst <- xpathSApply(doc, '//div[@class='gs_rs']', xmlValue)
# ..to be extended for individual needs
options(warn=(-1))
dat <- data.frame(TITLES = tit, PUBLICATION = pub,
YEAR = as.integer(gsub(".*\\s(\\d{4})\\s.*", "\\1", pub)),
LINKS = lin)
options(warn=0)
return(dat)
}
result <- do.call("rbind", lapply(URLs, scraper_internal))
if (write == T) {
result$LINKS <- paste("=Hyperlink(","\"", result$LINKS, "\"", ")", sep = "")
write.table(result, "GScholar_Output.CSV", sep = ";",
row.names = F, quote = F)
shell.exec("GScholar_Output.CSV")
} else {
return(result)
}
}
# EXAMPLE:
input <- "allintitle:amphibian+diversity"
df <- GScholar_Scraper(input, since = 1980, citation = 1)
#install.packages("tm")
library(tm)
#install.packages("wordcloud")
library(wordcloud)
corpus <- Corpus(VectorSource(df$TITLES))
corpus <- tm_map(corpus, function(x)removeWords(x, c(stopwords(), "PDF", "B", "DOC", "HTML", "BOOK", "CITATION")))
corpus <- tm_map(corpus, removePunctuation)
tdm <- TermDocumentMatrix(corpus)
m <- as.matrix(tdm)
v <- sort(rowSums(m), decreasing = TRUE)
d <- data.frame(word = names(v), freq = v)
# remove numbers from strings:
d <- d[-grep("[0-9]", d$word), ]
# print wordcloud:
wordcloud(d$word, d$freq)
Reading and Text Mining a PDF-File in R
I just added this R-script that reads a PDF-file to R and does some text mining with it to my Github repo..
Subscribe to:
Posts (Atom)
