最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python創(chuàng)建屬于自己的單詞詞庫 便于背單詞

 更新時(shí)間:2019年07月30日 16:03:34   作者:虛谷dr  
這篇文章主要為大家詳細(xì)介紹了python創(chuàng)建屬于自己的單詞詞庫,便于背單詞,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了python創(chuàng)建單詞詞庫的具體代碼,供大家參考,具體內(nèi)容如下

基本思路:以COCA兩萬單詞表為基礎(chǔ),用python爬取金山詞霸的單詞詞性,詞義,音頻分別存入sqllite。背單詞的時(shí)候根據(jù)需要自定義數(shù)據(jù)的選擇方式。

效果如下:

代碼寫的比較隨意,還請(qǐng)見諒。

創(chuàng)建數(shù)據(jù)庫

復(fù)制代碼 代碼如下:
cu.execute('create table test (id INTEGER PRIMARY KEY AUTOINCREMENT,dc varchar(20),cx varchar(20),cy varchar(50),mp3 varchar(50));')

完整代碼,效率不高,不過夠用了

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讀取csv文件做K-means分析詳情

    Python讀取csv文件做K-means分析詳情

    這篇文章主要介紹了Python讀取csv文件做K-means分析詳情,基于時(shí)間序列的分析2D讀取時(shí)間列和高程做一下分析。下文更多詳細(xì)介紹需要的小伙伴可以參考一下
    2022-03-03
  • python中正則表達(dá)式?: ?= ?! 的用法

    python中正則表達(dá)式?: ?= ?! 的用法

    正則表達(dá)式中使用的??:、?=?和??!?是三種不同的正則表達(dá)式語法,它們分別代表非捕獲組、正向前瞻斷言和負(fù)向前瞻斷言,下面就來介紹一下這三種的方法,感興趣的可以了解一下
    2025-04-04
  • Python GUI編程 文本彈窗的實(shí)例

    Python GUI編程 文本彈窗的實(shí)例

    今天小編就為大家分享一篇Python GUI編程 文本彈窗的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2019-06-06
  • Python 解析pymysql模塊操作數(shù)據(jù)庫的方法

    Python 解析pymysql模塊操作數(shù)據(jù)庫的方法

    這篇文章主要介紹了Python 解析pymysql模塊操作數(shù)據(jù)庫的方法,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-02-02
  • tesseract-ocr使用以及訓(xùn)練方法

    tesseract-ocr使用以及訓(xùn)練方法

    這篇文章主要介紹了tesseract-ocr使用以及訓(xùn)練方法,結(jié)合圖文形式詳細(xì)分析了tesseract-ocr基本功能、用法、樣本訓(xùn)練與糾錯(cuò)技巧,需要的朋友可以參考下
    2023-04-04
  • python中面向?qū)ο蟮淖⒁恻c(diǎn)概述總結(jié)

    python中面向?qū)ο蟮淖⒁恻c(diǎn)概述總結(jié)

    大家好,本篇文章主要講的是python中面向?qū)ο蟮淖⒁恻c(diǎn)概述總結(jié),感興趣的同學(xué)趕快來看一看吧,對(duì)你有幫助的話記得收藏一下
    2022-02-02
  • 簡(jiǎn)單介紹Python的輕便web框架Bottle

    簡(jiǎn)單介紹Python的輕便web框架Bottle

    這篇文章主要介紹了Python的輕便web框架Bottle,因其注重輕便的設(shè)計(jì),與Flask一樣,Bottle框架的人氣同樣也非常高,需要的朋友可以參考下
    2015-04-04
  • Python遺傳算法Geatpy工具箱使用介紹

    Python遺傳算法Geatpy工具箱使用介紹

    這篇文章主要為大家介紹了Python遺傳算法Geatpy工具箱使用介紹,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-09-09
  • python 遍歷字符串(含漢字)實(shí)例詳解

    python 遍歷字符串(含漢字)實(shí)例詳解

    這篇文章主要介紹了python 遍歷字符串(含漢字)實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下
    2017-04-04
  • 如何使Python中的print()語句運(yùn)行結(jié)果不換行

    如何使Python中的print()語句運(yùn)行結(jié)果不換行

    這篇文章主要介紹了如何使Python中的print()顯示當(dāng)前語句后不換行,print() 是一個(gè)常用函數(shù),但是每次,print()語句顯示后都會(huì)換行,本問我們就來節(jié)日如何使print()顯示當(dāng)前語句后不換行,需要的朋友可以參考一下
    2022-03-03

最新評(píng)論

措美县| 汨罗市| 石阡县| 拜城县| 若羌县| 佳木斯市| 静海县| 通州区| 竹山县| 时尚| 武义县| 湘潭县| 井研县| 灵武市| 东海县| 峡江县| 龙游县| 宁海县| 明星| 白银市| 屏边| 海宁市| 临澧县| 巩留县| 郧西县| 含山县| 肇源县| 商南县| 北川| 甘南县| 漳州市| 溧阳市| 穆棱市| 什邡市| 石泉县| 苍南县| 凤山县| 云浮市| 南溪县| 武鸣县| 本溪市|