Fast Anki Cloze Deletion Cards

Ask specific questions about your target languages. Beginner questions welcome!
User avatar
rdearman
Site Admin
Posts: 7259
Joined: Thu May 14, 2015 4:18 pm
Location: United Kingdom
Languages: English (N)
Language Log: viewtopic.php?f=15&t=1836
x 23308
Contact:

Fast Anki Cloze Deletion Cards

Postby rdearman » Mon Jun 17, 2019 1:36 pm

I just wanted to share a script I made for generating tons of cloze deletion cards with a perl script. You need a text file with each sentence on a new line then just run the script below on the file. You just feed it the sentence file as the first argument and the output csv file you want to create as the second argument. If you want to know how to split a document on punctuation I covered that previously using libreoffice.

Code: Select all

#!/usr/bin/perl
use strict;
use warnings;

my $filename   = $ARGV[0];
my $outfile = $ARGV[1];

# my $filename = "psicologia_italian2.txt";
# my $outfile = "cloze_it.csv";
open(my $fh,  '<:encoding(UTF-8)', $filename) ;
open(my $ofh, '>:encoding(UTF-8)', $outfile);

while (my $row = <$fh>)
{
    my $wordcount;
    chomp $row;
    my @words =  $row =~ /\w+/g ;
    $wordcount = scalar( @words );
    for (my $a=0;$a<$wordcount;$a++)
    {
        print $ofh "{{c" . $a . "::" . $words[$a] . "}} " ;
    }
    print $ofh "\n";
}
print "done\n";


This will cloze delete every word in the file. When you import the file into anki make sure you specify a tab delimited file, or it will not work.
7 x
: 26 / 150 Read 150 books in 2024

My YouTube Channel
The Autodidactic Podcast
My Author's Newsletter

I post on this forum with mobile devices, so excuse short msgs and typos.

User avatar
cjareck
Brown Belt
Posts: 1047
Joined: Tue Apr 25, 2017 6:11 pm
Location: Poland
Languages: Polish (N) English, German, Russian(B1?) French (B1?), Hebrew(B1?), Arabic(A2?), Mandarin (HSK 2)
Language Log: https://forum.language-learners.org/vie ... =15&t=8589
x 2979
Contact:

Re: Fast Anki Cloze Deletion Cards

Postby cjareck » Mon Jun 17, 2019 8:03 pm

This script, unfortunately, will not help me solve the most complicated activity by creating cloze cards - choosing of appropriate pictures to prompt meaning of the hidden word ;)
1 x
Please feel free to correct me in any language


Listening: 1+ (83% content, 90% linguistic)
Reading: 1 (83% content, 90% linguistic)


MSA DLI : 30 / 141ESKK : 18 / 40


Mandarin Assimil : 62 / 105

User avatar
rdearman
Site Admin
Posts: 7259
Joined: Thu May 14, 2015 4:18 pm
Location: United Kingdom
Languages: English (N)
Language Log: viewtopic.php?f=15&t=1836
x 23308
Contact:

Re: Fast Anki Cloze Deletion Cards

Postby rdearman » Tue Jun 18, 2019 6:10 am

Hashimi wrote:
rdearman wrote:This will cloze delete every word in the file.


Can you show us an example?

Do you mean that if we put one sentence (for example, "This will cloze delete every word in the file."), it will generate these 9 sentences?

____ will cloze delete every word in the file.
This ____ cloze delete every word in the file.
This will ____ delete every word in the file.
This will cloze ____ every word in the file.
This will cloze delete ____ word in the file.
This will cloze delete every ____ in the file.
This will cloze delete every word ____ the file.
This will cloze delete every word in ____ file.
This will cloze delete every word in the ____ .

Exactly that.
0 x
: 26 / 150 Read 150 books in 2024

My YouTube Channel
The Autodidactic Podcast
My Author's Newsletter

I post on this forum with mobile devices, so excuse short msgs and typos.

User avatar
Adrianslont
Blue Belt
Posts: 827
Joined: Sun Aug 16, 2015 10:39 am
Location: Australia
Languages: English (N), Learning Indonesian and French
x 1936

Re: Fast Anki Cloze Deletion Cards

Postby Adrianslont » Tue Jun 18, 2019 7:20 am

