-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3-Preparation.R
More file actions
174 lines (119 loc) · 5.43 KB
/
3-Preparation.R
File metadata and controls
174 lines (119 loc) · 5.43 KB
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
################################################################################
# Amanda McGowin
# Thesis: An Analysis of Major Acquisition Reforms Through Text Mining and
# Grounded Theory Design
#
# 3. Data Preparation & Formatting
#
################################################################################
######################
# DATA PREPARATION #
######################
# Read in Expert Compendium Data Files
datafiles <- lapply(Sys.glob("../COMPENDIUM/*.txt"), read_file)
# create an empty tibble to hold data
data_tb <- tribble(~DOCUMENT, ~DATE, ~CLASS1, ~CLASS2, ~NAME, ~TYPE, ~SOURCE,
~URL, ~EXCERPT, ~NOTES, ~BIO, ~TXT)
# clean & Sort Expert Compendium Data
for(i in seq_along(datafiles)){
# parse document by variable using <NEXT> as deliminator % tidy data and
# remove variable names from variable data
x <- clean(datafiles, i) %>%
strip_var_names()
data_tb <- insert_row(data_tb, x) # insert data line into tibble
}
# Read in Defense Acquisition Reform/Legislation Data Files
datafiles <- lapply(Sys.glob("../REFORM/*.txt"), read_file)
# clean & Sort Defense Acquisition Reform/Legislation Data
for(i in seq_along(datafiles)){
# parse document by variable using <NEXT> as deliminator % tidy data and
# remove variable names from variable data
x <- clean(datafiles, i) %>%
str_replace_all(., pattern = "\\[.+?\\]", replacement = " ") %>%
str_replace_all(., pattern = "\\<.+?\\>", replacement = " ") %>%
str_replace_all(., pattern = "-", replacement = " ") %>%
strip_var_names()
data_tb <- insert_row(data_tb, x) # insert data line into tibble
}
# convert following variables to required format (date, logical, & factor)
# and remove numbers from text
data_tb %<>%
mutate(DATE = as.Date(DATE, format = "%d %b %Y"),
DOCUMENT = as.factor(DOCUMENT),
CLASS1 = as.factor(CLASS1),
CLASS2 = as.logical(CLASS2),
NAME = as.factor(NAME),
TYPE = as.factor(TYPE),
SOURCE = as.factor(SOURCE),
TXT = str_replace_all(TXT, pattern = "[0-9]", replacement = "")
)
remove(datafiles, i, x)
# remove non-ASCII characters from the text
data_tb$TXT <- iconv(data_tb$TXT, "", "ASCII", "")
# set seed for random number generation for replication purposes
set.seed(1234)
######################
# Formatting & Misc #
######################
# color brewer color palats
pal <- brewer.pal(8, "Dark2")
#pal2 <- brewer.pal(9, "Set1")
# tibble containing frequent Acquisition words that do not add to the true
# theam(s) of the document
common_ACQ <- tibble(word = c("acquisition", "defense", "dod", "title",
"section", "ii", "iii", "shall", "amended", "pub",
"subsec", "div", "xhtml", "subtitle", "u.s.c",
"uscode.house.gov", "subsection", "oct", "sec",
"ckhrpdgx", "htp", "lojewih", "e.g", "ve", "vii",
"viii", "xii", "jan", "nov", "dec", "ix", "req",
"granu", "pre", "limtitle", "leid", "ausc",
"chapter"))
# Negation words: for bi-gram sentiment analysis
negation_words <- c("not", "no", "never", "without")
# percentage of word usage across all expert's opinions
data_pct <- data_tb %>%
prep_analysis_word("TXT") %>%
filter(CLASS1 == "Compendium") %>%
ungroup() %>%
count(word) %>%
transmute(word, all_words = n / sum(n))
# word_tb <- data_tb %>%
# group_by(NAME) %>%
# unnest_tokens(word, TXT) %>%
# anti_join(stop_words) %>%
# anti_join(common_ACQ)
# write_csv(word_tb, "C:/Users/Amanda/Documents/AFIT/- THESIS/DATA & ANALYSIS/Analysis/Plots/words2.csv")
#######################################################
# Import & Clean Data for Grounded Theory Validation #
#######################################################
# Read in Expert Compendium Data Files
datafiles <- lapply(Sys.glob("../GT Validation/*.txt"), read_file)
# create an empty tibble to hold data
subset_tb <- tribble(~DOCUMENT, ~DATE, ~CLASS1, ~CLASS2, ~NAME, ~TYPE, ~SOURCE,
~URL, ~EXCERPT, ~NOTES, ~BIO, ~TXT)
# clean & Sort Defense Acquisition Reform/Legislation Data
for(i in seq_along(datafiles)){
# parse document by variable using <NEXT> as deliminator % tidy data and
# remove variable names from variable data
x <- clean(datafiles, i) %>%
str_replace_all(., pattern = "\\[.+?\\]", replacement = " ") %>%
str_replace_all(., pattern = "\\<.+?\\>", replacement = " ") %>%
str_replace_all(., pattern = "-", replacement = " ") %>%
strip_var_names()
subset_tb <- insert_row(subset_tb, x) # insert data line into tibble
}
# convert following variables to required format (date, logical, & factor)
# and remove numbers from text
subset_tb %<>%
mutate(DATE = as.Date(DATE, format = "%d %b %Y"),
DOCUMENT = as.factor(DOCUMENT),
CLASS1 = as.factor(CLASS1),
CLASS2 = as.logical(CLASS2),
NAME = as.factor(NAME),
TYPE = as.factor(TYPE),
SOURCE = as.factor(SOURCE),
TXT = str_replace_all(TXT, pattern = "[0-9]", replacement = "")
)
remove(datafiles, i, x)
# remove non-ASCII characters from the text
subset_tb$TXT <- iconv(subset_tb$TXT, "", "ASCII", "")