From e86c0d12e1db944ec26522ab51aaee39451bb4b6 Mon Sep 17 00:00:00 2001 From: shawyeok Date: Wed, 23 Oct 2019 00:16:00 +0800 Subject: [PATCH] support sentence translate --- README.md | 24 +++++++++++++++--------- dic.sh | 19 +++++++++++++++---- test/test.sh | 8 ++++---- test/words.txt | 4 +++- words.txt | 6 ------ 5 files changed, 37 insertions(+), 24 deletions(-) delete mode 100644 words.txt diff --git a/README.md b/README.md index 5286450..415253a 100644 --- a/README.md +++ b/README.md @@ -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. 世界你好; +网络释义: 你好世界;别来无恙;哈罗; ``` diff --git a/dic.sh b/dic.sh index 4f8a5ab..cdc9b4a 100755 --- a/dic.sh +++ b/dic.sh @@ -1,12 +1,23 @@ #!/bin/bash -word=$1 -curl -SsL "http://cn.bing.com/dict/search?q=$word" |\ +function printUsage() { + cat < +EOF +} + +if [ $# -eq 0 ]; then + printUsage + exit 1 +fi + +query="$@" +curl -sSLG "http://cn.bing.com/dict/search" --data-urlencode "q=$query" |\ grep -Eo '' |\ sed -E 's//\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"; diff --git a/test/test.sh b/test/test.sh index 24cd621..ca2587a 100644 --- a/test/test.sh +++ b/test/test.sh @@ -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 \ No newline at end of file +done < "$PRGDIR/./words.txt" diff --git a/test/words.txt b/test/words.txt index 9044703..306b250 100644 --- a/test/words.txt +++ b/test/words.txt @@ -3,4 +3,6 @@ powerful docker laptop pc -computer \ No newline at end of file +computer +rain man +shell script \ No newline at end of file diff --git a/words.txt b/words.txt deleted file mode 100644 index 9044703..0000000 --- a/words.txt +++ /dev/null @@ -1,6 +0,0 @@ -power -powerful -docker -laptop -pc -computer \ No newline at end of file