|
|
@ -1,7 +1,7 @@ |
|
|
|
#!/usr/bin/env python3.8 |
|
|
|
# -*- coding: UTF-8 -*- |
|
|
|
""" |
|
|
|
Create description for a word to be added to the word list |
|
|
|
Create the description for a word to be added to the word list |
|
|
|
Usage: addword.py <word> |
|
|
|
""" |
|
|
|
import sys |
|
|
@ -15,57 +15,50 @@ def main(): |
|
|
|
print(__doc__) |
|
|
|
sys.exit(1) |
|
|
|
word = sys.argv[1] |
|
|
|
prons = get_pronunciation_files(word) |
|
|
|
phones = get_phonetic_transcriptions(word) |
|
|
|
britsh_eng = '[🔊]('+prons[0]+')' + ' ' + phones[0] |
|
|
|
american_eng = '[🔊]('+prons[1]+')' + ' ' + phones[1] |
|
|
|
pronunciations = get_pronunciations(word) |
|
|
|
britsh_eng = '[🔊](' + pronunciations[0][0] +')' + ' ' + pronunciations[0][1] |
|
|
|
american_eng = '[🔊](' + pronunciations[1][0] +')' + ' ' + pronunciations[1][1] |
|
|
|
line = '| ' + word + ' | ' + britsh_eng + ' | ' + american_eng + ' | ' + ' ' + '|' |
|
|
|
print(line) |
|
|
|
|
|
|
|
def get_pronunciation_files(word): |
|
|
|
"""Return the word's prounciation files from youdao.com if available |
|
|
|
"""Return the word's pronunciation files from youdao.com if available |
|
|
|
(British English and American English) as a list""" |
|
|
|
word = word.strip() |
|
|
|
prons = [] |
|
|
|
prons = [" ", " "] |
|
|
|
url = "http://dict.youdao.com/dictvoice?audio="+word+"&type=1" |
|
|
|
try: |
|
|
|
urllib.request.urlopen(url).read() |
|
|
|
prons.append(url) |
|
|
|
except urllib.error.URLError: |
|
|
|
urllib.request.urlopen(url) |
|
|
|
prons[0] = url |
|
|
|
urllib.request.urlopen(url) |
|
|
|
prons[1] = url |
|
|
|
except: |
|
|
|
return prons |
|
|
|
|
|
|
|
url = re.sub("type=1", "type=2", url) |
|
|
|
try: |
|
|
|
urllib.request.urlopen(url).read() |
|
|
|
prons.append(url) |
|
|
|
return prons |
|
|
|
except urllib.error.URLError: |
|
|
|
return prons |
|
|
|
|
|
|
|
def get_phonetic_transcriptions(word): |
|
|
|
"""Return the word's phonetic transcriptions from youdao.com if available |
|
|
|
British English and American English as a list""" |
|
|
|
def get_pronunciations(word): |
|
|
|
"""Return the word's pronouciation URLs and phonetic transcriptions |
|
|
|
from youdao.com if available""" |
|
|
|
word = word.strip() |
|
|
|
url = "http://dict.youdao.com/w/eng/"+word |
|
|
|
phones = [] |
|
|
|
word_url = "http://dict.youdao.com/w/eng/"+word |
|
|
|
britsh_eng = [" ", "/ /"] |
|
|
|
american_eng = [" ","/ /"] |
|
|
|
try: |
|
|
|
response = urllib.request.urlopen(url).read() |
|
|
|
except urllib.error.URLError: |
|
|
|
return phones |
|
|
|
response = urllib.request.urlopen(word_url).read() |
|
|
|
soup = BeautifulSoup(response, "html.parser") |
|
|
|
spans = soup.find_all('span', {'class' : 'pronounce'}) |
|
|
|
lines = [span.get_text() for span in spans] |
|
|
|
match = re.findall(r'\[.+\]', lines[0]) |
|
|
|
if match: |
|
|
|
phones.append(match[0].replace('[', '/').replace(']', '/')) |
|
|
|
else: |
|
|
|
phones.append(" ") |
|
|
|
britsh_eng[0] = "http://dict.youdao.com/dictvoice?audio="+word+"&type=1" |
|
|
|
britsh_eng[1] = match[0].replace('[', '/').replace(']', '/') |
|
|
|
match = re.findall(r'\[.+\]', lines[1]) |
|
|
|
if match: |
|
|
|
phones.append(match[0].replace('[', '/').replace(']', '/')) |
|
|
|
else: |
|
|
|
phones.append(" ") |
|
|
|
return phones |
|
|
|
american_eng[0] = "http://dict.youdao.com/dictvoice?audio="+word+"&type=2" |
|
|
|
american_eng[1] = match[0].replace('[', '/').replace(']', '/') |
|
|
|
except: |
|
|
|
return britsh_eng, american_eng |
|
|
|
|
|
|
|
return britsh_eng, american_eng |
|
|
|
|
|
|
|
if __name__ == '__main__': |
|
|
|
main() |
|
|
|