chiark / gitweb /
improved handling of ordinary numbers and description of relations
[familyTree.git] / familyTree / askQuestion.py
1 #!/usr/bin/python
2
3 import sqlite3
4 import findYear
5 from string import Template
6
7 global link_Template 
8 link_Template= Template("<a href = http://www.chiark.greenend.org.uk/ucgi/~naath/$script>$text</a>")
9
10 def run_query(s,t):
11         c = make_cursor()
12         return c.execute(s,t)
13
14 def print_row(row,newLine):
15         out = ''
16         for item in row:
17                 out = out + str(item)+'|'
18         return out[:-1] + newLine
19
20 def print_query(s,t,newLine):
21         printMe = ''
22         for row in run_query(s,t):
23                 printMe = printMe + print_row(row,newLine)              
24         return printMe
25
26 def is_number(s):
27     try:
28         float(s)
29         return 1
30     except ValueError:
31         return 0
32
33 def print_tagged_query(relationship,s,t,newLine):
34         mine = ''
35         for row in run_query(s,t):
36                 mine = mine + print_tagged_name(relationship,row,newLine)
37         return mine
38
39
40 def relationship_html(ID,ID2,newLine):
41         if newLine=='<br>':
42                 relationship = common_ancestors(ID,ID2,newLine)[2]
43                 script = "ancestors.py?ID="+str(ID)+"&ID2="+str(ID2)
44                 url = link_Template.substitute(script = script,text = "Common ancestors")
45                 return relationship + ' '+url + newLine
46         else:
47                 return ''
48
49 def terr_html(terr,newLine):
50         if newLine=='<br>':
51                 return link_Template.substitute(script = "territory.py?terr="+terr, text=terr)
52         else:
53                 return terr
54 def name_html(row,html):
55         if html=='<br>':
56                 html=1
57         elif html=='\n':
58                 html=0
59         if html == 1:
60                 script = "person.py?ID=" + str(row[1])
61                 name = row[0]
62                 return link_Template.substitute(script = script, text = name)
63         if row[0] == None:
64                 return row[1]
65         else:
66                 return row[0] + "," +str(row[1])
67
68 def print_tagged_name(relationship,row,newLine):
69         if row[0]==None:
70                 out = relationship + " not yet entered: " + row[1]
71         else:
72                 if newLine == '<br>':
73                         html = 1
74                 else:
75                         html=0
76                 if relationship =='':
77                         out = name_html(row,html) + '   '
78                 else:
79                         out = relationship + ": " + name_html(row,html)
80         return out + newLine
81
82
83 def ordinal_numbers(number):
84         out = str(number)
85         if number==1:
86                 out = out +'st'
87         elif number==2:
88                 out = out +'nd'
89         elif number==3:
90                 out = out +'rd'
91         else:
92                 out = out +'th'
93         return out
94
95 def list_territories(newLine):
96         s = "SELECT DISTINCT territory"\
97         +" FROM territories"\
98         +" ORDER BY territory;"
99
100         out = ''
101         for row in run_query(s,()):
102                 out =out + terr_html(row[0],newLine) +newLine
103         return out
104
105 def list_people(newLine):
106         s = "SELECT name,id,bornyear"\
107         +" FROM people"\
108         +" ORDER BY bornyear;"
109
110         out = ''
111         year = 0
112         out = out + 'born in unknown year:' +newLine
113         for row in run_query(s,()):
114                 if row[2]!=0 and row[2]/100==0:
115                         out = out +newLine+ 'born in 1st century:' +newLine
116
117                 if row[2]/100!=year/100:
118                         century = row[2]/100 + 1
119                         out = out +newLine+ 'born in ' 
120
121                         out = out +ordinal_numbers(century) + ' century:' + newLine
122
123                 out = out + name_html(row,newLine) +newLine
124                 year = row[2]
125         return out
126
127 def count_names(newLine):
128         s = "SELECT firstName, count(*)"\
129         +" FROM people"\
130         +" GROUP BY firstName"\
131         +" ORDER BY count(*) DESC;"
132
133         out = print_query(s,(),newLine)
134         
135
136         return out
137
138 def all_ancestors(personID,newLine):
139         #find parents
140         s = "SELECT people.Name,parents.parentID FROM"\
141                 +" parents LEFT JOIN people"\
142                 +" ON parents.parentID = people.ID"\
143                 +" WHERE parents.ID = ?"\
144                 +" AND parents.parentID <> '.';"
145
146
147         ancestors = [personID]
148         allAncestors = [personID]
149         trackLevel = [0]
150         level = 0
151
152         t = "SELECT name,id FROM people WHERE id==?"
153         id = (personID,)
154
155         out = "Ancestors of "
156         for row in run_query(t,id):
157                 out = out + name_html(row,newLine)+newLine
158
159         while len(ancestors)>0:
160                 level = level+1
161                 newA =[]
162                 out = out+newLine + parent_level(level,'parent') +':' + newLine
163                 for ancestor in ancestors:
164                         id = (ancestor,)
165                         for row in run_query(s,id):
166                                 out = out + name_html(row,newLine)+newLine
167                                 if row[1] not in allAncestors and is_number(row[1])!=0:
168                                         newA.append(row[1])
169                                         allAncestors.append(row[1])
170                                         trackLevel.append(level)
171                 ancestors = newA
172         
173
174         return [out, allAncestors,trackLevel]
175
176
177 def common_ancestors(IDA, IDB,newLine):
178         out = 'Common ancestors of:' + newLine
179
180         s = "SELECT name,id FROM people WHERE id==?"
181
182
183         names=[]
184         for id in (IDA,IDB):
185                 t = (id,)
186                 for row in run_query(s,t):
187                         out = out + name_html(row,newLine)+newLine
188                         names.append(row[0])
189                 if id==IDA:
190                         out = out + 'and'
191                 out = out + newLine
192
193         if len(names)!=2:
194                 related = 'No details held on one party'
195                 out = out + related
196                 return [out,[],related]
197         
198
199         a = all_ancestors(IDA,newLine)
200         b = all_ancestors(IDB,newLine)
201         
202         ancestorsB = set(b[1])
203         ancestorsA = set(a[1])
204
205         common = ancestorsA.intersection(ancestorsB)
206         common = list(common)
207
208
209         aLevels=[]
210         bLevels=[]
211         for c in common:
212                 i = a[1].index(c)
213                 aLevels.append(a[2][i])
214                 i = b[1].index(c)
215                 bLevels.append(b[2][i])
216
217         s = "SELECT Name, ID, bornyear"\
218         +" FROM people"\
219         +" WHERE ID IN ("
220         for i in range(len(common)):
221                 s = s+ "?,"
222         if len(common)>0:
223                 s = s[:-1]
224
225
226         s = s+") ORDER BY bornyear;"
227
228
229         if len(common)==0:
230                 related = names[0]+' and '+names[1]+' are not related'
231                 out = out + newLine + related
232                 return [out, common,related]
233
234
235         out = out + print_tagged_query('',s,common,newLine)
236
237         indexA=[]
238         indexB=[]
239
240         for i in range(len(common)):
241                 if aLevels[i] == min(aLevels):
242                         indexA.append(i)
243                 if bLevels[i] == min(bLevels):
244                         indexB.append(i)
245         
246
247
248         s = "SELECT name, id"\
249         +" FROM people"\
250         +" WHERE id=?"
251
252         out  = out + newLine + 'Most Recent Common Ancestors:' + newLine
253         for a in indexA:
254                 t = (common[a],)
255                 out = out + print_tagged_query('',s,t,newLine)
256                 if a!=indexA[-1]:
257                         out = out + 'and' + newLine
258
259         out = out + parent_level(aLevels[indexA[0]],'parent')
260         if len(indexA) >1:
261                 out = out + 's'
262
263         out = out + ' of ' + name_html([names[0],IDA],newLine)+newLine
264
265         out = out + newLine
266         for b in indexB:
267                 t = (common[b],)
268                 out = out + print_tagged_query('',s,t,newLine)
269                 if b!=indexB[-1]:
270                         out = out + 'and' + newLine
271
272         out = out + parent_level(bLevels[indexB[0]],'parent')
273         if len(indexB)>1:
274                 out = out + 's'
275         out = out + ' of ' + name_html([names[1],IDB],newLine)+newLine
276
277
278         al = aLevels[indexA[0]]
279         bl = bLevels[indexB[0]]
280
281         related = relationship(al,bl,names)
282         out = out+newLine + related
283
284         return [out,common,related]
285
286 def relationship(level1, level2,names):
287
288         if level1==0 and level2==0:
289                 return names[0] + ' is ' +names[1]
290         if level1==0:
291                 return names[0] + ' is ' + names[1] + '\'s '+ parent_level(level2,'parent')
292         if level2==0:
293                 return names[1] + ' is ' + names[0] + '\'s '+ parent_level(level1,'parent')
294
295
296         if level1>=level2:
297                 remove = level1-level2
298                 cousinNum = level2-1
299         else:
300                 remove = level2-level1
301                 cousinNum = level1-1
302
303         if cousinNum==0:
304                 uaLevel =  parent_level(remove,'uncle or aunt')
305                 if level1<= level2:
306                         return names[0] + ' is ' + names[1] + '\'s ' + uaLevel
307
308                 if level2<level1:
309                         return names[1] + ' is ' + names[0] + '\'s ' + uaLevel
310
311         c=ordinal_numbers(cousinNum)
312         if remove == 1:
313                 rem = 'once'
314         elif remove ==2:
315                 rem = 'twice'
316         else:
317                 rem = str(remove) + ' times'            
318
319         r = names[0] +' and '+names[1] +' are ' + c + ' cousins '
320         if remove !=0:
321                 r = r+ rem + ' removed'
322
323         return r
324
325 def parent_level(level,type):
326         if level == 0:
327                 if type=='parent':
328                         return 'self'
329                 else:           
330                         return 'sibling'
331         out = type
332         if level ==1:
333                 return out
334         out = 'grand '+out
335         if level ==2:
336                 return out
337         for i in range(2,level):
338                 out = 'great '+out
339         return out
340
341 def rulers_of(aTerritory,newLine):
342
343         tq = "SELECT name, people.ID, startyear,stopyear,territory"\
344                 +" FROM territories INNER JOIN people"\
345                 +" ON people.ID = territories.ID"\
346                 +" WHERE territory LIKE ?"\
347                 +" ORDER BY territory,startyear,stopyear;"
348
349
350
351         thisT  = ''
352         last = ''
353         out = ''
354         for row in run_query(tq,(aTerritory+'%',)):
355                 if row[4]!=last and last!='':
356                         out  = out + 'Rulers of '+terr_html(last,newLine) +':'+ newLine +thisT +newLine
357                         thisT = ''
358
359                 thisT = thisT +name_html(row,newLine)
360                 thisT = thisT +' from ' + str(row[2])+' to '+str(row[3]) + newLine
361                 last = row[4]
362
363         out  = out + 'Rulers of '+terr_html(row[4],newLine) +':'+ newLine +thisT
364
365         return out      
366
367 def person_info(personID,newLine):
368         t = (personID,)
369
370         output = '';
371         
372         #Id, Name, Dates, Style, Style-Dates
373         s = "SELECT * FROM people WHERE ID = ?"
374         for row in run_query(s,t):
375                 output = output + 'ID: '+str(row[0]) +newLine
376                 output = output + print_tagged_name('Name',[row[1], row[0]],newLine) +newLine
377                 output = output + 'Born: '+row[3] + newLine
378                 output = output + 'Died: '+row[5] + newLine
379
380         s = "SELECT * FROM styles WHERE ID = ?"
381         for row in run_query(s,t):
382                 output = output +newLine+ 'Style: '+row[1] + newLine
383
384                 output = output + 'Territories:' + newLine
385
386                 u = "SELECT * FROM territories"\
387                 +"  WHERE ID =? AND startYear =? AND stopYear=?"
388                 v=(personID,row[3],row[5])
389
390                 any = 0
391                 for r in run_query(u,v):
392                         output = output + terr_html(r[1],newLine) +','
393                         any = 1
394                 if any ==1:
395                         output = output[:-1] + newLine
396
397                 output = output +  'From: '+row[2] + newLine
398                 output = output +  'To: '+row[4] + newLine
399
400         output = output + newLine
401         #find parents
402         s = "SELECT people.Name,parents.parentID FROM"\
403                 +" parents LEFT JOIN people"\
404                 +" ON parents.parentID = people.ID"\
405                 +" WHERE parents.ID = ?"
406         for row in run_query(s,t):
407                 output = output + print_tagged_name('Parent',row,newLine)
408
409         #find spouses
410         s = "SELECT people.NAME, marriages.IDb from"\
411                 +" marriages LEFT JOIN people"\
412                 +" ON people.ID = marriages.IDb"\
413                 +" WHERE marriages.IDa = ?"
414         for row in run_query(s,t):
415                 output = output + newLine
416                 output = output + print_tagged_name('Spouse',row,newLine)
417                 output = output + relationship_html(personID,row[1],newLine)
418
419         s = "SELECT people.NAME, marriages.IDa from"\
420                 +" marriages LEFT JOIN people"\
421                 +" ON people.ID = marriages.IDa"\
422                 +" WHERE marriages.IDb = ?"
423         for row in run_query(s,t):    
424                 output = output + newLine
425                 output = output + print_tagged_name('Spouse',row,newLine)
426                 output = output + relationship_html(personID,row[1],newLine)
427
428         output = output + newLine
429
430         #find children
431         s = "Select people.NAME, people.ID from"\
432                 +" people INNER JOIN parents"\
433                 +" ON people.ID = parents.ID"\
434                 +" WHERE parents.parentID = ?"
435
436         for row in run_query(s,t):
437                 output = output  + print_tagged_name('Child',row,newLine)
438
439                  #find children's other parent
440                 u = "Select people.NAME, parents.parentID FROM"\
441                 +" parents LEFT JOIN people"\
442                 +" ON people.ID = parents.parentID"\
443                 +" WHERE parents.ID = ? AND parents.parentID <> ?"
444
445                 ids = (row[1],t[0])
446
447                 for row in run_query(u,ids):
448                         output = output + print_tagged_name('With',row,newLine)
449
450         output = output + newLine
451
452         return output
453
454 def connect():
455         global conn
456         conn = sqlite3.connect('/home/naath/familyTreeProject/familyTree/tree.db')
457         return conn
458
459 def make_cursor():
460         return conn.cursor()
461         
462 def close(conn):
463         conn.close
464
465 #def main():
466
467 #       [c, conn] = connect()   
468 #
469 #       person_info(1,c)
470 #       person_info(17,c)
471 #       person_info(38,c)
472 #       person_info(90,c)
473 #
474 #       close(conn)