-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFlashcard.py
563 lines (475 loc) · 18.4 KB
/
Flashcard.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
from Constants import *
import random
class Deck:
##########MOVING CARDS##############
def nextCard(self):
"""
there must be a card in the deck
sets the next card in the main chain as current as called
returns true if done, false otherwise
"""
if self.numCards == 0:
print("The deck is currently empty.")
return False
currentCard = self.getCurrentCard()
nextCard = currentCard.getNext()
if nextCard == None:
print("There is no next card in the main chain.")
return False
self.setCurrentCard(nextCard)
return True
def lastCard(self):
"""
there must be a card in the deck
sets the last card in the main chain as current as called
returns true if done, false otherwise
"""
if self.numCards == 0:
print("The deck is currently empty.")
return False
currentCard = self.getCurrentCard()
lastCard = currentCard.getLast()
if lastCard == None:
print("There is no last card in the main chain.")
return False
self.setCurrentCard(lastCard)
return True
def forwardCard(self):
if self.numCards == 0:
print("The deck is currently empty.")
return False
currentCard = self.getCurrentCard()
forwardCard = currentCard.getForward()
if forwardCard == None:
print("There is no forward card in the side chain.")
return False
self.setCurrentCard(forwardCard)
return True
def rearCard(self):
if self.numCards == 0:
print("The deck is currently empty.")
return False
currentCard = self.getCurrentCard()
rearCard = currentCard.getRear()
if rearCard == None:
print("There is no rear card in the side chain.")
return False
self.setCurrentCard(rearCard)
return True
def frontCard(self):
if self.numCards == 0:
print("The deck is currently empty.")
return False
currentCard = self.getCurrentCard()
text = currentCard.getFront()
print(SEPARATOR)
print("This is the front of the card: \n")
print(text)
print("\n")
print(SEPARATOR)
return True
def backCard(self):
if self.numCards == 0:
print("The deck is currently empty.")
return False
currentCard = self.getCurrentCard()
text = currentCard.getBack()
print(SEPARATOR)
print("This is the back of the card: \n")
print(text)
print("\n")
print(SEPARATOR)
return True
def mainCard(self):
if self.numCards == 0:
print("The deck is currently empty.")
return False
currentCard = self.getCurrentCard()
mainCard = self.find_main_card(currentCard)
self.setCurrentCard(mainCard)
def rateCard(self, rating):
if self.numCards == 0:
print("The deck is currently empty.")
return False
currentCard = self.getCurrentCard()
currentCard.setRate(rating)
return True
def getRate(self):
if self.numCards == 0:
print("The deck is currently empty.")
return False
currentCard = self.getCurrentCard()
ratings = currentCard.getRate()
print("These are the ratings for this card: \n")
print(ratings)
return True
def correctCard(self, answer):
if self.numCards == 0:
print("The deck is currently empty.")
return False
currentCard = self.getCurrentCard()
currentCard.addStatistic(answer)
return True
def getCorrect(self):
if self.numCards == 0:
print("The deck is currently empty.")
return False
currentCard = self.getCurrentCard()
currentCard.calculateStatistic()
return True
def resetCard(self):
if self.numCards == 0:
print("The deck is currently empty.")
return False
currentCard = self.getCurrentCard()
currentCard.resetStatistic()
currentCard.resetRate()
return True
def entireCard(self):
if self.numCards == 0:
print("The deck is currently empty.")
return False
currentCard = self.getCurrentCard()
mainCard = self.find_main_card(currentCard)
rearCard = mainCard.getRear()
self.setCurrentCard(rearCard)
return True
####################
def setAsRandom(self):
self.randomCard = True
def setAsNotRandom(self):
self.randomCard = False
def getIsRandom(self):
return self.randomCard
def getRandomCard(self):
"""
current implementation just selects a randomly generated carf
Next phase implementation will create some sort of clustering algorithm to
suggest the next card for use
the random card will always be on the main chain
"""
total = len(self.cardList)
randomIndex = random.randint(0, total - 1)
randomCard = self.getCard(randomIndex)
assert randomCard.getMain()
self.setCurrentCard(randomCard)
return randomCard
def getCard(self, index):
"""
index must be in the main chain
"""
assert type(index) == int and (index < len(self.cardList) or len(self.cardList) == 0)
if self.numCards == 0:
return None
return self.cardList[index]
def removeCard(self, positionNum):
"""
only removes cards form the main line
"""
# TODO need to deal with Main cards removal and chain cards removal
num = len(self.cardList)
if num == 0:
print("No cards to remove")
return
if num == 1:
self.cardList.remove(self.cardList[positionNum])
self.setCurrentCard(None)
elif positionNum == 0:
tail = self.getCard(num - 1)
head = self.getCard(1)
tail.setNext(head)
head.setLast(tail)
self.cardList.remove(self.cardList[positionNum])
self.setCurrentCard(head)
elif positionNum == num - 1:
tail = self.getCard(num - 2)
head = self.getCard(0)
tail.setNext(head)
head.setLast(tail)
self.cardList.remove(self.cardList[positionNum])
self.setCurrentCard(tail)
else:
last = self.getCard(positionNum - 1)
nextCard = self.getCard(positionNum + 1)
last.setNext(nextCard)
nextCard.setLast(last)
self.cardList.remove(self.cardList[positionNum])
self.numCards -= 1
def addCard(self, card):
assert type(card) == Card
newCard = card
numCards1 = len(self.cardList)
self.cardList.append(newCard)
if numCards1 == 1:
firstCard = self.cardList[0]
newCard.setLast(firstCard)
newCard.setNext(firstCard)
firstCard.setNext(newCard)
firstCard.setLast(newCard)
elif numCards1 > 1: #ALREADY MORE THAN ONE CARD IN DECK
lastCard = self.cardList[numCards1 - 1 ]
firstCard = self.cardList[0]
lastCard.setNext(newCard)
newCard.setLast(lastCard)
newCard.setNext(firstCard)
firstCard.setLast(newCard)
elif numCards1 == 0:
newCard.setLast(newCard)
newCard.setNext(newCard)
self.numCards += 1
def create_card_chain(self, questionsList, answer):
firstQuestion = questionsList[0]
firstCard = self.createCardHelper(firstQuestion, answer)
firstCard.setMain(True) # part of main chain of cards
self.setCurrentCard(firstCard)
entireQuestion = self.createEntireQuestion(questionsList)
newCard = Card()
newCard.setFront(entireQuestion)
newCard.setBack(answer)
firstCard.setRear(newCard)
newCard.setForward(firstCard)
newCard.setMain(False)
self.numCards += 1
remainingQuestions = questionsList[1:]
previousCard = firstCard
for question in remainingQuestions:
#newCard = self.createCardHelper(question, answer)
newCard = Card()
newCard.addFront(question)
newCard.addBack(answer)
newCard.setMain(False)
self.numCards += 1
previousCard.addForward(newCard)
newCard.addRear(previousCard)
previousCard = newCard
def text_to_cards(self, processed_text_list):
assert type(processed_text_list) == list
for qaTuple in processed_text_list:
questionsList = qaTuple[0]
answer = qaTuple[1]
if len(questionsList) == 1:
question = questionsList[0]
newCard = self.createCardHelper(question, answer)
newCard.setMain(True) # main chain card
self.setCurrentCard(newCard)
entireQuestion = self.createEntireQuestion(questionsList)
entireCard = Card()
entireCard.setFront(entireQuestion)
entireCard.setBack(answer)
newCard.setRear(entireCard)
entireCard.setForward(newCard)
entireCard.setMain(False)
self.numCards += 1
else:
self.create_card_chain(questionsList, answer)
def createEntireQuestion(self, questionsList):
entireQuestion = ""
for element in questionsList:
entireQuestion += element
return entireQuestion
def createCardHelper(self, question, answer):
newCard = Card()
newCard.addFront(question)
newCard.addBack(answer)
self.addCard(newCard)
return newCard
def custom_chain_card_creation(self, main_index, question, answer):
"""
main index is int position of the card of which you want to add a side chain to
"""
mainCard = self.cardList[main_index]
newCard = Card()
newCard.addFront(question)
newCard.addBack(answer)
self.numCards += 1
self.setMain(False)
self.setCurrentCard(newCard)
# TODO
def find_main_card(self, card):
assert type(card) == Card or type(card) == None
if card.getMain():
return card
forward = self.search_forward_chain(card)
backward = self.search_rear_chain(card)
return forward if forward != None else backward #one forward or backward must be on the main chain
def search_forward_chain(self, card):
assert type(card) == Card or card == None
if card == None:
return None
if card.getMain():
return card
newCard = card.getForward()
return self.search_forward_chain(newCard)
def search_rear_chain(self, card):
assert type(card) == Card or card == None
if card == None:
return None
if card.getMain():
return card
newCard = card.getRear()
return self.search_rear_chain(newCard)
def custom_main_card_creation(self, question, answer):
newCard = self.createCardHelper(question, answer)
newCard.setMain(True)
self.numCards += 1
self.setCurrentCard(newCard)
def setCurrentCard(self, card):
assert (type(card) == Card) or (card == None)
self.currentCard = card
def getCurrentCard(self):
return self.currentCard
def __init__(self, name = None, isRandom = False):
self.currentCard = None
self.cardList = []
self.numCards = 0
self.deckName = name
self.randomCard = isRandom
def __str__(self):
return self.deckName
def __repr__(self):
return self.deckName
class Card:
def getName(self):
return self.cardName
def getFront(self):
return self.frontText
def getBack(self):
return self.backText
def getForward(self):
return self.forwardChain
def getRear(self):
return self.rearChain
def getNext(self):
return self.nextCard
def getLast(self):
return self.lastCard
def getMain(self):
return self.mainChain
def getRate(self):
return self.rateCard
def setName(self, name):
assert type(name) == str
self.cardName = name
def setFront(self, text):
assert type(text) == str
self.frontText = text
def setBack(self, text):
assert type(text) == str
self.backText = text
def setForward(self, card):
assert type(card) == Card
self.forwardChain = card
def setRear(self, card):
assert type(card) == Card
self.rearChain = card
def setNext(self, card):
assert type(card) == Card
self.nextCard = card
def setLast(self, card):
assert type(card) == Card
self.lastCard = card
def setMain(self, main):
assert type(main) == bool
self.mainChain = main
def setRate(self, rate):
assert type(rate) == int
self.rateCard.append(rate)
def resetRate(self):
print("All ratings for this current card are reset. ")
self.ratecard = []
"""
Creates a new instance of a flash card
Attributes and Preconditions:
front [str]/[None] is the forward side of card: the question
back [str]/[None] is the back side of the card: the answer
forward [Card]/[None] is the following card in this chain with the same or
related answer (not every card with the same answer will be in the same chain)
rear [Card]/[None] is the previous card in the chain
following [Card]/[None] is the next card in the the pile of flashcards
last [Card]/[None] is the previous card in the pile of flashcards
main [bool] is whether the card is the main chain - the command main will take you from any card in the chain to the main card in the chain
statistics [List of [bool]] - list with each time card is answered correctly or not,
where True is answered correctly, False is not
Class invariants:
After every single time the card is seen, the user must enter a statistic, or skip
"""
def __init__(self, name = None, front = None, back = None, forward = None, rear = None, following = None, last = None, main = True):
"""
Instantiates a new card with no connections
"""
assert (name == None or type(name) == str)
assert (front == None or type(front) == str)
assert (back == None or type(back) == str)
assert (forward == None or type(forward) == Card)
assert (rear == None or type(rear) == Card)
assert (following == None or type(following) == Card)
assert (last == None or type(last) == Card)
self.cardName = name
self.frontText = front
self.backText = back
self.forwardChain = forward
self.rearChain = rear
self.nextCard = following
self.lastCard = last
self.mainChain = main
self.rateCard = []
self.statistics = []
def editCard(self, card_face, edits):
"""
self card is a valid [Card] object
card_face is a [bool], true for front, false for back,
edits is a [string]
changes the text on the card at the given positionNum, and on the card face
specified with the requested edits
"""
if card_face:
self.setFront(edits)
else:
self.setBack(edits)
def addRear(self, backCard):
assert type(backCard) == Card
self.rearChain = backCard
def addForward(self, forwardCard):
assert type(forwardCard) == Card
self.forwardChain = forwardCard
def addFront(self, text):
assert type(text) == str
self.frontText = text
def addBack(self, text):
assert type(text) == str
self.backText = text
def resetStatistic(self):
print("All Statistics for this current card are about to be cleared. ")
self.statistics = []
def addStatistic(self, boolean):
assert (type(boolean) == bool or boolean == SKIP)
self.statistics.append(boolean)
def calculateStatistic(self):
"""
calculate the percentage of times this card was answered correctly and incorrectly
prints the percentage correct [float] and prints the percentage incorrect
returns NONE
"""
statistics = self.statistics
total = len(statistics)
if total == 0:
print("No Statistics associated yet")
return
correct = 0
incorrect = 0
skip = 0
for i in range(total):
if statistics[i] == True:
correct += 1
elif statistics[i] == False:
incorrect += 1
else:
skip += 1
print("Number of times this card has been seen: " + str(total) + ".")
print("The percentage correct is " + str(100 * correct/total) + "%.")
print("The percentage incorrect is " + str(100 * incorrect/total) + "%.")
print("The percentage skipped is " + str(100 * skip/total) + "%.")
def __str__(self):
return "Card ID: \n" + str(id(self)) + "\nThis is the front: \n " + self.getFront() + "\nThis is the back: \n" + self.getBack() + "\n" + "Main Card " if self.mainChain else "Card ID: \n" + str(id(self)) + "\nThis is the front: \n " + self.getFront() + "\nThis is the back: \n" + self.getBack() + "\n" +" Not Main Card"
def __repr(self):
return "Card ID: \n" + str(id(self)) + "\nThis is the front: \n " + self.getFront() + "\nThis is the back: \n" + self.getBack() + "\n" + "Main Card " if self.mainChain else "Card ID: \n" + str(id(self)) + "\nThis is the front: \n " + self.getFront() + "\nThis is the back: \n" + self.getBack() + "\n" +" Not Main Card"