-
Notifications
You must be signed in to change notification settings - Fork 0
/
relation.go
458 lines (397 loc) · 10.4 KB
/
relation.go
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
package model
import "fmt"
// Relation describes the relationship between the From person and the To
type Relation struct {
From *Person
To *Person
CommonAncestor *Person
FromGenerations int // number of generations between the From person and CommonAncestor. 1 = common ancestor is parent of from
ToGenerations int // number of generations between the To person and CommonAncestor. 1 = common ancestor is parent of to
ClosestDirectRelation *Relation // if the To person does not share an ancestor with the From person, then this is the relation of From to a person who was the partner/spouse of a direct relation of To
// ToSpouseGenerations int // number of generations between the To person and the spouse of SpouseRelation. 0 = spouse, 1 = parent of spouse
SpouseRelation *Relation // the relationship of the To person to the spouse of the ClosestDirectRelation
}
func (r *Relation) IsUnknown() bool {
return r == nil
}
func (r *Relation) IsSelf() bool {
if r == nil {
return false
}
return r.To.SameAs(r.From)
}
// IsDirectAncestor reports whether the To person is a direct ancestor of the From person
func (r *Relation) IsDirectAncestor() bool {
if r == nil {
return false
}
return r.To.SameAs(r.CommonAncestor)
}
// IsParent reports whether the To person is a parent of the From person
func (r *Relation) IsParent() bool {
if r == nil {
return false
}
return r.To.SameAs(r.CommonAncestor) && r.FromGenerations == 1
}
// IsDirectDescendant reports whether the To person is a direct descendant of the From person
func (r *Relation) IsDirectDescendant() bool {
if r == nil {
return false
}
return r.From.SameAs(r.CommonAncestor)
}
// IsChild reports whether the To person is a child of the From person
func (r *Relation) IsChild() bool {
if r == nil {
return false
}
return r.From.SameAs(r.CommonAncestor) && r.ToGenerations == 1
}
// IsCloseToDirectAncestor reports whether To person is a direct ancestor or a child or spouse of the From person.
func (r *Relation) IsCloseToDirectAncestor() bool {
if r == nil || r.To.IsUnknown() {
return false
}
if r.IsDirectAncestor() {
return true
}
if !r.To.Father.IsUnknown() {
prel := r.ExtendToParent(r.To.Father)
if prel.IsDirectAncestor() {
return true
}
}
if !r.To.Mother.IsUnknown() {
prel := r.ExtendToParent(r.To.Mother)
if prel.IsDirectAncestor() {
return true
}
}
// if this person is not a direct relation but is the spouse of one
if r.ClosestDirectRelation != nil && r.SpouseRelation.IsSelf() {
if r.ClosestDirectRelation.IsDirectAncestor() {
return true
}
}
return false
}
// HasCommonAncestor reports whether the From and To person have a common ancestor, including
// if the To person is a direct ancestor of the From person
func (r *Relation) HasCommonAncestor() bool {
if r == nil {
return false
}
return !r.CommonAncestor.IsUnknown()
}
// Name returns the name of the relation between From and To, in the
// form that "To is the Name() of From"
func (r *Relation) Name() string {
if r.CommonAncestor != nil {
if r.To.SameAs(r.CommonAncestor) {
// Direct ancestor
if r.FromGenerations == 0 {
return "same person as"
}
name := r.To.Gender.RelationToChildrenNoun()
if r.FromGenerations == 1 {
return name
}
name = "grand" + name
if r.FromGenerations == 2 {
return name
}
if r.FromGenerations < 5 {
for i := 2; i < r.FromGenerations; i++ {
name = "great " + name
}
} else {
name = fmt.Sprintf("%dx great "+name, r.FromGenerations-2)
}
return name
}
if r.From.SameAs(r.CommonAncestor) {
// Direct descendent
if r.ToGenerations == 0 {
return "self"
}
name := r.To.Gender.RelationToParentNoun()
if r.ToGenerations == 1 {
return name
}
name = "grand" + name
if r.ToGenerations == 2 {
return name
}
if r.FromGenerations < 5 {
for i := 2; i < r.ToGenerations; i++ {
name = "great " + name
}
} else {
name = fmt.Sprintf("%dx great "+name, r.FromGenerations-2)
}
return name
}
if r.FromGenerations == 1 && r.ToGenerations == 1 {
switch r.From.Gender {
case GenderMale:
return "brother"
case GenderFemale:
return "sister"
default:
return "sibling"
}
}
if r.FromGenerations > 1 && r.ToGenerations == 1 {
var name string
switch r.To.Gender {
case GenderMale:
name = "uncle"
case GenderFemale:
name = "aunt"
}
if name != "" {
for i := 2; i < r.FromGenerations; i++ {
name = "great " + name
}
return name
}
}
name := "cousin"
degree := "first"
removal := ""
switch r.FromGenerations {
case 3:
degree = "second"
case 4:
degree = "third"
case 5:
degree = "fourth"
case 6:
degree = "fifth"
case 7:
degree = "sixth"
case 8:
degree = "seventh"
case 9:
degree = "eighth"
case 10:
degree = "ninth"
case 11:
degree = "tenth"
case 12:
degree = "eleventh"
}
if degree != "" {
degree += " "
}
switch abs(r.FromGenerations - r.ToGenerations) {
case 0:
case 1:
removal = "once"
case 2:
removal = "twice"
case 3:
removal = "three times"
case 4:
removal = "four times"
case 5:
removal = "five times"
case 6:
removal = "six times"
case 7:
removal = "seven times"
case 8:
removal = "eight times"
}
if removal != "" {
removal = " " + removal + " removed"
}
return degree + name + removal
}
if r.ClosestDirectRelation != nil && r.SpouseRelation != nil {
var rel string
if r.SpouseRelation.IsSelf() {
if r.ClosestDirectRelation.IsParent() {
switch r.To.Gender {
case GenderMale:
return "step-father"
case GenderFemale:
return "step-mother"
default:
return "step-parent"
}
}
switch r.To.Gender {
case GenderMale:
rel = "husband"
case GenderFemale:
rel = "wife"
default:
rel = "spouse"
}
} else if r.SpouseRelation.FromGenerations == 1 {
switch r.To.Gender {
case GenderMale:
rel = "father-in-law"
case GenderFemale:
rel = "mother-in-law"
default:
rel = "parent-in-law"
}
} else if r.SpouseRelation.ToGenerations == 1 {
switch r.To.Gender {
case GenderMale:
rel = "stepson"
case GenderFemale:
rel = "stepdaughter"
default:
rel = "stepchild"
}
}
if rel != "" {
if r.ClosestDirectRelation.From.SameAs(r.ClosestDirectRelation.To) {
return rel
}
return rel + " of the " + r.ClosestDirectRelation.Name()
}
}
return "unknown relation"
}
const MaxDistance = 1000
// Distance computes a score for how distant the relationship is.
// A self relationship has score 0, a parent/child/spouse has score 1
// A direct ancestor scores 1 per generation step
// A direct descendant scores 1 per generation step
// A descendant of a common ancestor scores 1 per generation step to the common ancestor and 2 per generation down beyond the first
// A non-direct relationship doubles the score of the closest direct relationship
func (r *Relation) Distance() int {
if r == nil {
return MaxDistance
}
if r.From.SameAs(r.To) {
// self
return 0
}
if r.CommonAncestor != nil {
// Is the From person the common ancestor
if r.From.SameAs(r.CommonAncestor) {
// The To person is a descendant
return r.ToGenerations
}
// Is the To person the common ancestor
if r.To.SameAs(r.CommonAncestor) {
// The To person is a direct ancestor
return r.FromGenerations
}
// direct relationship
return r.FromGenerations + 2*(r.ToGenerations-1)
} else if r.ClosestDirectRelation != nil && r.SpouseRelation != nil {
// indirect relationship
if r.SpouseRelation.Distance() == 0 {
return r.ClosestDirectRelation.Distance() + 1
}
return r.ClosestDirectRelation.Distance() + r.SpouseRelation.Distance()*2
}
return MaxDistance
}
// ExtendToSpouse produces a new relationship extended to the spouse of the To person
func (r *Relation) ExtendToSpouse(spouse *Person) *Relation {
return &Relation{
From: r.From,
To: spouse,
ClosestDirectRelation: r,
SpouseRelation: Self(spouse),
}
}
// ExtendToChild produces a new relationship extended to the child of the To person
func (r *Relation) ExtendToChild(ch *Person) *Relation {
if r.CommonAncestor != nil {
return &Relation{
From: r.From,
To: ch,
CommonAncestor: r.CommonAncestor,
FromGenerations: r.FromGenerations,
ToGenerations: r.ToGenerations + 1,
}
} else if r.ClosestDirectRelation != nil && r.SpouseRelation != nil {
return &Relation{
From: r.From,
To: ch,
ClosestDirectRelation: r.ClosestDirectRelation,
SpouseRelation: r.SpouseRelation.ExtendToChild(ch),
}
} else {
return &Relation{
From: r.From,
To: ch,
}
}
}
// ExtendToParent produces a new relationship extended to the parent of the To person
func (r *Relation) ExtendToParent(parent *Person) *Relation {
if r.CommonAncestor != nil {
if r.CommonAncestor.SameAs(r.To) {
return &Relation{
From: r.From,
To: parent,
CommonAncestor: parent,
FromGenerations: r.FromGenerations + 1,
ToGenerations: 0,
}
} else {
return &Relation{
From: r.From,
To: parent,
CommonAncestor: r.CommonAncestor,
FromGenerations: r.FromGenerations,
ToGenerations: r.ToGenerations - 1,
}
}
// TODO
// } else if r.ClosestDirectRelation != nil && r.SpouseRelation != nil {
// return &Relation{
// From: r.From,
// To: ch,
// ClosestDirectRelation: r.ClosestDirectRelation,
// SpouseRelation: r.SpouseRelation.ExtendToChild(ch),
// }
} else {
return &Relation{
From: r.From,
To: parent,
}
}
}
func abs(i int) int {
if i < 0 {
return -i
}
return i
}
func Parent(child *Person, parent *Person) *Relation {
return &Relation{
From: child,
To: parent,
CommonAncestor: parent,
FromGenerations: 1,
ToGenerations: 0,
}
}
func Self(p *Person) *Relation {
return &Relation{
From: p,
To: p,
CommonAncestor: p,
FromGenerations: 0,
ToGenerations: 0,
}
}
func Spouse(p *Person, spouse *Person) *Relation {
return &Relation{
From: p,
To: spouse,
ClosestDirectRelation: Self(p),
SpouseRelation: Self(spouse),
}
}