-
Notifications
You must be signed in to change notification settings - Fork 0
/
memoryAlgorithm.py
95 lines (67 loc) · 3.02 KB
/
memoryAlgorithm.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
import csv
import random
class memoryFilter():
def _init_(self):
pass
def addMemory(self, memoryData):
self.memoryData = memoryData
self.databasePath = "imageDatabase/database.csv"
with open(self.databasePath, mode='a', newline="") as memoryFile:
self.memoryFile = csv.writer(memoryFile, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
self.memoryFile.writerow(self.memoryData + [3])
def changeMemoryPosition(self, memoryName, answerRatio):
self.memoryName = memoryName
self.answerRatio = answerRatio
self.databasePath = "imageDatabase/database.csv"
self.memories = []
self.count = 0
with open(self.databasePath) as memoryFile:
self.memoryFile = csv.reader(memoryFile)
for line in self.memoryFile:
if line[1] == self.memoryName:
self.memoryPos = self.count
print(self.memoryPos)
self.memories.append(line)
self.count += 1
if self.answerRatio[0]/(sum(self.answerRatio)) <= 1/5:
self.memories[self.memoryPos][-1] = 1
elif 1/5 < self.answerRatio[0]/(sum(self.answerRatio)) <= 2/5:
self.memories[self.memoryPos][-1] = 2
elif 2/5 < self.answerRatio[0]/(sum(self.answerRatio)) <= 3/5:
self.memories[self.memoryPos][-1] = 3
elif 3/5 < self.answerRatio[0]/(sum(self.answerRatio)) <= 4/5:
self.memories[self.memoryPos][-1] = 4
elif 4/5 < self.answerRatio[0]/(sum(self.answerRatio)):
self.memories[self.memoryPos][-1] = 5
self.f = open(self.databasePath, "w")
self.f.truncate()
self.f.close()
with open(self.databasePath, mode='a', newline="") as memoryFile:
self.memoryFile = csv.writer(memoryFile, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
for i in self.memories:
self.memoryFile.writerow(i)
memoryFile.close()
def pickMemory(self):
self.memoryList = []
self.databasePath = "imageDatabase/database.csv"
with open(self.databasePath) as memoryFile:
self.memoryFile = csv.reader(memoryFile)
for line in self.memoryFile:
if line[-1] == "1":
for i in range(5):
self.memoryList.append(line)
elif line[-1] == "2":
for i in range(4):
self.memoryList.append(line)
elif line[-1] == "3":
for i in range(3):
self.memoryList.append(line)
elif line[-1] == "4":
for i in range(2):
self.memoryList.append(line)
elif line[-1] == "5":
for i in range(1):
self.memoryList.append(line)
return(random.choice(self.memoryList))
algo = memoryFilter()
print(algo.pickMemory())