-
Notifications
You must be signed in to change notification settings - Fork 0
/
ord
executable file
·201 lines (178 loc) · 5.81 KB
/
ord
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
#!/bin/bash
# Change to the directory of the script
cd "$(dirname "${BASH_SOURCE[0]}")" || exit
# Function to print help message
print_help() {
echo "Usage: ord [OPTIONS] SEARCH_TERM"
echo "Options:"
echo " Search Faroese to Faroese (default)"
echo " -s Search using regular expression"
echo " -a Search all words"
echo " -A Search all descriptions+words"
echo " -E Search Faroese to English"
echo " -e Search English to Faroese"
echo " -D Search Faroese to Danish"
echo " -d Search Danish to Faroese"
echo " -r Search Russian to Faroese"
echo " -n Search Names in Faroese"
echo " -h Display this help message"
exit 1
}
# Function to convert string to lowercase
to_lowercase() {
echo "$1" | tr '[:upper:]' '[:lower:]'
}
# Default behavior is searching Faroese to Faroese
# Default search directories
default_search_dirs=("fo-fo" "buskaparord" "samheitaordabok" "yrkordabok")
search_dirs=("${default_search_dirs[@]}")
#search_dir="fo-fo"
regex_search=false
desc_search=false
word_search=false
# Parse options
while getopts ":eEhdDsaAnr" opt; do
case $opt in
E)
search_dirs="fo-en"
unset "search_dirs[1]" # Exclude "buskaparord"
unset "search_dirs[2]" # Exclude "samheitaordabok"
unset "search_dirs[3]" # Exclude "yrkordabok"
;;
e)
search_dirs="en-fo"
unset "search_dirs[1]" # Exclude "buskaparord"
unset "search_dirs[2]" # Exclude "samheitaordabok"
unset "search_dirs[3]" # Exclude "yrkordabok"
;;
D)
search_dirs="fo-da"
unset "search_dirs[1]" # Exclude "buskaparord"
unset "search_dirs[2]" # Exclude "samheitaordabok"
unset "search_dirs[3]" # Exclude "yrkordabok"
;;
d)
search_dirs="da-fo"
unset "search_dirs[1]" # Exclude "buskaparord"
unset "search_dirs[2]" # Exclude "samheitaordabok"
unset "search_dirs[3]" # Exclude "yrkordabok"
;;
s)
regex_search=true
;;
a)
word_search=true
;;
A)
desc_search=true
;;
r)
search_dirs="ru-fo"
unset "search_dirs[1]" # Exclude "buskaparord"
unset "search_dirs[2]" # Exclude "samheitaordabok"
unset "search_dirs[3]" # Exclude "yrkordabok"
;;
n)
search_dirs="navn"
unset "search_dirs[1]" # Exclude "buskaparord"
unset "search_dirs[2]" # Exclude "samheitaordabok"
unset "search_dirs[3]" # Exclude "yrkordabok"
;;
h)
print_help
;;
\?)
echo "Invalid option: -$OPTARG"
print_help
;;
esac
done
# Shift the options to access the positional argument (SEARCH_TERM)
shift "$((OPTIND - 1))"
# Check if SEARCH_TERM is provided
if [ -z "$1" ]; then
echo "Search term not provided."
print_help
fi
# Function to print a separator line
print_separator() {
echo "--------------------------------------------------"
}
# Convert the search term to lowercase and extract the first letter
search_term="$1"
search_term_lowercase=$(to_lowercase "$search_term")
# Get the first letter from the lowercase search term
first_letter="${search_term_lowercase:0:1}"
# Search the TSV file and print the description
echo "Orðið leitað eftir: $1"
# Default search
if [ $regex_search = false ] && [ $word_search = false ] && [ $desc_search = false ] ; then
# Loop through the search directories
for search_dir in "${search_dirs[@]}"; do
description=$(grep -m 1 -i "^$search_term_lowercase" "$search_dir/${first_letter}_words_combined.tsv" | cut -f 2)
words=$(cut -f 1 "$search_dir/${first_letter}_words_combined.tsv" | grep -i "^$search_term_lowercase")
echo "------------- $search_dir -----------------"
# Split and print the description on separate lines
if echo "$description" | grep -q '[0-9]'; then
echo "$description" | awk -F'[0-9]' '{ for(i=2; i<=NF; i++) print "--------------------------------------------------\n" $i }'
else
echo "$description"
fi
print_separator
echo "Líknandi orð: $search_dir"
echo $words
print_separator
echo ""
done
fi
# Option -[edEd] -r
if [ $desc_search = false ] && [ $regex_search = true ] ; then
# If regex search option is provided, perform regex search
# Loop through the search directories
for search_dir in "${search_dirs[@]}"; do
if [ "$regex_search" = true ]; then
cut -f 1 "$search_dir/${first_letter}_words_combined.tsv" | grep -i -P "$search_term"
else
echo "Einki orð funni"
fi
done
fi
# Option -[eEdDr] -a
if [ $word_search = true ] && [ $desc_search = false ] && [ $regex_search = false ] ; then
for search_dir in "${search_dirs[@]}"; do
# Iterate through all files and perform the search
echo "------------- $search_dir -----------------"
for file in "$search_dir/"*_words_combined.tsv; do
# Search all the alphabet for the word
col_word=$(cut -f 1 $file | grep -i -P "$search_term")
if [ -n "$col_word" ]; then
print_separator
echo "$col_word"
fi
done
done
fi
# Option -[eEdDr] -A
if [ $desc_search = true ] && [ $word_search = false ] && [ $regex_search = false ] ; then
for search_dir in "${search_dirs[@]}"; do
echo "------------- $search_dir -----------------"
# Iterate through all files and perform the search
for file in "$search_dir/"*_words_combined.tsv; do
# Search within descriptions
description=$(grep -i -P "$search_term" "$file" | cut -f 1)
if [ -n "$description" ]; then
# Display extra information
if [ $search_dir = "ru-fo" ] ; then
ruDescription=$(grep -i -P "$search_term" "$file")
while IFS= read -r desc; do
print_separator
echo "$desc"
done <<< "$ruDescription"
else
print_separator
echo "$description"
fi
fi
done
done
fi