Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the other example. #3

Open
wants to merge 46 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
dcc0393
Add a example of lookin up from yahoo dict (eng -> cht)
hwchiu Feb 24, 2017
3586f9b
Use the python script to add words
hwchiu Feb 25, 2017
f89eb6c
Since the anki only support the python3 now. Modify related function …
hwchiu Feb 25, 2017
7307598
Update README.md
hwchiu Jul 31, 2017
1d0ff13
Update README.md
hwchiu Jul 31, 2017
b583461
Workaround for calling python binary
hwchiu Aug 5, 2017
d0c8e4c
Add message about takes time.
hwchiu Aug 5, 2017
85c998c
Fix wrong count
hwchiu Aug 5, 2017
550f894
Add the config to set {Collection,Deck,File}
hwchiu Aug 5, 2017
4876f99
Update README.md
hwchiu Aug 13, 2017
ca1fabe
Add sound media to Anki collection
ex860 Aug 22, 2017
2ceedb8
Merge pull request #2 from ex860/master
hwchiu Aug 22, 2017
7b1c3b9
Add the encoding='utf-8' line in the open section.
ex860 Aug 23, 2017
106e2cb
1. Delete the Chinese part of the part of speech
ex860 Aug 23, 2017
7221dcc
Merge pull request #3 from ex860/master
hwchiu Aug 23, 2017
28a1b73
fix a small bug for counting example sentence
eronagishi Aug 27, 2017
e23a0aa
Fix a small bug for counting the example sentences correctly
ex860 Aug 28, 2017
f3197ab
Merge branch 'master' of https://github.com/ex860/addToAnki
ex860 Aug 28, 2017
5a934d2
I added the Linux Download directory for conveniency
ex860 Aug 31, 2017
48af18d
Merge pull request #5 from ex860/master
hwchiu Sep 4, 2017
cc898bd
Fix the multi-word bug
ex860 Sep 4, 2017
85f86a7
Merge branch 'master' of https://github.com/ex860/addToAnki
ex860 Sep 4, 2017
46b12fb
Fix the multi-word searching error
ex860 Sep 5, 2017
e3b6567
Fix the bug about fetching sound media and the multi-word example sen…
ex860 Sep 5, 2017
84efbc7
Merge pull request #6 from ex860/master
hwchiu Sep 5, 2017
5909e92
Fix the endline error
ex860 Sep 5, 2017
5eedf6f
Temp change
ex860 Sep 6, 2017
3bc8f7d
Fetch the furigana and the kanji but stuck at the solution of fetchin…
ex860 Sep 6, 2017
f5743db
Upload the input of JP
ex860 Sep 6, 2017
f5d9724
Now the program could add the furigana automatically!
ex860 Sep 7, 2017
5a9e304
Replace some expression from A=A+X to A+=X
ex860 Sep 7, 2017
695fe33
New th backup of the english.py
ex860 Sep 7, 2017
90ae1bf
Complete the feature which could fetch the Japanese sound media from …
ex860 Sep 7, 2017
59d04f5
Merge pull request #7 from ex860/master
hwchiu Sep 7, 2017
37e237e
The new english.py seems to work normally, so delete the backup file …
ex860 Sep 8, 2017
faa9058
Move the Download_dir to the config.json
ex860 Sep 9, 2017
dd9b226
Merge pull request #8 from ex860/master
hwchiu Sep 9, 2017
2ae8b24
Don't do anything if looking up the word is fail
hwchiu Sep 9, 2017
2467c72
Dynamically check the language of anki
hwchiu Sep 9, 2017
6caa8f2
Add a setup.py to install dependency
hwchiu Sep 9, 2017
cd02b62
Updage README.md
hwchiu Sep 9, 2017
fca8e2c
add gitignore file
hwchiu Sep 9, 2017
587f7f5
Add the empty word, wrong spelling, and the word which doesn't include
ex860 Sep 10, 2017
1da3791
Add the typo checker
ex860 Sep 10, 2017
7500aa2
Fix the typo
ex860 Sep 10, 2017
380c2db
Merge pull request #9 from ex860/master
hwchiu Sep 10, 2017
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.pyc
Binary file added __pycache__/unicode_support_checker.cpython-35.pyc
Binary file not shown.
75 changes: 75 additions & 0 deletions addToAnkiEnglish.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#!/usr/bin/env python
# This is for libanki 2.0.12
# See libanki/tests/*

import sys, re, unicode_support_checker
from anki import Collection as aopen

if __name__ != '__main__' or len(sys.argv) < 5:
sys.exit("Usage: "+sys.argv[0]+" '<collection path>' '<deck name>' '<card front>' '<card back>'")

