-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrelation.pl
More file actions
executable file
·701 lines (666 loc) · 19.8 KB
/
relation.pl
File metadata and controls
executable file
·701 lines (666 loc) · 19.8 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
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
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
#!/usr/bin/perl -w
# This program was generated by lines2perl, which is part of Gedcom.pm.
# Gedcom.pm is Copyright 1999-2005, Paul Johnson (pjcj@cpan.org)
# Version 1.1502 - 20th December 2005
# Gedcom.pm is free. It is licensed under the same terms as Perl itself.
# The latest version of Gedcom.pm should be available from my homepage:
# http://www.pjcj.net
use strict;
require 5.005;
use diagnostics;
use integer;
use Getopt::Long;
use Gedcom::LifeLines 1.15;
my $Ged; # Gedcom object
my %Opts; # options
my $_Traverse_sub; # subroutine for traverse
sub out { print STDERR @_ unless $Opts{quiet} }
sub outf { printf STDERR @_ unless $Opts{quiet} }
sub initialise ()
{
die "usage: $0 -gedcom_file file.ged\n"
unless GetOptions(\%Opts,
"gedcom_file=s",
"quiet!",
"validate!",
) and defined $Opts{gedcom_file};
local $SIG{__WARN__} = sub { out "\n@_" };
out "reading...";
$Ged = Gedcom->new
(
gedcom_file => $Opts{gedcom_file},
callback => sub { out "." }
);
if ($Opts{validate})
{
out "\nvalidating...";
my %x;
my $vcb = sub
{
my ($r) = @_;
my $t = $r->{xref};
out "." if $t && !$x{$t}++;
};
$Ged->validate($vcb);
}
out "\n";
set_ged($Ged);
}
$SIG{__WARN__} = sub
{
out $_[0] unless $_[0] =~ /^Use of uninitialized value/
};
# /*
# relation - a LifeLines relation computing program
# by Jim Eggert (eggertj@atc.ll.mit.edu)
# Version 1, 21 November 1992
# Version 2, 23 November 1992 (completely revamped)
# Version 3, (changed format slightly, modified code somewhat)
# Version 4, 6 July 1993 (added English language)
# Version 5, 6 September 1993 (generified language)
#
# This program calculates the relationship between individuals in a
# database. It does so in three modes. Mode 1 just does one pair of
# individuals and then exits. Mode 2 does any number of pairs with a
# common "from" person. Mode 3 does all pairs with a common "from"
# person. In general, mode 1 is fastest for simple relationships, but
# if you want one complicated relationship, you may as well do them all.
#
# Each computed relation is composed of the minimal combination of
# parent (fm), sibling (bsS), child (zdC), and spouse (hw) giving the
# relational path from the "from" person to the "to" person. Each
# incremental relationship (or hop) is coded as follows, with the
# capital letters denoting a person of unknown gender:
# father f
# mother m
# parent P (not used)
# brother b
# sister s
# sibling S
# son z (sorry)
# daughtr d
# child C
# husband h
# wife w
# spouse O (sorry again, but usually not possible)
#
# The report gives the steps required to go from the first person to
# the second person. Thus the printout
# I93 John JONES fmshwz I95 Fred SMITH
# means that John Jones' father's mother's sister's husband's wife's son
# is Fred Smith. Notice in this case, the sister's husband's wife is
# not the same as the sister, and the husband's wife's son is not the
# same as the husband's son. Thus in more understandable English, John
# Jones' paternal grandmother's sister's husband's wife's son from
# another marriage is Fred Smith.
#
# The program will do a trivial parsing of the path string. You can
# change the language_table to have it print in different languages, as
# long as the word order is unchanged.
#
# If there is no relation, the program says so. That at least should be
# easy to explain. Mode 3 only prints out those individuals who are
# related to the "from" individual.
# */
my $plist;
my $hlist;
my $mark;
my $keys;
my $found;
my $do_names;
my $language;
my $language_table;
my $token;
my $untoken;
sub include ($$$$$)
{
my($person, $hops, $keypath, $path, $pathend) = @_;
my $and;
my $entry;
my $eq;
my $pkey;
if (($person && ($found == 0)))
{
$pkey = &key($person);
if ($entry = $mark->{$pkey})
{
if ((&strcmp($entry, "is not related to") == 0))
{
$found = 1;
$plist = [];
$hlist = [];
$mark->{&save($pkey)} = &save(&concat($path, $pathend));
$keys->{&save($pkey)} = &save(&concat(&concat($keypath, "@"), $pkey));
}
}
else
{
push @$plist, &save($pkey);
push @$hlist, $hops;
$mark->{&save($pkey)} = &save(&concat($path, $pathend));
$keys->{&save($pkey)} = &save(&concat(&concat($keypath, "@"), $pkey));
}
}
undef
}
sub get_token ($)
{
my($input) = @_;
my $eq;
my $first_delim;
my $not;
# /* Parse a token from the input string.
# Tokens are separated by one or more "@"s.
# Set global parameter token to the first token string.
# Set global parameter untoken to the rest of the string after first token.
# */
# /* strip leading @s */
#
$untoken = &save($input);
$first_delim = &index($untoken, "@", 1);
LOOP: while (($first_delim == 1))
{
$untoken = &save(&substring($untoken, 2, &strlen($untoken)));
$first_delim = &index($untoken, "@", 1);
}
# /* get token and untoken */
#
if ((! $first_delim))
{
$token = &save($untoken);
$untoken = &save("");
}
else
{
$token = &save(&substring($untoken, 1, ($first_delim - 1)));
$untoken = &save(&substring($untoken, ($first_delim + 1), &strlen($untoken)));
}
undef
}
sub parse_relation ($$)
{
my($relation, $keypath) = @_;
my $charcounter;
my $le;
my $not;
my $strlen;
if ((! $language))
{
display " ";
display $relation;
if ($do_names)
{
$untoken = $keypath;
display &get_token($untoken);
LOOP: while (&strlen($untoken))
{
display &get_token($untoken);
display " ";
display $token;
display " ";
display &name(&indi($token));
}
}
display " ";
}
else
{
$charcounter = 1;
$untoken = $keypath;
display &get_token($untoken);
LOOP: while (($charcounter <= &strlen($relation)))
{
display $language_table->{&substring($relation, $charcounter, $charcounter)};
if ($do_names)
{
display &get_token($untoken);
display " ";
display $token;
display " ";
display &name(&indi($token));
}
$charcounter = ($charcounter + 1);
}
display " is ";
}
undef
}
sub main ()
{
my $child;
my $cnum;
my $eq;
my $fam;
my $female;
my $from_key;
my $from_person;
my $hopcount;
my $keypath;
my $male;
my $mode;
my $ne;
my $num;
my $path;
my $pathend;
my $person;
my $pkey;
my $pnum;
my $prev_hopcount;
my $spouse;
my $strcmp;
my $to_key;
my $to_person;
my $want_another;
$mark = {};
$keys = {};
$plist = [];
$hlist = [];
$language_table = {};
$language_table->{"f"} = "'s father";
$language_table->{"m"} = "'s mother";
$language_table->{"P"} = "'s parent";
$language_table->{"b"} = "'s brother";
$language_table->{"s"} = "'s sister";
$language_table->{"S"} = "'s sibling";
$language_table->{"z"} = "'s son";
$language_table->{"d"} = "'s daughter";
$language_table->{"C"} = "'s child";
$language_table->{"h"} = "'s husband";
$language_table->{"w"} = "'s wife";
$language_table->{"O"} = "'s spouse";
display &getindimsg($from_person, "Enter person to compute relation from:");
$from_key = &save(&key($from_person));
$hopcount = 0;
$prev_hopcount = (- 1);
$found = 0;
display &include($from_person, $hopcount, "", "", "");
display &getintmsg($mode, "Enter 1 for a single relation, 2 for several, 3 for all:");
display &getintmsg($language, "Enter 0 for brief, 1 for English-language relationships:");
display &getintmsg($do_names, "Enter 0 to omit, 1 to output names of all intervening relatives:");
if (($mode == 1))
{
display &getindimsg($to_person, "Enter one person to compute relation to:");
$to_key = &save(&key($to_person));
if (&strcmp($from_key, $to_key))
{
$mark->{$to_key} = "is not related to";
}
else
{
$plist = [];
$hlist = [];
}
}
LOOP: while ($pkey = (shift @$plist))
{
$person = &indi($pkey);
$hopcount = (shift @$hlist);
$path = $mark->{$pkey};
$keypath = $keys->{$pkey};
if (($hopcount != $prev_hopcount))
{
display &print(".");
$prev_hopcount = $hopcount;
}
$hopcount = ($hopcount + 1);
display &include(&father($person), $hopcount, $keypath, $path, "f");
display &include(&mother($person), $hopcount, $keypath, $path, "m");
$cnum = 0;
LOOP: for $child ( do { my $e = &parents($person); $e ? $e->children : ()} )
{
$cnum++;
if (&male($child))
{
$pathend = "b";
}
elsif (&female($child))
{
$pathend = "s";
}
else
{
$pathend = "S";
}
display &include($child, $hopcount, $keypath, $path, $pathend);
}
$pnum = 0;
LOOP: for $fam ($person->fams)
{
for $spouse ($fam->parents || undef)
{
next if $spouse && $spouse->xref eq $person->xref;
$pnum++;
if (&male($spouse))
{
$pathend = "h";
}
elsif (&female($spouse))
{
$pathend = "w";
}
else
{
$pathend = "O";
}
display &include($spouse, $hopcount, $keypath, $path, $pathend);
$cnum = 0;
LOOP: for $child ( do { my $e = $fam; $e ? $e->children : ()} )
{
$cnum++;
if (&male($child))
{
$pathend = "z";
}
elsif (&female($child))
{
$pathend = "d";
}
else
{
$pathend = "C";
}
display &include($child, $hopcount, $keypath, $path, $pathend);
}
}
}
}
if (($mode == 1))
{
display $from_key;
display " ";
display &name(&indi($from_key));
display &parse_relation(&save($mark->{$to_key}), $keys->{$to_key});
display $to_key;
display " ";
display &name(&indi($to_key));
display "\n";
}
if (($mode == 2))
{
$want_another = 1;
LOOP: while ($want_another)
{
display &getindimsg($to_person, "Enter person to compute relation to:");
$to_key = &save(&key($to_person));
display $from_key;
display " ";
display &name(&indi($from_key));
if ($path = $mark->{$to_key})
{
display &parse_relation(&save($path), $keys->{$to_key});
}
else
{
display " is not related to ";
}
display $to_key;
display " ";
display &name($to_person);
display "\n";
display &getintmsg($want_another, "Enter 0 if done, 1 if you want another to person:");
}
}
if (($mode == 3))
{
display $from_key;
display " ";
display &name(&indi($from_key));
display " --->\n";
$num = 0;
LOOP: for $to_person ($Ged->individuals)
{
$num++;
$to_key = &save(&key($to_person));
if ($path = $mark->{$to_key})
{
display &parse_relation(&save($path), $keys->{$to_key});
display $to_key;
display " ";
display &name($to_person);
display "\n";
}
}
}
undef
}
initialise();
main();
flush();
0
__END__
Original LifeLines program follows:
/*
relation - a LifeLines relation computing program
by Jim Eggert (eggertj@atc.ll.mit.edu)
Version 1, 21 November 1992
Version 2, 23 November 1992 (completely revamped)
Version 3, (changed format slightly, modified code somewhat)
Version 4, 6 July 1993 (added English language)
Version 5, 6 September 1993 (generified language)
This program calculates the relationship between individuals in a
database. It does so in three modes. Mode 1 just does one pair of
individuals and then exits. Mode 2 does any number of pairs with a
common "from" person. Mode 3 does all pairs with a common "from"
person. In general, mode 1 is fastest for simple relationships, but
if you want one complicated relationship, you may as well do them all.
Each computed relation is composed of the minimal combination of
parent (fm), sibling (bsS), child (zdC), and spouse (hw) giving the
relational path from the "from" person to the "to" person. Each
incremental relationship (or hop) is coded as follows, with the
capital letters denoting a person of unknown gender:
father f
mother m
parent P (not used)
brother b
sister s
sibling S
son z (sorry)
daughtr d
child C
husband h
wife w
spouse O (sorry again, but usually not possible)
The report gives the steps required to go from the first person to
the second person. Thus the printout
I93 John JONES fmshwz I95 Fred SMITH
means that John Jones' father's mother's sister's husband's wife's son
is Fred Smith. Notice in this case, the sister's husband's wife is
not the same as the sister, and the husband's wife's son is not the
same as the husband's son. Thus in more understandable English, John
Jones' paternal grandmother's sister's husband's wife's son from
another marriage is Fred Smith.
The program will do a trivial parsing of the path string. You can
change the language_table to have it print in different languages, as
long as the word order is unchanged.
If there is no relation, the program says so. That at least should be
easy to explain. Mode 3 only prints out those individuals who are
related to the "from" individual.
*/
global(plist)
global(hlist)
global(mark)
global(keys)
global(found)
global(do_names)
global(language)
global(language_table)
global(token)
global(untoken)
proc include(person,hops,keypath,path,pathend)
{
if (and(person,eq(found,0))) {
set(pkey,key(person))
if (entry,lookup(mark,pkey)) {
if (eq(strcmp(entry,"is not related to"),0)) {
set(found,1)
list(plist)
list(hlist)
insert(mark,save(pkey),save(concat(path,pathend)))
insert(keys,save(pkey),save(concat(concat(keypath,"@"),pkey)))
}
}
else {
enqueue(plist,save(pkey))
enqueue(hlist,hops)
insert(mark,save(pkey),save(concat(path,pathend)))
insert(keys,save(pkey),save(concat(concat(keypath,"@"),pkey)))
}
}
}
proc get_token(input) {
/* Parse a token from the input string.
Tokens are separated by one or more "@"s.
Set global parameter token to the first token string.
Set global parameter untoken to the rest of the string after first token.
*/
/* strip leading @s */
set(untoken,save(input))
set(first_delim,index(untoken,"@",1))
while (eq(first_delim,1)) {
set(untoken,save(substring(untoken,2,strlen(untoken))))
set(first_delim,index(untoken,"@",1))
}
/* get token and untoken */
if (not(first_delim)) {
set(token,save(untoken))
set(untoken,save(""))
}
else {
set(token,save(substring(untoken,1,sub(first_delim,1))))
set(untoken,save(
substring(untoken,add(first_delim,1),strlen(untoken))))
}
}
proc parse_relation(relation,keypath) {
if (not(language)) {
" " relation
if (do_names) {
set(untoken,keypath)
call get_token(untoken)
while(strlen(untoken)) {
call get_token(untoken)
" " token " " name(indi(token))
}
}
" "
}
else {
set(charcounter,1)
set(untoken,keypath)
call get_token(untoken)
while (le(charcounter,strlen(relation))) {
lookup(language_table,substring(relation,charcounter,charcounter))
if (do_names) {
call get_token(untoken)
" " token " " name(indi(token))
}
set(charcounter,add(charcounter,1))
}
" is "
}
}
proc main ()
{
table(mark)
table(keys)
list(plist)
list(hlist)
table(language_table)
insert(language_table,"f","'s father")
insert(language_table,"m","'s mother")
insert(language_table,"P","'s parent")
insert(language_table,"b","'s brother")
insert(language_table,"s","'s sister")
insert(language_table,"S","'s sibling")
insert(language_table,"z","'s son")
insert(language_table,"d","'s daughter")
insert(language_table,"C","'s child")
insert(language_table,"h","'s husband")
insert(language_table,"w","'s wife")
insert(language_table,"O","'s spouse")
getindimsg(from_person,
"Enter person to compute relation from:")
set(from_key,save(key(from_person)))
set(hopcount,0)
set(prev_hopcount,neg(1))
set(found,0)
call include(from_person,hopcount,"","","")
getintmsg(mode,"Enter 1 for a single relation, 2 for several, 3 for all:")
getintmsg(language,
"Enter 0 for brief, 1 for English-language relationships:")
getintmsg(do_names,
"Enter 0 to omit, 1 to output names of all intervening relatives:")
if (eq(mode,1)) {
getindimsg(to_person,
"Enter one person to compute relation to:")
set(to_key,save(key(to_person)))
if (strcmp(from_key,to_key)) {
insert(mark,to_key,"is not related to")
}
else {
list(plist)
list(hlist)
}
}
while (pkey,dequeue(plist)) {
set(person,indi(pkey))
set(hopcount,dequeue(hlist))
set(path,lookup(mark,pkey))
set(keypath,lookup(keys,pkey))
if (ne(hopcount,prev_hopcount)) {
print(".")
set(prev_hopcount,hopcount)
}
set(hopcount,add(hopcount,1))
call include(father(person),hopcount,keypath,path,"f")
call include(mother(person),hopcount,keypath,path,"m")
children(parents(person),child,cnum) {
if (male(child)) { set(pathend,"b") }
elsif (female(child)) { set(pathend,"s") }
else { set(pathend,"S") }
call include(child,hopcount,keypath,path,pathend)
}
families(person,fam,spouse,pnum) {
if (male(spouse)) { set(pathend,"h") }
elsif (female(spouse)) { set(pathend,"w") }
else { set(pathend,"O") }
call include(spouse,hopcount,keypath,path,pathend)
children(fam,child,cnum) {
if (male(child)) { set(pathend,"z") }
elsif (female(child)) { set(pathend,"d") }
else { set(pathend,"C") }
call include(child,hopcount,keypath,path,pathend)
}
}
}
if (eq(mode,1)) {
from_key " " name(indi(from_key))
call parse_relation(save(lookup(mark,to_key)),lookup(keys,to_key))
to_key " " name(indi(to_key)) "\n"
}
if (eq(mode,2)) {
set(want_another,1)
while (want_another) {
getindimsg(to_person,"Enter person to compute relation to:")
set(to_key,save(key(to_person)))
from_key " " name(indi(from_key))
if (path,lookup(mark,to_key)) {
call parse_relation(save(path),lookup(keys,to_key))
}
else { " is not related to " }
to_key " " name(to_person) "\n"
getintmsg(want_another,
"Enter 0 if done, 1 if you want another to person:")
}
}
if (eq(mode,3)) {
from_key " " name(indi(from_key)) " --->\n"
forindi(to_person,num) {
set(to_key,save(key(to_person)))
if (path,lookup(mark,to_key)) {
call parse_relation(save(path),lookup(keys,to_key))
to_key " " name(to_person) "\n"
}
}
}
}