-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun.sh
More file actions
executable file
·166 lines (141 loc) · 5.49 KB
/
run.sh
File metadata and controls
executable file
·166 lines (141 loc) · 5.49 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
#!/bin/bash
################################################################################
# Spanish learner helper (possible all-language-lerner-helper) #
# App shows random word from a file (specified or default one) and prompts #
# user to input word in foreign language. User gets feedback if input was #
# correct and in both cases he is suppoerted with original word (and possibly) #
# example sentences). #
# All insertions are logged into file (default: history.txt). #
# One suppoerted argument is a file containing words. #
# Script is dedicated to run with crontab in hourly schedule. #
# #
# przemek@videveritatis.pl #
# 2015.06.22 vodnic #
################################################################################
# Input file's format:
# eng_word<tab>spa_word<tab>eng_examp(optional)<tab>spa_examp(optional)<\n>
# Spaces should be replaced with underscore (_) character.
# In two or more translations, they should be seperated with slash (/)
# character without spaces before and after.
# In case of articles, use only definded ones (la/el/las/los).
################################################################################
# Rewrite arguments from file to handy variables.
processArguments () {
ENGLISH_WORD=$(echo $1 | sed -r 's/_/ /g')
SPANISH_WORD=$(echo $2 | sed -r 's/_/ /g')
ENGLISH_EXAMPLE=$(echo $3 | sed -r 's/_/ /g')
SPANISH_EXAMPLE=$(echo $4 | sed -r 's/_/ /g')
}
# Treat sources with and without articles as with same format.
checkArticle () {
ARTICLE=""
if [[ $1 == la_* ]] || [[ $1 == el_* ]]; then
ARTICLE="*"
ARTICLE_LENGTH=3
fi
if [[ $1 == las_* ]] || [[ $1 == los_* ]]; then
ARTICLE="*"
ARTICLE_LENGTH=4
fi
}
# Link to wider description on wiktionary or google translate.
# TODO: Case with mixed two definitions, one singleword, and second mulitword
# and/or one with article and second not.
preapreLink () {
LINK="http://en.wiktionary.org/wiki/$SPANISH_WORD#Spanish"
if [ "$ARTICLE" != "" ]; then
LINK="http://en.wiktionary.org/wiki/${SPANISH_WORD:$ARTICLE_LENGTH}#Spanish"
fi
WORD_COUNT=$(echo $SPANISH_WORD | wc -w)
if [ "$ARTICLE" != "" ] && [ $WORD_COUNT -gt 2 ]; then
LINK="https://translate.google.pl/?hl=pl#es/en/$SPANISH_WORD"
fi
if [ "$ARTICLE" == "" ] && [ $WORD_COUNT -gt 1 ]; then
LINK="https://translate.google.pl/?hl=pl#es/en/$SPANISH_WORD"
fi
if [ "$SPANISH_SECOND" != "" ]; then
LINK="http://en.wiktionary.org/wiki/$SPANISH_SECOND#Spanish"
fi
}
# Save (or not) an event of insertin word by user to log file and error db.
logEntry() {
if [ "$DISABLE_LOG" == "" ]; then
DATE=$(date "+%Y-%m-%d %H:%M:%S")
if [ "$TYPE" == "info" ]; then
LOG="[CORR-$DATE] $SPANISH_WORD, $ENGLISH_WORD,"
fi
if [ "$TYPE" == "error" ]; then
LOG="[ERR!-$DATE] $SPANISH_WORD, $ENGLISH_WORD, $INPUT"
ERROR_ENTRY="$ENGLISH_WORD\t$SPANISH_WORD\t$ENGLISH_EXAMPLE\t$SPANISH_EXAMPLE"
ERROR_DB=$(echo "$ERROR_ENTRY" | sed -r 's/ /_/g')
echo -e "$ERROR_DB" >> db_errors.csv
fi
echo $LOG >> history.txt
fi
}
# Prompt for word in spanish and verifies its correctness.
# TODO: Case with mixed two definitions, one singleword, and second mulitword
# and/or one with article and second not.
readAndVerifyWord () {
if [[ "$SPANISH_WORD" == *"/"* ]]; then
SPANISH_FIRST=$(echo "$SPANISH_WORD" | cut -d"/" -f 1)
SPANISH_SECOND=$(echo "$SPANISH_WORD" | cut -d'/' -f 2)
fi
INPUT=$(zenity --entry \
--text="$ENGLISH_WORD $ARTICLE" \
--title="Learn a word!")
if [ "$SPANISH_SECOND" != "" ]; then
if [ "$INPUT" == "$SPANISH_FIRST" ] || [ $INPUT == "$SPANISH_SECOND" ]; then
TYPE="info"
STATUS="Correct!"
fi
fi
if [ "$INPUT" == "$SPANISH_WORD" ]; then
TYPE="info"
STATUS="Correct!"
fi
if [ "$TYPE" == "" ]; then
TYPE="error"
STATUS="Wrong!"
fi
}
# Prepare text for a final message box.
prepareText () {
MAIN_TEXT="<span size=\"xx-large\"><a href='$LINK'>$SPANISH_WORD</a></span>\n$ENGLISH_WORD"
if [ "$ENGLISH_EXAMPLE" != "" ]; then
EXAMPLES="\n\n<i>$ENGLISH_EXAMPLE\n$SPANISH_EXAMPLE</i>"
fi
TEXT=$MAIN_TEXT$EXAMPLES
}
# Final step.
showFinalBox () {
zenity \
--$TYPE \
--text="$TEXT" \
--title="$STATUS" \
--ok-label="Got It!"
}
# Here happens whole magic.
main () {
processArguments $1 $2 $3 $4
checkArticle $2
readAndVerifyWord
logEntry
preapreLink
prepareText
showFinalBox
}
################################################################################
# App start - read file with words and go to main() func.
# NOTE: all possible errors are ignored because of spam caused by zenity
# probelms with GTK config. In case of debugging necesity, remove
# dev null destination at will.
FILE=$1
DISABLE_LOG=$2
if [ "$FILE" == "" ]; then
FILE="maindb.csv"
fi
LINE="$(sort --random-sort $FILE | head -n 1)"
#main $LINE 2>/dev/null
main $LINE
################################################################################