免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
楼主: lys5300
打印 上一主题 下一主题

抛砖引玉(a dictionary based for Terminal) [复制链接]

论坛徽章:
0
21 [报告]
发表于 2011-04-17 14:27 |只看该作者
匹配 \1 的地方 就是搜索的单词


另外我刚才查个几个多种形态的单词 比如 power 即是名词 也是动词 还是形容词 我写的通用匹配是没有问题的

论坛徽章:
0
22 [报告]
发表于 2011-04-17 14:28 |只看该作者
就是匹配 s/<em>\(.*\)<\/em>/\1/g 中的\1 我想让\1 高亮显示

论坛徽章:
0
23 [报告]
发表于 2011-04-17 14:36 |只看该作者
现在我在匹配 单词 的地方 给它加上了 中括号  比如 搜索hello ,在例句中我会将hello 变成[hello]

附上修改后的完整 sh


#!/bin/sh

word=$1
cache=/tmp/dictionary/$word
wordf=.file.xml.dic
if [ -z $word ];then
    echo
    echo "dir [word]"
    echo
    exit 1

fi

if [ -e $cache ];then
    cat $cache
    exit 0
elif [ ! -e ${cache%/*} ];then
   mkdir -p ${cache%/*}
fi

wget -q "http://dict.cn/ws.php?utf8=true&q=$word" -O $wordf

if grep 'sugg' $wordf 1>/dev/null 2>&1
then
    echo "YOu may want to search these words.:"
    cat $wordf | sed -e 's/<[\/]*sugg>//g' | grep '^[a-zA-Z]' #| tr ["\n"] ["\t"]
    rm $wordf
    exit 2
else
    cat $wordf|sed -e 's/<[\/]*\(def\|sent\|orig\|trans\)>//g ;s/&lt;em&gt;\(.*\)&lt;\/em&gt;/[\1]/g' |grep '^[a-zA-Z]' > $cache && cat $cache
    rm $wordf
    exit 0
fi

exit 0

论坛徽章:
0
24 [报告]
发表于 2011-04-17 14:40 |只看该作者
回复 21# chenbin200818


    没说你的有问题,只是这种写法不好,不具备通用性……


    高亮显示你怎么用的?这样试试:
  1. echo -e "\033[32;1m==$word ==\033[0m"
复制代码

论坛徽章:
0
25 [报告]
发表于 2011-04-17 14:41 |只看该作者
回复 23# chenbin200818


    看清楼主的问题,,,不要中间的缓存文件……

论坛徽章:
0
26 [报告]
发表于 2011-04-17 15:04 |只看该作者
这样可以去掉中间文件,直接赋值给 变量wordf
wordf=`wget -q "http://dict.cn/ws.php?utf8=true&q=$word" -O -`

完整的sh
#!/bin/sh
#translate between English and Chinese
if [ $# -ne 1 ];then
    echo "usage0 [word]"
    echo "lease input a word"
    exit 1
fi

word=$1
cache=/tmp/dictionary/$word

if [ -e $cache ];then
    cat $cache
    exit 0
elif [ ! -e ${cache%/*} ];then
   mkdir -p ${cache%/*}
fi

wordf=`wget -q "http://dict.cn/ws.php?utf8=true&q=$word" -O -`

if  echo "$wordf" |grep 'sugg' 1>/dev/null 2>&1
then
    echo "YOu may want to search these words.:"
    echo "$wordf" | sed -e 's/<[\/]*sugg>//g' | grep '^[a-zA-Z]' #| tr ["\n"] ["\t"]
    exit 2
else
    echo "$wordf"|sed -e 's/<[\/]*\(def\|sent\|orig\|trans\)>//g ;s/&lt;em&gt;\(.*\)&lt;\/em&gt;/[\1]/g' |grep '^[a-zA-Z]' > $cache && cat $cache
    exit 0
fi

exit 0

论坛徽章:
0
27 [报告]
发表于 2011-04-17 15:10 |只看该作者
都是弓虽人!
弱弱的问一下:大家写shell写的如此精简,到底是精简之后效率确实很高,还是show一下自己对s ...
saintdragon 发表于 2011-04-17 13:59



    老大指点的是

论坛徽章:
0
28 [报告]
发表于 2011-04-17 15:21 |只看该作者
刚才发现这个网页也可以翻译中文 所以又改了下 可以搜索中文了
真正的中英文互译 脚本
名副其实的 translate in English and Chinese

一个初级的可商用版本出来了

#!/bin/sh
#translate between English and Chinese
if [ $# -ne 1 ];then
    echo "usage0 [word]"
    echo "lease input a word"
    exit 1
fi

word=$1
cache=/tmp/dictionary/$word

if [ -e $cache ];then
    cat $cache
    exit 0
elif [ ! -e ${cache%/*} ];then
   mkdir -p ${cache%/*}
fi

wordf=`wget -q "http://dict.cn/ws.php?utf8=true&q=$word" -O -`
if  echo "$wordf" |grep 'sugg' 1>/dev/null 2>&1
then
    echo "YOu may want to search these words.:"
    echo "$wordf" | sed -e 's/<[\/]*sugg>//g' | grep '^[^<]' #| tr ["\n"] ["\t"]
    exit 2
else
    echo "$wordf"|sed -e 's/<[\/]*\(def\|sent\|orig\|trans\)>//g ;s/&lt;em&gt;\(.*\)&lt;\/em&gt;/[\1]/g' |grep '^[^<]' > $cache && cat $cache
    exit 0
fi

exit 0

论坛徽章:
0
29 [报告]
发表于 2011-04-17 17:24 |只看该作者
回复 28# chenbin200818


    呵呵,看来我理解错xiaopang了,不好意思哦。年轻人火气大。

谢谢你们的热心帮助。我把你们的方法融合进去了。并且添加了一些其它的。现在应该算是基本可以了。还写了一个makefile,将它安装到/usr/bin/下。
  1. #!/bin/bash
  2. #        This is a dictionary based for Termial.
  3. #        Its cache directory is in /tmp/dictionary/
  4. word=$1
  5. cache=/tmp/dictionary/$word
  6. if [ $# -gt 1 ] || [ -z $word ];then
  7.         echo
  8.         echo "dir [word]"
  9.         echo
  10.         exit 1
  11. fi
  12. if [ -e $cache ];then
  13.         cat $cache
  14.         exit 0
  15. elif [ ! -e ${cache%/*} ];then
  16.         mkdir -p ${cache%/*}
  17. fi

  18. #        抓取网页快照。
  19. wordf=$(wget -q "http://dict.cn/ws.php?utf8=true&q=$word" -O -)

  20. #        截取指定字符段。
  21. #        判断是否有单词匹配,如果没有给出相近的。否则就进行查询。
  22. if echo "$wordf"|grep 'sugg' 1>/dev/null 2>&1
  23. then
  24.         echo "You may want to search these words."
  25.         echo "$wordf"|sed -e 's/<[\/]*sugg>//g'|grep '^[^<]'|tr ["\n"] ["\t"]
  26.         echo
  27.         exit 2
  28. else
  29.         echo "$wordf"|sed -e 's/<[\/]*\(def\|sent\|orig\|trans\)>//g' -e 's/&lt;em&gt;\(.*\)&lt;\/em&gt;/[\1]/g' -e 's/\(&gt;\|&lt;\)/ /g'|grep '^[^<]'>$cache && cat $cache
  30.         #cache如果大于一百兆,提示用户。进行释放。
  31.         total=$(du ${cache%/*}|cut -d/ -f 1)
  32.         if [ $total -gt 102400 ];then
  33.                 echo
  34.                 echo -e "\tThe dictionary cache is beyond 100M.Maybe you can release some space."
  35.         fi
  36. fi
  37.         exit 0
复制代码

论坛徽章:
0
30 [报告]
发表于 2011-04-17 17:31 |只看该作者
这句话让我很无语……
看楼上
xiaopan3322 发表于 2011-04-17 13:27



    呵呵,理解错了。理解万岁。友谊长存。。。。。谢了阿。。呵呵
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP