-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.R
More file actions
165 lines (111 loc) ยท 4.24 KB
/
Copy pathmain.R
File metadata and controls
165 lines (111 loc) ยท 4.24 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
#!/usr/bin/env Rscript
suppressPackageStartupMessages({
library(yaml)
})
# ------------------------------ Load modules ------------------------------
source("code/init_utilitis.R")
# Core pipeline
source("code/get_time_v1.R")
source("code/document_parser.R")
source("code/clean_text.R")
source("code/tokenizer.R")
source("code/txt_ngram_to_tidy.R")
# Analysis
source("code/txt_frequency.R")
# ------------------------------ Read command line arguments ------------------------------
args <- commandArgs(trailingOnly = TRUE)
config_file <- "config/default.yaml"
if ("--config" %in% args) {
config_index <- which(args == "--config") + 1
if (config_index > length(args)) {
stop("ERROR: Missing config file after --config")
}
config_file <- args[config_index]
}
# ------------------------------ Validate config file ------------------------------
if (!file.exists(config_file)) {
stop(paste("ERROR: Config file does not exist:", config_file))
}
cat("๐ Loading config:", config_file, "\n")
config <- yaml::read_yaml(config_file)
# ------------------------------ Read configuration values ------------------------------
directory <- config$input$directory
language <- config$language$selected
ngram_number <- config$text_processing$ngram
custom_stopwords <- config$text_processing$custom_stopwords
project_name <- config$project$name
# ------------------------------ Validate required configuration ------------------------------
if (is.null(directory)) stop("ERROR: input$directory missing in YAML")
if (is.null(language)) stop("ERROR: language$selected missing in YAML")
if (is.null(ngram_number)) stop("ERROR: text_processing$ngram missing in YAML")
# ------------------------------ Load packages ------------------------------
required_packages <- c(
"stringr",
"lubridate",
"tidytext",
"stopwords",
"tidyverse",
"ggplot2",
"yaml"
)
manage_packages(required_packages)
# ------------------------------ Create project structure ------------------------------
if (is.null(project_name) || project_name == "") {
project_name <- "default_project"
}
timestamp <- format(Sys.time(), "%Y-%m-%d_%H-%M-%S")
output_dir <- file.path(
"results",
project_name,
timestamp
)
metadata_dir <- file.path(output_dir, "metadata")
processed_dir <- file.path(output_dir, "processed")
analysis_dir <- file.path(output_dir, "analysis")
plots_dir <- file.path(output_dir, "plots")
dir.create(metadata_dir, recursive = TRUE, showWarnings = FALSE)
dir.create(processed_dir, recursive = TRUE, showWarnings = FALSE)
dir.create(analysis_dir, recursive = TRUE, showWarnings = FALSE)
dir.create(plots_dir, recursive = TRUE, showWarnings = FALSE)
cat("๐ Output directory:", output_dir, "\n")
# ------------------------------ Save config used ------------------------------
file.copy(
config_file,
file.path(output_dir, "config_used.yaml")
)
# ------------------------------ Start pipeline ------------------------------
cat("\n๐ Starting pipeline\n")
# ------------------------------ STEP 1 โ Extract metadata and dates ------------------------------
cat("\n๐
Extracting document metadata...\n")
main_get_time(
directory = directory,
date_hour = timestamp,
output_dir = metadata_dir
)
# ------------------------------ STEP 2 โ Parse and tokenize corpus ------------------------------
cat("\n๐ Processing corpus...\n")
tokens_df <- main_process_texts(
directory = directory,
language = language,
ngram_number = ngram_number,
date_hour = timestamp,
metadata_dir = metadata_dir,
output_dir = processed_dir,
custom_stopwords = custom_stopwords
)
# ------------------------------ STEP 3 โ Frequency analysis ------------------------------
if (isTRUE(config$analysis$frequency)) {
cat("\n๐งฎ Running frequency analysis...\n")
analyze_frequency(
tokens_df = tokens_df,
output_dir = analysis_dir,
plots_dir = plots_dir
)
}
# ------------------------------ STEP 4 โ Category analysis ------------------------------
if (isTRUE(config$analysis$categories)) {
cat("\n๐ Category analysis enabled\n")
# Future implementation
}
# ------------------------------ Finish ------------------------------
cat("\nโ
Pipeline completed successfully.\n")