Mass-replacing text based on text on a certain field.
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.
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
Support Staff 1 Posted by Damien Elmes on 22 Jul, 2015 07:11 PM
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 Posted by keldi on 23 Jul, 2015 12:12 PM
Hi, Glob
Probably you can use JavaScript to do the replacement in cards, without editing notes content.
Also it's quite easy to do the replacement in LibreOffice/Excel/so on: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):
=SUBSTITUTE(B2;A2;"{...}")whereB2is Vocabluary andA2is Sentence cell.3 Posted by Glob on 31 Jul, 2015 03:51 AM
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 Posted by Glob on 31 Jul, 2015 07:38 AM
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 Posted by Glob on 31 Jul, 2015 08:23 AM
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\DeinflectionRulesI 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 Posted by glob2 on 31 Jul, 2015 08:45 AM
EDIT: I, see it's updated now, sorry about that.
Support Staff 7 Posted by Damien Elmes on 31 Jul, 2015 11:02 AM
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 Posted by keldi on 31 Jul, 2015 01:05 PM
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:
Back Template:
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 Posted by keldi on 31 Jul, 2015 08:58 PM
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:
Back Template:
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).System closed this discussion on 22 Feb, 2016 11:53 PM.