opexxx

vcetoanki.py

Apr 23rd, 2014
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.23 KB | None | 0 0
  1. import sys,os
  2. sys.path.insert(0,os.getcwd()+os.sep+"libanki")
  3. sys.path.append(os.getcwd()+os.sep+"simplejson")
  4. sys.path.append(os.getcwd()+os.sep+"sqlalchemy")
  5.  
  6. import re
  7. import codecs
  8. import hashlib
  9. import anki
  10.  
  11. from anki.importing import Importer, ForeignCard
  12. from anki.deck import DeckStorage
  13. from anki.stdmodels import BasicModel
  14.  
  15. class VceImporter(Importer):
  16.     def __init__(self, *args):
  17.         Importer.__init__(self, *args)
  18.         self.orda = ord('A')
  19.    
  20.     def format_question(self, raw):
  21.         question = {}
  22.         raw = re.split(r'^Explanation\/Reference:', raw, flags=re.S|re.M)
  23.         question['explanation'] = (raw[1].strip())
  24.         raw = re.split(r'^Section:', raw[0], flags=re.S|re.M)
  25.         question['section'] = (raw[1].strip())
  26.         raw = re.split(r'^Answer:', raw[0], flags=re.S|re.M)
  27.         question['answer'] = [ord(c) - self.orda for c in raw[1].strip()]
  28.         raw = re.split(r'^[A-Z]\.$', raw[0], flags=re.M)
  29.         question['answers'] = [a.strip() for a in raw[1:]]
  30.         question['question'] = raw[0].strip()
  31.    
  32.         return question
  33.    
  34.     def foreignCards(self):
  35.         lines = ''
  36.         with open("test.txt", 'r') as f:
  37.             for line in f:
  38.                 lines += line
  39.  
  40.         exams = [x.strip() for x in re.split(r'^Exam [A-Z]$', lines, flags=re.MULTILINE)[1:]]
  41.         exams = [re.split(r'^QUESTION [0-9][0-9]?[0-9]?', x, flags=re.MULTILINE)[1:] for x in exams]
  42.         exams = [[q.strip() for q in x] for x in exams]
  43.  
  44.         #orda = ord('A')
  45.         hashes = []
  46.    
  47.         exams = [[self.format_question(q) for q in e] for e in exams]
  48.         n = '\n'
  49.         br = '<br />'
  50.    
  51.         cards = []
  52.         for e in exams:
  53.             for q in e:
  54.                 the_hash = hashlib.sha1(q['question']).digest()
  55.                 if the_hash in hashes:
  56.                     continue
  57.                 hashes.append(the_hash)
  58.    
  59.                 front = q['question'] + n*3 + 'Answers:\n\n' + (n*2).join([chr(x + self.orda) + '. ' + q['answers'][x] for x in range(len(q['answers']))])
  60.                 back = (n*2).join([chr(a + self.orda) + '. ' + q['answers'][a] for a in q['answer']]) + n*3 + q['explanation']
  61.                
  62.                 card = ForeignCard()
  63.                 card.fields = [unicode(front.replace(n, br), "utf-8"), unicode(back.replace(n, br), "utf-8")]
  64.                 cards.append(card)
  65.         return cards
  66.    
  67.     def fields(seld):
  68.         return 2
  69.  
  70. deck = DeckStorage.Deck(r'test.anki')
  71. deck.addModel(BasicModel())
  72. imp = VceImporter(deck, '')
  73.  
  74. imp.doImport()
  75.  
  76. deck.save()
  77. deck.close()
  78. '''
  79. with open("C:\\output_v2.csv", 'w') as f:
  80.        for e in exams:
  81.            for q in e:
  82.                the_hash = hashlib.sha1(q['question']).digest()
  83.                if the_hash in hashes:
  84.                    continue
  85.                hashes.append(the_hash)
  86.    
  87.                front = q['question'] + n*3 + 'Answers:\n\n' + (n*2).join([chr(x + orda) + '. ' + q['answers'][x] for x in range(len(q['answers']))])
  88.                back = (n*2).join([chr(a + orda) + '. ' + q['answers'][a] for a in q['answer']]) + n*3 + q['explanation']
  89.                
  90.                f.write(front.replace(n, br) + '~' + back.replace(n, br) + n)'''
Add Comment
Please, Sign In to add comment