/
sed_sentence_chunker.sh
1373 lines (1039 loc) · 52.6 KB
/
sed_sentence_chunker.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
export LANG=C.UTF-8
# sed_sentence_chunker.sh
# Created: 2017-Jul-20 | Victoria Stuart | "mail"..@t.."VictoriasJourney.com"
# Last updated: 2017-Dec-30
# ----------------------------------------------------------------------------
# local: /mnt/Vancouver/Programming/scripts/sed_sentence_chunker/sed_sentence_chunker.sh
# GitHub: https://github.com/victoriastuart/biomedical-sentence-splitter
# ============================================================================
# USAGE:
# ======
# ./sed_sentence_chunker.sh
# bash sed_sentence_chunker.sh
# This script processes text files in the "input/" directory, and outputs to the
# "output/" directory.
# ============================================================================
# PYTHON SCRIPT USAGE:
# ====================
# To use this "sed_sentence_chunker.sh" bash script in a Python script; run
# this script in a directory that contains your text/input files in an "input/"
# directory. Note that you must also (manually) create an "output/" directory.
# ============================================================================
# APPROACH:
# =========
# 1. Preprocessing
# 2. Split sentences
# 3. Postprocessing
# ============================================================================
# TWO VARIATIONS OF THIS SCRIPT:
# ==============================
# If desired you can edit this script for alternative runtime options, as
# summarized here.
# ----------------------------------------------------------------------------
# SCRIPT VARIANT 1: specify input, output files on the command line.
# ------------------------------------------------------------------
# Usage:
# ./sed_sentence_chunker.sh <input_file> <output_file>
# bash sed_sentence_chunker.sh <input_file> <output_file>
# Example:
# ./sed_sentence_chunker.sh chunk_test_input.txt chunk_test_output.txt
# 1. Add these at/near top of script (note: cannot have spaces around " = " sign):
# input=$1
# output=$2
# 2. Comment out or delete this code section (after the Technical Notes, below):
# FILES=$(find input -type f -iname "*")
#
# for f in $FILES
# do
# sed -i -e 's/ffi/ffi/g
# s/fi/fi/g
# ... snip ...
# s/x/x/g' $f
# 3. Change "$f" in this line to "$input":
# sed 's/pp\.\s/Cho4Ph/g' $f > tmp_file
# 4. Near the bottom of the script, add these,
# sed 's/Dr,/Dr./g' tmp_file > $output
# rm tmp_file
# and delete these:
# sed -i 's/Dr,/Dr./g' tmp_file
# mv tmp_file output/$outname
# ----------------------------------------------------------------------------
# SCRIPT VARIANT 2: directly pass input text on the command line.
# ------------------------------------------------------------------
# Usage:
# . sed_sentence_chunker.sh <<< "quoted input text / sentences" ## << note: dot space command
# source sed_sentence_chunker.sh <<< "quoted input text / sentences" ## alternative (script sourcing)
# ----------------------------------------
# Examples:
# . sed_sentence_chunker.sh <<< "This is sentence 1. This is sentence 1."
# or:
# S="This is sentence 3. This is sentence 4."
# . sed_sentence_chunker.sh <<< $S
# 1. Add these at/near top of script (note: cannot have spaces around " = " sign):
# input=$1
# outfile="" ## output file
# OUTPUT="" ## output variable
# 2. Comment out or delete this code section (after the Technical Notes, below):
# FILES=$(find input -type f -iname "*")
#
# for f in $FILES
# do
# sed -i -e 's/ffi/ffi/g
# s/fi/fi/g
# ... snip ...
# s/x/x/g' $f
# 3. Change "$f" in this line to "$input":
# sed 's/pp\.\s/Cho4Ph/g' $f > tmp_file
# 4. Near the bottom of the script, add these,
# sed 's/Dr,/Dr./g' tmp_file > out_file
# OUTPUT=$(printf out_file)
# export $OUTPUT
# rm -f tmp*
# and delete these:
# sed -i 's/Dr,/Dr./g' tmp_file
# mv tmp_file output/$outname
# ============================================================================
# TECHNICAL NOTES:
# ================
# ----------------------------------------------------------------------------
# SCRIPT NAME ...:
# ----------------
# If the script name is too long for convenient use, just rename it; e.g.: ssc
# Run this script on my "chunk_test_input.txt" file to get an idea of it's
# capability (or to run your own unit tests).
# If needed you can use the Linux "pwgen" command to generate alphanumeric
# UID: "pwgen 8 2" will generate two (unique) 8-character alphanumeric strings.
# Example: $ pwgen 8 2 >> eej8Ae2p | air4Coo2
# ----------------------------------------------------------------------------
# FIRST SED COMMAND IN THIS SCRIPT:
# ---------------------------------
# After much (!) experimentation, it appears that the first sed command (below),
# outputting to the "tmp_file", MUST involve an "-r" argument (that in turn
# expects a regex expression). To achieve this, it is best to use the first
# command, as shown below. [Otherwise, you end up with blank output.]
# ----------------------------------------------------------------------------
# [a-zA-Z] vs. [A-Za-z] :
# -----------------------
# [a-zA-Z] **also** matches the ASCII characters between z and A: [ \ ] ^ _ `
# [A-Za-z] will only match the alphabet
# https://stackoverflow.com/questions/4923380/difference-between-regex-a-z-and-a-za-z
# http://www.asciitable.com/
# https://en.wikipedia.org/wiki/ASCII#/media/File:USASCII_code_chart.png
# ----------------------------------------------------------------------------
# REGEX EXPRESSIONS:
# ------------------
# I predominantly use two sed expressions -- the second, here, involving regex:
# sed -i 's/foo/bar/g'
# sed -i -r 's/foo\s?/bar/g' ## \s? : 0 or 1 (?) spaces (\s)
# . : any char, including newline (\n)_
# \. : period (literal period)
# -i : --in-place
# Regex "special" characters,
# [\^$.|?*+()
# have special meaning / function, and will thus need to be \-escaped.
# { and } are literal characters, unless they're part of a valid regular
# expression token such as a quantifier, e.g.: {3}.
# https://www.regular-expressions.info/refcharacters.html
# ----------------------------------------
# HERE IS MY (WORKING) EXPERIENCE RE: SED AND REGEX:
# In non-regex sed expressions, those special characters will need to be \-escaped
# to indicate that they are a regex special (not a literal) character.
# in regex (-r) sed expressions, they will be recognized as regex special
# characters, and will not have to be \-escaped.
# Exception: as noted, [ is a special character in regex -- denoting (e.g.) the
# start of a character class / set (https://www.regular-expressions.info/charclass.html).
# HOWEVER, unlike ?{}*^$ etc., in non-regex sed expressions, we need to escape
# it, \[ if we want to match a literal "[" in our expressions. [That applies,
# also, to regex (-r) sed expressions!].
# To match the start (^) or end ($) of a line. don't ever \-escape the ^ or $.
# To match the end of a line (EOL) ending (e.g.) with: ... the end.
#
# sed 's/the end\.$/the end.\n\Period./g' ## \. : literal period; $ EOL
# sed -r 's/the end\.$/the end.\n\Period./g' ## \. : literal period; $ EOL
# sed 's/the end.$/the end.\n\Period./g' ## . : any single characer; $ EOL
# sed -r 's/the end.$/the end.\n\Period./g' ## . : any single characer; $ EOL
# To match a literal $, anywhere in a line / sentence, \-escape the $ ( \$ ):
#
# sed 's/\$/\n/g'
# sed -r 's/\$/\n/g'
# Likewise (viz-a-viz: ^ $), there is no need to ever escape * if you intend it
# to match 0 or more of the preceding expression:
#
# sed 's/foo\s*bar/Foo.\n(Bar!)/g' ## matches 0 or more spaces between foo and bar
# sed -r 's/foo\s*bar/Foo.\n(Bar!)/g' ## ditto
#
# sed -r 's/foo\*bar/Foo.\n(Bar!)/g' ## matches foo*bar
# sed 's/foo*bar/Foo.\n(Bar!)/g' ## matches foobar (0 or more o)
# sed -r 's/foo*bar/Foo.\n(Bar!)/g' ## matches foobar (0 or more o)
# sed 's/foob*ar/Foo.\n(Bar!)/g' ## matches foobar (0 or more b)
# sed -r 's/foob*ar/Foo.\n(Bar!)/g' ## matches foobar (0 or more b)
# sed 's/fooz*bar/Foo.\n(Bar!)/g' ## matches foobar (0 or more z)
# sed -r 's/fooz*bar/Foo.\n(Bar!)/g' ## matches foobar (0 or more z)
#
# compare to:
#
# sed 's/foo?bar/Foo.\n(Bar!)/g' ## does NOT match foobar; MATCHES foo?bar (literal ?)
# sed 's/foo\?bar/Foo.\n(Bar!)/g' ## matches foobar (0 or 1 o); does not match foo?bar
# sed -r 's/foo?bar/Foo.\n(Bar!)/g' ## matches foobar (0 or 1 o); does not match foo?bar
# ----------------------------------------
# MORE EXAMPLES:
# model: sed 's/foo/bar/g'
# sed 's/foo\s\?bar/Foo.\nBar!/g'
# sed -r 's/foo\s?bar/Foo.\nBar!/g'
#
## 0 or 1 (?) spaces (\s)
## matches: foobar | foo bar
## does not match: foo bar | foo bar | ...
# sed 's/foo\s\{0,3\}bar/Foo.\nBar!/g'
# sed -r 's/foo\s{0,3}bar/Foo.\nBar!/g'
#
## {0,3} : 0, 1, 2 or 3 of preceding sequence (here: space, \s)
## matches: foobar | foo bar | foo bar | foo bar
## does not match: foo bar | foo bar | ...
# Regarding [ :
# sed 's/foo\s\?\[bar]/Foo.\n(Bar!)/g'
# sed -r 's/foo\s?\[bar]/Foo.\n(Bar!)/g'
#
## \[: match literal [
## matches: foo[bar] | foo [bar]
## does not match: foo [bar] | foo [bar] | foo [bar] | foo [bar] | ...
## does not match: foobar | foo bar | foo bar | ...
# sed 's/foo\s\?[bar]/Foo.\n(Bar!)/g'
# sed -r 's/foo\s?[bar]/Foo.\n(Bar!)/g'
#
## matches: foobar | foo bar
## replacing foo with Foo. and [bar] with (Bar!)ar
## (with a line break, \n, between them)!
## does not match: foo[bar] | foo [bar | ...
#
## Here, even in a non-regex sed expression, [bar] is being processed as a
## character class (like [A-Za-z0-9]), and so will match the b in foobar, but
## not the b in foo[bar]. To match the literal [ in that non-regex sed expression,
## \-escape the [, \[ , as shown further above / here:
# sed 's/foo\s\{0,3\}\[bar]/Foo.\n(Bar!)/g'
# sed -r 's/foo\s{0,3}\[bar]/Foo.\n(Bar!)/g'
#
## matches: foo[bar] | foo [bar] | foo [bar] | foo [bar]
## does not match: foo [bar] | foo [bar] | ...
## does not match: foobar | foo bar | foo bar | ...
# ----------------------------------------
# sed -r 's/\.([A-Z])\.$/.\1Shah7a/g'
#
## \. : literal period; ([A-Z]) : ASCII capitals in character class ();
## $ : end of line, non-escaped; . : period (do not need to escape in
## replace portion of the sed expression; \1 : replace with captured
## characters (class); Shah7a : an alphanumeric "tag" / substitution /
## UID (that I will replace later with the text it represents: .)
# sed -r 's/([[({\s])pp\.\s?([ivx0-9])/\1Cho4Ph\2/g' $f > tmp_file
#
## NOTE: that "[" MUST appear FIRST in the "[...]" character expression);
## i.e., [[...]. Also, if used, escape ] (i.e., \]). Lastly, as this is
## a -r regex expression, the ? is not \?-escaped; ...
# ----------------------------------------
# SED REGEX SUMMARY:
# ==================
# 1. No need to \-escape: ^ (start of line)
# $ (EOL)
# [] (character class / set) ## sed 's/foo[b]ar/foo\nbar/g'
# * (0 or more instances of matches for preceding expression)
#
# in: sed 's///g'
# or: sed -r 's///g'
# 2. \-escape: ? (0 or 1 of preceding expression) ## \?
# * (0 or more of preceding expression) ## \*
# { and } in {i,j} expressions ## \{0,3\}
#
# in: sed 's///g'
# not in: sed -r 's///g'
# ----------------------------------------
# In the script below, I tried to minimize the use of "lookaheads" () in
# my sed ( -r ) expressions, as I found these to increase the runtime.
# That is, where possible / practical, I tended to prefer the simpler
# sed -i 's///g' expressions.
# Expressions of the sort .{1,15}\.s\s* look complicated, but they are pretty
# simple! Basically it says: match any character ( . ), appearing 1-15
# times ( {1,15}, that is followed by a period ( \.) and any space ( \s\s* ) ...
# Likewise: ^[A-Z].{1,5}\. says match any 1..5 preceding characters that are
# not capitals, followed by a period ...
#
# sed -i -r "s/[.](.[^0-9]{1,15})[.]/Shah7a\1./g" tmp_file
#
# likewise translates to: match, in place, a period [.] that is followed by
# any span of 1-15 characters {1,15}, that are not 0 through 9 [^0-9],
# followed by another period [.]. All of that is this bit: .[^0-9]{1,15})[.]
#
# The second ("replace")_half of that regex expression states: replace replace
# THOSE periods (matched as described) with the unique alphanumeric string,
# Shah7a, followed by a period.
#
# sed -i -r "s/.[^.]\{1,15\}.\s\s*/\n\n/g" tmp_file
#
# Match any character ( . ), appearing 1-15 times ( {1,15} that is NOT a
# period ( !. ), but is followed by a period ( \.) and any space ( \s\s* ),
# and split ( \n\n ) at that position.
# https://www.gnu.org/software/sed/manual/html_node/Regular-Expressions.html
# http://www.rexegg.com/regex-quickstart.html
# ----------------------------------------------------------------------------
# ABBREVIATIONS -- JOURNAL TITLES; AUTHORS ...
# --------------------------------------------
# Journal author name initials and journal title abbreviations are a huge
# programmatic, i.e. technical difficulty. While my approach, below, minimizes
# the disruptions of those viz-a-viz bone fide sentence chunking, some issues
# will inevitably remain. E.g., some very short sentences may not get split
# from the others. C'est la vie!
# ----------------------------------------------------------------------------
# THESE COMMENTS:
# ---------------
# I deleted all of these comments from this script, leaving only the commands.
# The runtimes (time ./sed_sentence_chunker.sh) were essentially identical.
# ----------------------------------------------------------------------------
# OLDER NOTES / REFERENCE ...
# ---------------------------
# These notes are no longer relevant viz-a-viz this script, but are useful
# re: my earlier versions -- and general knowledge (preserved here!).
# ----------------------------------------
# ESCAPING SINGLE QUOTES WITHIN SINGLE-QUOTED EXPRESSIONS:
# --------------------------------------------------------
# To escape a single quote within a single-quoted sed expression, you need to
# terminate / chain the single quotes. E.g., to escape an internal ', terminate
# the sed single-quoted expression with another (internal) ', then escape the
# internal single quote inside the sed expression: "'", then add back (chain)
# another single quote ' to "continue / chain" the sed expression. Similarly,
# to escape (e.g.) a bracket [ ] inside the optional match [] pattern within a
# sed expression, chain the sed command, quoting the bracket term: ['"]"'] ...
# https://stackoverflow.com/questions/18370536/sed-or-operator-in-set-of-regex
# https://stackoverflow.com/questions/14813145/boolean-or-in-sed-regex
# https://serverfault.com/questions/466118/using-sed-to-remove-both-an-opening-and-closing-square-bracket-around-a-string
# ... all members of a character class lose special meaning (with a few
# exceptions). And ] loses its meaning if it is placed first.
# That observation is important re: the "([])}])" pattern below (that searches
# for characters ")", "}" and ")"). You MUST list the "]" closing bracket
# (within the "([ ])" character class), with the "]" square bracket listed FIRST:
# "([])}])".
# The following should capture all permutations of two contiguous sentences,
# where the inter-sentence boundary may contain any permutation of terminal
# punctuation (".", "!", "?"), parentheses and brackets ("(", "{, "[", ")", "}",
# "]", and any combination of quotation marks -- and split those sentences!
# sed -i -r 's/([A-Z]\.)\s\s*([A-Z])/\1\n\n\2/g' tmp_file
# To "follow" these, focus on the second part (after the \n break):
# '"'"' = escaped single quotation, used internally in single-quoted sed expression
# Since multiple spaces were converted (above) to single spaces, sentences will
# be separated by 0 or 1 spaces. Hence, the ".?" expression, below, will match
# 0 or 1 characters, between the two parts of these sed regex expressions
# [sentences will be split (\n) at those places].
# Replace -- again -- multiple spaces with single space:
# sed -i 's/ */ /g' tmp_file
# ----------------------------------------
# MORE REGEX EXAMPLES -- QUOTATION MARKS AND BRACKETS:
# ----------------------------------------------------
# bn="ant bat, cat; dog; (eel), [fish]: 'horse - jackal \"kangaroo\" {lemur} / moose | possum \ quail"
# echo $bn
# ant bat, cat; dog; (eel), [fish]: 'horse - jackal "kangaroo" {lemur} / moose | possum \ quail
# echo $bn; echo $bn | sed 's/[][(){} -,;:\x27"\|/]/./g' ## \x27 : single quote
# ant bat, cat; dog; (eel), [fish]: 'horse - jackal "kangaroo" {lemur} / moose | possum \ quail
# ant.bat..cat..dog...eel....fish....horse...jackal..kangaroo...lemur....moose...possum...quail
# echo $bn; echo $bn | sed 's/[][(){} -,;:\x27"\|/]/./g ; s/\.\{1,\}/./g' ## \x27 : single quote
# ant bat, cat; dog; (eel), [fish]: 'horse - jackal "kangaroo" {lemur} / moose | possum \ quail
# ant.bat.cat.dog.eel.fish.horse.jackal.kangaroo.lemur.moose.possum.quail
# NOTES:
# * not a regex (-r) sed expression, so need to escape the {} in {1,} --> \{1,\}
# * to easily escape a single quote ' in a 'single-quoted string', substitute it with: \x27
# * to include literal [] brackets inside a [] character class, they must appear in this order immediately after the leading (character class) [:
# [][...]
# echo 'donkey [horse]' | sed 's/[[]//g'
# donkey horse]
# echo 'donkey [horse]' | sed 's/[]]//g'
# donkey [horse
# echo 'donkey [horse]' | sed 's/[[]]//g' ## << does not work! [[]] ...
# donkey [horse]
# echo 'donkey [horse]' | sed 's/[][]//g' ## << ... use THIS! [][]
# donkey horse
# ----------------------------------------
# UPDATED [2017-11-24]:
# ---------------------
# With my substitution of ' " ( ) [ ] { } I no longer have to worry about
# those when splitting sentences -- this HUGELY simplifies things!! :-D
# [E.g., look at the "main processing loops" in my older
# "sed_sentence_chunker{1|2|3}.sh" scripts!]
# As well, I took the approach that since they will not be especially relevant
# for my BioNLP work, tokenized sentences, etc. of deleting all double quotation
# marks: ". As well, I delete all single quotes around sentences (keeping
# internal single quotes / apostrophes, with the exception that I expand most
# common contractions; e.g. it's --> it is ...). This (also) greatly simplifies
# the processing, i.e. sentence chunking / splitting! :-D
# ============================================================================
# ============================================================================
# PRELIMINARIES:
# ==============
# https://stackoverflow.com/questions/4638874/how-to-loop-through-a-directory-recursively-to-delete-files-with-certain-extensi
# FILES=$(find ./input-z -type f -iname "*")
# ... As a number of people have commented, this will fail if there are spaces in filenames.
# You can work around this by temporarily setting the IFS (internal field separator) to the newline character. ...
IFS=$'\n'; set -f
# be sure to include "unset IFS; set +f" when done, near the bottom of the script
FILES=$(find ./input -type f -iname "*") ## ALL files, recursively
# can also use this, in for loop a few lines below:
# for f in $(find ./input-z -type f -iname "*")
# echo '------------------------------------------------------------------------------'
# echo '$FILES:' ## single-quoted, prints: $FILES:
# echo "$FILES" ## double-quoted, prints path/, filename (one per line)
# echo '------------------------------------------------------------------------------'
for f in $FILES
do
cp "$f" "tmp_file" ## work on a copy so that input file $f is not modified
# ----------------------------------------------------------------------
# Preprocessing step -- replace various annoyances (different types of quotation marks; ligatures; ...):
# https://stackoverflow.com/questions/26568952/how-to-replace-multiple-patterns-at-once-with-sed
# https://stackoverflow.com/questions/24509214/how-to-escape-single-quote-in-sed
# Escape ' within single-quoted sed '...' expressions by substituting those ' with \x27; e.g.:
# s/'/'/g --> s/'/\x27/g
sed -i -e 's/ffi/ffi/g
s/fi/fi/g
s/ff/ff/g
s/fl/fl/g
s/ffl/ffl/g
s/…/.../g
s/�/μ/g
s//μ/g
s//®/g
s/␣/α/g
s//β/g
s/␦/δ/g
s/5Ј-/5\x27-/g
s/-3Ј/-3\x27/g
s/þ/+/g
s/¼/=/g
s/ϭ/=/g
s/Ɛ/=/g
s/Ͻ/</g
s/Ͼ/>/g
s/␥/γ/g
s//ε/g
s//ζ/g
s/Ϫ/-/g
s/À/-/g
s/# OLD:/=/g
s/ ؍ ./=/g
s//X/g
s/Ն/≥/g
s/Ն/≤/g
s/Յ/+/g
s/Ã/*/g
s/Â/x/g
s/¥/x/g
s///g
s/™//g
s/®//g
s/→/>/g
s/–/-/g
s/Ϯ/±/g
s/؉/+/g
s/ϫ/x/g
s/ϳ/~/g
s/ʽ/\x27/g
s/ʻ/\x27/g
s/“/"/g
s/ˮ/"/g
s/”/"/g
s/״/"/g
s/ʺ/"/g
s/′′/"/g
s/〃/"/g
s/’/\x27/g
s/ʼ/\x27/g
s/‘/\x27/g
s/′/\x27/g
s/`/\x27/g
s/׳/\x27/g
s/ʹ/\x27/g
s/ꞌ/\x27/g
s/ˊ/\x27/g
s/ˋ/\x27/g
s/ˌ/\x27/g
s/—/-/g
s/؊/-/g
s/ϩ/+/g
s/ϫ/x/g' tmp_file
# ============================================================================
# SPECIAL CASES -- COMMON ABBREVIATIONS:
# --------------------------------------
# ----------------------------------------
# PAGE NUMBER ABBREVIATIONS:
# Approach: substitute a unique alphanumeric string for "pp." (we will restore
# it later). Generated via the Linux command: pwgen 6 1
# Page number abbreviation "pp.", followed by a space; unlikely to appear'
# at EOL, so we can do a simple substitution:
sed -i 's/pp\.\s/Cho4Ph/g' tmp_file
# [ in character expression [] must appear first: [[]; -r regex, therefore
# [I will process the "p." abbreviation after I strip the document of
# extraneous whitespace.]
# ============================================================================
# REMOVE URLs
# Here is the approach that I used to remove URLs, etc. from my files
# not sed -r .... therefore \-escape the ? :
# sed -i -e 's/http[s]\?:\/\/\S*//g ; s/www\.\S*//g ; s/ftp:\S*//g ; s/doi:\S*//g' tmp_file
sed -i -e 's/http[s]\?:\/\/\S*//g ; s/www\.\S*//g ; s/ftp:\S*//g ; s/[dD][oO][iI]:\s\?\S*//g' tmp_file
# However, that expression leaves "blank" lines, that this perl expression removes:
perl -i -pe 's/^'`echo "\012"`'${2,}//g' tmp_file ## 012 is the octal form of \n
# Posted to / explained at:
# https://stackoverflow.com/questions/4283344/sed-to-remove-urls-from-a-file/47821796#47821796
# ... includes an alternative to using "branch labels" to deal with newlines, \n, with sed ...
# ============================================================================
# REMOVE (SOME) REFERENCES:
perl -i -pe 's/^Reference:.*$//g;s/^Ref:.*$//g;s/^Citation:.*$//g; s/^'`echo "\012"`'${2,}//g' tmp_file
# The last bit removes the non-printing newlines (\n) that are left behind.
# Test:
#
# Ongoing work in the Black lab seeks to uncover biomarkers of response and toxicity to new immunotherapeutic agents used in the fight against lung cancer.
# Reference: Madeline Krentz Gober, James P. Collard, Katherine Thompson, Esther P. Black.A microRNA signature of response to erlotinib is descriptive of TGFβ behaviour in NSCLC.
# Ref: Madeline Krentz Gober, James P. Collard, Katherine Thompson, Esther P. Black.A microRNA signature of response to erlotinib is descriptive of TGFβ behaviour in NSCLC.
# Citation: Madeline Krentz Gober, James P. Collard, Katherine Thompson, Esther P. Black.A microRNA signature of response to erlotinib is descriptive of TGFβ behaviour in NSCLC.
# Our previous work identified a 13-gene miRNA signature predictive of response to the epidermal growth factor receptor (EGFR) inhibitor, erlotinib, in Non-Small Cell Lung Cancer cell lines.
#
# perl -pe 's/^Reference:.*$//g;s/^Ref:.*$//g;s/^Citation:.*$//g; s/^'`echo "\012"`'${2,}//g' <that text>
# ============================================================================
# WHITESPACE, TABS:
# Remove leading, trailing whitespace and multiple spaces from sentences:
# https://www.cyberciti.biz/tips/delete-leading-spaces-from-front-of-each-word.html
sed -i 's/^[ \t]*//; s/[ \t]*$//' tmp_file ## two (chained) sed expressions
# Replace multiple spaces with single space:
sed -i 's/ */ /g' tmp_file
# ============================================================================
# REMAINING PAGE NUMBER ABBREVIATIONS:
# The page number abbreviation "p." is more complicated than "pp.". We
# needed to process "pp." (above) BEFORE "p.", otherwise substitution
# of the "p." in "pp." will incorrectly get substituted with "Cho4Ph".
sed -i -r 's/([[({\s])p\.\s?([ivx0-9])/\1Eiph2T\2/g' tmp_file
# [ in character class [] must appear first: [[...]
# ============================================================================
# BIOCHEMICAL TEXT -- AMINO ACIDS:
# Need to do these before processing periods, as (e.g.) the p. ("protein")
# in p.Arg62His (an amino acid substitution / variant) will be processed
# as an abbreviation, and/or split into a sentence at that period ...
sed -i 's/p.Ala/HieN7uuP/g' tmp_file ## Ala Alanine (A)
sed -i 's/p.Arg/Nae0RaeZ/g' tmp_file ## Arg Arginine (R)
sed -i 's/p.Asn/see7AuK6/g' tmp_file ## Asn Asparagine (N)
sed -i 's/p.Asp/chaeJeu1/g' tmp_file ## Asp Aspartic Acid (D)
sed -i 's/p.Cys/EiV6Gaix/g' tmp_file ## Cys Cysteine (C)
sed -i 's/p.Gln/Ufaiph2b/g' tmp_file ## Gln Glutamine (Q)
sed -i 's/p.Glu/Goh8eish/g' tmp_file ## Glu Glutamic Acid (E)
sed -i 's/p.Gly/xei1Phei/g' tmp_file ## Gly Glycine (G)
sed -i 's/p.His/aak0eVei/g' tmp_file ## His Histidine (H)
sed -i 's/p.Ile/vai9aeS3/g' tmp_file ## Ile Isoleucine (I)
sed -i 's/p.Leu/ohzah5Ei/g' tmp_file ## Leu Leucine (L)
sed -i 's/p.Lys/Oa4Aequo/g' tmp_file ## Lys Lysine (K)
sed -i 's/p.Met/TheeWie7/g' tmp_file ## Met Methionine (M)
sed -i 's/p.Phe/ohNa9pe0/g' tmp_file ## Phe Phenylalanine (F)
sed -i 's/p.Pro/Eetaib7k/g' tmp_file ## Pro Proline (P)
sed -i 's/p.Trp/ga3yeeGh/g' tmp_file ## Trp Tryptophan (W)
sed -i 's/p.Tyr/DuY2Gub7/g' tmp_file ## Tyr Tyrosine (Y)
sed -i 's/p.Ser/oezoo9Ca/g' tmp_file ## Ser Serine (S)
sed -i 's/p.Thr/wahRoo7E/g' tmp_file ## Thr Threonine (T)
sed -i 's/p.Val/ieKai4oo/g' tmp_file ## Val Valine (V)
# ----------------------------------------------------------------------------
# GENOMIC VARIANTS:
# ... a letter prefix should be used to indicate the type of reference sequence used.
# Accepted prefixes are;
# "g." for a genomic reference sequence
# "c." for a coding DNA reference sequence
# "n." for a non-coding DNA reference sequence
# "r." for an RNA reference sequence (transcript)
# "p." for a protein reference sequence
# ============================================================================
# PERIODS:
# To better deal with the many complications associated with periods,
# first delete all spaces preceding and proceeding periods. This will
# take care of, e.g.: U. S. A. | The end . | V. A. Stuart |
# J. Am. Soc. Chem. ...
sed -i 's/\s*\././g' tmp_file
sed -i 's/\.\s*/./g' tmp_file
# ----------------------------------------
# Ellipses (ellipsis: ...) -- convert 3 or more periods (.) to an ellipsis:
sed -i 's/\.\{3,\}/.../g' tmp_file
# .. then store those ellipses as a UID:
sed -i 's/\.\.\./Iet1auki/g' tmp_file
# ... and finally convert remaining tandem periods (..) to a single period:
sed -i 's/\.\././g' tmp_file
# ----------------------------------------
# version (v.) abbreviation (v. + 0 or 1 character + any number):
sed -i -r 's/v\.\s?([0-9])/Eegh5eel\1/g' tmp_file
# ----------------------------------------
# versus (vs.) abbreviation:
sed -i 's/vs\./Air5ah/g' tmp_file
# ----------------------------------------
# "E.g.", "e.g.", "I.e." or "i.e.":
sed -i 's/[eE]\.g\./Va1Eed/g' tmp_file
sed -i 's/[iI]\.e\./Uchee4/g' tmp_file
# ----------------------------------------
# "cc.", "CC." or "cf.":
# This also captures "Hcc" (hepatocellular carcinoma) at the end of a sentence: Hcc.
# sed -i 's/[cC]\.\?[cC]\./Ri9Ohk/g' tmp_file
# Here is a workaround:
sed -i 's/[^Hh][cC]\.\?[cC]\./Ri9Ohk/g' tmp_file
# " cc " or " CC ":
sed -i 's/\s[cC][cC]\s/ Ri9Ohk /g' tmp_file
sed -i 's/c\.\?f\./Tig8shei/g' tmp_file
sed -i 's/\scf\s/ Tig8shei /g' tmp_file
# ----------------------------------------
# "et al." abbreviation (will restore, with period, later):
sed -i 's/et al\./et al/g' tmp_file
# ----------------------------------------
# "Fig.", "fig.", "Figs.", "figs.":
# As I don't otherwise process commas, I can simply use them as a facile
# substitution for periods (later swapping , for . in post-processing):
sed -i -r 's/([fF]ig[s])\./\1,/g' tmp_file
# ----------------------------------------
# Personal titles (again, temporarily replace '.' with ','):
sed -i 's/Dr\./Dr,/g' tmp_file
sed -i 's/Drs\./Drs,/g' tmp_file
sed -i 's/Mr\./Mr,/g' tmp_file
sed -i 's/Mrs\./Mrs,/g' tmp_file
sed -i 's/Ms\./Ms,/g' tmp_file
sed -i 's/St\./St,/g' tmp_file
# ============================================================================
# OTHER BIOCHEMICAL TEXT:
# ----------------------------------------
# SINGLE QUOTATIONS:
# Note that some single quotes (i.e. apostrophes), e.g., 5'-, 3'-, ...
# are important in biochemistry / chemistry. To be safe, we'll proactively
# capture / protect these:
sed -i "s/3'/tho6Si2o/g" tmp_file ## e.g.: 3'-end
sed -i "s/5'/oochie8P/g" tmp_file ## e.g.: 5'-ATGGCTCGATCTTA...
sed -i "s/A's/ohph5AN6/g" tmp_file ## e.g.: (multiple adenines) multiple A's precede
sed -i "s/C's/Ji4oopow/g" tmp_file ## e.g.: (multiple adenines) multiple C's precede
sed -i "s/G's/Aeyahk4A/g" tmp_file ## e.g.: (multiple adenines) multiple G's precede
sed -i "s/T's/oogeel3W/g" tmp_file ## e.g.: (multiple adenines) multiple T's precede
# ----------------------------------------
# BIOCHEMICAL, CHEMICAL PRIMES:
sed -i "s/1'/hooPhil4/g" tmp_file
sed -i "s/2'/He5EiS1Z/g" tmp_file
sed -i "s/3'/IeghuP3V/g" tmp_file
sed -i "s/4'/Loh4aeri/g" tmp_file
sed -i "s/5'/Aht9Vohs/g" tmp_file
sed -i "s/6'/ReiR5zee/g" tmp_file
sed -i "s/7'/eiTei4ri/g" tmp_file
sed -i "s/8'/ay0ePicu/g" tmp_file
sed -i "s/9'/seeHush2/g" tmp_file
# ============================================================================
# REMAINING SINGLE, DOUBLE QUOTATIONS:
# Delete all double quotations: not particularly needed in NLP, e.g. tokenized text:
sed -i 's/"//g' tmp_file
# ----------------------------------------------------------------------------
# CONTRACTIONS:
# Deal with common contractions, before dealing with single quotes / apostrophes.
# ----------------------------------------
# First, expand common contractions:
sed -i -r "s/([a-z])'d/\1 did/g" tmp_file ## otherwise, 'd* becomes did*
sed -i -r "s/([a-z])'m/\1 am/g" tmp_file ## otherwise, 'm* becomes am*; e.g. to 'mess' with >> to amess' with
sed -i "s/won't/will not/g" tmp_file ## do this rule before the following rule
sed -i "s/n't/ not/g" tmp_file ## isn't | shouldn't | wouldn't | wouldn't | ...
sed -i "s/'ll/ will/g" tmp_file
sed -i "s/'re/ are/g" tmp_file
sed -i "s/'ve/ have/g" tmp_file
sed -i "s/here's/here is/g" tmp_file ## here's | Here's | there's | There's | where's | Where's ...
sed -i "s/I'd/I would/g" tmp_file
sed -i "s/It's/It is/g" tmp_file
sed -i "s/\sit's/ it is/g" tmp_file
sed -i "s/That's/That is/g" tmp_file
sed -i "s/that's/that is/g" tmp_file
sed -i "s/What's/What is/g" tmp_file
sed -i "s/\swhat's/ what is/g" tmp_file
# ----------------------------------------
# Next, substitute remaining contractions with UID (restore in post-processing):
sed -i -r "s/([a-zI])'d/\1chaSaib7/g" tmp_file ## e.g.: I'd | how'd | who'd | why'd | ...
sed -i -r "s/([a-zI])'ll/\1UivahJ5e/g" tmp_file ## e.g.: I'll
sed -i -r "s/([a-zI])'m/\1chahei1O/g" tmp_file ## e.g.: I'm
sed -i -r "s/([a-z])'t/\1Zeep7Auy/g" tmp_file
sed -i -r "s/([a-z])'nt/\1Zeep7Auy/g" tmp_file ## e.g.: is'nt [grammatical (spelling) error]
sed -i -r "s/([a-z])'re/\1Phoh5eil/g" tmp_file ## e.g.: you're | We're responsible ...
# ------------------
sed -i "s/'six/eKu6eech/g" tmp_file ## e.g.: escape 'six
sed -i "s/'seven/pahl8Avu/g" tmp_file ## e.g.: escape 'seven
sed -i -r "s/([a-z])'s/\1zaoGii5p/g" tmp_file ## e.g.: there's | various possessives: Victoria's | women's | ...
# ------------------
# UPDATE: the following expression left (when apostrophes restored) artefacts like this:
# 'mess'[orig text] >> [processing: this script] >> mess' [output]:
#
# sed -i -r "s/([a-z])'\s/\1ueKek3oh/g" tmp_file ## e.g.: plural noun possessives ending in "s": girls' dresses | Wilsons' house | ...
#
# It is not needed, with the inclusion of the "final" rule, below: sed -i "s/'//g" tmp_file
# ------------------
sed -i -r "s/([a-z])'t/\1iCuRahb6/g" tmp_file ## e.g.: isn't
sed -i -r "s/([a-zI])'ve/\1Roopes5f/g" tmp_file ## e.g.: I've' | (+)'ve
# less common / archaic:
sed -i "s/ma'am/Quei2Eex/g" tmp_file
sed -i "s/ne'er/IeDae7Lu/g" tmp_file ## e.g.: ne'er-do-well
sed -i -r "s/o'([a-z])/Xahc3Iel\1/g" tmp_file ## e.g.: o'clock
sed -i "s/'twas/uph4aida/g" tmp_file ## e.g. 'twas the night; escapes: 'two | 'twenty ...
# Finally, delete all remaining single quotations, apostrophes:
sed -i "s/'//g" tmp_file
# WITH THE EXPRESSION ABOVE, THIS SHOULD **NOT** BE NEEDED:
# Delete single quotations, apostrophes at end of words:
# sed -i "s/'\s/ /g" tmp_file ## e.g.: missed' that
# sed -i "s/'\././g" tmp_file ## e.g.: missed.' That
# sed -i "s/\.'/./g" tmp_file ## e.g.: missed'. That
# ============================================================================
# PREPROCESSING MISCELLANY:
# ----------------------------------------
# Delete tandem commas, semicolons:
sed -i 's/,,/,/g' tmp_file
sed -i 's/;;/;/g' tmp_file
# ----------------------------------------
# Clean up improperly-terminated sentences (e.g. ?!!?!?!??!):
# ------------------
# Tandem question, exclamation marks:
for i in {1..8}
do
sed -i 's/??/?/g' tmp_file ## not regex (-r), so those those are
sed -i 's/!!/!/g' tmp_file ## literal ? ! character substitutions
done
# ------------------
# Remaining [.!?] permutations:
sed -i 's/!?/?/g' tmp_file
sed -i 's/?!/?/g' tmp_file
sed -i 's/?\./?/g' tmp_file
sed -i 's/!\./!/g' tmp_file
sed -i 's/\.?/?/g' tmp_file
sed -i 's/\.!/!/g' tmp_file
# ============================================================================
# BRACKETS:
# These can be annoying, especially re: processing. They are important in
# chemistry / biochemistry, however (e.g. chemical / biochemical names), so
# for now just do the usual substitute / replace later approach.
# The order of these steps is important: do ( [ {, then ) ] } associated
# with periods (to split at those), then do left-over ( ) [ ] { }.
# ----------------------------------------
# Simplify [{ as ( ; simplify ]} as ) :
sed -i 's/\[/(/g' tmp_file ## \-escape the [ : \[
sed -i 's/]/)/g' tmp_file
sed -i 's/{/(/g' tmp_file
sed -i 's/}/)/g' tmp_file
# ----------------------------------------
# Angle brackets { < | > }:
# Deal with these first: (angle brackets used as mathematical inequalities);
# include "p" to capture (e.g.) "p < 0.001" or "p > 0.001 :
sed -i -r 's/([0-9p])\s?<\s?([0-9])/\1Woxoh4ph\2/g' tmp_file
sed -i -r 's/([0-9p])\s?>\s?([0-9])/\1aeja8ohM\2/g' tmp_file
# not "sed -r", therefore \-escape "?" (regex 0 or 1 modifier) -- \? :
sed -i 's/\s\?<\s\?=\s\?/aev3Shoo/g' tmp_file
sed -i 's/\s\?>\s\?=\s\?/iez7ieVi/g' tmp_file
# ... then remove all other angle brackets:
sed -i 's/</(/g' tmp_file
sed -i 's/>/)/g' tmp_file
# ----------------------------------------
# Delete spaces following leading parentheses; delete spaces preceding lagging parentheses:
sed -i 's/(\s\?/(/g' tmp_file
sed -i 's/\s\?)/)/g' tmp_file
# ----------------------------------------
# Delete empty and multiple parentheses:
sed -i 's/(\s\?)//g' tmp_file
sed -i 's/(\{2,\}/(/g' tmp_file
sed -i 's/)\{2,\}/)/g' tmp_file
# ----------------------------------------
# Split parentheses associated with punctuation (.?!) at the ends of sentences:
sed -i 's/\.)\s\?/.)\n/g' tmp_file
sed -i 's/\.\s\?(/.\n(/g' tmp_file
sed -i 's/?)\s\?/?)\n/g' tmp_file
sed -i 's/?\s\?(/?\n(/g' tmp_file
sed -i 's/!)\s\?/!)\n/g' tmp_file
sed -i 's/!\s\?(/!\n(/g' tmp_file
# ----------------------------------------
# Split lines on ") (", only if first parenthesized expression is at the end of a sentence:
sed -i 's/[.!?]\s\?)\s\?(/.)\n(/g' tmp_file
# ----------------------------------------
# Clean up: remove parentheses at start or end of lines:
# First, (again) remove all leading and trailing whitespace from sentences, as well as multiple spaces:
sed -i 's/^[ \t]*//; s/[ \t]*$//' tmp_file ## two (chained) sed expressions
sed -i 's/^(//g' tmp_file
sed -i 's/)$//g' tmp_file
# ============================================================================
# AUTHOR INITIALS; JOURNAL TITLE ABBREVIATIONS:
# =============================================
sed -i -r 's/(\.[A-Z][a-z]{0,13})\./\1Shah7a/g' tmp_file
# (Proc.NatlShah7aAcad.SciShah7aUShah7aS.AShah7a104, 9346
sed -i -r 's/(Shah7a[A-Z][a-z]{0,13})\./\1Shah7a/g' tmp_file
# (Proc.NatlShah7aAcadShah7aSciShah7aUShah7aSShah7aAShah7a104, 9346
# ----------------------------------------------------------------------------
# Match abbreviations at the start of a line.
sed -i -r 's/(^[A-Z][a-z]{0,13})\./\1Shah7a/g' tmp_file
# ----------------------------------------------------------------------------
# Capture the first abbreviation inside a parenthesis ( ( ):
sed -i -r 's/(\([A-Z][a-z]{0,13})\./\1Shah7a/g' tmp_file ## \-escaped, literal ( inside () character substitution
# ----------------------------------------------------------------------------
# Authors' names -- additional processing: