Guest User

Cloze splitter

a guest
Nov 21st, 2020
345
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.15 KB | None | 0 0
  1. import random
  2. import tkinter
  3. import tkinter.filedialog
  4. import io
  5.  
  6. def gen_notes(book):
  7.     start = 0
  8.     while start < len(book):
  9.         passage = book[start:start + 25]
  10.         out = ""
  11.         start += 10
  12.         cloze_done = False
  13.         for line,s in enumerate(passage):
  14.             if line >= 15 and not cloze_done:
  15.                 words = s.split()
  16.                 candidates = [i for (i,w) in enumerate(words) if len(w) > 4]
  17.                 try:
  18.                     idx = random.choice(candidates)
  19.                     s = ' '.join(words[:idx]) + ' {{c1::' + words[idx] + '}} ' + ' '.join(words[idx+1:])
  20.                     cloze_done = True
  21.                 except IndexError:
  22.                     pass
  23.                 pass
  24.             out += (s + '\n')
  25.         yield out
  26.  
  27. tk = tkinter.Tk()
  28.  
  29. bookname = tkinter.filedialog.askopenfilename()
  30. notes = gen_notes(io.open(bookname, encoding="utf8").read().splitlines())
  31.  
  32. def copy_note():
  33.     global tk, notes
  34.     tk.clipboard_clear()
  35.     tk.clipboard_append(next(notes))
  36.  
  37. tkinter.Button(tk, text = "copy next", command = copy_note)\
  38.        .pack(fill=tkinter.BOTH, expand=True)
  39. tk.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment