From 889259af02b351c7ff7063a728f017ed89dee7dd Mon Sep 17 00:00:00 2001 From: wangyijie Date: Sat, 7 Oct 2017 23:00:22 +0800 Subject: [PATCH 1/5] add pronunciation feature --- ici/ici.py | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/ici/ici.py b/ici/ici.py index 8bd03f6..374d038 100644 --- a/ici/ici.py +++ b/ici/ici.py @@ -6,8 +6,12 @@ import sys import getopt import logging +import subprocess +from time import sleep from xml.dom import minidom from collections import namedtuple +from tempfile import NamedTemporaryFile + try: from urllib.request import urlopen except ImportError: @@ -59,12 +63,31 @@ def show(node): show(e) +def pronounce(node): + pros2play = [] + get_all_pros(node, pros2play) + subprocess.Popen("mpg123 -q "+" ".join(pros2play), shell=True) + + +def get_all_pros(node, pros2play): + if not node.hasChildNodes(): + if node.nodeType == node.TEXT_NODE and node.data != '\n': + tag_name = node.parentNode.tagName + if tag_name == 'pron': + response = urlopen(node.data) + with NamedTemporaryFile("wb",delete=False, suffix=".mp3") as f: + f.write(response.read()) + pros2play.append(f.name) + else: + for e in node.childNodes: + get_all_pros(e, pros2play) + + def main(): try: - options, args = getopt.getopt(sys.argv[1:], ["help"]) + options, args = getopt.getopt(sys.argv[1:], "p", ["help"]) except getopt.GetoptError as e: pass - match = re.findall(r'[\w.]+', " ".join(args).lower()) words = "_".join(match) response = get_response(words) @@ -72,6 +95,9 @@ def main(): return root = read_xml(response) show(root) + for option in options: + if '-p' in option: + pronounce(root) if __name__ == '__main__': From da33898b6252d5f8d2025c1ad2317bb39cf4c69c Mon Sep 17 00:00:00 2001 From: wangyijie Date: Sat, 7 Oct 2017 23:08:37 +0800 Subject: [PATCH 2/5] update README.md --- README.md | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index e81dbe7..fcfd31f 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,11 @@ pip install ici ### 使用方法: -$ ici what +$ ici [-p] what + +argument: + -p  查词的同时播放读音(需要mpg123); + [wɒt] From 841e78b1ee135beebf9612e61f8af7c97ab24aa7 Mon Sep 17 00:00:00 2001 From: wangyijie Date: Sat, 7 Oct 2017 23:33:09 +0800 Subject: [PATCH 3/5] rm useless code --- README.md | 2 -- ici/ici.py | 1 - 2 files changed, 3 deletions(-) diff --git a/README.md b/README.md index fcfd31f..74ae634 100644 --- a/README.md +++ b/README.md @@ -9,11 +9,9 @@ pip install ici ### 使用方法: $ ici [-p] what - argument: -p  查词的同时播放读音(需要mpg123); - [wɒt] [wɑ:t] diff --git a/ici/ici.py b/ici/ici.py index 374d038..8266b65 100644 --- a/ici/ici.py +++ b/ici/ici.py @@ -7,7 +7,6 @@ import getopt import logging import subprocess -from time import sleep from xml.dom import minidom from collections import namedtuple from tempfile import NamedTemporaryFile From d96860c36485def45a056e79466bb3b4abea0c5d Mon Sep 17 00:00:00 2001 From: wangyijie Date: Mon, 9 Oct 2017 23:47:14 +0800 Subject: [PATCH 4/5] add pronunciation --- ici/ici.py | 21 +++------------------ 1 file changed, 3 insertions(+), 18 deletions(-) diff --git a/ici/ici.py b/ici/ici.py index 8266b65..cfd6ee0 100644 --- a/ici/ici.py +++ b/ici/ici.py @@ -9,7 +9,6 @@ import subprocess from xml.dom import minidom from collections import namedtuple -from tempfile import NamedTemporaryFile try: from urllib.request import urlopen @@ -63,23 +62,9 @@ def show(node): def pronounce(node): - pros2play = [] - get_all_pros(node, pros2play) - subprocess.Popen("mpg123 -q "+" ".join(pros2play), shell=True) - - -def get_all_pros(node, pros2play): - if not node.hasChildNodes(): - if node.nodeType == node.TEXT_NODE and node.data != '\n': - tag_name = node.parentNode.tagName - if tag_name == 'pron': - response = urlopen(node.data) - with NamedTemporaryFile("wb",delete=False, suffix=".mp3") as f: - f.write(response.read()) - pros2play.append(f.name) - else: - for e in node.childNodes: - get_all_pros(e, pros2play) + elements = node.getElementsByTagName("pron") + pron_urls = [a.firstChild.data for a in elements] + subprocess.Popen("mpg123 -q " + " ".join(pron_urls), shell=True) def main(): From 65a16ca68a6e341edbdc6143355686c26f74d2c7 Mon Sep 17 00:00:00 2001 From: wangyijie Date: Mon, 9 Oct 2017 23:50:38 +0800 Subject: [PATCH 5/5] add blanks --- ici/ici.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ici/ici.py b/ici/ici.py index cfd6ee0..ea265da 100644 --- a/ici/ici.py +++ b/ici/ici.py @@ -9,7 +9,6 @@ import subprocess from xml.dom import minidom from collections import namedtuple - try: from urllib.request import urlopen except ImportError: @@ -72,6 +71,7 @@ def main(): options, args = getopt.getopt(sys.argv[1:], "p", ["help"]) except getopt.GetoptError as e: pass + match = re.findall(r'[\w.]+', " ".join(args).lower()) words = "_".join(match) response = get_response(words)