Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

executable file 139 lines (109 sloc) 2.375 kb
#!/bin/bash
if [[ -z $BASH ]]; then
cat >&2 <<MSG
Ndic is a bash program, and musb be run with bash.
MSG
exit 1
fi
# variables
word=''
meanings=()
is_debug=false
is_speakable=false
main () {
if [[ -z $1 ]]; then
print_help
exit 1
fi
# reset option index
OPTIND=1
# parse arguments
while getopts 'hdsc' opt; do
case $opt in
h) # help
print_help
exit 0
;;
d) # turn on debug
is_debug=true
;;
s) # speak word
is_speakable=true
;;
c) # get the clipboard
word=$(pbpaste)
echo "$word"
;;
*)
print_help
exit 1
;;
esac
done
# set word if empty
[[ -z "$word" ]] && word=${@:$OPTIND}
debug "WORD: $word"
if [[ $word ]]; then
search # search only if the word is not empty
fi
}
debug () {
if $is_debug; then
echo "$@"
fi
}
search () {
local url result
url=`get_url`
result=`curl -s "$url"`
debug "URL: $url"
print_result "$result"
speak_word
}
get_url () {
# use mobile assistant dictionary
echo "http://m.endic.naver.com/assistDic.nhn?query=${word// /%20}"
}
print_result () {
local str="$1"
local regex='<p class="ly_p">([^<]+)</p>'
debug "HTML: $str"
while [[ $str =~ $regex ]]; do
# print meaning
echo "${BASH_REMATCH[1]}"
# store meaning to array
meanings+=("${BASH_REMATCH[1]}")
# delete matched string
str=${str/"${BASH_REMATCH}"/}
done
}
speak_word () {
# If the results exists and OS is 'Darwin'(Mac), speak the word.
if [[ ${is_speakable} = true \
&& $(uname) =~ Darwin* \
&& ${#meanings[@]} > 0 ]]; then
say "${word}" 2> /dev/null # ignore if error occurred
fi
}
print_help () {
cat <<MSG
Usage: ndic [-hds] <word>
Description:
Find the meaning of <word> in English-Korean Dictionary.
(Powered by Naver)
Options:
-h Show help message
-d Turn on debug mode
-s Speak the word
-c Search by clipboard contents
Examples:
$ ndic nice
[형용사](기분) 좋은, 즐거운, 멋진
[명사]니스 ((프랑스 남동부의 피한지))
$ ndic "good thing" # use quotes if a word has spaces
[구어] 좋은 일; 좋은 착상; 행운; 경구; 진미; 사치품
$ ndic -s nice # search and speak the word
$ ndic -c # search by clipboard contents
MSG
}
main "$@"
Jump to Line
Something went wrong with that request. Please try again.