rdearman wrote:I just wanted to share a script I made for generating tons of cloze deletion cards with a perl script. You need a text file with each sentence on a new line then just run the script below on the file. You just feed it the sentence file as the first argument and the output csv file you want to create as the second argument. If you want to know how to split a document on punctuation I covered that previously using libreoffice.

Code: Select all

#!/usr/bin/perl
use strict;
use warnings;

my $filename   = $ARGV[0];
my $outfile = $ARGV[1];

# my $filename = "psicologia_italian2.txt";
# my $outfile = "cloze_it.csv";
open(my $fh,  '<:encoding(UTF-8)', $filename) ;
open(my $ofh, '>:encoding(UTF-8)', $outfile);

while (my $row = <$fh>)
{
    my $wordcount;
    chomp $row;
    my @words =  $row =~ /\w+/g ;
    $wordcount = scalar( @words );
    for (my $a=0;$a<$wordcount;$a++)
    {
        print $ofh "{{c" . $a . "::" . $words[$a] . "}} " ;
    }
    print $ofh "\n";
}
print "done\n";


This will cloze delete every word in the file. When you import the file into anki make sure you specify a tab delimited file, or it will not work.


Rdearman , I am very interested in this but I am an A0 at running scripts. “Just run the script below” means close to zero to me. This has happened a few times on the forum but this script interests me enough to shyly ask how to do that.

I’m not bad with using software (not clueless) but programming and scripts are a black hole for me.

Would you be so kind as to give me a quick guide? I guess it involves the command line thingy invoked from the windows button. I imagine I need to have my text file in the right place. Do I need to install anything - something Called Perl maybe?

I did have a quick look around YouTube for guidance but got lost quickly.

If you could give me a hand without too much trouble or point me in a direction where I could self teach I would be grateful.
1 x

User avatar
rdearman
Site Admin
Posts: 7259
Joined: Thu May 14, 2015 4:18 pm
Location: United Kingdom
Languages: English (N)
Language Log: viewtopic.php?f=15&t=1836
x 23308
Contact:

Re: Fast Anki Cloze Deletion Cards

Postby rdearman » Tue Jun 18, 2019 7:51 am

Windoze... sigh. OK. It is still fairly simple, so I'll try to outline it quickly.

  • Save the script into a file, you can name it whatever you want, I named it cloze.pl for my example.
  • Put the script and the file you want to transform together in a folder.
  • Install Perl on windows by going to strawberryperl.com and downloading Strawberry Perl. Either 64 or 32 bit depending on your computer.
  • After it is installed you'll find an option in your start menu called "Perl (Command line)" which you click and a cmd box appears.
  • Navigate to the folder with the two files and run the command:

    Code: Select all

    perl cloze.pl input.txt output.csv
  • Then import the output.csv file into anki. Change "Fields separated by colon" to "Fields separated by tabs" and it should suck in all the cards.

If you have any problems just post them here, because if you have the problem someone else might too. I didn't have perl on my windows machine, so I wrote out the steps above as I did it myself, so they should work for you.
5 x
: 26 / 150 Read 150 books in 2024

My YouTube Channel
The Autodidactic Podcast
My Author's Newsletter

I post on this forum with mobile devices, so excuse short msgs and typos.

User avatar
Adrianslont
Blue Belt
Posts: 827
Joined: Sun Aug 16, 2015 10:39 am
Location: Australia
Languages: English (N), Learning Indonesian and French
x 1936

Re: Fast Anki Cloze Deletion Cards

Postby Adrianslont » Tue Jun 18, 2019 11:41 am

rdearman wrote:Windoze... sigh. OK. It is still fairly simple, so I'll try to outline it quickly.

  • Save the script into a file, you can name it whatever you want, I named it cloze.pl for my example.
  • Put the script and the file you want to transform together in a folder.
  • Install Perl on windows by going to strawberryperl.com and downloading Strawberry Perl. Either 64 or 32 bit depending on your computer.
  • After it is installed you'll find an option in your start menu called "Perl (Command line)" which you click and a cmd box appears.
  • Navigate to the folder with the two files and run the command:

    Code: Select all

    perl cloze.pl input.txt output.csv
  • Then import the output.csv file into anki. Change "Fields separated by colon" to "Fields separated by tabs" and it should suck in all the cards.

If you have any problems just post them here, because if you have the problem someone else might too. I didn't have perl on my windows machine, so I wrote out the steps above as I did it myself, so they should work for you.


