-
Notifications
You must be signed in to change notification settings - Fork 0
/
WADP.sh
executable file
·3192 lines (3176 loc) · 110 KB
/
WADP.sh
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
#!/bin/bash -
##############################################################################
# WADP.sh
copyright="(c) 2015-2024 Cardiff University; Licensed under the EUPL v. 1.2 or later"
####
version="1.1"
# DESCRRIPTION: processes word-association data
################# defining timeout variables
TMOUTSPLASH=3600 # timeout for splash screen of module selection
TMOUT1=10800 # timeout for categorisations in seconds
TMOUT2=60 # timeout for exit questions at the end of categoriser
TMOUT3=600 # timeout for return to main menu after categoriser
# TMOUT=20 # general timeout for read, not active
################# defining functions ###############################
#
#######################
# define help function
#######################
help ( ) {
echo "
DESCRRIPTION: $(basename $0) processes word-association data
SYNOPSIS: $(basename $0) [OPTIONS]
OPTIONS: -d run in debugging mode
-h display this help message
-V display version number
NOTE: all other functions are accessed interactively.
"
}
#######################
# define splash function
#######################
splash ( ) {
echo "WADP $copyright"
echo
echo
echo
echo
echo " WORD ASSOCIATION DATA PROCESSOR"
echo " version $version"
echo
echo
echo
echo
echo " Please choose a module and press ENTER:"
echo
echo " (c) categoriser"
echo " (r) reporter"
echo " (a) administrator"
echo " (x) exit"
echo
read -t $TMOUTSPLASH -p ' ' module < /dev/tty
# handle possible timeout
if [ $? == 0 ]; then
:
else
echo ""
echo "the session TIMED OUT, please start a new WADP session."
module='X'
fi
case $module in
C|c) echo "loading categoriser module ..."
run_categoriser
#read -t $TMOUTSPLASH -p 'Press ENTER to return to the main menu.' resp
;;
R|r) echo "loading reporter module ..."
run_reporter
read -t $TMOUTSPLASH -p 'Press ENTER to return to the main menu.' resp
;;
A|a) echo "loading administrator module ..."
run_administrator
read -t $TMOUTSPLASH -p 'Press ENTER to return to the main menu.' resp
;;
X|x) echo "This window can now be closed"; exit 0
;;
*) echo "$module is not a valid choice."
sleep 1
return
;;
esac
}
#######################
# define run_categoriser function
#######################
run_categoriser ( ) {
printf "\033c"
echo
echo
echo
echo
echo
echo " Drag the data to be categorised into this window and press ENTER."
echo
read -rp ' ' infile < /dev/tty
# get rid of any single quotation marks that might have attached
export infile="$(sed "s/'//g" <<<"$infile")"
# get WSL path if required
if [ "$WSL" ]; then
infile=$(wslpath -u "$infile")
if [ "$diagnostic" ]; then
echo "infile is $infile"
sleep 3
fi
fi
if [ -z "$infile" ]; then
echo "A data file to be categorised must be provided. Please drop the file into this window."
read -rp ' ' infile < /dev/tty
if [ -z "$infile" ]; then
echo "No data file provided. Exiting." >&2
return
fi
# get WSL path if required
if [ "$WSL" ]; then
infile=$(wslpath -u "$infile")
if [ "$diagnostic" ]; then
echo "infile is $infile"
sleep 3
fi
fi
fi
# change dir to that of the in-file
export working_dirname="$(dirname "$infile" | sed "s/'//g")"
cd "$working_dirname" 2>/dev/null || dirfail=true
if [ "$diagnostic" ]; then
echo "now in $(pwd). dirname is $working_dirname"
read -p 'press ENTER to continue ' xxx < /dev/tt
fi
printf "\033c"
echo
echo
echo
echo
echo
echo " If an existing database is to be used, drag it into"
echo " this window and press ENTER. Otherwise just press ENTER."
echo
read -rp ' ' database < /dev/tty
# get rid of single quotation marks
export database="$(sed "s/'//g" <<<"$database")"
# get WSL path if required
if [ "$WSL" ]; then
# only execute next command if db was provided
if [ -n "$database" ]; then
database=$(wslpath "$database")
fi
if [ "$diagnostic" ]; then
echo "database is $database"
sleep 3
fi
fi
printf "\033c"
# sort out potential WSL problems
if [ "$WSL" ]; then
# if it wasn't possible to cd earlier, warn if in -d mode
if [ "$dirfail" ]; then
if [ "$diagnostic" ]; then
echo "cd failed, still in $(pwd)"
read -p 'press ENTER to continue ' xxx < /dev/tt
fi
fi
# now run categoriser.sh for cygwin
cd "$working_dirname" || echo "ERROR: could not change dir to $working_dirname"
if [ "$database" ]; then
if [ "$diagnostic" ]; then
echo "categoriser.sh $ratID $database $infile"
read -p 'press ENTER to continue ' xxx < /dev/tt
fi
categoriser -a $ratID "$database" "$infile"
read -t $TMOUT3 -p 'Press ENTER to return to the main menu.' resp
# handle possible timeout
if [ $? == 0 ]; then
printf "\033c"
splash
else
echo "TIMEOUT"
echo "any unsaved categorisations were saved in the database (see above for the name of the database)"
exit 0
fi
else
if [ "$diagnostic" ]; then
echo "categoriser.sh -a $ratID $infile"
read -p 'press ENTER to continue ' xxx < /dev/tt
fi
categoriser -a $ratID "$infile"
read -t $TMOUT3 -p 'Press ENTER to return to the main menu.' resp
# handle possible timeout
if [ $? == 0 ]; then
printf "\033c"
splash
else
echo " TIMEOUT"
echo "any unsaved categorisations were saved in the database (see above for the name of the database)"
exit 0
fi
fi
else # if not running under WSL
if [ "$database" ]; then
categoriser -a $ratID "$database" "$infile"
read -t $TMOUT3 -p 'Press ENTER to return to the main menu.' resp
# handle possible timeout
if [ $? == 0 ]; then
printf "\033c"
splash
else
echo "TIMEOUT"
echo "any unsaved categorisations were saved in the database (see above for the name of the database)"
exit 0
fi
else
categoriser -a $ratID "$infile"
read -t $TMOUT3 -p 'Press ENTER to return to the main menu.' resp
# handle possible timeout
if [ $? == 0 ]; then
printf "\033c"
splash
else
echo "TIMEOUT"
echo "any unsaved categorisations were saved in the database (see above for the name of the database)"
exit 0
fi
fi
fi
}
#############################################################################
# define categoriser function
#############################################################################
categoriser ( ) (
####
# DESCRRIPTION: assigns categories to word-association data
################ the following section can be adjusted
# the key used for category assignments
key='
(A) Affix manipulation (e.g. irony -> ironic)
(CR) Cue-Response collocation (e.g. fence -> post)
(CRRC) Cue-Response & Response Cue collocation (e.g. rock -> hard)
(E) Erratic (e.g. wolf -> and)
(F) similar in Form only (e.g. fence -> hence)
(I) two-step association (e.g. weak -> Monday, via 'week')
(L) Lexical set (e.g. bean -> vegetable / pea)
(LCR) Lexical set & Cue-Response collocation (e.g. gold -> silver)
(LRC) Lexical set & Response-Cue collocation (e.g. cheese -> bread)
(OC) Other Conceptual (e.g. fence -> field)
(OCCR) Other Conceptual & Cue-Response colloc. (e.g. long -> corridor)
(OCRC) Other Conceptual & Response-Cue colloc. (e.g. attack -> knife)
(RC) Response-Cue collocation (e.g. fence -> electric)
(S) Synonym (e.g. delay -> impede)
(SCR) Synonym & Cue-Response collocation (e.g. torch -> light)
(SRC) Synonym & Response-Cue collocation (e.g. shove -> push)
(SS) Synonym in wider sense (not necessarily (e.g. joint -> unification)
same part of speech or number)
'
# list of allowed categories
allowed_categories='A,CR,CRRC,E,F,I,L,LCR,LRC,OC,OCCR,OCRC,RC,S,SCR,SRC,SS'
# database matching mode (write "TRUE" after the equal sign to turn one of them on)
approximate_match=
plural_match=
################# end of user-adjustable section
################# defining functions ###############################
# define csv_parser function
############################
csv_parser ( ) {
sed $extended -e 's/\|/PIPE/g' \
-e 's/,\"\"\"/,\" DOUBLEQUOTES /g' -e 's/\"\"\",/ DOUBLEQUOTES \",/g' -e 's/\"\"/ DOUBLEQUOTES /g' \
-e 's/(([^\",]+)|(\"[^\"]+\")|(\"\")|(\"[^\"]+\"\"[^"]+\"\"[^\"]+\")+)/\1\|/g' -e 's/\|$//g' \
-e 's/\|,/\|/g' -e 's/,,/\|\|/g' -e 's/\|,/\|\|/g' -e 's/^,/\|/g' -e 's/\"//g' -e 's/ / /g' "$1"
}
#######################
# define add_windows_returns function
#######################
add_windows_returns ( ) {
sed "s/$/$(printf '\r')/g" "$1"
}
#######################
# define remove_windows_returns function
#######################
remove_windows_returns ( ) {
sed "s/$(printf '\r')//g" "$1"
}
#######################
# define remove_boms_macOS function
#######################
remove_boms_macOS ( ) {
sed $'s/\xEF\xBB\xBF//' "$1"
}
#######################
# define help function
#######################
help ( ) {
echo "
DESCRRIPTION: $(basename $0) assigns categories to word-association data
SYNOPSIS: $(basename $0) [-r] [(DATABASE.dat|DATABASE.csv)] WA-FILE.csv
Items in square brackets are optional. Order is significant.
OPTIONS: -a run as auxiliary script to WADP
-r if used, this will result in rater IDs listed in output file
-V to display version, copyright and licensing information
"
}
#######################
# define add_to_name function
#######################
# this function checks if a file name (given as argument) exists and
# if so appends a number to the end so as to avoid overwriting existing
# files of the name as in the argument or any with the name of the argument
# plus an incremented count appended.
####
add_to_name ( ) {
count=
if [ "$(grep -E '.csv$' <<<"$1")" ]; then
if [ -e "$1" ]; then
add=-
count=1
new="$(sed 's/\.csv//' <<< "$1")"
while [ -e "$new$add$count.csv" ];do
(( count += 1 ))
done
else
count=
add=
fi
output_filename="$(sed 's/\.csv//' <<< "$1")$add$count.csv"
elif [ "$(grep -E '.dat$' <<< "$1")" ]; then
if [ -e "$1" ]; then
add=-
count=1
new="$(sed 's/\.dat//' <<< "$1")"
while [ -e "$new$add$count.dat" ];do
(( count += 1 ))
done
else
count=
add=
fi
output_filename="$(sed 's/\.dat//' <<< "$1")$add$count.dat"
elif [ "$(grep '.txt' <<< "$1")" ]; then
if [ -e "$1" ]; then
add=-
count=1
new="$(sed 's/\.txt//' <<< "$1")"
while [ -e "$new$add$count.txt" ];do
(( count += 1 ))
done
else
count=
add=
fi
output_filename="$(sed 's/\.txt//' <<< "$1")$add$count.txt"
else
if [ -e "$1" ]; then
add=-
count=1
while [ -e "$1"-$count ]
do
(( count += 1 ))
done
else
count=
add=
fi
output_filename=$(echo ""$1"$add$count")
fi
}
###################
# standard menu function (displays standard menu)
###################
standard_menu ( ) {
printf "\033c"
echo "Please categorise the following pair:"
echo " "
echo " $cue -> $(sed -e 's/_DOT_/\./g' -e 's=_SLASH_=/=g' -e "s/_APOSTROPHE_/\'/g" -e 's/_LBRACKET_/(/g' -e 's/_RBRACKET_/)/g' -e 's/_ASTERISK_/\*/g' -e 's/_PLUS_/\+/g' -e 's/DOUBLEQUOTES/\"/g' -e 's/_/ /g'<<< $response)"
echo " "
echo "type a choice and press ENTER:"
echo "$key"
echo " (X) exit (work will be saved)"
if [ -n "$previous_pair" ]; then
echo " (B) back to previous pair"
fi
echo " "
read -t $TMOUT1 -p '>>> ' category < /dev/tty
# timeout handling
if [ $? == 0 ]; then
category=$(tr '[[:lower:]]' '[[:upper:]]' <<< $category)
echo "You entered: $category"
sleep 0.4
else
echo "TIMEOUT"
category='X'
fi
}
###################
# back menu function (displays standard menu)
###################
back_menu ( ) {
printf "\033c"
echo "Previously you rated this pair as follows:"
echo " "
echo " $(echo "$previous_pair" | cut -d ':' -f 2 | sed $extended -e 's/\|/ -> /' -e 's/\|/ /' -e 's/_/ /g')"
echo " "
echo "type a fresh choice and press ENTER:"
echo "$key"
read -t $TMOUT1 -p '>>> ' old_category < /dev/tty
# timeout handling
if [ $? == 0 ]; then
old_category=$(tr '[[:lower:]]' '[[:upper:]]' <<< $old_category)
echo "You entered: $old_category"
sleep 0.4
else
echo "TIMEOUT"
category='' # no category assigned
fi
}
###################
# exit_routine function (saves output files)
###################
exit_routine ( ) {
echo "saving files ..."
# if there is a previous pair,
if [ "$previous_pair" ]; then
# write current_assignment to output list
echo "$previous_pair$rater_id" >> $SCRATCHDIR/$categorised_out
# update db with current_assignment's response and cat
if [ -z "$(grep -E '\|$' <<< $previous_pair)" ]; then
# if the category assigned is not empty
echo "$(cut -d '|' -f 2-3 <<< $previous_pair)$rater_id" >> $DBSCRATCHDIR/$previous_cue 2> /dev/null
fi
fi
### write db file (this is tab delimited with one cue per line)
for part in $(ls $DBSCRATCHDIR); do
echo "$part $(tr '\n' ' ' < $DBSCRATCHDIR/$part)" >> $SCRATCHDIR/finished_db.dat
done
# undo any Windows damage
#if [ "$WSL" ]; then
# conv -U "$SCRATCHDIR/finished_db.dat" 2>/dev/null
#fi
# check if db file is different from previous db file and if NOT, delete new db
if [ "$db_filename" ] && [ -z "$(diff -q $SCRATCHDIR/finished_db.dat "$db_filename")" ]; then
echo "Database remains unchanged."
db_nochange=true
else
# ask if previous db should be updated or not
if [ "$db_filename" ]; then
if [ "$db_is_dat" ]; then
db_filename_only="$(basename "$db_filename")"
read -t $TMOUT2 -p ' Update database (U) or create new database? (n) ' retain < /dev/tty
# handle possible timeout
if [ $? == 0 ]; then
:
else
echo "timeout"
fi
if [ "$retain" == "n" ] || [ "$retain" == "N" ]; then
if [ $DARWIN ]; then
add_to_name db-$(date "+%d-%m-%Y_%H.%M.%S").dat
else
add_to_name db-$(date "+%d-%m-%Y_%H:%M:%S").dat
fi
dboutfilename=$output_filename
echo " $db_filename_only left unchanged, new database named $dboutfilename."
else
# archive previous state of database in invisible directory
mkdir .previous_databases 2>/dev/null
add_to_name .previous_databases/$db_filename_only
old_db_name="$output_filename"
mv "$db_filename" "$old_db_name"
# prepare name for updated db
dboutfilename="$db_filename_only"
echo " Database file \"$dboutfilename\" updated."
fi
else
# if the input db was a csv file
if [ $DARWIN ]; then
add_to_name db-$(date "+%d-%m-%Y_%H.%M.%S").dat
else
add_to_name db-$(date "+%d-%m-%Y_%H:%M:%S").dat
fi
dboutfilename=$output_filename
echo " New database file saved as \"$dboutfilename\"."
fi
else
# if we have no input db
if [ $DARWIN ]; then
add_to_name db-$(date "+%d-%m-%Y_%H.%M.%S").dat
else
add_to_name db-$(date "+%d-%m-%Y_%H:%M:%S").dat
fi
dboutfilename=$output_filename
echo " New database file saved as \"$dboutfilename\"."
fi
# make doubly sure nothing is overwritten
add_to_name $dboutfilename
dboutfilename=$output_filename
cp $SCRATCHDIR/finished_db.dat $dboutfilename
echo $dboutfilename > $SCRATCHDIR/autosavedbname
fi
### check if out-file is required
if [ -z "$final_writeout" ]; then
echo "The rating is not complete, yet."
read -t $TMOUT2 -p 'Output list for the part that is complete? (Y/n)' req_out < /dev/tty
# handle possible timeout
if [ $? == 0 ]; then
:
else
echo "timeout"
fi
if [ "$req_out" == "n" ] || [ "$req_out" == "N" ]; then
if [ "$db_nochange" ]; then
:
else
echo "updated database file only..."
sleep 0.5
fi
else
write_inout
echo "Output written to \"$categorised_out\""
fi
else
write_inout
echo "Output written to \"$categorised_out\""
fi
### tidy up
rm db.dat.tmp 2> /dev/null &
rm -r $SCRATCHDIR $DBSCRATCHDIR 2> /dev/null &
### ask if dir should be opened
read -t $TMOUT2 -p 'Would you like to open the output directory? (Y/n)' a < /dev/tty
# handle possible timeout
if [ $? == 0 ]; then
:
else
echo "timeout"
fi
if [ "$a" == "y" ] || [ "$a" == "Y" ] || [ -z "$a" ]; then
if [ "$(grep 'Microsoft' <<< $platform)" ]; then
explorer.exe `wslpath -w "$PWD"`
elif [ "$(grep 'Darwin' <<< $platform)" ]; then
open .
else
xdg-open .
fi
fi
}
###################
# write inout function; writes the wa input file out with assigned categories
###################
write_inout ( ) {
# sort the in_cues
in_cues=$(tr '|' '\n' <<< $in_cues | sort | tr '\n' '|')
# write header
if [ "$in_with_ID" ]; then
# assemble in variable in_header_out
# first get whatever was used for the heading of respondent IDs
in_header_out="$(head -1 <<< "$in_respondentIDs")|"
# get cues in original order and insert 'category' after each
for cue in $(sed $extended 's/\|/ /g' <<< $in_cues); do
if [ "$output_raterIDs" ]; then
in_header_out+="$cue|category|rated by|"
else
in_header_out+="$cue|category|"
fi
done
# new write to outfile
sed $extended -e 's/^/\"/' -e 's/\|$/\"/g' -e 's/\|/\",\"/g' <<< "$in_header_out" > "$categorised_out"
# correct erroneous newline characters in Windows
if [ "$WSL" ]; then
#conv -U "$categorised_out" 2>/dev/null
tr '\n' '*' < "$categorised_out" | sed 's/*//g' > "$categorised_out."
echo "" >> "$categorised_out."
mv "$categorised_out." "$categorised_out"
#conv -U "$categorised_out" 2>/dev/null
fi
if [ "$diagnostic" ]; then
echo "header is: "$in_header_out""
fi
else
# assemble in variable in_header_out
# get cues in original order and insert 'category' after each
for cue in $(sed $extended 's/\|/ /g' <<< $in_cues); do
if [ "$output_raterIDs" ]; then
in_header_out+="$cue|category|rated by|"
else
in_header_out+="$cue|category|"
fi
done
# new write to outfile
sed $extended -e 's/^/\"/' -e 's/\|$/\"/g' -e 's/\|/\",\"/g' <<< "$in_header_out" > "$categorised_out"
if [ "$WSL" ]; then
#conv -U "$categorised_out" 2>/dev/null
tr '\n' '*' < "$categorised_out" | sed 's/*//g' > "$categorised_out."
echo "" >> "$categorised_out."
mv "$categorised_out." "$categorised_out"
#conv -U "$categorised_out" 2>/dev/null
fi
if [ "$diagnostic" ]; then
echo "header is: "$in_header_out""
fi
fi
# write rows
if [ -e $SCRATCHDIR/$categorised_out ]; then
if [ "$in_with_ID" ]; then
for ID in $(tail -n +2 <<< "$in_respondentIDs"); do
(( row += 1 ))
# assemble row in variable out_row
# first get ID
out_row="$ID|"
# now get pairs for that row (it needs to check if row is complete and only prints complete rows)
# This is because header and rows could possibly be out of alignment in the case of incomplete rows, though because
# headers and rows have responses in alphabetical order, and ratings are also gone through in alphabetical order
# by response, it seems to hold up. Nevertheless, for the moment, partial rows are not printed unless -d option active, see below
responses_in_this_row="$(grep -E "^$row:" $SCRATCHDIR/$categorised_out | sort | cut -d '|' -f 2-3)"
if [ "$output_raterIDs" ]; then
responses_in_this_row="$(sed 's/;/\",\"/' <<< "$responses_in_this_row")"
else
responses_in_this_row="$(cut -d ';' -f 1 <<< "$responses_in_this_row")"
fi
if [ "$(wc -l <<< "$responses_in_this_row")" -eq "$in_columns" ]; then
# if we have a complete row, write it out
out_row+="$(tr '\n' '|' <<< "$responses_in_this_row")"
write_neatly >> "$categorised_out"
else
if [ $diagnostic ]; then
# this is for outputting partial rows of categorised responses. This feature is experimental
if [ "$(wc -l <<< "$responses_in_this_row")" -gt 1 ]; then
echo "incomplete row added to output"
out_row+="$(tr '\n' '|' <<< "$responses_in_this_row")"
write_neatly | sed 's/$/,NB: this row is not yet complete; the remainder will be output once categorisation for this row is complete. Output of incomplete rows is an experimental feature. Only the output of completed rows has been fully tested./' >> "$categorised_out"
else
echo "\"$(sed 's/_/ /g' <<< $ID)\",\"NB: this row is not yet complete; once the categories for the complete row have been assigned, they will be output. The categorisations were saved in the database.\"" >> "$categorised_out"
fi
else
echo "\"$(sed 's/_/ /g' <<< $ID)\",\"NB: this row is not yet complete; once the categories for the complete row have been assigned, they will be output. The categorisations were saved in the database.\"" >> "$categorised_out"
fi
fi
# clear variable for next row
out_row=
# undo any damage by cygwin
#if [ "$WSL" ]; then
# conv -U "$categorised_out" 2>/dev/null
#fi
done
else
for n in $(eval echo {1..$(( $in_rows - 1 ))}); do
(( row += 1 ))
# assemble row in variable out_row (it needs to check if row is complete and only prints complete rows)
# This is because header and rows could possibly be out of alignment in the case of incomplete rows, though because
# headers and rows have responses in alphabetical order, and ratings are also gone through in alphabetical order
# by response, it seems to hold up. Nevertheless, for the moment, partial rows are not printed unless -d option active, see below
responses_in_this_row="$(grep -E "^$row:" $SCRATCHDIR/$categorised_out | sort | cut -d '|' -f 2-3)"
if [ "$output_raterIDs" ]; then
responses_in_this_row="$(sed 's/;/\",\"/' <<< "$responses_in_this_row")"
else
responses_in_this_row="$(cut -d ';' -f 1 <<< "$responses_in_this_row")"
fi
#echo "responses_in_this_row: $(wc -l <<< "$responses_in_this_row") of $in_columns"
if [ "$(wc -l <<< "$responses_in_this_row")" -eq "$in_columns" ]; then
# if we have a complete row, write it out
out_row+="$(tr '\n' '|' <<< "$responses_in_this_row")"
write_neatly >> "$categorised_out"
else
if [ $diagnostic ]; then
# this is for outputting partial rows of categorised responses. This feature is experimental
if [ "$(wc -l <<< "$responses_in_this_row")" -gt 1 ]; then
echo "incomplete row added to output"
out_row+="$(tr '\n' '|' <<< "$responses_in_this_row")"
write_neatly | sed 's/$/,NB: this row is not yet complete; the remainder will be output once categorisation for this row is complete. Output of incomplete rows is an experimental feature. Only the output of completed rows has been fully tested./' >> "$categorised_out"
else
echo "\"NB: this row is not yet complete; once the categories for the complete row have been assigned, they will be output. The categorisations were saved in the database.\"" >> "$categorised_out"
fi
else
echo "\"NB: this row is not yet complete; once the categories for the complete row have been assigned, they will be output. The categorisations were saved in the database.\"" >> "$categorised_out"
fi
fi
# clear variable for next row
out_row=
done
fi
else # this is the else for checking if SCRATCHDIR/categorised_out exists
echo "$categorised_out will only contain a header because of the small number of ratings performed."
echo "This output file only contains a header because no categorisations have been recorded. Once categorisations have been recorded, they will be printed in future output files." >> "$categorised_out"
sleep 1
if [ $diagostic ]; then
echo "looking at $row in $SCRATCHDIR/$categorised_out..."
grep -E "^$row:" $SCRATCHDIR/$categorised_out
read -p ' press ENTER to continue ' a < /dev/tty
fi
fi
}
###################
# write neatly function (write categorised_out file; func used by write_inout)
###################
write_neatly ( ) {
sed $extended -e 's/^/\"/' -e 's/\|$/\"/g' -e 's/\|/\",\"/g' -e 's/\–/-/g' -e 's/_DOT_/\./g' -e 's=_SLASH_=/=g' -e "s/_APOSTROPHE_/\'/g" -e 's/_LBRACKET_/(/g' -e 's/_RBRACKET_/)/g' -e 's/_ASTERISK_/\*/g' -e 's/_PLUS_/\+/g' -e 's/_/ /g' -e 's/DOUBLEQUOTES/\"\"/g' <<< $out_row
}
################## end defining functions ########################
# initialise some variables
extended="-r"
# check what platform we're under
platform=$(uname -v)
# and make adjustments accordingly
if [ "$(grep 'Microsoft' <<< $platform)" ]; then
WSL=true
elif [ "$(grep 'Darwin' <<< $platform)" ]; then
extended="-E"
DARWIN=true
else
LINUX=true
fi
# analyse options
local OPTIND ahrvV opt # making sure the options are only for this script, leaving out -d as that can be global
while getopts adhrvV opt
do
case $opt in
a) auxiliary=true
;;
d) diagnostic=true
;;
h) help
exit 0
;;
r) output_raterIDs=true
;;
v) verbose=true
;;
V) echo "$(basename $0) - version $version"
echo "$copyright"
exit 0
;;
esac
done
shift $((OPTIND -1))
# set terminal window
printf '\e[8;33;83t'
if [ "$auxiliary" ]; then
printf "\033c"
echo " CATEGORISER MODULE"
echo
echo
echo
echo
else
# splash screen
printf "\033c"
echo "Word Association Data Processor - $copyright"
echo
echo
echo
echo
echo
echo " WORD ASSOCIATION DATA CATEGORISER"
echo " version $version"
echo
echo
echo
echo
echo
echo
echo
fi
################ checks on input files
# initialise some variables
grand_db=
db_filename=
wa_in_filename=
db_is_dat=
# check what sorts of input files we have got and check if they exist
case $# in
0) echo "ERROR: no input files provided. Minimally, one input file needs to be provided for rating. See $(basename "$0") -h or the manual for details." >&2
exit 1
;;
1) if [ -s "$1" ]; then
# remove any Windows returns
remove_windows_returns "$1" > "$1.corr"
remove_boms_macOS "$1.corr" > "$1"
rm "$1.corr"
else
echo "ERROR: could not open $1" >&2
exit 1
fi
if [ "$(grep -E '\.csv$' <<<"$1" 2>/dev/null)" ]; then
# testing if $1 is a db file by looking for fields with first,
# second, tenth and fourteenth category of the variable allowed_categories
if [ "$(grep -E "$(cut -d ',' -f 1,2,10,14 <<< $allowed_categories| sed $extended -e 's/,/,|,/g' -e 's/^/,/' -e 's/$/,/g' )" "$1")" ]; then
echo "ERROR: \"$1\" appears to be a database file. A .csv file for rating also needs to be provided."
exit 1
fi
read -p ' No database for category lookup was provided. Continue? (Y/n)' d \
< /dev/tty
if [ "$(grep -E 'N|n' <<< $d)" ]; then
echo "exiting"
exit 0
fi
echo; echo
wa_in_filename="$1"
no_db=db
else
echo "ERROR: minimally, one input .csv file needs to be provided for rating. See the manual for details." >&2
exit 1
fi
;;
2) if [ -s "$1" ] && [ -s "$2" ]; then
# remove any Windows returns
remove_windows_returns "$1" > "$1.corr"
remove_boms_macOS "$1.corr" > "$1"
rm "$1.corr"
remove_windows_returns "$2" > "$2.corr"
remove_boms_macOS "$2.corr" > "$2"
rm "$2.corr"
else
echo "ERROR: could not access file(s) $1 and/or $2" >&2
exit 1
fi
# if a .dat and a .csv file are provided
if [ "$(grep -E '\.csv' <<<"$2")" ] && [ "$(grep -E '\.dat' <<<"$1")" ]; then
# test if $2 is NOT a db file
if [ -z "$(grep -E "$(cut -d ',' -f 1,2,10,14 <<< $allowed_categories| sed $extended -e 's/,/,|,/g' -e 's/^/,/' -e 's/$/,/g' )" "$2")" ]; then
wa_in_filename="$2"
else
echo "ERROR: \"$2\" appears to be a database file." >&2
exit 1
fi
# test if $1 is a db file
if [ "$(grep -E "$(cut -d ',' -f 1,2,10,14 <<< $allowed_categories | sed $extended -e 's/,/ .+|/g' -e 's/^/\.+|/' -e 's/$/ /g')" "$1")" ]; then
db_filename="$1"
db_is_dat=true
else
echo "ERROR: \"$1\" does not appear to be a properly formatted .dat file" >&2
exit 1
fi
# if two .csv files are provided
elif [ "$(grep -E '\.csv'<<<"$1")" ] && [ "$(grep -E '\.csv'<<<"$2")" ]; then
# test if $1 is db file
if [ "$(grep -E "$(cut -d ',' -f 1,2,10,14 <<< $allowed_categories| sed $extended -e 's/,/,|,/g' -e 's/^/,/' -e 's/$/,/g' )" "$1")" ]; then
grand_db="$1"
db_filename="$1"
else
echo "ERROR: \"$1\" does not appear to be a database file." >&2
exit 1
fi
# test if $2 is not a db file
if [ -z "$(grep -E "$(cut -d ',' -f 1,2,10,14 <<< $allowed_categories| sed $extended -e 's/,/,|,/g' -e 's/^/,/' -e 's/$/,/g' )" "$2")" ]; then
wa_in_filename="$2"
else
echo "ERROR: \"$2\" appears to be a database file, but should be a file with data to be rated." >&2
exit 1
fi
else
echo "ERROR: $1 and $2 do not appear to be of the correct format combination or order." >&2
echo "They should either be two .csv files or a .dat file and a .csv file." >&2
exit 1
fi
;;
*) echo "ERROR: more than three input files provided: $@" >&2
exit 1
;;
esac
# check if infile might already be categorised
if [ $(head -1 "$wa_in_filename" | grep -o 'category' | wc -l) -gt 1 ]; then
echo "$wa_in_filename appears to be rated already. Please choose a data file which has not yet been rated." >&2
exit 1
elif [ "$(grep 'categorised_' <<<"$wa_in_filename")" ]; then
read -p "$wa_in_filename looks as though it has already been rated. Are you sure you wish to continue? (y/N)" sure < /dev/tt
if [ "$sure" == "y" ] || [ "$sure" == "Y" ]; then
:
else
exit 1
fi
fi
################ create two scratch directories
# first one to keep db sections in
DBSCRATCHDIR=$(mktemp -dt categoriserXXX)
# if mktemp fails, use a different method to create the SCRATCHDIR
if [ "$DBSCRATCHDIR" == "" ] ; then
mkdir ${TMPDIR-/tmp/}categoriserXXX.1$$
DBSCRATCHDIR=${TMPDIR-/tmp/}categoriserXXX.1$$
fi
if [ "$diagnostic" == true ]; then
open $DBSCRATCHDIR 2> /dev/null || xdg-open $DBSCRATCHDIR 2> /dev/null || explorer.exe `wslpath -w "$DBSCRATCHDIR"`
fi
# second one to keep other auxiliary and temporary files in
SCRATCHDIR=$(mktemp -dt categoriserXXX)
# if mktemp fails, use a different method to create the SCRATCHDIR
if [ "$SCRATCHDIR" == "" ] ; then
mkdir ${TMPDIR-/tmp/}categoriserXXX.1$$
SCRATCHDIR=${TMPDIR-/tmp/}categoriserXXX.1$$
fi
if [ "$diagnostic" == true ]; then
open $SCRATCHDIR 2> /dev/null || xdg-open $SCRATCHDIR 2> /dev/null || explorer.exe `wslpath -w "$SCRATCHDIR"`
fi
# create name of rated WA output file
wa_in_filename_only="$(basename "$wa_in_filename")"
add_to_name "categorised_$wa_in_filename_only"
categorised_out="$output_filename"
################ collecting analyst's ID in the background
(read -p 'Please type your rater ID (or leave blank) and press ENTER: ' analyst_id < /dev/tty
if [ "$analyst_id" ]; then
echo $analyst_id > $SCRATCHDIR/analyst_id
else
echo "anonymous" > $SCRATCHDIR/analyst_id
fi
# inform user
echo ""
echo ""
echo ""
echo ""
if [ -z "$no_db" ]; then echo "Loading database $db_filename ..."; fi
sleep 1
if [ "$db_is_dat" ]; then
echo "Detected $(cat $SCRATCHDIR/db_total_cues) cue words ..."
elif [ -z "$no_db" ]; then
echo "Detected $(cat $SCRATCHDIR/db_total_cues) cue words and $(cat $SCRATCHDIR/db_rows) rows of responses ..."
fi
) &
################# processing database file ###################
# initialise some variables
db_with_ID=
db_with_rater_ID=
db=
respondentIDs=
db_raterIDs=
db_columns=
db_total_cues=
db_rows=
db=
if [ "$db_is_dat" ]; then
##### if db is a .dat file #####
# count cues in db
db_total_cues=$(cat "$db_filename" | wc -l)
# db_columns are variable for .dat files, so this is not meaningful
# db_rows is same as db_total_cues
# write info to file to be retrieved by other processes
echo $db_total_cues > $SCRATCHDIR/db_total_cues
# split db into response - category pairs per line in files named after cues
while read line; do
file=$(cut -f 1 <<< "$line")
echo "$(cut -f 2- <<< "$line" | tr ' ' '\n')" \
> $DBSCRATCHDIR/$file
done < "$db_filename"
elif [ -z "$no_db" ]; then
##### if db is a .csv file #####
# parse csv db file and
# - eliminate potential trouble characters
# - replace spaces w/ underscore and ^M with \n and ',' with '|'
# - copy file to SCRATCHDIR
csv_parser "$db_filename" |\
sed $extended -e 's/ /_/g' -e 's/\-/–/g' -e 's/\./_DOT_/g' -e 's=/=_SLASH_=g' -e "s/'/_APOSTROPHE_/g" -e 's/\`//g' -e 's/\[/_LBRACKET_/g' -e 's/(/_LBRACKET_/g' -e 's/)/_RBRACKET_/g' -e 's/\]/_RBRACKET_/g' -e 's/\*/_ASTERISK_/g' -e 's/+/_PLUS_/g' | tr '\r' '\n' > $SCRATCHDIR/db.csv
# checking if respondent ID is included
if [ -n "$(head -1 $SCRATCHDIR/db.csv | cut -d '|' -f 1 | grep 'ID')" ]; then
db_with_ID=true
fi
# count db_columns
db_columns=$(( 1 + $(head -1 $SCRATCHDIR/db.csv | tr -dc '|' | wc -c) ))
# put database in memory and detect and isolate any respondent IDs
if [ "$db_with_ID" ]; then
# put IDs in variable
respondentIDs=$(cut -d '|' -f 1 $SCRATCHDIR/db.csv)
db="$(cut -d '|' -f 2-$db_columns $SCRATCHDIR/db.csv)"
# count db_rows
db_rows=$(( $(wc -l <<< "$db") - 1 ))
# count cues in db
db_total_cues=$(( ($db_columns / 2) - 1 ))
# inform user
echo "respondent IDs detected ..."
else
db="$(cat $SCRATCHDIR/db.csv)"
# count db_rows
db_rows=$(( $(wc -l <<< "$db") - 1 ))
# count cues in db
db_total_cues=$(( $db_columns / 2 ))
fi
# write info to files to be retrieved by other processes
echo $db_total_cues > $SCRATCHDIR/db_total_cues
echo $db_rows > $SCRATCHDIR/db_rows
# check if format is consistent
if [ "$(head -1 <<< "$db" | grep -E -o "WA[[:digit:]]+\|[[:upper:]]*[[:lower:]]+\|*" | wc -l)" -ne "$db_total_cues" ]; then
# let ID entering process finish first
wait
echo "WARNING: there appears to be an inconsistency in the first row of \"$db_filename\". It should contain sequences of \"WA000\" (where 000 is any number of digits), followed, in the next field, by a single word indicating the cue (either all lower case or mixed case). Optionally the first field of the first row can contain text that includes the words \"ID\", and the last field of the first row can contain text that includes either the words \"rater ID\". Please check \"$db_filename\" to make certain that it conforms to these specifications and then retry."
rm -r $SCRATCHDIR $DBSCRATCHDIR &
exit 1
fi
# check fields were properly separated: each line should contain the same number of field separators. If that is not the case, throw error and exit
while read line; do
if [ "$(tr -dc '|' <<< $line | wc -c)" -eq "$(( $db_columns - 1 ))" ]; then
:
else
echo "ERROR: field separation inconsistency. There should be exactly $db_columns fields per line. $(tr -dc '|' <<< $line | wc -c) were detected here:
$line" >&2
rm -r $SCRATCHDIR $DBSCRATCHDIR &
exit 1
fi
done <<< "$db"
# make sure the analyst ID has been obtained before proceeding
# split db into response - category pairs
i=1
ii=2
while [ $ii -le $db_columns ]; do
for line in $(cut -d '|' -f $i-$ii <<< "$db"); do
tmplist="$tmplist
$line"