-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.R
1505 lines (1118 loc) · 57.2 KB
/
app.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
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
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# if (!require("pacman")) install.packages("pacman")
# pacman::p_load(shiny, knitr, tidyverse, readtext, udpipe, tools, DT, textcat,
# eeptools, childesr, zoo, koRpus, koRpus.lang.en, colourpicker)
library(shiny)
library(knitr) # To prepare Rmarkdown instructions
library(tidyverse) # For data manipulation
library(readtext) # Read in .doc and .docx files
library(udpipe) # Part-of-speech-tagger
library(tools) # To get file extension
library(DT) # To create a datatable
library(textcat) # To detect the language of a text
library(eeptools) # Calculate ages
library(childesr) # Look up CHILDES norms
library(zoo) # For the time series analysis.
library(koRpus) # To obtain lexical diversity measures
library(koRpus.lang.en) # Corpus English Language
library(colourpicker)
colours <- read_csv("colours.csv")
shinyApp(
ui <- fluidPage(#theme = "flatly.css",
# Instructions page ----
navbarPage("MiMo",
tabPanel("Instructions",
uiOutput('Rmarkdown_instructions')
),
# Let's get started navbar ----
navbarMenu("Let's get started!",
#(1) Enter text tab panel ----
tabPanel("(1) Enter text",
radioButtons("radio", label = h3("How do you wish to enter your data?"),
choices = list("Upload file (.doc, .docx, or .txt)" = 1, "Enter text in textbox" = 2),
width = '100%', selected = 1),
conditionalPanel(condition = "input.radio == 1",
fileInput("text_file", "Select file",
multiple = FALSE,
accept = c("text/plain",
"application/vnd.openxmlformats-officedocument.wordprocessingml.document",
"application/msword")
)
),
conditionalPanel(condition = "input.radio == 2",
textAreaInput("text_file_TA", "Enter text here...",
placeholder = "Enter text here...",
width = "100%", height = "100%", resize = "both")
# verbatimTextOutput("value")
)
),
#(2) Check language tab panel ----
tabPanel("(2) Check language",
htmlOutput("text_example"),
radioButtons("proceed", label = h3("How do you wish to proceed?"),
choices = list("Continue" = 1, "Select another language" = 2),
width = '100%', selected = 1),
conditionalPanel(condition = "input.proceed == 2",
textAreaInput("manual_language", "Enter a language...",
placeholder = "Enter language name here...",
width = "1000px", resize = "both"),
textAreaInput("manual_url", "(OPTIONAL) Enter a model repo...",
placeholder = "Enter location of repo here...",
width = "1000px", resize = "both")
)
)
),
# Let's explore nav bar ----
navbarMenu("Let's explore!",
#(1) coloured output tab panel ----
tabPanel("(1) Coloured output",
tags$head(
tags$style(HTML({"
.mytooltip {
position: relative;
display: inline-block;
}
.mytooltip .tooltiptext {
visibility: hidden;
width: 120px;
background-color: #4d0026;
color: #fff;
text-align: center;
border: 6px solid #ff80ff;
padding: 5px 0;
/* Position the tooltip */
position: absolute;
z-index: 1;
bottom: 100%;
left: 50%;
margin-left: -60px;
}
.mytooltip:hover .tooltiptext {
visibility: visible;
}
"}))
),
h3("Table will take a few seconds to appear/refresh..."),
DT::dataTableOutput("table_coloured")
),
#(2) Syntactic measures tab panel-----
tabPanel("(2) Syntactic measures",
h3("Table will take a few seconds to appear/refresh..."),
DT::dataTableOutput("table_summaries"),
br()
), # end of tab panel ----
#(3) Lexical measures tab panel-----
tabPanel("(3) Lexical measures",
h3("Table will take a few seconds to appear/refresh..."),
DT::dataTableOutput("table_summaries2")
), # end of tab panel
#(4) Tags -----
tabPanel("(4) Tags",
mainPanel(
DT::dataTableOutput("tag_table")
)
) # end of tags tabpanel
), # end of nav bar menu
# Cheat sheet tab panel----
tabPanel("Cheat sheet",
uiOutput('cheatsheet')
),
# Colour tab panel----
tabPanel("Colours",
selectInput(inputId = "colour_scheme",
label = h3("Select colour scheme"),
choices = list("All colours" = 2,
"Verb-related words only" = 3,
"Noun-related words only" = 4,
"Linking words (conjunctions and Prepositions)" = 5),
selected = 2),
h3("Widgets contain hexadecimal colour codes.
Colours may be conveniently copied and pasted by copying and pasting these codes."),
br(),
h3("Word classes in the Verb Complex (sometimes called Verb Phrase)"),
htmlOutput("colour_picker_verb"),
htmlOutput("colour_picker_copula"),
htmlOutput("colour_picker_auxiliary"),
htmlOutput("colour_picker_particle"),
htmlOutput("colour_picker_advb"),
br(),
h3("Word classes in the Noun Phrase"),
htmlOutput("colour_picker_noun"),
htmlOutput("colour_picker_det"),
htmlOutput("colour_picker_adj"),
htmlOutput("colour_picker_pron"),
br(),
h3("Linking words"),
htmlOutput("colour_picker_prep"),
htmlOutput("colour_picker_sub"),
htmlOutput("colour_picker_coord"),
br(),
h3("Other"),
htmlOutput("colour_picker_punct"),
htmlOutput("colour_picker_interjection")
), # End of tabPanel "Colors"
# Extra... nav bar ----
navbarMenu("Extra...",
# Coloured output CnP ----
tabPanel("(1) Coloured output CnP",
h3("This table can be copied and pasted into a Word Processor Document, e.g. Microsoft Word"),
h3("Table will take a few seconds to appear/refresh..."),
DT::dataTableOutput("table_coloured_reduced")
), # end of Tab Panel
tabPanel("(2) Plot data", # Plot data ----
sidebarPanel(
selectInput(inputId = "collection3",
label = "Collection",
choices = c("PLEASE CHOOSE...",
"Eng-NA", "Eng-UK", "KIDEVAL",
"Spanish", "French", "German", "Japanese", "EastAsian",
"Clinical-MOR", "Biling"),
selected = NULL
),
selectInput(inputId = "variable3",
label = "Variable",
choices = c(
"MLU in morphemes" = "mlu_m",
"MLU in words" = "mlu_w",
"HDD" = "hdd",
"TTR" = "ttr"),
selected = "mlu_m"
),
numericInput(inputId = "num_utts3",
label = "Min. utterances",
value = 100
),
numericInput(inputId = "bin_width3",
label = "Bin width",
value = 10
),
sliderInput(inputId = "shading3",
label = "Shading",
min = 0,
max = 0.3,
value = 0.1
),
sliderInput(inputId = "trim_data3",
label = "Trim data?",
min = 0,
max = 100,
value = c(1,100)
),
hr(),
h3("Show speaker"),
textInput(inputId = "child_name",
label = "Name of Child",
placeholder = "Child Name"),
# Copy the line below to make a date selector
textInput(inputId = "value",
label = "Value for MLU/HDD etc",
placeholder = "Value"),
dateInput("dob3", label = h4("Date of Birth")),
dateInput("dot3", label = h4("Date of Test")),
htmlOutput("age")
), # sidebarpanel
mainPanel(
h3("Table will take a few seconds to appear/refresh..."),
plotOutput("DIY_plot",
dblclick = "plot_dblclick3",
brush = brushOpts(
id = "plot_brush3",
resetOnNew = TRUE
))
)
) # end of tabPanel
) # end of navbar menu
) # end of nav bar page
),
# server statement----
server <- function(input, output, session){
# ***REACTIVE STATEMENTS*** ----
# text (read in text file) ----
text <- reactive({
if(is.null(input$text_file) & input$text_file_TA=="") return(NULL)
# browser()
if(is.null(input$text_file)==FALSE){
text <- readtext(input$text_file$datapath)$text
}
if(input$text_file_TA!=""){
text <- input$text_file_TA
}
#
return(text)
})
# lang (obtaining language) ----
lang <- reactive({
if(is.null(input$text_file) & input$text_file_TA=="") return(NULL)
if(is.null(input$text_file)==FALSE){
text <- readtext(input$text_file$datapath)$text
}
if(input$text_file_TA!=""){
text <- input$text_file_TA
}
lang <- textcat(text)
return(lang)
})
# table (showing transcript)----
table <- reactive({
if(is.null(input$text_file) & input$text_file_TA=="") return(NULL)
if(is.null(input$text_file)==FALSE){
text <- readtext(input$text_file$datapath)$text
}
if(input$text_file_TA!=""){
text <- input$text_file_TA
}
if(input$manual_language==""){
lang <- textcat(text)} else{
lang <- input$manual_language
}
lang <- tolower(lang)
# THIS LINE IS A BREAK LINE
# browser(); one <- 1; one <- 1; one <- 1; one <- 1; one <-1
str_split_keep_delimiter <- function(string, delV){ #string and delimiter vector
for(i in 1:length(delV)){
search_string <- paste0("(", delV[i], ")")
replace_string <- "\\1***"
string <- str_replace_all(string, search_string, replace_string)
}
string <- str_split(string, "[\x2a][\x2a][\x2a]")
string <- unlist(string)
string <- string[which(string != "")] # removes any blanks which may have been created
return(string)
}
# Convert text object to vector
alphanumeric <- function(x){
return(grepl("[a-zA-Z0-9]",x))
}
text <- str_split_keep_delimiter(text, c("[\x2e]+[\x22|\x27]*[\x20]*([\\[|\x28][^\\[|\x28]*[\\]|\x29][\x20]*)*",
"[\x21]+[\x22|\x27]*[\x20]*([\\[|\x28][^\\[|\x28]*[\\]|\x29][\x20]*)*",
"[\x3f]+[\x22|\x27]*[\x20]*([\\[|\x28][^\\[|\x28]*[\\]|\x29][\x20]*)*")
)
text <- as.data.frame(text)
text <- text %>% filter(alphanumeric(text) == TRUE) # Gets rid of blank lines / lines with only punctuation
text$text <- str_trim(text$text) # Trims lead/trailing spaces
extract_speaker <- function(x){
x <- str_trim(x) # trim leading and trailing spaces
x <- strsplit(x, " ")[[1]][1] # split by space and identify first word
x <- stringr::str_match(x, "[a-zA-Z]+[\x3a]$") # identify whether first word could be speaker
return(x)
}
remove_speaker <- function(x){
x <- gsub("[a-zA-Z]+[\x3a]", "", x) # replace speaker with ""
return(x)
}
text$speaker <- sapply(text$text, extract_speaker)
text %>%
fill(speaker, .direction = "down") -> text
text$speaker[which(is.na(text$speaker))] <- "xxx:"
speakers <- text$speaker
text$text <- sapply(text$text, remove_speaker)
#Remove standalone punctation
text$text <- gsub("[\x20][:punct:][\x20]", "", text$text) # needs to be changed
# Calculate Num Words - using spaces to delimit words
count_words_using_spaces <- function(x){
return(str_count(x, "[^\x20]+"))
}
remove_non_alphanumeric <- function(x){
return(gsub("[^\x20a-zA-Z0-9]", "", x))
}
extract_comments_as_vector <- function(x){
result <- unlist(str_extract_all(x, "[\x28][^\x28|\x29]*[\x29]"))
result <- paste0("", result)
result <- as.vector(result)
return(result)
}
extract_tags_as_vector <- function(x){
result <- unlist(str_extract_all(x, "\\[[^\\[]*\\]"))
result <- paste0("", result)
result <- as.vector(result)
return(result)
}
extract_comments_as_string <- function(x){
result <- unlist(str_extract_all(x, "[\x28][^\x28|\x29]*[\x29]"))
result <- paste0("", result, collapse = "")
return(result)
}
extract_tags_as_string <- function(x){
result <- unlist(str_extract_all(x, "\\[[^\\[]*\\]"))
result <- paste0("", result, collapse = "")
return(result)
}
replace_comments <- function(x){
result <- str_replace_all(x, "[\x28][^\x28|\x29]*[\x29]", "\x28")
return(result)
}
replace_tags <- function(x){
result <- str_replace_all(x, "\\[[^\\[]*\\]", "\\[")
return(result)
}
insert_comments <- function(x, y){ ## Doesn't work
y <- str_replace_all(y, "[\x28]", "OPENBRACKETHERE")
y <- str_replace_all(y, "[\x29]", "CLOSEDBRACKETHERE")
for(i in 1:length(y)){
x <- str_replace(x, "[\x28]", y[i])
}
x <- str_replace_all(x, "OPENBRACKETHERE","\x28")
x <- str_replace_all(x, "CLOSEDBRACKETHERE", "\x29")
return(x)
}
insert_tags <- function(x, y){ ## x = string, y = vector
y <- str_replace_all(y, "\\[", "OPENBRACKETHERE")
y <- str_replace_all(y, "\\]", "CLOSEDBRACKETHERE")
for(i in 1:length(y)){
x <- str_replace(x, "\\[", y[i])
}
x <- str_replace_all(x, "OPENBRACKETHERE","\\[")
x <- str_replace_all(x, "CLOSEDBRACKETHERE", "\\]")
return(x)
}
remove_comments <- function(str){
str <- str_replace_all(str, "[\x28][^\x28]*[\x29]", "")
return(str)
}
remove_tags <- function(str){
str <- str_replace_all(str, "\\[[^\\[]*\\]", "")
return(str)
}
Num_Words <- count_words_using_spaces(remove_non_alphanumeric(remove_comments(remove_tags(text$text))))
text$comments <- sapply(text$text, extract_comments_as_string)
text$tags <- sapply(text$text, extract_tags_as_string)
comments <- text$comments
tags <- text$tags
text$text <- replace_comments(text$text)
text$text <- replace_tags(text$text)
text_comments_extracted <- gsub("\\[", "", text$text)
text_comments_extracted <- gsub("\\(", "", text_comments_extracted)
has_period <- function(x){
return(grepl("[\x2e]", x))
}
has_question_mark <- function(x){
return(grepl("[\x3f]", x))
}
has_single_exclamation_mark <- function(x){
return(str_count(x, "[\x21]") == 1)
}
has_multiple_exclamation_marks <- function(x){
return(str_count(x, "[\x21]") >= 2)
}
text$mood <- ""
text$mood[has_period(text$text)] <- "isdeclarative"
text$mood[has_question_mark(text$text)] <- "isinterrogative isquestion"
text$mood[has_single_exclamation_mark(text$text)] <- "isimperative"
text$mood[has_multiple_exclamation_marks(text$text)] <- "isexclamative"
mood <- text$mood
# Download language model and parse text
if(input$manual_url==""){
model <- udpipe_download_model(lang, model_dir = tempdir()) # NB can add "model_dir = tempdir()"
}
if(input$manual_url!=""){
model <- udpipe_download_model(lang, model_dir = tempdir(), udpipe_model_repo = input$manual_url)
}
model <- udpipe_load_model(model$file_model)
# browser(); one <- 1; one <- 1; one <- 1; one <- 1; one< -1
text <- udpipe_annotate(model, text$text)
text <- as.data.frame(text)
text$morpheme <- 1
text$morpheme[which(text$upos == "PUNCT")] <- 0 # So we don't count punctuation as a morpheme
if(grepl("english", lang, ignore.case=TRUE)){source("english_labelling_rules.R", local = TRUE)}
text$upos[which(text$dep_rel == "cop")] <- "COPULA"
# Identify number of clauses
# browser(); one <- 1; one <- 1; one <- 1; one <- 1; one <- 1; one <- 1; one <-1
text$num_clause <- as.numeric(text$upos == "VERB" | text$upos == "COPULA")
text$num_fin_clause <- as.numeric(grepl("VerbForm=Fin", text$feats))
verb_form <- function(x){
return(
case_when(
grepl("Tense=Past[\x7c]VerbForm=Fin",x) == TRUE ~ "hasPastTense",
grepl("Tense=Pres[\x7c]VerbForm=Fin", x) == TRUE ~ "hasPresTense hasPresentTense",
grepl("Tense=Past[\x7c]VerbForm=Part", x) == TRUE ~ "hasPastParticiple",
grepl("Tense=Pres[\x7c]VerbForm=Part", x) == TRUE ~ "hasPresentParticiple hasPresParticiple",
grepl("VerbForm=Inf", x) == TRUE ~ "hasInfinitive",
TRUE ~ ""
)
)
}
text$verb_form <- sapply(text$feats, verb_form)
rel_clause <- function(x){
return(
case_when(
grepl("relcl", x) == TRUE ~ "hasRelativeClause",
TRUE ~ ""
)
)
}
text$rel_clause <- sapply(text$dep_rel, rel_clause)
highlight <- function(text, colour){ # highlights text in a particular colour
result <- paste0("<span style=\"background-color:", colour, ";\">",
" ", text, " ",
"</span>")
return(result)
}
text$features_coloured <- paste0(highlight(text$dep_rel, "#cc99ff"), # Violet
highlight(text$token, "#ff6666"), # Orange
highlight(text$upos, "#ffc299"), # Green
highlight(text$xpos, "#9999ff"), # Dark blue
highlight(text$feats, "#c68c53"), # Brown
" "
)
add_tool_tip <- function(text, label){
result <- paste0("<div class=\"mytooltip\">",
text,
"<span class=\"tooltiptext\">",
label,
"</span>",
"</div>")
return(result)
}
if(is.null(input$VERB_colour)) {VERB_colour <- "#FFAB94"} else {VERB_colour <- input$VERB_colour}
if(is.null(input$COPULA_colour)) {COPULA_colour <- "#FFAB94"} else {COPULA_colour <- input$COPULA_colour}
if(is.null(input$AUXILIARY_colour)) {AUXILIARY_colour <- "#FAD4CB"} else {AUXILIARY_colour <- input$AUXILIARY_colour}
if(is.null(input$PARTICLE_colour)) {PARTICLE_colour <- "#FAD4CB"} else {PARTICLE_colour <- input$PARTICLE_colour}
if(is.null(input$ADVB_colour)) {ADVB_colour <- "#FAD4CB"} else {ADVB_colour <- input$ADVB_colour}
if(is.null(input$NOUN_colour)) {NOUN_colour <- "#B6B6F5"} else {NOUN_colour <- input$NOUN_colour}
if(is.null(input$DET_colour)) {DET_colour <- "#ADFFFF"} else {DET_colour <- input$DET_colour}
if(is.null(input$ADJ_colour)) {ADJ_colour <- "#ADFFFF"} else {ADJ_colour <- input$ADJ_colour}
if(is.null(input$PRON_colour)) {PRON_colour <- "#99FF69"} else {PRON_colour <- input$PRON_colour}
if(is.null(input$PREP_colour)) {PREP_colour <- "#FFFF52"} else {PREP_colour <- input$PREP_colour}
if(is.null(input$SUB_colour)) {SUB_colour <- "#FCAD46"} else {SUB_colour <- input$SUB_colour}
if(is.null(input$COORD_colour)) {COORD_colour <- "#FFCD7D"} else {COORD_colour <- input$COORD_colour}
if(is.null(input$PUNCT_colour)) {PUNCT_colour <- "#eeeedd"} else {PUNCT_colour <- input$PUNCT_colour}
if(is.null(input$INTERJECTION_colour)) {INTERJECTION_colour <- "#C29A72"} else {INTERJECTION_colour <- input$INTERJECTION_colour}
highlight_wc <- function(string, wc){
if(is.na(wc)){return(string)}
# red (original colours - user may change)
else if(wc == "VERB"){result <- add_tool_tip(highlight(paste0("<b>",string,"</b>"), VERB_colour), "VERB")}
else if(wc == "COPULA"){result <- add_tool_tip(highlight(paste0("<b>", string, "</b>"), COPULA_colour), "COPULA")}
# orange
else if(wc == "SCONJ"){result <- add_tool_tip(highlight(string, SUB_colour), "SCONJ.")}
# light orange
else if(wc == "CCONJ"){result <- add_tool_tip(highlight(string, COORD_colour), "CCONJ.")}
# green
else if(wc == "PRON"){result <- add_tool_tip(highlight(string, PRON_colour), "PRON.")}
# pink
else if(wc == "AUX"){result <- add_tool_tip(highlight(string, AUXILIARY_colour), "AUX.")}
else if(wc == "ADV"){result <- add_tool_tip(highlight(string, ADVB_colour), "ADV.")}
else if(wc == "PART"){result <- add_tool_tip(highlight(string, PARTICLE_colour), "PARTICLE")}
# dark blue
else if(wc == "NOUN"){result <- add_tool_tip(highlight(string, NOUN_colour), "NOUN")}
else if(wc == "PROPN"){result <- add_tool_tip(highlight(string, NOUN_colour), "PROPN")}
# cyan
else if(wc == "DET"){result <- add_tool_tip(highlight(string, DET_colour), "DET.")}
else if(wc == "DET.poss"){result <- add_tool_tip(highlight(string, DET_colour), "DET.poss")}
else if(wc == "ADJ"){result <- add_tool_tip(highlight(string, ADJ_colour), "ADJ.")}
else if(wc == "NUM"){result <- add_tool_tip(highlight(string, DET_colour), "NUM.")}
# brown
else if(wc == "INTJ"){result <- add_tool_tip(highlight(string, INTERJECTION_colour), "INTJ")}
# yellow
else if(wc == "ADP"){result <- add_tool_tip(highlight(string, PREP_colour), "PREP.")}
# grey
else if(wc == "PUNCT"){result <- add_tool_tip(highlight(string, PUNCT_colour), "PUNCT.")}
else if(wc == "X"){result <- add_tool_tip(highlight(string, "#b8b894"), "X")}
else if(wc == "SYM"){result <- add_tool_tip(highlight(string, "#b8b894"), "SYM")}
else{result <- string}
return(result)
}
text %>% filter(!is.na(upos)) -> text
text$coloured <- mapply(highlight_wc, text$token, text$upos) # Applies html formatting to tokens
# Create placemarkers used for inserting comments and tags back into text
text$coloured[which(text$token == "(")] <- "("
text$coloured[which(text$token == "[")] <- "["
# create variable showing line of text
text$line <- NULL
get_doc_number <- function(x){
return(as.numeric(substr(x, 4, nchar(x))))
}
text$line <- sapply(text$doc, get_doc_number)
# create variable which will allow user to search for word by class
text$upos[which(text$upos == "ADP")] <- "PREP"
text$hasclass <- paste0("has", tolower(text$upos))
text$neg <- ""
text$neg[which(grepl("not", text$token))] <- "hasneg"
text$neg[which(grepl("n't", text$token))] <- "hasneg"
# Reshape data
text %>%
group_by(line) %>%
summarise(sentence_coloured = paste(coloured, collapse = " "),
sentence = paste(token, collapse = " "),
features_coloured = paste(features_coloured, collapse = " "),
features = paste(feats, collapse = " "),
has_class = paste(hasclass, collapse = " "),
pos_tags = paste(upos, collapse = " "),
neg = paste(neg, collapse = " "),
`Num Morphs` = sum(morpheme),
num_clause = sum(num_clause),
num_fin_clause = sum(num_fin_clause),
verb_form = paste(verb_form, collapse = " "),
rel_clause = paste(rel_clause, collapse = " ")
) -> text
text$NPexpansion <- ""
text$NPexpansion[grepl("((DET|DET\x2eposs|ADJ|NUM)\x20)+(NOUN|PROPN)", text$pos_tags)] <- "hasNPexpansion"
text$VCexpansion <- ""
text$VCexpansion[grepl("((AUX|ADV|VERB|PART)\x20)+(VERB)", text$pos_tags)] <- "hasVCexpansion hasVPexpansion"
text$clause2 <- ""
text$multipleclauses <- ""
text$clause3 <- ""
text$clause4 <- ""
text$clause5 <- ""
text$clause2[which(text$num_fin_clause == 2)] <- "has2clauses"
text$multipleclauses[which(text$num_fin_clause >= 2)] <- "hasmultipleclauses, iscomplex"
text$clause3[which(text$num_fin_clause == 3)] <- "has3clauses"
text$clause4[which(text$num_fin_clause == 4)] <- "has4clauses"
text$clause5[which(text$num_fin_clause == 5)] <- "has5clauses"
# This section has functions to colour comments and tags. The comments and tags are then
# inserted back into the sentence_coloured variable, and coloured accordingly
colour_comments <- function(x){
result <- str_replace_all(x, "[\x28]", "<span style=\"color:#333399;\">(")
result <- str_replace_all(result, "[\x29]", ")</span>")
return(result)
}
colour_tags <- function(x){
result <- str_replace_all(x, "\\[", "<span style=\"color:#992600;\">\\[")
result <- str_replace_all(result, "\\]", "\\]</span>")
return(result)
}
for(i in 1:nrow(text)){
text$sentence_coloured[i] <- colour_comments(insert_comments(text$sentence_coloured[i],
extract_comments_as_vector(comments[i])))
text$sentence_coloured[i] <- colour_tags(insert_tags(text$sentence_coloured[i],
extract_tags_as_vector(tags[i])))
}
text$speaker <- speakers
text$speaker_no_html <- speakers
unique_speakers <- unique(speakers)
unique_speakers <- unique_speakers[which(unique_speakers != "")]
speaker_colours <- c("#f2ffe6", "#ffddcc", "#e6f7ff", "#ffe6ff",
"#ffffcc", "#ffe6ff", "#ccddff", "#ffcce0",
"#ccccff", "#ff0000", "#81a375", " #ccffcc")
speaker_colours <- c(speaker_colours, rep("#ffffff", 100)) # just in case there are lots of speakers!
for(i in 1:nrow(text)){
text$speaker[i] <- highlight(text$speaker[i],
speaker_colours[which(unique_speakers == text$speaker[i])])
}
text$mood <- mood
text$passive <- ""
text$passive[which(grepl("Voice=Pass", text$features))] <- "haspassive"
text$relativepronoun <- ""
text$relativepronoun[which(grepl("PronType=Rel", text$features))] <- "hasrelativepronoun"
text$modal <- ""
text$modal[which(grepl("MD", text$features))] <- "hasmodalverb"
text$speaker[which(text$`Num Morphs`==0)] <- ""
text$speaker_no_html[which(text$`Num Morphs`==0)] <- ""
text$`Num Words` <- Num_Words
text$text_comments_extracted <- text_comments_extracted
# Create variable to allow user to identify lines with comments or tags
hascomment <- rep("", length(comments))
hascomment[which(comments!="")] <- "hascomment"
text$hascomment <- hascomment
hastag <- rep("", length(tags))
hastag[which(tags!="")] <- "hastag"
text$hastag <- hastag
# Create a column to allow user to search for specific tags
tags_plus_content <- str_replace_all(tags, "\x20", "") #remove gaps
tags_plus_content <- str_replace_all(tags_plus_content, "\\[", "hastag") #start with "hastag"
tags_plus_content <- str_trim(str_replace_all(tags_plus_content, "\\]", "\x20")) #remove final brackets
text$tags_plus_content <- tags_plus_content # create variable
text$tags <- tags
# Code produces position_within_turn and turn_length variables.
text$new_turn <- 0
speaker <- ""
for(i in 1:nrow(text)){
if(text$speaker[i] != "" & text$speaker[i] != speaker){text$new_turn[i] <- 1}
if(text$speaker[i] != "" & text$speaker[i] != speaker){speaker <- text$speaker[i]}
}
text$position_within_turn <- NA
position_within_turn <- 1
for(i in 1:nrow(text)){
if(text$new_turn[i] == 1){position_within_turn <- 0}
if(text$speaker[i] != ""){position_within_turn <- position_within_turn + 1}
text$position_within_turn[i] <- position_within_turn
}
text$position_within_turn[which(text$speaker == "")] <- 0
text$turn_length <- NA
top <- 0
for(i in nrow(text):1){
top <- max(top, text$position_within_turn[i])
text$turn_length[i] <- top
if(text$position_within_turn[i] == 1){top <- 0}
}
text$turn_length_string <- paste0("turn", text$turn_length)
text$turn_length_string[which(text$turn_length == 0)] <- ""
text$turn_length_string[which(text$turn_length == 5)] <- "turn5, turn5plus"
text$turn_length_string[which(text$turn_length >= 6)] <- "turn5plus"
text$turn_length_first_turn <- NA
text$turn_length_first_turn[which(text$new_turn == 1)] <- text$turn_length[which(text$new_turn == 1)]
text %>% select(line, speaker, sentence_coloured,
`Num Morphs`, `Num Words`,
num_clause, num_fin_clause, turn_length,
hascomment, hastag, tags_plus_content, tags,
mood, neg, verb_form,
NPexpansion, VCexpansion,
has_class,
clause2, clause3, clause4, clause4, multipleclauses,
passive, relativepronoun, rel_clause, modal, text_comments_extracted,
new_turn, position_within_turn, turn_length, turn_length_string, turn_length_first_turn,
features_coloured, speaker_no_html) -> text
tag_list <- paste0(text$tags, collapse = "")
tag_list <- unique(extract_tags_as_vector(tag_list))
for(i in 1:length(tag_list)){
text[[tag_list[i]]] <- str_count(text$sentence_coloured,
str_replace_all(str_replace_all(tag_list[i],"\\[", "\\\\["), "\\]", "\\\\]"))
}
return(text)
})
# table_lex (HDD etc) ----
table_lex <- reactive({
df <- table()
df %>% group_by(speaker, speaker_no_html) %>% filter(speaker != "") %>%
summarise(lex = paste(text_comments_extracted, collapse = " ")) -> df
df$hdd <- as.numeric(NA)
df$ttr <- as.numeric(NA)
for(i in 1:nrow(df)){
corpus <- koRpus::tokenize(df$lex[i], lang = "en", format = "obj")
hdd <- HDD(corpus)
ttr <- TTR(corpus)
df$types[i] <- length(types(corpus))
df$tokens[i] <- length(tokens(corpus))
df$hdd[i] <- as.numeric(koRpus::summary(hdd)[2])
df$ttr[i] <- as.numeric(koRpus::summary(ttr)[2])
}
return(df)
})
# df_childes_DIY ----
df_childes_DIY <- reactive({
if(input$collection3 == "PLEASE CHOOSE...")return(NULL)
if(input$collection3 == "KIDEVAL"){
kideval_corpora_id <- c(65, #Bates
60, #Bernstein
71, #Bliss
76, #Bloom70
41, #Bloom73
73, #Braunwald
36, #Brown
29, #Clark
39, #Demetras1
46, #Demetras2
50, #Feldman
43, #Gathercole
64, #Gleason
57, #Hall
31, #Higginson
48, #HSLLD
54, #MacWhinney
47, #McCune
30, #NewEngland
63, #Post
49, #Providence
55, #Sachs
61, #Snow
67, #Supes
66, #Tardif
62, #Valian
52, #VanHouten
32, #VanKleeck
56, #Warren
69) #Weist
# df <- get_speaker_statistics(role = "Target_Child")
df <- read.csv("speaker_statistics.csv")
df <- df[which(df$corpus_id %in% kideval_corpora_id), ]
}
if(input$collection3 != "KIDEVAL"){
# df <- get_speaker_statistics(collection = input$collection3, role = "Target_Child")
df <- read.csv("speaker_statistics.csv")
df %>% filter(collection_name == input$collection3) -> df
}
df <- as.data.frame(df)
df %>% filter(num_utterances >= input$num_utts3) -> df
df %>% arrange(target_child_age) %>% filter(is.na(target_child_age) == FALSE) -> df
age_range <- max(df$target_child_age) - min(df$target_child_age)
upper_age_bound <- min(df$target_child_age) + age_range*(input$trim_data3[2]/100)
lower_age_bound <- min(df$target_child_age) + age_range*(input$trim_data3[1]/100)
df %>%
filter(target_child_age >= lower_age_bound) %>%
filter(target_child_age <= upper_age_bound) ->
df
if(input$variable3 == "mlu_m"){
df$dv <- df$mlu_m
}
if(input$variable3 == "mlu_w"){
df$dv <- df$mlu_w
}