-
Notifications
You must be signed in to change notification settings - Fork 6
/
profiles.R
executable file
·131 lines (108 loc) · 3.27 KB
/
profiles.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#!/usr/local/bin/rscript
library(jsonlite,quietly = TRUE,warn.conflicts = FALSE)
library(tibble,quietly = TRUE,warn.conflicts = FALSE)
library(dplyr,quietly = TRUE,warn.conflicts = FALSE)
library(pluralize,quietly = TRUE,warn.conflicts = FALSE)
library(glue,quietly = TRUE,warn.conflicts = FALSE)
library(stringr,quietly = TRUE,warn.conflicts = FALSE)
#####################################
getLeaders <- function(country){
leaders <- tibble(country = character(),
name = character(),
title = character(),
startYr = character(),
endYr = character())
titles <- names(country$leaders)
for(title in titles){
for(leader in country$leaders[[title]]){
row <- list(
country = unlist(country$countryname)[1],
name = leader$name,
title = title,
startYr = leader$startYr,
endYr = leader$endYr
)
leaders <- bind_rows(row,leaders)
}
}
leaders
}
getGov <- function(country){
gov <- tibble(country = character(),
name = character(),
description = character(),
startYr = character(),
endYr = character())
for(entry in country$government){
row <- list(
country = unlist(country$countryname)[1],
name = entry$name,
description = entry$description,
startYr = entry$startYr,
endYr = entry$endYr
)
gov <- bind_rows(row,gov)
}
gov
}
#####################################
singularize.words <- function(words_vector){
sapply(words_vector,function(words){
str_replace_all(words,pattern='_',' ')%>%
str_split('(-| )')%>%
lapply(function(word){
singularize(word)%>%
glue_collapse(sep=' ')
})%>%
unlist()
})
}
#####################################
getPeople <- function(dat){
leadersOut <- tibble(country = character(),
name = character(),
title = character(),
startYr = character(),
endYr = character())
govOut <- tibble(country = character(),
name = character(),
description = character(),
startYr = character(),
endYr = character())
for(country in dat){
fields <- names(country)
hasLeaders <- 'leaders'%in% fields
hasGov <- 'government' %in% fields
if(hasGov){
hasGov <- length(country$government) != 0
}
if(hasLeaders){
leaders <- getLeaders(country)
leaders$title <- singularize.words(leaders$title)
leadersOut <- bind_rows(leadersOut,leaders)
} else {
leaders <- NA
}
if(hasGov){
gov <- getGov(country)
govOut <- bind_rows(govOut,gov)
} else {
gov <- NA
}
}
out <- list('leaders' = leadersOut,
'gov' = govOut)
out
}
#####################################
if(length(commandArgs(trailingOnly = TRUE))!=2){
stop('usage : profiles.R -cinfo.json -outfolder')
}
file <- commandArgs(trailingOnly = TRUE)[1]
dest <- commandArgs(trailingOnly = TRUE)[2]
dat <- read_json(file)
people <- getPeople(dat)
for(n in names(people)){
filename <- paste(dest,'/','people_',n,'.csv',sep='')
write.csv(people[[n]],filename)
}