Mass-replacing text based on text on a certain field.

Glob's Avatar

Glob

22 Jul, 2015 10:45 AM

I have about 25000+ notes on this deck so manually doing them is out of question.
Example:

Vocabulary:言葉
Sentence:この言葉の意味が分かりません。

Note that 言葉 is present in both fields.
Now, I'd like to mass-replace all the {{Vocabulary}} in my sentences field with {...} making it something like a pseudo-cloze.
Converting it to a cloze card is also out of question since this is a vocabulary deck.

Is it possible with regex? or maybe excel? I haven't got any ideas to be honest.

  1. Support Staff 1 Posted by Damien Elmes on 22 Jul, 2015 07:11 PM

    Damien Elmes's Avatar

    I'm afraid Anki has no such feature to do this, though you may be able to accomplish it by exporting a csv, doing the edit in a spreadsheet program, and then importing back into Anki.

  2. 2 Posted by keldi on 23 Jul, 2015 12:12 PM

    keldi's Avatar

    Hi, Glob

    Probably you can use JavaScript to do the replacement in cards, without editing notes content.
    Add this code to the Card Template (but I'm not sure it would work on all clients, I've checked it only on PC version of Anki):

    <script>
    var str = "{{Sentence}}";
    var res = str.replace("{{Vocabulary}}", "{...}");
    { document.write(res) }
    </script>
    
    Also it's quite easy to do the replacement in LibreOffice/Excel/so on:
    screenshot =SUBSTITUTE(B2;A2;"{...}") where B2 is Vocabluary and A2 is Sentence cell.
  3. 3 Posted by Glob on 31 Jul, 2015 03:51 AM

    Glob's Avatar

    Thanks for the reply.
    I love that inline javascript, however.....

    Actually, after I asked this question here, I realized that the text replacement wasn't as simple as it seems, so I asked the guys at SO (http://stackoverflow.com/questions/31585019/advanced-text-replaceme...)

    So here's a copy-paste of the tricky part
    3. Case 3: (for mixed Kana and Kanji)
    This is the most complex one. For this one, I'd like the script/solution to change only the matching strings, i.e., it will ignore what doesn't match and only replace those with found matches. What it does is it takes the longest possible match and replace accordingly.

    上げる \t 彼は荷上げた物をあみだなに上げた。
    would become

    上げる \t 彼は荷{...}た物をあみだなに{...}た。

    Note here that the first column has 上げる but the second column has 上げた because it has changed in tense (First column has る while the second one has た).

    So, Ideally the solution should take the longest string found in both columns, in this case it is 上げ, so this is the only string replaced with {...}, while it leaves .

    Another example (For Vocabulary with a leading Kana instead of Kanji)

    が増える \t 値段がが増える
    would become
    が増える \t 値段が{...}

    In this case it ignores the leading and only replaces が増える

    Or an even more complicated case:

    お金持ち \t おお金持った (only 3 matching strings)
    would become
    お金持ち \t お{...}った


    Is this possible with inline javascript? I'd like to use that js so I won't have to permanently change my deck. I reckon it's impossible to do those replacements without regex...

    Btw, I also came across another deck of mine that I'd also like to pseudo-cloze but I reckon it'd use a different script because it has a different pattern. I'm wondering if it's possible to do even more complex replacements than the ones above.

    example.

    お~になる \t {...}田中先生はもうお帰りになった・なりました。into
    お~になる \t {...}田中先生はもう{...}帰り{...}った・{...}。
    i.e., it replaces the longest match in between the tilde and replaces everything after in the sentence regardless of what the first field is.

    That's just one case, there are more tricky cases since this one is a grammar deck. However I'd like to know if it's possible beforehand because if it's possible, I'd be posting a wall of text of cases :P. Thanks again.

  4. 4 Posted by Glob on 31 Jul, 2015 07:38 AM

    Glob's Avatar

    Btw, I'm not sure why but the script works for the back template but not for the front template.
    Well, actually it doesn't work when reviewing, i.e., if it is used on the front template.
    It works on preview and on the template editor, and works when placed at the back side in all cases.
    What happens is, when reviewing only the cloze sentence is shown on the top left corner with the other fields not showing.

  5. 5 Posted by Glob on 31 Jul, 2015 08:23 AM

    Glob's Avatar

    Hm.... I've no idea why but my post before that ^ seems to have been lost in the internet.
    Anyways.....

    Thanks for the script, it's almost perfect however...
    The truth is after I posted my original post, I realized that the problem isn't as simple as it seems so I asked the guys at SO (http://stackoverflow.com/questions/31585019/advanced-text-replaceme...).
    The tricky part is Case 3

    Recently though, I've come across another deck and thought that it'd also be wonderful If I could Cloze them, but this is an even more difficult case, and I reckon inline js can't do it.

    Here's an example:
    お~になる \t {...}田中先生はもうお帰りになった・なりました。into
    お~になる \t {...}田中先生はもう{...}帰り{...}った・{...}。
    i.e., it replaces the longest match in between the tilde and replaces everything after in the sentence regardless of what the first field is.

    There are more cases but that's a general case. It's actually a grammar deck so the rules are more complex than my previous case from my vocab deck. Examples similar to the previous case are still present though.

    There are numerous Inflection rules and I thought It'd be a daunting task but luckily it hit me that JGlossator actually uses a Deinflection library which can be used here
    (http://sourceforge.net/projects/jglossator/files/JGlossator_v4.8/) \JGlossator\Dic\DeinflectionRules

    I know this might be quite difficult, I might just ask the people at SO for an answer, I'll wait for your reply though.

  6. 6 Posted by glob2 on 31 Jul, 2015 08:45 AM

    glob2's Avatar

    EDIT: I, see it's updated now, sorry about that.

  7. Support Staff 7 Posted by Damien Elmes on 31 Jul, 2015 11:02 AM

    Damien Elmes's Avatar

    Sorry, your posts were caught in the spam filter, so I had to manually approve them. I'm afraid this is a bit outside the scope of the offical support we can provide here, but a member of the community may be able to help so I'll move this to the community forum.

  8. 8 Posted by keldi on 31 Jul, 2015 01:05 PM

    keldi's Avatar

    Hi, Glob

    JavaScript can do replacement with regex, so if you are able to change whatever you need with regex, in theory you'll be able to do it directly in Anki. But I have only little knowledge of both JS and regex, so it's a good idea to ask for help at SO.

    One more example of code. Probably this solve the problem with using script at back template.

    Vocabluary: 上げる
    Sentence: 彼は荷物をあみだなに上げた。

    Front Template:

    <div id = 'fronttemplate'><div>
    <script>
    var str = "{{Sentence}}";
    var res = str.replace(/{{Vocabulary}}?/, "{...}");
    var element = document.getElementById("fronttemplate");
    element.innerHTML = res;
    </script>
    

    Back Template:

    <div id=backtemplate></div>
    <script>
    var str2 = "{{Sentence}}";
    var res2 = str2.replace(/{{Vocabulary}}?/, "{...}");
    var element = document.getElementById("backtemplate");
    element.innerHTML = res2;</script>
    <hr id=answer>
    <div>
    {{Vocabulary}}<br>
    {{Sentence}}
    </div>
    

    The expected result for back of card:

    彼は荷物をあみだなに{...}た。
     ----------------------------
    上げる
    彼は荷物をあみだなに上げた。
    

    At /{{Vocabulary}}?/ between slashes there is a regex for the case, when last symbol of Vocabulary word can change (I have no idea whether it would be useful for you or not. Just, as I've said, an example).

  9. 9 Posted by keldi on 31 Jul, 2015 08:58 PM

    keldi's Avatar

    It seems spam filter had caught my answer as well... Or may be something was wrong with my internet connection. I'll try to send comment again. Sorry if it becomes a duplicate.

    Hi, Glob

    JavaScript can do replacement with regex, so if you are able to change whatever you need with regex, in theory you'll be able to do it directly in Anki. But I have only little knowledge of both JS and regex, so it's a good idea to ask for help at SO.

    One more example of code. Probably this solve the problem with using script at back template.

    Vocabluary: 上げる
    Sentence: 彼は荷物をあみだなに上げた。

    Front Template:

    <div id = 'fronttemplate'><div>
    <script>
    var str = "{{Sentence}}";
    var res = str.replace(/{{Vocabulary}}?/, "{...}");
    var element = document.getElementById("fronttemplate");
    element.innerHTML = res;
    </script>
    

    Back Template:

    <div id=backtemplate></div>
    <script>
    var str2 = "{{Sentence}}";
    var res2 = str2.replace(/{{Vocabulary}}?/, "{...}");
    var element = document.getElementById("backtemplate");
    element.innerHTML = res2;</script>
    <hr id=answer>
    <div>
    {{Vocabulary}}<br>
    {{Sentence}}
    </div>
    

    The expected result for back of card:

    彼は荷物をあみだなに{...}た。
      ----------------------------
    上げる
    彼は荷物をあみだなに上げた。
    

    At /{{Vocabulary}}?/ between slashes there is a regex for the case, when last symbol of Vocabulary word can change (I have no idea whether it would be useful for you or not. Just, as I've said, an example).

  10. System closed this discussion on 22 Feb, 2016 11:53 PM.

Comments are currently closed for this discussion. You can start a new one.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac