python創(chuàng)建屬于自己的單詞詞庫 便于背單詞
本文實(shí)例為大家分享了python創(chuàng)建單詞詞庫的具體代碼,供大家參考,具體內(nèi)容如下
基本思路:以COCA兩萬單詞表為基礎(chǔ),用python爬取金山詞霸的單詞詞性,詞義,音頻分別存入sqllite。背單詞的時(shí)候根據(jù)需要自定義數(shù)據(jù)的選擇方式。
效果如下:
代碼寫的比較隨意,還請(qǐng)見諒。
創(chuàng)建數(shù)據(jù)庫
完整代碼,效率不高,不過夠用了
import requests from bs4 import BeautifulSoup import re import traceback import sqlite3 import time import sys def ycl(word): try: url = "http://www.iciba.com/{}".format(word) headers = { 'Host': 'www.iciba.com', 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:43.0) Gecko/20100101 Firefox/43.0', 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', 'Accept-Language': 'zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3', 'Accept-Encoding': 'gzip, deflate', 'Referer': 'http://www.baidu.com', 'Connection': 'keep-alive', 'Cache-Control': 'max-age=0', } response = requests.get(url = url,headers = headers) soup = BeautifulSoup(response.text,"lxml") #輸出單詞詞性 cx = soup.find(class_='base-list switch_part')(class_='prop') #輸出詞性詞義 mp3 = soup.find_all(class_='new-speak-step')[1] pattern = re.compile(r'http://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+.mp3') mp3url = re.findall(pattern,mp3['ms-on-mouseover']) mp3url = '.'.join(mp3url) r = requests.get(mp3url) #單詞音頻輸出路徑 dress = "E:\\sound\\" mp3path = dress +word+".mp3" with open(mp3path, 'wb') as f: f.write(r.content) #獲取詞性個(gè)數(shù) meanings =soup.find_all(class_='prop') #實(shí)行每個(gè)詞性的詞義同行輸出 for i in range(len(meanings)): s = soup.find(class_='base-list switch_part')('li')[i]('span') c = cx[i].text a = '' for x in range(len(s)): b = s[x].text a = a + b print(word) print(c) print(a) # 存入數(shù)據(jù)庫的方法 conn = sqlite3.connect("word.db") cu = conn.cursor() sql =cu.execute("INSERT INTO test (id,dc,cx,cy,mp3)VALUES(NULL,'%s','%s','%s','%s');"%(word,c,a,mp3path)) print(sql) conn.commit() print('\n') except Exception as e: print(e) print("error") with open("log.txt",'a') as f: f.write(word+'\n') def duqudanci(file): wordcount = 0 for line in open(file): word = line.strip('\n') wordcount += 1 print(wordcount) ycl(word) if __name__ == '__main__': conn = sqlite3.connect("word.db") cu = conn.cursor() word = "" #需要爬取的單詞 duqudanci(sys.argv[1]) print('下載完成') conn.commit() conn.close()
自定義背單詞: 根據(jù)需要可以將單詞放入txt文件中進(jìn)行測(cè)試,可以輸出詞義拼寫單詞,也可以輸出單詞,選擇對(duì)應(yīng)釋義。 當(dāng)然還可以給每個(gè)單詞詞義加兩個(gè)屬性值,分別表示學(xué)習(xí)次數(shù)和答錯(cuò)次數(shù),然后可以根據(jù)這兩個(gè)值來選擇單詞,如果有興趣的話,可以嘗試一下。
import sqlite3 import random import sys from playsound import playsound # 中譯英 def CtoE(): for j in list1: sql =cu.execute('select id,dc,cx,cy,mp3 from wordinfo where id = ?',(j,)) for it in sql: # 返回的是元組,直接對(duì)元組查詢 c=0 while c<3: print("當(dāng)前單詞ID = "+str(it[0])) print("釋義:"+it[3]) # 播放音頻 playsound(it[4]) a = input("請(qǐng)拼寫單詞,共有三次機(jī)會(huì):") if a == it[1]: print("拼寫正確") break; c += 1 print('第%d次拼寫錯(cuò)誤'%c) print('\n') print("下一個(gè)") print('\n') # 英譯中 def EtoC(): for j in list1: sql =cu.execute('select id,dc,cx,cy,mp3 from wordinfo where id = ?',(j,)) d =0 for it in sql: # 返回的是元組,直接對(duì)元組查詢 c=0 while c<3: # 釋放list2 list2 = [] sql =cu.execute('select cy from wordinfo where id !=? order by random() limit 3',(j,)) for t in sql: for o in range(len(t)): #將隨機(jī)取出的數(shù)據(jù)放入列表 list2.append(t[o]) # 加入正確答案 p = random.randint(0,3) list2.insert(p,it[3]) print("當(dāng)前單詞ID = "+str(it[0])) print("選擇單詞的對(duì)應(yīng)釋義:----"+it[1]) playsound(it[4]) dict1 = {'A':list2[0],'B':list2[1],'C':list2[2],'D':list2[3]} print("A:"+dict1.get('A')+'\n') print("B:"+dict1.get('B')+'\n') print("C:"+dict1.get('C')+'\n') print("D:"+dict1.get('D')+'\n') answer1 = input("請(qǐng)選擇,共有三次機(jī)會(huì)(大寫):") if dict1.get(answer1)== it[3]: print("正確") break; c += 1 print('第%d次拼寫錯(cuò)誤'%c) d += 1 print('\n') print("下一個(gè)") print('\n') def main(file): for line in open(file): word = line.strip('\n') sql =cu.execute('select id from wordinfo where dc = ?',(word,)) for x in sql: list1.append(x[0]) cho = input("英譯中請(qǐng)選1,中譯英請(qǐng)選2:") if cho =="1": EtoC() elif cho =="2": CtoE() else: print("錯(cuò)誤,請(qǐng)重試") if __name__ == '__main__': conn = sqlite3.connect("word.db") cu = conn.cursor() list1 = [] word = "" main(sys.argv[1]) conn.commit() conn.close()
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
使用Python第三方庫pygame寫個(gè)貪吃蛇小游戲
這篇文章主要介紹了使用Python第三方庫pygame寫個(gè)貪吃蛇小游戲,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-03-03基于tensorflow for循環(huán) while循環(huán)案例
這篇文章主要介紹了基于tensorflow for循環(huán) while循環(huán)案例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-06-06python區(qū)塊鏈簡(jiǎn)易版交易實(shí)現(xiàn)示例
這篇文章主要為大家介紹了python區(qū)塊鏈簡(jiǎn)易版交易實(shí)現(xiàn)示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05基于python實(shí)現(xiàn)在excel中讀取與生成隨機(jī)數(shù)寫入excel中
最近接個(gè)項(xiàng)目,項(xiàng)目要求是這樣的:在一份已知的excel表格中讀取學(xué)生的學(xué)號(hào)與姓名,再將這些數(shù)據(jù)放到新的excel表中的第一列與第二列,最后再生成隨機(jī)數(shù)作為學(xué)生的考試成績(jī),具體實(shí)現(xiàn)代碼大家參考下本文2018-01-01Pycharm中出現(xiàn)ImportError:DLL load failed:找不到指定模塊的解決方法
這篇文章主要介紹了Pycharm中出現(xiàn)ImportError:DLL load failed:找不到指定模塊的解決方法,需要的朋友可以參考下2019-09-09python下載安裝requests庫的簡(jiǎn)單步驟
這篇文章主要給大家介紹了關(guān)于python下載安裝requests庫的簡(jiǎn)單步驟,使用Python的requests庫下載文件是一種常見的操作,文中通過圖文介紹的非常詳細(xì),需要的朋友可以參考下2023-11-11Python實(shí)現(xiàn)讀寫INI配置文件的方法示例
這篇文章主要介紹了Python實(shí)現(xiàn)讀寫INI配置文件的方法,結(jié)合實(shí)例形式分析了Python針對(duì)ini配置文件的讀寫操作類定義及使用方法,需要的朋友可以參考下2018-06-06Python?設(shè)計(jì)模式創(chuàng)建型單例模式
這篇文章主要介紹了Python?設(shè)計(jì)模式創(chuàng)建型單例模式,即Singleton,單例是一種設(shè)計(jì)模式,應(yīng)用該模式的類只會(huì)生成一個(gè)實(shí)例,下文詳細(xì)介紹需要的小伙伴可以參考一下2022-02-02Linux添加Python?path方法及修改環(huán)境變量的三種方法
這篇文章主要介紹了Linux添加Python?path方法及修改環(huán)境變量的三種方法,Linux 下設(shè)置環(huán)境變量有三種方法,一種用于當(dāng)前終端,一種用于當(dāng)前用戶,一種用于所有用戶,本文對(duì)每種方法給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-07-07