Thanks for that Rick.

After stumbling around I managed to get there. I have turned a novel into 84000+ cloze cards. Some aggressive deletion is in order. Your script made the .csv file in a trillionth of a nano second once I figured out what to do.

I feel like a big kid now.

I will find a more suitable text - I just had that one to hand, already divided into 1 sentence per line for some other reason - so I thought I'd give it a go.

There is a problem with some of the cards, though - all the apostrophes are missing! Any ideas about how to rectify that?

Cheers
1 x

User avatar
rdearman
Site Admin
Posts: 7259
Joined: Thu May 14, 2015 4:18 pm
Location: United Kingdom
Languages: English (N)
Language Log: viewtopic.php?f=15&t=1836
x 23308
Contact:

Re: Fast Anki Cloze Deletion Cards

Postby rdearman » Tue Jun 18, 2019 11:45 am

hummm... I didn't account for punctuation marks. I suspect commas and other "non word" characters have also been stripped. There is a saying in programming.

A programmer has a problem with his code, so he decides to add a regular expression to his code. Now the programmer has two problems with his code.
5 x
: 26 / 150 Read 150 books in 2024

My YouTube Channel
The Autodidactic Podcast
My Author's Newsletter

I post on this forum with mobile devices, so excuse short msgs and typos.

User avatar
Adrianslont
Blue Belt
Posts: 827
Joined: Sun Aug 16, 2015 10:39 am
Location: Australia
Languages: English (N), Learning Indonesian and French
x 1936

Re: Fast Anki Cloze Deletion Cards

Postby Adrianslont » Tue Jun 18, 2019 11:58 am

rdearman wrote:hummm... I didn't account for punctuation marks. I suspect commas and other "non word" characters have also been stripped. There is a saying in programming.

A programmer has a problem with his code, so he decides to add a regular expression to his code. Now the programmer has two problems with his code.

That’s almost all Greek to me - I don’t know what a “regular expression” is (no need to explain it) but I can sort of guess from the context.

I have worked with programmers and remember striking this problem before - 10 years ago. It all came good in the end, sort of.
0 x

User avatar
rdearman
Site Admin
Posts: 7259
Joined: Thu May 14, 2015 4:18 pm
Location: United Kingdom
Languages: English (N)
Language Log: viewtopic.php?f=15&t=1836
x 23308
Contact:

Re: Fast Anki Cloze Deletion Cards

Postby rdearman » Tue Jun 18, 2019 12:19 pm

OK, I've modified it to deal with words which contain ' or - so it can now successfully deal with: l'orange and part-time.

Code: Select all

#!/usr/bin/perl
use strict;
use warnings;

my $filename   = $ARGV[0];
my $outfile = $ARGV[1];


open(my $fh,  '<:encoding(UTF-8)', $filename) ;
open(my $ofh, '>:encoding(UTF-8)', $outfile);

while (my $row = <$fh>)
{
    my $wordcount;
    chomp $row;
    my @words =  $row =~ /\w+[\'\-]\w+|\w+/g ;
    $wordcount = scalar( @words );
    for (my $a=0;$a<$wordcount;$a++)
    {
   print $ofh "{{c" . $a . "::" . $words[$a] . "}} " ;
    }
    print $ofh "\n";
}
print "done\n";

3 x
: 26 / 150 Read 150 books in 2024

My YouTube Channel
The Autodidactic Podcast
My Author's Newsletter

I post on this forum with mobile devices, so excuse short msgs and typos.

User avatar
Brun Ugle
Black Belt - 2nd Dan
Posts: 2273
Joined: Mon Jul 27, 2015 12:48 pm
Location: Steinkjer, Norway
Languages: English (N), Norwegian (~C1/C2), Spanish (B1/B2), German (A2/B1?), Japanese (very rusty)
Language Log: https://forum.language-learners.org/vie ... 15&t=11484
x 5821
Contact:

Re: Fast Anki Cloze Deletion Cards

Postby Brun Ugle » Tue Jun 18, 2019 6:34 pm

Rick, you were looking for ideas for your video series. May I suggest a how-to-video on this? Step by step, for dummies version.
4 x


Return to “Practical Questions and Advice”

Who is online

Users browsing this forum: No registered users and 2 guests