-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy path.Rhistory
512 lines (512 loc) · 21.8 KB
/
.Rhistory
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
.rs.restartR()
#
# .rs.restartR()
#help(textrpp_initialize)
textrpp_initialize(
condaenv = "berttopic2",
refresh_settings = TRUE
)
# Load and prepare data
data1 <- Language_based_assessment_data_8[c("satisfactiontexts", "swlstotal")]
colnames(data1) <- c("text", "score")
data2 <- Language_based_assessment_data_8[c("harmonytexts", "hilstotal")]
colnames(data2) <- c("text", "score")
data3 <- Language_based_assessment_data_3_100[1:2]
colnames(data3) <- c("text", "score")
data <- dplyr::bind_rows(data1, data2, data3)
# Create BERTopic model trained on data["text"] help(textTopics)
bert_model <- text::textTopics(data = data,
variable_name = "text",
embedding_model = "distilroberta",
min_df = 2,
set_seed = 42,
save_dir="./results")
testthat::expect_equal(bert_model$preds$t_1[2],
.1115696,
tolerance = 0.0001)
# Testing how individual topics are associated with "score"
test2 <- text::textTopicsTest(
model = bert_model,
pred_var_x = "score",
test_method = "linear_regression"
)
# Testing how individual topics are associated with "score"
help(textTopicsTest)
test2 <- text::textTopicsTest(
model = bert_model,
pred_var_x = "score",
test_method = "linear_regression"
)
testthat::expect_equal(test2[[1]]$test$x.score.estimate[1],
.1056764,
tolerance = 0.0001)
test2
testthat::expect_equal(test2$test$x.score.estimate[1],
.1056764,
tolerance = 0.0001)
plots <- text::textTopicsWordcloud(
model = bert_model,
test = test2,
figure_format = "png",
save_dir = "./results"
)
devtools::document()
devtools::build()
# Testing how individual topics are associated with "score"
test2 <- text::textTopicsTest(
model = bert_model,
pred_var_x = "score",
test_method = "linear_regression"
)
# Testing how individual topics are associated with "score"
test2 <- text::textTopicsTest(
model = bert_model,
pred_var_x = "score",
test_method = "linear_regression"
)
testthat::expect_equal(test2$test$x.score.estimate[1],
.1056764,
tolerance = 0.0001)
plots <- text::textTopicsWordcloud(
model = bert_model,
test = test2,
figure_format = "png",
save_dir = "./results"
)
# Testing how individual topics are associated with "score"
test2 <- text::textTopicsTest(
model = bert_model,
pred_var_x = "score",
test_method = "linear_regression"
)
testthat::expect_equal(test2$test$x.score.estimate[1],
.1056764,
tolerance = 0.0001)
plots <- text::textTopicsWordcloud(
model = bert_model,
test = test2,
figure_format = "png",
save_dir = "./results"
)
plots <- text::textTopicsWordcloud(
model = bert_model,
test = test2,
p_threshold = 0.05,
figure_format = "png",
save_dir = "./results"
)
plots <- text::textTopicsWordcloud(
model = bert_model,
test = test2,
p_threshold = 0.05,
figure_format = "png",
save_dir = "./results"
)
plots <- text::textTopicsWordcloud(
model = bert_model,
save_dir = "./results",
figure_format = "png"
)
plots <- text::textTopicsWordcloud(
model = bert_model,
test = test2,
p_threshold = 0.05,
figure_format = "png",
save_dir = "./results"
)
plots <- text::textTopicsWordcloud(
model = bert_model,
test = test2,
p_alpha = 0.05,
figure_format = "png",
save_dir = "./results"
)
devtools::install()
devtools::document()
.rs.restartR()
devtools::document()
devtools::build()
rcmdcheck::rcmdcheck()
devtools::document()
rcmdcheck::rcmdcheck()
devtools::document()
devtools::document()
devtools::build()
rcmdcheck::rcmdcheck()
devtools::document()
pkgload::dev_help('textTopicsWordcloud')
test2$test$x.score.estimate[1]
devtools::build()
devtools::document()
text = "John, please get that article on www.linkedin.com to me by 5:00PM
on Jan 9th 2012. 4:00 would be ideal, actually. If you have any
questions, You can reach me at (519)-236-2723x341 or get in touch with
my associate at harold.smith@gmail.com or on Twitter at @harold_smith__"
text_cleaned <- textCleaning(text)
text_cleaned
paste0("John, please get that article on www.linkedin.com to me by 5:00PM on",
"Jan 9th 2012. 4:00 would be ideal, actually. If you have any ",
"questions, You can reach me at (519)-236-2723x341 or get in touch with ",
"my associate at harold.smith@gmail.com or on Twitter at @harold_smith__")
text = paste0("John, please get that article on www.linkedin.com to me by 5:00PM on",
"Jan 9th 2012. 4:00 would be ideal, actually. If you have any ",
"questions, You can reach me at (519)-236-2723x341 or get in touch with ",
"my associate at harold.smith@gmail.com or on Twitter at @harold_smith__")
text
text = paste0("John, please get that article on www.linkedin.com to me by 5:00PM on",
"Jan 9th 2012. 4:00 would be ideal, actually. If you have any ",
"questions, You can reach me at (519)-236-2723x341 or get in touch with ",
"my associate at harold.smith@gmail.com or on Twitter at @harold_smith__")
text_cleaned <- textCleaning(text)
text_cleaned
text = paste0("John, please get that article on www.linkedin.com to me by 5:00PM on ",
"Jan 9th 2012. 4:00 would be ideal, actually. If you have any ",
"questions, You can reach me at (519)-236-2723x341 or get in touch with ",
"my associate at harold.smith@gmail.com or on Twitter at @harold_smith__ !")
text_cleaned <- textCleaning(text)
# List of placeholders to check
placeholders <- c("<URL>", "<TIME>", "<DATE_STRING>", "<PHONE_NUMBER>", "<EMAIL_ADDRESS>", "<@_SYMBOL>")
# Check if all placeholders are in the text
all_present <- all(sapply(placeholders, function(placeholder) grepl(placeholder, text)))
all_present
# Test that all placeholders are in the text
for (placeholder in placeholders) {
textthat::expect_equal(
grepl(placeholder, text),
TRUE,
info = paste("Placeholder", placeholder, "is missing.")
)
}
testthat::expect_equal(
grepl(placeholder, text),
TRUE,
info = paste("Placeholder", placeholder, "is missing.")
)
testthat::expect_equal(
grepl(placeholder, text_cleaned),
TRUE,
info = paste("Placeholder", placeholder, "is missing.")
)
text_cleaned
# Test that all placeholders are in the text
for (placeholder in placeholders) {
testthat::expect_equal(
grepl(placeholder, text_cleaned),
TRUE,
info = paste("Placeholder", placeholder, "is missing.")
)
}
# List of placeholders to check
placeholders <- c("<URL>", "<TIME>", "<DATE_STRING>",
"<PHONE_NUMBER>", "<EMAIL_ADDRESS>",
"<adfasfasf>",
"<@_SYMBOL>")
# Test that all placeholders are in the text
for (placeholder in placeholders) {
testthat::expect_equal(
grepl(placeholder, text_cleaned),
TRUE,
info = paste("Placeholder", placeholder, "is missing.")
)
}
# List of placeholders to check
placeholders <- c("<URL>", "<TIME>", "<DATE_STRING>",
"<PHONE_NUMBER>", "<EMAIL_ADDRESS>",
#"<adfasfasf>",
"<@_SYMBOL>")
# Test that all placeholders are in the text
for (placeholder in placeholders) {
testthat::expect_equal(
grepl(placeholder, text_cleaned),
TRUE,
info = paste("Placeholder", placeholder, "is missing.")
)
}
devtools::document()
devtools::document()
rcmdcheck::rcmdcheck()
pkgdown::build_site(new_process = FALSE)
devtools::build()
devtools::document()
pkgdown::build_site(new_process = FALSE)
install.packages("topics")
install.packages("topics")
# Load and prepare data
data1 <- Language_based_assessment_data_8[c("satisfactiontexts", "swlstotal")]
colnames(data1) <- c("text", "score")
data2 <- Language_based_assessment_data_8[c("harmonytexts", "hilstotal")]
colnames(data2) <- c("text", "score")
data3 <- Language_based_assessment_data_3_100[1:2]
colnames(data3) <- c("text", "score")
data <- dplyr::bind_rows(data1, data2, data3)
# Create BERTopic model trained on data["text"] help(textTopics)
bert_model <- text::textTopics(data = data,
variable_name = "text",
embedding_model = "distilroberta",
min_df = 2,
set_seed = 42,
save_dir="./results")
#install.packages("topics")
#
# .rs.restartR()
#help(textrpp_initialize)
textrpp_initialize(
condaenv = "berttopic2",
refresh_settings = TRUE
)
.rs.restartR()
#install.packages("topics")
#
# .rs.restartR()
#help(textrpp_initialize)
textrpp_initialize(
condaenv = "berttopic2",
refresh_settings = TRUE
)
library(text)
#install.packages("topics")
#
# .rs.restartR()
#help(textrpp_initialize)
textrpp_initialize(
condaenv = "berttopic2",
refresh_settings = TRUE
)
# Create BERTopic model trained on data["text"] help(textTopics)
bert_model <- text::textTopics(data = data,
variable_name = "text",
embedding_model = "distilroberta",
min_df = 2,
set_seed = 42,
save_dir="./results")
testthat::expect_equal(bert_model$preds$t_1[2],
.1115696,
tolerance = 0.0001)
# Testing how individual topics are associated with "score"
test2 <- text::textTopicsTest(
model = bert_model,
pred_var_x = "score",
test_method = "linear_regression"
)
plots <- text::textTopicsWordcloud(
model = bert_model,
test = test2,
# p_alpha = 0.05,
figure_format = "png",
save_dir = "./results"
)
plots <- text::textTopicsWordcloud(
model = bert_model,
test = test2,
# p_alpha = 0.05,
figure_format = "png",
seed = 42,
save_dir = "./results"
)
plots <- text::textTopicsWordcloud(
model = bert_model,
save_dir = "./results",
figure_format = "png",
seed = 42,
)
unlink("./results", recursive = TRUE)
save_dir_temp <- tempdir()
# Load and prepare data
data1 <- Language_based_assessment_data_8[c("satisfactiontexts", "swlstotal")]
colnames(data1) <- c("text", "score")
data2 <- Language_based_assessment_data_8[c("harmonytexts", "hilstotal")]
colnames(data2) <- c("text", "score")
data3 <- Language_based_assessment_data_3_100[1:2]
colnames(data3) <- c("text", "score")
data <- dplyr::bind_rows(data1, data2, data3)
# Create BERTopic model trained on data["text"] help(textTopics)
bert_model <- text::textTopics(data = data,
variable_name = "text",
embedding_model = "distilroberta",
min_df = 2,
set_seed = 42,
save_dir= save_dir_temp)
testthat::expect_equal(bert_model$preds$t_1[2],
.1115696,
tolerance = 0.0001)
# Testing how individual topics are associated with "score"
test2 <- text::textTopicsTest(
model = bert_model,
pred_var_x = "score",
test_method = "linear_regression"
)
plots <- text::textTopicsWordcloud(
model = bert_model,
test = test2,
# p_alpha = 0.05,
figure_format = "png",
seed = 42,
save_dir = save_dir_temp
)
plots <- text::textTopicsWordcloud(
model = bert_model,
save_dir = save_dir_temp,
figure_format = "png",
seed = 42,
)
predictions_powerprevsen_4 <- textPredict(
texts = schone_training$text_english[1:50],
model_info = "implicitpower_roberta23_previoussentence_nilsson2024",
story_id = schone_training$story_id_num[1:50],
participant_id = schone_training$participant_id[1:50],
dataset_to_merge_predictions = schone_training[1:50,],
previous_sentence = T)
# Manually recreate the dataset for 20 participants and 80 stories
PSE_stories <- tibble(data.frame(
Unnamed_0 = 1:62,
Participant_ID = rep(paste0('P', sprintf('%02d', 1:31)), each = 2), # 31 participants, each contributing 2 stories
Picture_ID = rep(c('IMG001', 'IMG002'), times = 31),
Story_Text = c(
"In the heart of the old forest, there was a place where magic still thrived. The trees whispered ancient secrets, and the ground pulsed with life.",
"The sun was shining brightly as the family arrived at the park for a picnic. Laughter filled the air as the children ran to play.",
"Emily loved to paint. One day, she found a magical brush that brought her paintings to life. Her world was suddenly filled with vibrant creatures.",
"A young boy named Jake let go of his balloon, watching it soar into the sky. He smiled as it drifted away, imagining where it would go.",
"The sea was calm, and the boat gently rocked as the fisherman cast his net. He hummed an old tune, feeling at peace with the world.",
"The library was quiet except for the soft sound of turning pages. A girl sat in the corner, lost in a world of dragons and knights.",
"The mountain path was steep, but they pressed on, determined to reach the summit. Clouds gathered overhead, and a storm was brewing.",
"The train sped through the countryside, and the passengers stared out at the passing fields. In one car, a young woman scribbled furiously in a notebook.",
"The city streets were alive with the buzz of activity. People hurried by, oblivious to the man playing a sad tune on his guitar.",
"The forest was dark and dense, but they felt a sense of wonder as they ventured deeper. Something ancient watched them from the shadows.",
"The campfire crackled as stories were shared. The group huddled closer, the cold night air nipping at their faces.",
"The waterfall roared as it plunged into the river below. The mist hung in the air, creating a rainbow in the afternoon light.",
"The old house creaked with every step. Dust hung in the air, and the furniture was covered in white sheets, as though it had been abandoned for years.",
"The beach was deserted, except for a lone figure walking along the shoreline. She picked up a shell and held it to her ear, listening for the ocean.",
"The stars twinkled overhead as the children lay in the grass, pointing out constellations. The night was still, and the world seemed infinite.",
"The garden was in full bloom, with flowers of every color swaying gently in the breeze. A bumblebee buzzed from one blossom to the next.",
# Continuing with similar multi-sentence stories
"The desert stretched out before them, an endless sea of sand. The heat was oppressive, but they knew they had to keep moving.",
"The market was bustling with vendors selling everything from spices to silk. The air was filled with the scent of exotic foods.",
"The rain poured down, soaking them to the bone, but they laughed anyway. It was a memory they would cherish forever.",
"The ship sailed on, its sails full of wind, cutting through the waves. Below deck, the crew prepared for the long journey ahead.",
"The autumn leaves crunched underfoot as they walked through the park. The crisp air was filled with the scent of pine and earth.",
"The old library was filled with books of every kind. She wandered through the aisles, running her fingers along the spines.",
"The clock ticked slowly, marking the minutes as they passed. She stared at the door, waiting for him to return.",
"The snow fell softly, blanketing the world in white. Everything was still, as though the earth was holding its breath.",
"The forest floor was covered in a thick layer of fallen leaves, and their footsteps crunched loudly with every step.",
"The cat stretched lazily in the sun, its tail flicking idly. It watched the birds in the trees with mild interest.",
"The bakery smelled of fresh bread and cinnamon, making her stomach growl. She couldn't resist buying a warm croissant.",
"The storm raged outside, but inside the cabin, the fire crackled warmly. They sat together, sipping hot cocoa and watching the flames dance.",
"The train rumbled over the tracks, and she stared out the window, lost in thought. The scenery blurred as her mind wandered.",
"The sound of waves crashing against the rocks filled the air. He stood at the edge of the cliff, watching the ocean with awe.",
"The bustling city square was full of life, with street performers and food stalls at every corner. It was a sensory overload.",
"The smell of pine filled the air as they hiked through the forest. The trail wound through the trees, leading them deeper into the wilderness.",
# Continuing for the rest of the stories
"The airplane soared above the clouds, and the passengers looked out at the endless sky. Below them, the world seemed so small.",
"The sun set behind the mountains, casting a golden glow over the landscape. They stood in silence, taking in the beauty.",
"The ancient ruins were overgrown with vines and moss, but they could still see the outlines of old buildings and statues.",
"The moonlit path led them through the woods, the branches swaying gently in the breeze. The night was calm, but full of mystery.",
"The ship creaked as it swayed in the harbor. Seagulls called out overhead, their cries echoing in the still air.",
"The room was filled with the sound of laughter and music as they danced the night away. It was a night to remember.",
"The train came to a stop, and the passengers began to disembark. She took a deep breath, ready for the adventure ahead.",
"The firework exploded in a burst of color, lighting up the night sky. The crowd cheered as the next one launched into the air.",
"In the heart of the old forest, there was a place where magic still thrived. The trees whispered ancient secrets, and the ground pulsed with life.",
"The sun was shining brightly as the family arrived at the park for a picnic. Laughter filled the air as the children ran to play.",
"Emily loved to paint. One day, she found a magical brush that brought her paintings to life. Her world was suddenly filled with vibrant creatures.",
"A young boy named Jake let go of his balloon, watching it soar into the sky. He smiled as it drifted away, imagining where it would go.",
"The sea was calm, and the boat gently rocked as the fisherman cast his net. He hummed an old tune, feeling at peace with the world.",
"The library was quiet except for the soft sound of turning pages. A girl sat in the corner, lost in a world of dragons and knights.",
"The mountain path was steep, but they pressed on, determined to reach the summit. Clouds gathered overhead, and a storm was brewing.",
"The train sped through the countryside, and the passengers stared out at the passing fields. In one car, a young woman scribbled furiously in a notebook.",
"The city streets were alive with the buzz of activity. People hurried by, oblivious to the man playing a sad tune on his guitar.",
"The forest was dark and dense, but they felt a sense of wonder as they ventured deeper. Something ancient watched them from the shadows.",
"The campfire crackled as stories were shared. The group huddled closer, the cold night air nipping at their faces.",
"The waterfall roared as it plunged into the river below. The mist hung in the air, creating a rainbow in the afternoon light.",
"The old house creaked with every step. Dust hung in the air, and the furniture was covered in white sheets, as though it had been abandoned for years.",
"The beach was deserted, except for a lone figure walking along the shoreline. She picked up a shell and held it to her ear, listening for the ocean.",
"The stars twinkled overhead as the children lay in the grass, pointing out constellations. The night was still, and the world seemed infinite.",
"The garden was in full bloom, with flowers of every color swaying gently in the breeze. A bumblebee buzzed from one blossom to the next.",
# Continuing with similar multi-sentence stories
"The desert stretched out before them, an endless sea of sand. The heat was oppressive, but they knew they had to keep moving.",
"The market was bustling with vendors selling everything from spices to silk. The air was filled with the scent of exotic foods.",
"The rain poured down, soaking them to the bone, but they laughed anyway. It was a memory they would cherish forever.",
"The ship sailed on, its sails full of wind, cutting through the waves. Below deck, the crew prepared for the long journey ahead.",
"The autumn leaves crunched underfoot as they walked through the park. The crisp air was filled with the scent of pine and earth.",
"The old library was filled with books of every kind. She wandered through the aisles, running her fingers along the spines."
),
story_id = 1:62
))
# Create datasets for testing merging feature help(reframe)
# Participant level data for testing data merge
PSE_stories_participant_level <- PSE_stories %>%
group_by(Participant_ID) %>%
reframe(stories=paste(Story_Text, collapse = " "))
# Story level data for testing data merge
PSE_stories_story_level <- PSE_stories
# Sentence level data for testing data merge
PSE_stories_sentence_level <- PSE_stories %>%
filter()
#split to sentences help(unnest)
PSE_stories_sentence_level <- PSE_stories %>%
mutate(Story_Text = strsplit(Story_Text, "[\\.\\!\\?]\\s+")) %>%
tidyr::unnest(Story_Text)
implicit_motive <- text::textClassify(
model_info = "implicitpower_roberta23_previoussentence_nilsson2024",
texts = PSE_stories_participant_level$stories,
show_texts = T
)
library(text)
textrpp_initialize()
.rs.restartR()
library(text)
textrpp_initialize()
implicit_motive <- text::textClassify(
model_info = "implicitpower_roberta23_previoussentence_nilsson2024",
texts = PSE_stories_participant_level$stories,
show_texts = T
)
implicit_motive
implicit_motive <- text::textClassify(
model_info = "implicitachievment_roberta23_previoussentence_nilsson2024",
texts = PSE_stories_participant_level$stories,
show_texts = T
)
implicit_motive <- text::textClassify(
model_info = "implicitaffiliation_roberta23_previoussentence_nilsson2024",
texts = PSE_stories_participant_level$stories,
show_texts = T
)
implicit_motive
implicit_motive <- text::textClassify(
model_info = "implicitachievement_roberta23_previoussentence_nilsson2024",
texts = PSE_stories_participant_level$stories,
show_texts = T
)
implicit_motive
implicit_achievement <- text::textClassify(
model_info = "implicitachievement_roberta23_previoussentence_nilsson2024",
texts = PSE_stories_participant_level$stories,
show_texts = T
)
schone_training <- readRDS("/Users/oscarkjell/Desktop/schone_training_150424.rds")
predictions_powerrob_2 <- textPredict(
texts = schone_training$text_english[1:50],
model_info = "implicitpower_roberta23_nilsson2024",
story_id = schone_training$story_id_num[1:50],
dataset_to_merge_predictions = schone_training[1:50,],
show_texts = T)
predictions_powerrob_2
predictions_powerrob_1 <- textPredict(
texts = schone_training$text_english[1:50],
model_info = "implicitpower_roberta23_nilsson2024")
predictions_powerrob_1
predictions_powerrob_2$person_predictions$person_class_no_wc_correction
predictions_powerrob_2$person_predictions
predictions_powerrob_2$person_predictions$person_class_no_wc_correction
predictions_powerrob_4 <- textPredict(
texts = schone_training$text_english[1:50],
model_info = "implicitpower_roberta23_nilsson2024",
story_id = schone_training$story_id_num[1:50],
participant_id = schone_training$participant_id[1:50],
dataset_to_merge_predictions = schone_training[1:50,])
predictions_powerrob_4
implicit_power <- text::textClassify(
model_info = "implicitpower_roberta23_previoussentence_nilsson2024",
texts = PSE_stories_participant_level$stories,
show_texts = T
)
devtools::check_win_release() # If you’re on linux or the mac, use devtools::build_win() to check on windows.