language_setting = {}

def CheckLanguageSetting(models):
global language_setting
if models.byName('基本型(含反向的卡片)') is not None:
language_setting['Back'] = '背面'
language_setting['Front'] = '正面'
language_setting['Type'] = '基本型(含反向的卡片)'
else:
language_setting['Back'] = 'Back'
language_setting['Front'] = 'Front'
language_setting['Type'] = 'Basic (and reversed card)'

# Mung arguments
coll_file = sys.argv[1]
deck_name = sys.argv[2]
card_front = sys.argv[3]
card_back = sys.argv[4]
# todo Not even this works. It refuses to embed the newlines in the Card
card_back = re.sub(r"\\n", "\n", card_back)

# All Decks are in a single Collection
print("Get Collection/Deck '"+coll_file+"/"+deck_name+"'")
deck = aopen( coll_file );
deckId = deck.decks.id( deck_name )

# todo Not sure why a simple 'select' doesnt do the model stuff for me...
deck.decks.select( deckId )
#check languages
CheckLanguageSetting(deck.models)

# Select the adequate language of your type of card
basic_model = deck.models.byName(language_setting['Type'])
# basic_model = deck.models.byName('Basic (and reversed card)')
basic_model['did'] = deckId
deck.models.save( basic_model )
deck.models.setCurrent( basic_model )

# todo I don't see any other ways to prevent creating a new Deck
if deck.cardCount == 0:
sys.exit("ERROR: Collection/Deck '"+coll_file+"/"+deck_name+"' does not exist.")

print("Deck has "+str(deck.cardCount())+" cards")

# Build the card
# todo Using .decode('utf-8'), I no longer get 'duplicate card' errors :p
print("Make a new Card for: "+card_front)
fact = deck.newNote()
# Select the adequate language of your type of card
fact[language_setting['Front']] = card_front # fact['Front'] = card_front
fact[language_setting['Back']] = card_back # fact['Back'] = card_front

# Add Card to the Deck
try:
deck.addNote( fact )
except(Exception, e):
if hasattr(e, "data"):
sys.exit("ERROR: Could not add '"+e.data['field']+"': "+e.data['type'])
else:
sys.exit(e)

# Done.
print("Save the Deck")
deck.save()
deck.close()
14 changes: 8 additions & 6 deletions add_to_anki-2.0.12.py → addToAnkiJapanese.py
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@
deck_name = sys.argv[2]
card_front = sys.argv[3]
card_back = sys.argv[4]
card_read = sys.argv[5]
# todo Not even this works. It refuses to embed the newlines in the Card
card_back = re.sub(r"\\n", "\n", card_back)
card_read = re.sub(r"\\n", "\n", card_read)

# All Decks are in a single Collection
print("Get Collection/Deck '"+coll_file+"/"+deck_name+"'")
Expand All @@ -23,7 +24,7 @@

# todo Not sure why a simple 'select' doesnt do the model stuff for me...
deck.decks.select( deckId )
basic_model = deck.models.byName('Basic')
basic_model = deck.models.byName('Japanese (recognition&recall)')
basic_model['did'] = deckId
deck.models.save( basic_model )
deck.models.setCurrent( basic_model )
Expand All @@ -37,14 +38,15 @@
# Build the card
# todo Using .decode('utf-8'), I no longer get 'duplicate card' errors :p
print("Make a new Card for: "+card_front)
fact = deck.newNote()
fact['Front'] = card_front.decode('utf-8')
fact['Back'] = card_back.decode('utf-8')
fact = deck.newNote()
fact['Expression'] = card_front
fact['Meaning'] = card_back
fact['Reading'] = card_read

# Add Card to the Deck
try:
deck.addNote( fact )
except Exception, e:
except(Exception, e):
if hasattr(e, "data"):
sys.exit("ERROR: Could not add '"+e.data['field']+"': "+e.data['type'])
else:
Expand Down
52 changes: 0 additions & 52 deletions add_to_anki-1.2.9.py

This file was deleted.

32 changes: 32 additions & 0 deletions examples/YahooDict/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
Introdution
-----------
This program could add the Yahoo dict sound media to the anki collection automatically.
Moreover, for the more efficient learning, I separated English-part and Chinese-part to front card and back card.

Requirement
-----------
- python3
- setuptools


Install
-------
You should install following packages
- BeautifulSoup
- pip3 install bs4
- pip3 install lxml
- pip3 install decorator
- pip3 install pyaudio
- pip3 install wget

or You can type following command to install
`python setup.py install`

