python-3.x 寻找一些帮助我的脚本自动标记医疗培训抽认卡

dba5bblo  于 2023-03-20  发布在  Python
关注(0)|答案(1)|浏览(124)

我正在启动一个项目,将anki(一个抽认卡应用程序)与网站集成,以使anki卡片与最新的医学文献保持同步。我首先建议使用自动标签来组织卡片(我有136,000张卡片)。因此,我写了一个脚本(对编码来说是新手),但我一直收到错误消息:

Exception has occurred: AttributeError
module 'genanki' has no attribute 'Collection'
  File "C:\Users\Doug\anki_tag_generator.py", line 30, in <module>
    collection = genanki.Collection(collection_path)
AttributeError: module 'genanki' has no attribute 'Collection'

我不知道如何修复这个问题。如果有人有任何建议,我将非常感激!我已经在这个异常上停留了大约一个星期了。下面是我到目前为止的基本代码::

import logging
import os
import re
import sys
import time
from datetime import datetime

import genanki
import requests
from bs4 import BeautifulSoup
from tqdm import tqdm

logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
logging.info('Starting execution...')

# Find the Anki path
anki_path = None
anki_dir = "Anki2"
for root, dirs, files in os.walk(os.path.expanduser("~")):
    if anki_dir in dirs:
        anki_path = os.path.join(root, anki_dir)
        break

if not anki_path:
    print("Anki directory not found!")
    sys.exit()

# Open the Anki collection
collection_path = os.path.join(anki_path, "C:\\Users\\Doug\\AppData\\Roaming\\Anki2\\Convertor\\collection.anki2")
collection = genanki.Collection(collection_path)

# Define the categories
categories = {
    'diagnosis': ['diagnosis', 'symptoms', 'signs'],
    'treatment': ['treatment', 'management', 'therapy'],
    'anatomy': ['anatomy'],
    'physiology': ['physiology'],
    'pathology': ['pathology'],
    'pharmacology': ['pharmacology', 'drugs', 'medications'],
    'microbiology': ['microbiology', 'infectious'],
    'surgery': ['surgery', 'procedures', 'operative'],
    'radiology': ['radiology', 'imaging', 'scans'],
}

# Get the current deck
deck = collection.decks.current()

# Get the deck name, model, and card types
deck_name = deck['name']
model = collection.models.current()
model_name = model['name']
fields = model['flds']

# Define the note fields
fields = [{"name": field['name']} for field in fields]

# Define the model
model = genanki.Model(
    model['id'],
    model_name,
    fields=fields,
    templates=[
        {
            "name": "Card 1",
            "qfmt": "{{" + fields[1]['name'] + "}}",
            "afmt": "{{FrontSide}}<hr id=\"answer\">{{" + fields[2]['name'] + "}}<br>{{" + fields[-1]['name'] + "}}",
        },
    ],
)

# Define the deck
deck = genanki.Deck(
    deck['id'],
    deck_name,
)

# Read in the existing cards and their IDs
existing_cards = {}
for note in collection.find_notes('deck:"' + deck_name + '"'):
    existing_cards[note.fields[0]] = note

# Scrape the content from each URL and suggest tags for each page
urls = ["https://step1.medbullets.com/", "https://step2.medbullets.com/", "https://www.onlinemeded.com/"]
with tqdm(total=len(urls), ncols=80, dynamic_ncols=True) as pbar:
    for url in urls:
        try:
            page = requests.get(url)
            soup = BeautifulSoup(page.content, 'html.parser')
        except Exception as e:
            logging.warning(f"Failed to scrape {url}: {str(e)}")
            continue

        # Extract all links from the page
        links = soup.find_all('a')

        # Extract information from the page and add tags based on that information
        tags = set()
        for category, keywords in categories.items():
            for keyword in keywords:
                if re.search(keyword, str(soup), re.IGNORECASE):
                    tags.add
(category)

    # Check if the note already exists and update the tags if it does
if url in existing_cards:
        existing_note = existing_cards[url]

        # Add the tags to the note if they don't exist
        existing_tags = existing_note.tags
        for tag in tags:
            if tag not in existing_tags:
                existing_note.tags.append(tag)
            else:
                tags.remove(tag)
        existing_note.flush()

    # Otherwise, create a new note
else:
        fields = [url, str(soup), str(tags)]
        note = genanki.Note(
            model=model,
            fields=fields,
            tags=tags,
        )
        deck.add_note(note)

# Save the deck
col.decks.save(deck)
col.save()

# Output the tags for each note
for note in deck.notes:
    logging.info(f"{note.fields[0]}: {', '.join(note.tags)}")

# Output the finish time
logging.info(f"Execution finished at: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")

到目前为止,我试过更新pip和genanki,但似乎我“没有权限”这样做,除此之外,我试过将代码改为anki包,而不是集合,但没有成功。

tcomlyy6

tcomlyy61#

您正在使用的Genanki Package不包含名为Collection的类。它似乎使用了Packages的概念。
也许您认为存在的Collection类来自Anki包,作为链接的here。但是,它似乎是一个不推荐使用的类,所以也许有一个更好的方法来实现您正在尝试的目标?

相关问题