ssricardo / anki-plugins

Contains a set of addons for the open source application Anki Desktop
95 stars 20 forks source link

Diacritics in Fill the Blanks #138

Closed DrN8r closed 4 months ago

DrN8r commented 1 year ago

Hello Ricardo, i'm noob in coding and couldn't find a solution to this. The romanian language uses this diacritics (ș, ț, â, ă, î) in its alphabet which are very common in words and it would be very usefull to ignore them when typing the answer. Setting the option in config "feedback-ignore-accents": true didn't work with these characters. I also searched in the add on files in order to find something to modify, but it's over my head. If you ever have the time to take a look into this matter it will help me a lot with my work. Thank you !

ssricardo commented 12 months ago

Hey @DrN8r !

Wasn't it working for you? I just tested those chars that you mentioned, and it seems to work fine.

Just a detail: when you change the parameters for the add-on, you need to restart Anki to take effect

DrN8r commented 11 months ago

What i mean is that when i type in the answer without these chars the feedback shows incorrect answer (red).....it doesn't ignore them.

For example....the word in blank is ,, știință "... i want the feedback to say correct (green) if i type in both [ stiinta ] and [ știință ].

DrN8r commented 9 months ago

I tried other chars like é and ù, from french and i have the same problem...feedback is red if i type in e and u. Ignore accents is set on true.

DrN8r commented 5 months ago

Updated to version 24.04.01, this problem still persists. Couldn't find a way to ignore chars like ș,ț,â,ă,î in blanks.

Capture

here s an example with the char ă

DrN8r commented 5 months ago

Also tried rewriting the code in fill-blanks.js file using chatgpt, but no luck so far.

Capture

DrN8r commented 4 months ago

Finally figured it out with chat gpt, i modified the handler.py file, adding extra line of code. Feels so good!

import os import re from bs4 import BeautifulSoup import html import unicodedata

... (other classes and imports)

def normalize_text(text): """Normalize the text by removing diacritics.""" return ''.join( c for c in unicodedata.normalize('NFD', text) if unicodedata.category(c) != 'Mn' )

class TypeClozeHandler:

... (other methods)

def format_field_result(self, given: str, expected: str):
    given = given.strip()
    expected = expected.strip()

    # Normalize the given and expected answers to ignore diacritics
    normalized_given = normalize_text(given)
    normalized_expected = normalize_text(expected)

    if normalized_given == normalized_expected:
        return "<span class='cloze st-ok'>%s</span>" % html.escape(expected)
    if self.isIgnoreCase and normalized_given.lower() == normalized_expected.lower():
        return "<span class='cloze st-ok'>%s</span>" % html.escape(expected)
    return "<span class='cloze st-expected'>%s</span> <span class='cloze st-error'>(%s)</span>" % (html.escape(expected), html.escape(given))

# ... (other methods)
ssricardo commented 4 months ago

Hey, that's great! Really! Especially as I'm not having the time for it currently.

I'll try to integrate that and release a new version later. But oc, great that it works for you already!