Content
-------
- Front Card:
- sound media
- word
- example sentences
- Back Card
- explain
10 changes: 10 additions & 0 deletions examples/YahooDict/config_E.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"profiles": [
{
"file":"input_E",
"deck":"英文",
"collection":"C:/Users/Yu-Hsien/AppData/Roaming/Anki2/YuHsien/collection.anki2",
"download_dir":"C:/Users/Yu-Hsien/AppData/Roaming/Anki2/YuHsien/collection.media/"
}
]
}
10 changes: 10 additions & 0 deletions examples/YahooDict/config_J.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"profiles": [
{
"file":"input_J",
"deck":"日文",
"collection":"C:/Users/Yu-Hsien/AppData/Roaming/Anki2/YuHsien/collection.anki2",
"download_dir":"C:/Users/Yu-Hsien/AppData/Roaming/Anki2/YuHsien/collection.media/"
}
]
}
102 changes: 102 additions & 0 deletions examples/YahooDict/english.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import urllib.request;
from urllib.parse import quote
from bs4 import BeautifulSoup
import subprocess
import platform
import datetime
import json
import wget

Anki="../../addToAnkiEnglish.py"

def look_up_from_yahoo(word, Collection, Deck, Download_dir):
# Eliminate the end of line delimiter
word = word.splitlines()[0]
wordUrl = urllib.parse.quote(word, safe='')
url="https://tw.dictionary.search.yahoo.com/search?p={}".format(wordUrl)
content = urllib.request.urlopen(url).read()
soup = BeautifulSoup(content, 'lxml')
front_word = ""
back_word = ""

# Avoid the empty string
if(word == ""):
return False

# If the spelling of word is wrong, the program will skip it
wrongSpelling = soup.find('div', class_='compText mb-15 fz-m fc-4th')
if wrongSpelling is not None:
return False

# If there is a typo, maybe the Yahoo dict will detect
checkTypo = soup.find("span", id="term").get_text()
if(checkTypo != word):
word = checkTypo

print("-------------------------------------------------------------")
print('<<'+word+'>>')
print(" ")

# Get the URL of the sound media
soup_result = soup.find('span', id='iconStyle')
if soup_result is not None:
sound = json.loads(soup_result.get_text())
# Download the sound media and store at the specific directory (%username%/collection.media) and with a specific file name (Py_%word%.mp3)
for soundCnt in range(0,len(sound['sound_url_1'])):
if(bool(sound['sound_url_1'][soundCnt]) == True):
wget.download(sound['sound_url_1'][soundCnt]["mp3"], out=Download_dir+"Py_"+word+".mp3")
front_word += "[sound:Py_"+word+".mp3]"
break

# Insert the sound media into the card
front_word += word + "<br>"

explain = soup.find('div', class_='explain')
partOfSpeech = explain.find_all('div', class_='compTitle')

# POScont => the content of part of speech
POScont = explain.find_all('ul', class_='compArticleList')

for i in range(0,len(POScont)):
cnt = 1
if(len(partOfSpeech) == 0):
POSclean = ""
else:
POSclean = '(' + partOfSpeech[i].get_text().split('.')[0] + '.)' + "<br>"
front_word += POSclean
for j in POScont[i].find_all('span', id='example', class_='example'):
front_word += str(cnt) + '. '
for k in range(0,len(j.contents)-1):
front_word += j.contents[k].string
front_word += "<br>"
cnt = cnt + 1
back_word += POSclean
for j in POScont[i].find_all('h4'):
back_word += j.get_text() + '<br>'
print("")
print('front_card={}'.format(front_word))
print('back_card={}'.format(back_word))

if 0 == len(back_word):
return False
if "Windows" == platform.system():
subprocess.run(['python', Anki, Collection, Deck, front_word, back_word])
else:
subprocess.run(['python3', Anki, Collection, Deck, front_word, back_word])
return True

count=0
with open('config_E.json', encoding='utf-8') as data_file:
data = json.load(data_file)

start_time=datetime.datetime.now().replace(microsecond=0)
for profile in data["profiles"]:
with open(profile["file"], "r", encoding='utf-8') as file:
# Get the word from each line in file
for word in file:
if look_up_from_yahoo(word, profile["collection"], profile["deck"], profile["download_dir"]) is True:
count +=1

end_time=datetime.datetime.now().replace(microsecond=0)
print("-------------------------------------------------------------")
print("Takes {} to add {} cards".format(end_time - start_time ,count))
3 changes: 3 additions & 0 deletions examples/YahooDict/input_E
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
indent
legible
engage
6 changes: 6 additions & 0 deletions examples/YahooDict/input_J
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
宝石
マッサージ
お手洗い
大蒜
ご飯
Loading