Skip to content

Commit

Permalink
support sentence translate
Browse files Browse the repository at this point in the history
  • Loading branch information
Shawyeok committed Oct 22, 2019
1 parent c343c42 commit e86c0d1
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 24 deletions.
24 changes: 15 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,19 @@ Bing dictionary(http://cn.bing.com/dict) in command line.

## Quickstart
```shell
$ git clone 'https://github.com/Shawyeok/bing-dict.git'
$ sudo ln -sf "$PWD/bing-dict/dic.sh" /usr/local/bin/dic
$ dic powerful
powerful
美['paʊrf(ə)l],英['paʊə(r)f(ə)l]

adv. 很;
adj. 有权势的;有影响力的;强有力的;力量大的;
网络释义: 强大的;有权的;有权威的;
$ git clone 'https://github.com/Shawyeok/bing-dict.git'
$ sudo ln -sf "$PWD/bing-dict/dic.sh" /usr/local/bin/dic
$ dic powerful
powerful
美['paʊrf(ə)l],英['paʊə(r)f(ə)l]

adv. 很;
adj. 有权势的;有影响力的;强有力的;力量大的;
网络释义: 强大的;有权的;有权威的;
$ dic hello world
hello world


n. 世界你好;
网络释义: 你好世界;别来无恙;哈罗;
```
19 changes: 15 additions & 4 deletions dic.sh
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
#!/bin/bash

word=$1
curl -SsL "http://cn.bing.com/dict/search?q=$word" |\
function printUsage() {
cat <<EOF
Usage: dic <word[s]>
EOF
}

if [ $# -eq 0 ]; then
printUsage
exit 1
fi

query="$@"
curl -sSLG "http://cn.bing.com/dict/search" --data-urlencode "q=$query" |\
grep -Eo '<meta name="description" content="(.+) " ?/>' |\
sed -E 's/<meta name="description" content="必应词典为您提供.+的释义,(.+)" ?\/>/\1/' |\
sed -E 's/(.*)(,)(.*)/\1 \3/' | awk -v WORD=$word '{
sed -E 's/(.*)(,)(.*)/\1 \3/' | awk -v "QUERY=$query" '{
c=0;
print WORD;
print QUERY;
for(i=1;i<=NF;i++) {
if(match($i, "^[a-z]+\\.|网络释义:$") != 0) {
if(c==0) printf "\n";
Expand Down
8 changes: 4 additions & 4 deletions test/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
PRGDIR=`dirname $0`
EXEC="../dic.sh"

for word in `cat "$PRGDIR"/"./words.txt"`; do
echo "----<< $word >>----"
sh "$PRGDIR"/"$EXEC" "$word"
while read query; do
echo "----<< $query >>----"
sh "$PRGDIR"/"$EXEC" "$query"
echo
done
done < "$PRGDIR/./words.txt"
4 changes: 3 additions & 1 deletion test/words.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@ powerful
docker
laptop
pc
computer
computer
rain man
shell script
6 changes: 0 additions & 6 deletions words.txt

This file was deleted.

0 comments on commit e86c0d1

Please sign in to comment.