-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodels.py
More file actions
151 lines (116 loc) · 4.71 KB
/
Copy pathmodels.py
File metadata and controls
151 lines (116 loc) · 4.71 KB
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
from sqlalchemy import create_engine, ForeignKey, UniqueConstraint
from sqlalchemy import Column, Integer, String
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
from os import path, getcwd
engine = create_engine('sqlite:///' + path.join(getcwd(), 'data.db'))
Base = declarative_base()
class Resolution(Base):
__tablename__ = 'resolutions'
word1_id = Column(Integer, ForeignKey('words.id'), primary_key=True)
word2_id = Column(Integer, ForeignKey('words.id'), primary_key=True)
UniqueConstraint('word1_id', 'word2_id')
def __init__(self, word1, word2):
self.word1_id = word1.id
self.word2_id = word2.id
def save(self):
session.add(self)
session.commit()
def alreadyExists(self):
if self.id == None and session.query(Resolution).filter(Resolution.word1_id == self.word1.id, Resolution.word2_id == self.word2.id).union(
session.query(Resolution).filter(Resolution.word1_id == self.word2.id, Resolution.word2_id == self.word1.id)).count() > 0:
return True
else:
return False
class Word(Base):
__tablename__ = 'words'
id = Column(Integer, primary_key=True)
kana = Column(String)
kanji = Column(String)
definition = Column(String)
book = Column(String)
chapter = Column(String)
section = Column(String)
owner_id = Column(Integer, ForeignKey('words.id'))
def __init__(self, kana=None, definition=None, kanji=None, book=None, chapter=None, section=None):
self.kana = kana
self.kanji = kanji
self.definition = definition
self.book = book
self.chapter = chapter
self.section = section
def __repr__(self):
return "<Word(kana='%s', kanji='%s', definition='%s', book='%s', chapter='%s', section='%s')>" % (self.kana, self.kanji, self.definition, self.book, self.chapter, self.section)
def save(self):
if self.alreadyExists():
return
session.add(self)
session.commit()
def getAllChildren(self):
wordlist = []
q = session.query(Word).filter(Word.owner_id == self.id).all()
for word in q:
wordlist.append(word)
wordlist.extend(word.getAllChildren())
return wordlist
def minId(self):
minId = self.id
for word in self.getAllChildren():
if minId > word.id:
minId = word.id
return minId
def ownerOf(self, other):
other.owner_id = self.id
other.save()
def dupeOf(self, other):
self.owner_id = other.id
self.save()
def resolve(self, other):
r = Resolution(self, other)
r.save()
def isResolved(self, otherId):
if self.id == otherId:
return True
q = session.query(Resolution).filter(Resolution.word1_id == self.id, Resolution.word2_id == otherId).union(
session.query(Resolution).filter(Resolution.word1_id == otherId, Resolution.word2_id == self.id))
if q.count() > 0:
return True
else:
return False
def isDupe(self):
if self.owner_id == None:
return False
else:
return True
def alreadyExists(self):
if self.id == None and session.query(Word).filter(Word.kana == self.kana, Word.kanji == self.kanji, Word.definition == self.definition).count() > 0:
return True
else:
return False
def getPotentialDuplicates(self):
dupes = session.query(Word).filter(Word.kana != None, Word.kana.contains(self.kana), Word.owner_id == None)
if self.kanji != '':
dupes = dupes.union(session.query(Word).filter(Word.kanji != None, Word.kanji.contains(self.kanji), Word.owner_id == None))
dupes = dupes.all()
trueDupes = []
for dupe in dupes:
if self.isResolved(dupe.id):
continue
else:
trueDupes.append(dupe)
return trueDupes
def toArray(self):
return [self.minId(), self.kana, self.kanji, self.definition]
def getAll():
return session.query(Word).all()
def totalCount():
return session.query(Word).count()
def dupeCount():
return session.query(Word).filter(Word.owner_id != None).count()
def relationshipCount():
return session.query(Resolution).count()
def getById(wordId):
return session.query(Word).filter(Word.id == wordId).one()
Base.metadata.create_all(engine)
Session = sessionmaker(bind=engine)
session = Session()