r/Anki•
Sayonaroo

Is there a way to modify the cloze plugin code to cloze like this??

Solved

or if someone could write an anki plugin that does that i'd be grateful!

https://ankiweb.net/shared/info/317584266

https://ankiweb.net/shared/info/96220182

I want to create shortcut to create a a separate cloze for each syllable like if I were to select "19" in the example below and press the shortcut it generates

Canberra was founded in 1913.

Canberra was founded in {{c1::1}}{{c2::9}}13.

and I also wanted another shortcut for creating 3 clozes so that if I were to select "191" in the same example and press the shortcut it generates

Canberra was founded in {{c1::1}}{{c2::9}}{{c3::1}}3.

Posted Aug 1, 2018, 2:03 AM UTC

14
ijgnord•

I'm curious: What do you need this for: For what area of knowledge, what kind of cards? I assume your example with years is just an example ...


I'm not sure what you want: The examples you give you get one cloze for each character. Splitting a string after each character is not too hard so this should be doable.

But in your description you ask for splitting by "each syllable". That's much more complicated because the computer doesn't know what a syllable = a combination of letters is. There are external libraries that do this quite well but I'm not sure how this works in different, especially asian languages. How exactly do you want to have it split.

Why do you mention the space bar cloze addon. Just to tell us that you prefer ctrl+space over ctrl+shift+c or does this addon any additional functions.


soon a major new version named 2.1 will be released. Addons for the current version won't work. Many addons have already been ported but not all. So you could say if you need it for 2.0 or 2.1.

1
SayonarooOP•

This is for language learning. I want a cloze for each character for the language I'm learning. One character is one syllable for the language I'm learning

I mentioned those ones because they make clozing easier with the shortcut and in case it's easy to modify the code to do what I want

1
ijgnord•

I modified an existing addon. For me it works in 2.0.52. I'm not a programmer so use it at your own risk.

Important rule: Make sure to have a proper backups (like in the 3-2-1 rule) and you only know that a backup works if you have tried to restore it. So try to restore some data.

Put an empty file in your addons folder with the extension ".py" (like cloze_each_char.py) and put the follwowing code inside this file. Then restart Anki.

I used F2 for multiple clozes and F4 to not increment

# -*- coding: utf-8 -*-
# License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html

from anki.hooks import wrap
from aqt.editor import Editor
from anki.utils import json
import re

CtrlSpace = 'Ctrl+Space'
CtrlAltSpace = 'Ctrl+Alt+Space'

def cloze_each_char(self,state="increm"):
    text=self.web.selectedText()
    new = ''
    number = 0
    for name, val in self.note.items():
        m = re.findall("\{\{c(\d+)::", val)
        if m:
            number = max(number, sorted([int(x) for x in m])[-1])
    if state != 'increm' and number == 0:
        number = 1
    for c in text:
        if state == "increm":
            number += 1
        new += "{{c" + str(number) + "::" + c + "}}"    
    self.web.eval("document.execCommand('inserthtml', false, %s);" 
            %json.dumps(new))

def setupButtons(self):
    self._addButton("cloze_each_char", lambda s=self: cloze_each_char(self,state="increm"), 
                    text=u"cli", tip="cloze each character (F2)", key="F2")
    self._addButton("cloze_each_char", lambda s=self: cloze_each_char(self,state="fixed"), 
                    text=u"clf", tip="cloze each character fixed (F4)", key="F4")

Editor.cloze_each_char = cloze_each_char
Editor.setupButtons = wrap(Editor.setupButtons, setupButtons) 

With Ctrl+Space this didn't work. So I modified a different addon. This seems to work for me.

# -*- mode: Python ; coding: utf-8 -*-
# License: GNU GPL, version 3 or later; http://www.gnu.org/copyleft/gpl.html
# Modification of addon "space bar cloze", ID 317584266,  (c) 2016 Dmitry Mikheev


from aqt.utils import tooltip, showInfo
from anki.hooks import wrap
import aqt.editor
from aqt.editor import *

CtrlSpace = 'Ctrl+Space'
CtrlAltSpace = 'Ctrl+Alt+Space'


def cloze_each_char(self,state="increm"):
    text=self.web.selectedText()
    new = ''
    number = 0
    for name, val in self.note.items():
        m = re.findall("\{\{c(\d+)::", val)
        if m:
            number = max(number, sorted([int(x) for x in m])[-1])
    if state != 'increm' and number == 0:
        number = 1
    for c in text:
        if state == "increm":
            number += 1
        new += "{{c" + str(number) + "::" + c + "}}"    
    self.web.eval("document.execCommand('inserthtml', false, %s);" 
            %json.dumps(new))


def setupButtonz(self):
    s = QShortcut(
        QKeySequence(CtrlSpace), self.parentWindow)
    s.connect(s, SIGNAL('activated()'), lambda: cloze_each_char(self, state="increm"))

    s = QShortcut(
        QKeySequence(CtrlAltSpace), self.parentWindow)
    s.connect(s, SIGNAL('activated()'), lambda: cloze_each_char(self, state="fixed"))

aqt.editor.Editor.setupButtons = wrap(
    aqt.editor.Editor.setupButtons, setupButtonz)

You could report if these work for you and if not what error message you get.

1
SayonarooOP•

thanks for working on this for me. F2 works beautifully for me! :D

1