kaegi / MorphMan

Anki plugin that reorders language cards based on the words you know
Other
260 stars 66 forks source link

TypeError: list indices must be integers or slices, not strings when running highlight hook #168

Closed edoreld closed 3 years ago

edoreld commented 3 years ago

I got an error whenever I open a deck that has a card with the morphHighlight field modifier.

Errortrace:

Error
An error occurred. Please start Anki while holding down the shift key, which will temporarily disable the add-ons you have installed.
If the issue only occurs when add-ons are enabled, please use the Tools > Add-ons menu item to disable some add-ons and restart Anki, repeating until you discover the add-on that is causing the problem.
When you've discovered the add-on that is causing the problem, please report the issue on the add-on support site.
Debug info:
Anki 2.1.34 (8af8f565) Python 3.8.0 Qt 5.15.1 PyQt 5.15.1
Platform: Mac 10.15.7
Flags: frz=True ao=True sv=2
Add-ons, last update check: 2020-09-29 20:40:45
Add-ons possibly involved: ⁨MorphMan for Anki 21⁩

Caught exception:
Traceback (most recent call last):
  File "aqt/reviewer.py", line 298, in <lambda>
  File "</Applications/Anki.app/Contents/MacOS/decorator.pyc:decorator-gen-74>", line 2, in _answerCard
  File "anki/hooks.py", line 638, in decorator_wrapper
  File "anki/hooks.py", line 633, in repl
  File "</Applications/Anki.app/Contents/MacOS/decorator.pyc:decorator-gen-42>", line 2, in _answerCard
  File "anki/hooks.py", line 638, in decorator_wrapper
  File "anki/hooks.py", line 633, in repl
  File "aqt/reviewer.py", line 269, in _answerCard
  File "aqt/reviewer.py", line 118, in nextCard
  File "aqt/reviewer.py", line 183, in _showQuestion
  File "anki/cards.py", line 113, in question
  File "anki/cards.py", line 133, in render_output
  File "anki/template.py", line 217, in render
  File "anki/template.py", line 306, in apply_custom_filters
  File "anki/hooks.py", line 594, in runFilter
  File "/Users/jmartinezlago/Library/Application Support/Anki2/addons21/900801631/morph/newMorphHelper.py", line 248, in highlight
    tags = fieldDict['Tags'].split()
TypeError: list indices must be integers or slices, not str

The function where the error happens (the line contains tags = fieldDict['Tags'].split())

########## 4 - highlight morphemes using morphHighlight
def highlight(txt, extra, fieldDict, field, mod_field):
    """When a field is marked with the 'focusMorph' command, we format it by
    wrapping all the morphemes in <span>s with attributes set to its maturity"""
    from .util import getFilterByTagsAndType
    from .morphemizer import getMorphemizerByName
    from .morphemes import getMorphemes

    # must avoid formatting a smaller morph that is contained in a bigger morph
    # => do largest subs first and don't sub anything already in <span>
    def nonSpanSub(sub, repl, string):
        return ''.join(re.sub(sub, repl, s, flags=re.IGNORECASE) if not s.startswith('<span') else s for s in
                       re.split("(<span.*?</span>)", string))

    frequency_list_path = cfg('path_frequency')
    try:
        with codecs.open(frequency_list_path, encoding='utf-8') as f:
            frequency_list = [line.strip().split('\t')[0] for line in f.readlines()]
    except:
        frequency_list = []

    priority_db = main.MorphDb(cfg('path_priority'), ignoreErrors=True).db
    tags = fieldDict['Tags'].split() ### <- ERROR

    filter = getFilterByTagsAndType(fieldDict['Type'], tags)
    if filter is None:
        return txt
    morphemizer = getMorphemizerByName(filter['Morphemizer'])
    if morphemizer is None:
        return txt

    ms = getMorphemes(morphemizer, txt, tags)

    proper_nouns_known = cfg('Option_ProperNounsAlreadyKnown')

    for m in sorted(ms, key=lambda x: len(x.inflected), reverse=True):  # largest subs first
        locs = allDb().getMatchingLocs(m)
        mat = max(loc.maturity for loc in locs) if locs else 0

        if proper_nouns_known and m.isProperNoun():
            mtype = 'mature'
        elif mat >= cfg('threshold_mature'):
            mtype = 'mature'
        elif mat >= cfg('threshold_known'):
            mtype = 'known'
        elif mat >= cfg('threshold_seen'):
            mtype = 'seen'
        else:
            mtype = 'unknown'

        priority = 'true' if m in priority_db else 'false'

        focus_morph_string = m.show().split()[0]
        frequency = 'true' if focus_morph_string in frequency_list else 'false'

        repl = '<span class="morphHighlight" mtype="{mtype}" priority="{priority}" frequency="{frequency}" mat="{mat}">\\1</span>'.format(
            mtype=mtype,
            priority=priority,
            frequency=frequency,
            mat=mat
        )
        txt = nonSpanSub('(%s)' % m.inflected, repl, txt)
    return txt
ohare93 commented 3 years ago

Same. Hoping for a fix.

ianki commented 3 years ago

I think this is fixed on the latest beta.

Please try MorphMan Beta 20201121 for Anki 2.1.35