-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathclassify.sh
237 lines (198 loc) · 11.4 KB
/
classify.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
#!/bin/bash
#Set Script Name variable
SCRIPT="classify.sh"
print_help() {
printf "\nHelp documentation for ${BOLD}$SCRIPT ${NC}\n\n"
printf "The following command line options are recognized:\n"
printf " ${BOLD}-i ${NC}\t-- the input file coded in **UTF-8 without BOM**, containing the corpus for the classification;[here](https://github.com/collab-uniba/Emotion_and_Polarity_SO/wiki/File-format-for-classification-corpus).\n"
printf " ${BOLD}-d ${NC}\t-- the delimiter semicolon or comma used in the csv file.\n"
printf " ${BOLD}-m ${NC}\t-- path to the liblinear model will be used for classification; if you omit this input, the script will use the model trained on Stack Overflow on the emotion you specify\n"
printf " ${BOLD}-o ${NC}\t-- path to the n-grams folder containing UnigramsList.txt and BigramsList.txt used to train the model given in input\n"
printf " ${BOLD}-f ${NC}\t-- if you give the model as input you must specify n-grams path containing UnigramsList.txt and BigramsList.txt used to train the model given in input\n"
printf " ${BOLD}-e ${NC}\t-- the specific emotion for training the model, defined in joy, anger,sadness, love, surprise, fear.\n"
printf " ${BOLD}-p ${NC}\t-- wheter to calculate the feature of politeness, mood and modality.\n"
printf " ${BOLD}-l ${NC}\t-- indicates if the csv given in input has the column named label or not\n"
printf " ${BOLD}-X ${NC}\t -- the maximum amount of heap memory to use (e.g.: `-X 30000m`).\n"
printf " ${BOLD}-h ${NC}\t-- Displays this help message. No further functions are performed.\n\n"
printf "Example: ${BOLD} bash classify.sh -i love.csv -e love -g training_love/liblinear/NoDownSampling/modelLiblinear_0.Rda -l -f training_love/idfs -o training_love/n-grams -d semicolon ${NC}\n\n"
exit 1
}
# redefine an echo function depending on verbose switch
print() {
local level=$1
local code=$GREEN # default is "NOTICE"
if [ "${level}" = 'ERROR' ]; then
code=$RED
elif [ "${level}" = 'INFO' ]; then
code=$CYAN
fi
if [ "${VERBOSE}" == 'on' ]; then
printf "${code}[${level}]${NC}: " | tee -a $LOG
echo $2 | tee -a $LOG
else
printf "${code}[${level}]${NC}: "
echo $2
fi
}
HASLABEL=""
CALCPOLITEIMPOLITEMOODMODALITY=""
MODEL=""
# parse args
while getopts "i:d:e:lf:o:m:hp" FLAG; do
case $FLAG in
i ) INPUT=$OPTARG;;
f ) IDFPATH=$OPTARG;;
o) DICTIONARYPATH=$OPTARG;;
m ) MODEL=$OPTARG;;
l ) HASLABEL="-L";;
e ) EMOTION=$OPTARG
if [ "$EMOTION" != 'anger' ] && [ "$EMOTION" != 'fear' ] && [ "$EMOTION" != 'sadness' ] && [ "$EMOTION" != 'love' ] && [ "$EMOTION" != 'joy' ] && [ "$EMOTION" != 'surprise' ]; then
print "ERROR" "-e option has wrong argument."
exit 2;
fi;;
d ) DELIMITER=$OPTARG
if [ "$DELIMITER" != 'c' ] && [ "$DELIMITER" != 'sc' ] ; then
print "ERROR" "-d option has wrong argument."
exit 2;
fi;;
X ) MAX_HEAP_SIZE=$OPTARG;;
h ) print_help;;
p ) CALCPOLITEIMPOLITEMOODMODALITY="-polmod";;
\? ) #unrecognized option - show help
printf "INFO" "Use $SCRIPT -h to see the help documentation."
exit 2;;
: ) print "ERROR" "Missing option argument for -$OPTARG"
exit 2;;
* ) printf "ERROR" "Unimplemented option: -$OPTARG"
exit 2;;
esac
done
shift $((OPTIND-1)) #This tells getopts to move on to the next argument.
### end getopts code ###
filename=${INPUT##*/}
filename=${filename%.*}
# Create ids file
DELIMITER_CHAR=','
if [ "$DELIMITER" = 'sc' ] ; then
DELIMITER_CHAR=';'
fi;
awk -F "\"*"$DELIMITER_CHAR"\"*" '{print $1}' $INPUT | sed 's/^\"*//' > "classification_$filename""_$EMOTION"/features-ids.csv
if [ "$MODEL" != '' ] ; then
#use the model given as input
cp $MODEL r/Liblinear/
else
if [ "$EMOTION" = 'anger' ] ; then
MODEL=r/Liblinear/SOModels/modelAnger.Rda
elif [ "$EMOTION" = 'joy' ] ; then
MODEL=r/Liblinear/SOModels/modelJoy.Rda
elif [ "$EMOTION" = 'fear' ] ; then
MODEL=r/Liblinear/SOModels/modelFear.Rda
elif [ "$EMOTION" = 'love' ] ; then
MODEL=r/Liblinear/SOModels/modelLove.Rda
elif [ "$EMOTION" = 'sadness' ] ; then
MODEL=r/Liblinear/SOModels/modelSadness.Rda
elif [ "$EMOTION" = 'surprise' ] ; then
MODEL=r/Liblinear/SOModels/modelSurprise.Rda
fi;
cp $MODEL r/Liblinear/
#use the default grams and IDF
DICTIONARYPATH=java/res/n-gramsSO
IDFPATH=java/res/idfsSO
fi;
#: <<'CREATEDOCFORMAT'
# rm -rf "classification_$filename""_$EMOTION"
#Creating the format to give at python files.
if [ "$CALCPOLITEIMPOLITEMOODMODALITY" = '-polmod' ] ; then
if [ "$DELIMITER" = 'sc' ] ; then
java -jar -Xmx${MAX_HEAP_SIZE:-"30000m"} -XX:+UseConcMarkSweepGC java/Emotion_and_Polarity_SO.jar -i $INPUT -d ';' -t classification -Ex createDocFormat -e $EMOTION
elif [ "$DELIMITER"='c' ] ; then
java -jar -Xmx${MAX_HEAP_SIZE:-"30000m"} -XX:+UseConcMarkSweepGC java/Emotion_and_Polarity_SO.jar -i $INPUT -d ',' -t classification -Ex createDocFormat -e $EMOTION
fi;
# CREATEDOCFORMAT
# : <<'MODELPY'
#taking only the file.csv name, deleting path and the extension
#taking the files created for the two python files
cp "classification_$filename""_$EMOTION"/ElaboratedFiles/docs.py python/CalculatePoliteAndImpolite/
cp "classification_$filename""_$EMOTION"/ElaboratedFiles/docs.py python/CalculateMoodModality/
#starting python files for polite , impolite mood and modality extraction
cd python/CalculatePoliteAndImpolite
#alias pj="cd CalculatePoliteAndImpolite/"
python model.py
cd ../..
cp python/CalculatePoliteAndImpolite/textsPoliteAndImpolite.csv "classification_$filename""_$EMOTION"/ElaboratedFiles/
rm python/CalculatePoliteAndImpolite/textsPoliteAndImpolite.csv
# MODELPY
# : <<'MOODMODALITY'
cd python/CalculateMoodModality
python moodAndModality.py
cd ../..
cp python/CalculateMoodModality/textsMoodAndModality.csv "classification_$filename""_$EMOTION"/ElaboratedFiles/
rm python/CalculateMoodModality/textsMoodAndModality.csv
# MOODMODALITY
fi;
# : <<'UNIGRAMSBIGRAMS'
#copy the
mkdir -p "classification_$filename""_$EMOTION"/idfs
mkdir -p "classification_$filename""_$EMOTION"/n-grams
cp $IDFPATH/* "classification_$filename""_$EMOTION"/idfs/
cp $DICTIONARYPATH/* "classification_$filename""_$EMOTION"/n-grams/
#starting Emotion_and_Polarity_SO.jar to extract the features
if [ "$DELIMITER" = 'sc' ] ; then
if [ "$CALCPOLITEIMPOLITEMOODMODALITY" = '-polmod' ] ; then
java -jar -Xmx${MAX_HEAP_SIZE:-"30000m"} -XX:+UseConcMarkSweepGC java/Emotion_and_Polarity_SO.jar -i $INPUT -P "classification_$filename""_$EMOTION"/ElaboratedFiles/textsPoliteAndImpolite.csv -M "classification_$filename""_$EMOTION"/ElaboratedFiles/textsMoodAndModality.csv -d ';' $EXTRACTDICTIONARY -t classification -Ex SenPolImpolMoodModality -e $EMOTION
fi;
java -jar -Xmx${MAX_HEAP_SIZE:-"30000m"} -XX:+UseConcMarkSweepGC java/Emotion_and_Polarity_SO.jar -i $INPUT -d ';' -t classification -Ex unigrams_1 -e $EMOTION
java -jar -Xmx${MAX_HEAP_SIZE:-"30000m"} -XX:+UseConcMarkSweepGC java/Emotion_and_Polarity_SO.jar -i $INPUT -d ';' -t classification -Ex bigrams_1 -e $EMOTION
java -jar -Xmx${MAX_HEAP_SIZE:-"30000m"} -XX:+UseConcMarkSweepGC java/Emotion_and_Polarity_SO.jar -i $INPUT -d ';' -t classification -Ex unigrams_2 -e $EMOTION
java -jar -Xmx${MAX_HEAP_SIZE:-"30000m"} -XX:+UseConcMarkSweepGC java/Emotion_and_Polarity_SO.jar -i $INPUT -d ';' -t classification -Ex bigrams_2 -e $EMOTION
java -jar -Xmx${MAX_HEAP_SIZE:-"30000m"} -XX:+UseConcMarkSweepGC java/Emotion_and_Polarity_SO.jar -i $INPUT -d ';' -t classification -Ex wordnet $HASLABEL -e $EMOTION
elif [ "$DELIMITER"='c' ] ; then
if [ "$CALCPOLITEIMPOLITEMOODMODALITY" = '-polmod' ] ; then
java -jar -Xmx${MAX_HEAP_SIZE:-"30000m"} -XX:+UseConcMarkSweepGC java/Emotion_and_Polarity_SO.jar -i $INPUT -P "classification_$filename""_$EMOTION"/ElaboratedFiles/textsPoliteAndImpolite.csv -M "classification_$filename""_$EMOTION"/ElaboratedFiles/textsMoodAndModality.csv -d ',' $EXTRACTDICTIONARY -t classification -Ex SenPolImpolMoodModality -e $EMOTION
fi;
java -jar -Xmx${MAX_HEAP_SIZE:-"30000m"} -XX:+UseConcMarkSweepGC java/Emotion_and_Polarity_SO.jar -i $INPUT -d ',' -t classification -Ex unigrams_1 -e $EMOTION
java -jar -Xmx${MAX_HEAP_SIZE:-"30000m"} -XX:+UseConcMarkSweepGC java/Emotion_and_Polarity_SO.jar -i $INPUT -d ',' -t classification -Ex bigrams_1 -e $EMOTION
java -jar -Xmx${MAX_HEAP_SIZE:-"30000m"} -XX:+UseConcMarkSweepGC java/Emotion_and_Polarity_SO.jar -i $INPUT -d ',' -t classification -Ex unigrams_2 -e $EMOTION
java -jar -Xmx${MAX_HEAP_SIZE:-"30000m"} -XX:+UseConcMarkSweepGC java/Emotion_and_Polarity_SO.jar -i $INPUT -d ',' -t classification -Ex bigrams_2 -e $EMOTION
java -jar -Xmx${MAX_HEAP_SIZE:-"30000m"} -XX:+UseConcMarkSweepGC java/Emotion_and_Polarity_SO.jar -i $INPUT -d ',' -t classification -Ex wordnet $HASLABEL -e $EMOTION
fi;
# UNIGRAMSBIGRAMS
#merging the single features extracted
if [ "$CALCPOLITEIMPOLITEMOODMODALITY" = '-polmod' ] ; then
paste -d , "classification_$filename""_$EMOTION"/features-SenPolImpolMoodModality.csv "classification_$filename""_$EMOTION"/features-unigrams_1.csv "classification_$filename""_$EMOTION"/features-unigrams_2.csv "classification_$filename""_$EMOTION"/features-bigrams_1.csv "classification_$filename""_$EMOTION"/features-bigrams_2.csv "classification_$filename""_$EMOTION"/features-wordnet.csv > "classification_$filename""_$EMOTION"/features-$EMOTION.csv
else
paste -d , "classification_$filename""_$EMOTION"/features-ids.csv "classification_$filename""_$EMOTION"/features-unigrams_1.csv "classification_$filename""_$EMOTION"/features-unigrams_2.csv "classification_$filename""_$EMOTION"/features-bigrams_1.csv "classification_$filename""_$EMOTION"/features-bigrams_2.csv "classification_$filename""_$EMOTION"/features-wordnet.csv > "classification_$filename""_$EMOTION"/features-$EMOTION.csv
fi;
#rm "classification_$filename""_$EMOTION"/features-SenPolImpolMoodModality.csv
#rm "classification_$filename""_$EMOTION"/features-unigrams_1.csv
#rm "classification_$filename""_$EMOTION"/features-bigrams_1.csv
#rm "classification_$filename""_$EMOTION"/features-unigrams_2.csv
#rm "classification_$filename""_$EMOTION"/features-bigrams_2.csv
#rm "classification_$filename""_$EMOTION"/features-wordnet.csv
# : <<'END'
#run the R script for classification
#create a folder for the liblinear's generated outputs into the output folder
mv "classification_$filename""_$EMOTION"/features-$EMOTION.csv r/Liblinear/
modelName=${MODEL##*/}
cd r/Liblinear
rm -rf output/Results_$EMOTION
if [ "$HASLABEL" = '-L' ] ; then
Rscript classification.R Results_$EMOTION $modelName features-$EMOTION.csv 1 $EMOTION
else
Rscript classification.R Results_$EMOTION $modelName features-$EMOTION.csv 0 $EMOTION
fi;
cd ../..
mv r/Liblinear/output/Results_$EMOTION/* "classification_$filename""_$EMOTION"/
rm -r r/Liblinear/$modelName
rm -r r/Liblinear/output/Results_$EMOTION
rm -r "classification_$filename""_$EMOTION"/n-grams
rm -r "classification_$filename""_$EMOTION"/idfs
rm -r "classification_$filename""_$EMOTION"/ElaboratedFiles
rm "classification_$filename""_$EMOTION"/features-bigrams_1.csv
rm "classification_$filename""_$EMOTION"/features-bigrams_2.csv
rm "classification_$filename""_$EMOTION"/features-SenPolImpolMoodModality.csv
rm "classification_$filename""_$EMOTION"/features-unigrams_1.csv
rm "classification_$filename""_$EMOTION"/features-unigrams_2.csv
rm "classification_$filename""_$EMOTION"/features-wordnet.csv
# rm "classification_$filename""_$EMOTION"/features-"$EMOTION".csv
# END