1 # Community Data Science Collective R Utilities
3 # Copyright (c) 2010-2016 Benjamin Mako Hill and Aaron Shaw
4 # mako@atdot.cc, aaronshaw@northwestern.edu
6 ## functions to deal with namespace information
7 #####################################################################
8 load.wikia.namespaces <- function () {
10 wikia.namespaces <- read.delim("~/data/wikia_namespaces.tsv",
11 stringsAsFactors=TRUE, header=FALSE)
13 colnames(wikia.namespaces) <- c("wiki", "ns.num", "ns.string")
14 wikia.namespaces$ns.num <- as.factor(wikia.namespaces$ns.num)
15 return(wikia.namespaces)
18 # enwiki - move to barnstars directory
20 load.enwiki.namespaces <- function(){
21 enwiki.ns.num <- c(-2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
22 14, 15, 100, 101, 108, 109)
24 names(enwiki.ns.num) <- c( "Media", "Special", "", "Talk", "User", "User talk",
25 "Wikipedia", "Wikipedia talk","File", "File talk",
26 "MediaWiki", "MediaWiki talk", "Template", "Template talk",
27 "Help", "Help talk", "Category", "Category talk",
28 "Portal", "Portal talk", "Book","Book talk")
31 # function to take a list of article titles and a wiki name and return
32 # a list of numbered namespaces
33 titles.to.ns.num <- function (page.titles, wiki) {
34 # load wikia namespace data from disk if it does not exist
35 if (!exists("wikia.namespaces")) {
36 wikia.namespaces <- load.wikia.namespaces()
39 # page.titles <- d$title # DEBUG
40 ns.df <- wikia.namespaces[wikia.namespaces$wiki == wiki,
41 c("ns.num", "ns.string")]
43 namespaces <- as.character(ns.df$ns.num)
44 names(namespaces) <- ns.df$ns.string
46 # drop the zero, we'll deal with it later
47 namespaces <- namespaces [!namespaces == 0]
49 # change underscores to spaces (necessary?)
50 page.titles <- gsub('_', ' ', page.titles)
51 page.ns <- rep("0", length(page.titles))
53 for (ns in names(namespaces)) {
54 page.ns[grepl(paste('^', ns, ':', sep=""), page.titles)] <- namespaces[ns]
57 # return the list of namespaces as a factor
58 return(as.factor(page.ns))