-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmerge-dicts.sh
133 lines (115 loc) · 4.05 KB
/
merge-dicts.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
#!/bin/bash
#
# Dependencies:
# python3, pyglossary, mdict-utils, which
#
# Install all dependencies with:
# pip3 install mdict-utils python-lzo
# pyglossary better to be installed from a local folder with: python setup.py install (better to use my ready pyglossary zip file)
if command -v python3 >/dev/null 2>&1; then
if command -v pyglossary >/dev/null 2>&1; then
if command -v mdict >/dev/null 2>&1; then
echo -e "All dependencies are ready!\n"
else
echo "ERROR: mdict not found! Run 'pip3 install mdict-utils'!"
exit 1
fi
else
echo "ERROR: pyglossary not installed! Run 'pip3 install pyglossary'"
exit 1
fi
else
echo "ERROR: python not installed! Download and install from https://www.python.org/downloads"
exit 1
fi
read -p "How many .txt files do you want to merge? " num_files
num_files=$((num_files))
output_files=()
for ((i=1; i<=num_files; i++))
do
read -p "$i file name: " file
output_files+=("$file")
done
read -p "Output file name: " output
read -p "Do you want to sort the files (SLOW if the files are big and not needed if .mtxt)? (y/n): " choice
# Use the files in the cat command
case $choice in
y|Y) # Sort the files
cat "${output_files[@]}" | sort > "$output"
;;
n|N) # Do not sort the files
cat "${output_files[@]}" > "$output"
;;
*) # Invalid choice
echo "Invalid option. Please enter y or n."
;;
esac
src="$output"
read -p "Convert the resulted .mtxt to mdx (only if the output is .mtxt; but if not .mtxt type 'n')? (y/n): " choice3
case $choice3 in
y|Y) # Convert to mdx
printf ${src%.*} > description.html
printf ${src%.*} > title.html
mdict --title title.html --description description.html -a "${src%.*}.mtxt" "${src%.*}.mdx"
echo 'All done!'
exit 1
;;
n|N) # Do not convert to mdx
echo 'Your file is not .mtxt or dont want to convert it to mdx, or you you want to sort according to language!'
;;
*) # Invalid choice
echo "Invalid option. Please enter y or n."
;;
esac
src="$output"
read -p "Do you want to convert the merged file to .mtxt (with optional headwords sorting)? (y/n) " answer
case $answer in
y|Y) # Use Word Title option
echo 'Your file will be converted to .mtxt!'
;;
n|N) # Do not use Word Title option
echo 'Thank you for using my tool!'
exit 1
;;
*) # Invalid choice
echo "Invalid option. Please enter y or n."
;;
esac
read -p "Do you want to sort headwords before conversion to mtxt (ex. english then arabic headwords)? (y/n): " choice1
case $choice1 in
y|Y) # Sort the files
# Ask the user to input the sorting key
read -p "Enter the sorting key (e.g. en-ar, ar-en, en, ar) or press enter to use the default (en-ar): " sort_key
# If the user did not enter anything, use the default key
if [ -z "$sort_key" ]; then
sort_key="en-ar"
fi
# Use the sorting key in the Pyglossary command
pyglossary "$src" "${src%.*}1.mtxt" --write-format=OctopusMdictSource --sort --sort-key=":$sort_key"
printf ${src%.*} > description.html
printf ${src%.*} > title.html
echo 'Pyglossary sorting and conversion to mtxt done!'
;;
n|N) # Do not sort the files
pyglossary "$src" "${src%.*}1.mtxt" --write-format=OctopusMdictSource
echo 'Conversion to mtxt done without sorting!'
;;
*) # Invalid choice
echo "Invalid option. Please enter y or n."
;;
esac
read -p "Convert the resulted .mtxt file to mdx? (y/n): " choice2
src="$output"
case $choice2 in
y|Y) # Convert to mdx
mdict --title title.html --description description.html -a "${src%.*}1.mtxt" "${src%.*}.mdx"
echo 'All done!'
;;
n|N) # Do not convert to mdx
echo 'All done!'
;;
*) # Invalid choice
echo "Invalid option. Please enter y or n."
;;
esac
echo 'Thank you for using my tool!'