-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrandomCorpusScript.sh
More file actions
206 lines (180 loc) · 6.46 KB
/
Copy pathrandomCorpusScript.sh
File metadata and controls
206 lines (180 loc) · 6.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
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
#!/bin/bash
#The MIT License (MIT)
#
#Copyright (c) 2015 Hayda Almeida
#
#Permission is hereby granted, free of charge, to any person obtaining a copy
#of this software and associated documentation files (the "Software"), to deal
#in the Software without restriction, including without limitation the rights
#to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
#copies of the Software, and to permit persons to whom the Software is
#furnished to do so, subject to the following conditions:
#
#The above copyright notice and this permission notice shall be included in all
#copies or substantial portions of the Software.
#
#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
#IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
#FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
#AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
#LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
#OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
#SOFTWARE.
#
# script to perform random sampling and
# discriminate files to generate triage corpora
#
#
# Purpose: gather paths of corpus root and corpus src
# root --> parent folder, where train and test folders will be created
# src --> folder with all .XML files
#
paths(){
echo ""
read -p "Source files [path]: " CORPUS_SOURCE
read -p "Positive instances [path]: " CORPUS_POSITIVE
CORPUS_ROOT=${CORPUS_SOURCE%/*}
echo ""
echo "=============================================================="
echo "============= Corpus root: " $CORPUS_ROOT
echo "=========== Corpus source: " $CORPUS_SOURCE
echo "====== Positive instances: " $CORPUS_POSITIVE
echo "=============================================================="
echo ""
read -p "Are the directories correct? [Y/n] " PATHCONF
echo ""
}
#
# Purpose: copies only .XML files from negative/positive
# folders, avoiding duplicate files
#
copyXML(){
for dir in $CORPUS_SOURCE/*/
do
thisDir=`basename ${dir}`
if [ $thisDir = `basename ${CORPUS_POSITIVE}` ]
then cp $dir/*.xml $POSITIVES
echo "Positive files from $thisDir copied to $POSITIVES"
else
cp $dir/*.xml $NEGATIVES
echo "Negative files from $thisDir copied to $NEGATIVES"
fi
done
}
#
# Purpose: prompt and define balance and size of test corpus
#
balanceTest(){
echo ""
read -p "Size of TEST corpus: " TEST_SIZE
read -p "TEST - Positive instances [%]: " TEST_POS_RATIO
TEST_POS_AMOUNT=$(( (( $TEST_SIZE * $TEST_POS_RATIO )) /100))
TEST_NEG_AMOUNT=$(( $TEST_SIZE - $TEST_POS_AMOUNT ))
echo ""
echo "==============================================================="
echo "========== Test corpus size: " $TEST_SIZE
echo "=== Test positive instances: " $TEST_POS_AMOUNT
echo "=== Test negative instances: " $TEST_NEG_AMOUNT
echo "==============================================================="
echo ""
read -p "Are the size and balance correct? [Y/n] " BTESTCONF
echo ""
}
#
# Purpose: perform random sampling, and moves selected
# files from positive/negative to test_folder
#
moveTestFiles(){
mkdir -p $CORPUS_ROOT/test
TEST_FOLDER=$CORPUS_ROOT/test
# number of negative instances in all families
TEST_NEG_TOTAL=$(find /$NEGATIVES -name '*.xml' | wc -l)
cd $NEGATIVES
mv $(ls | shuf -n$TEST_NEG_AMOUNT) $TEST_FOLDER
echo "Moved " $TEST_NEG_AMOUNT " from " `basename ${NEGATIVES}` " to " $TEST_FOLDER
cd
# random positives, move it to test folder
cd $POSITIVES
mv $(ls | shuf -n$TEST_POS_AMOUNT) $TEST_FOLDER
echo "Moved " $TEST_POS_AMOUNT " from " `basename ${POSITIVES}` " to " $TEST_FOLDER
cd
}
#
# Purpose: prompt balance and size of train corpus
#
balanceTrain(){
echo ""
echo ""
read -p "Initial size of TRAIN corpus: " TRAIN_IN_SIZE
read -p "Initial Positive instances [%]: " TRAIN_POS_RATIO
read -p "Sampling factor [%]: " SAMP_FACTOR
read -p "Number of corpora [0 if no sampling]: " NUMBER_CORP
# calculating positive ratios
posPerc=( $( seq $(( $TRAIN_POS_RATIO - (( $SAMP_FACTOR * $NUMBER_CORP )) )) $SAMP_FACTOR $TRAIN_POS_RATIO ) )
TRAIN_POS_AMOUNT=$(( (($TRAIN_IN_SIZE*$TRAIN_POS_RATIO)) / 100 ))
# predicting corpora sizes
declare -A corpSize
for ((i = 0; i < ${#posPerc[@]}; i++ ))
do
corpSize[$i]=$(( (($TRAIN_POS_AMOUNT*100))/ ${posPerc[$i]} ))
done
echo "==============================================================="
echo "==== Train corpora to generate [#]: " ${#posPerc[@]}
echo "====== Train positive instances[#]: " $TRAIN_POS_AMOUNT
echo "===== Train positive instances [%]: " ${posPerc[@]}
echo "===== Train corpora sizes [approx]: " ${corpSize[@]}
echo "==============================================================="
echo ""
read -p "Continue with these parameters? [Y/n] " BTRAINCONF
echo ""
}
#
# Purpose: random sampling, copy it from positive/negative to train_folder
#
copyTrainFiles(){
for ((i = 0; i < ${#posPerc[@]}; i++ ))
do
negPerc=$((100 - ${posPerc[$i]}))
mkdir -p $CORPUS_ROOT/train_${posPerc[$i]}_$negPerc
TRAIN_FOLDER=$CORPUS_ROOT/train_${posPerc[$i]}_$negPerc
TRAIN_SIZE=$(( (($TRAIN_POS_AMOUNT*100))/ ${posPerc[$i]} ))
# negatives for this training set
TRAIN_NEG_AMOUNT=$(( (( $negPerc * $TRAIN_POS_AMOUNT )) / ${posPerc[$i]} ))
# random positives, copy it to train folder
cd $POSITIVES
cp $(ls | shuf -n$TRAIN_POS_AMOUNT) $TRAIN_FOLDER
echo "Copied " $TRAIN_POS_AMOUNT " from " `basename ${POSITIVES}` " to " `basename ${TRAIN_FOLDER}` "."
cd
# random negatives, copy it to train folder
cd $NEGATIVES
cp $(ls | shuf -n$TRAIN_NEG_AMOUNT) $TRAIN_FOLDER
echo "Copied " $TRAIN_NEG_AMOUNT " from " `basename ${NEGATIVES}` " to " `basename ${TRAIN_FOLDER}` "."
cd
done
}
#########################
### Main script order ###
#########################
# gather paths until user confirms the paths are correct
paths
while [ "$PATHCONF" != "Y" ]; do
paths
done
# creating folders for positive and negative files
mkdir $CORPUS_ROOT/negatives
NEGATIVES=$CORPUS_ROOT/negatives
mkdir $CORPUS_ROOT/positives
POSITIVES=$CORPUS_ROOT/positives
# copying only XML files - leaving duplicates behind
copyXML
balanceTest
while [ "$BTESTCONF" != "Y" ]; do
balanceTest
done
moveTestFiles
balanceTrain
while [ "$BTRAINCONF" != "Y" ]; do
balanceTrain
done
copyTrainFiles
##########################