Debian Codegraph @

t/lines/namefreq

raw
1 /*
2 namefreq
3
4 Tabulate frequency of first names in database.
5
6 Version 1 - 1993 Jun 16 - John F. Chandler
7 Version 2 - 1993 Jun 18 (sort output by frequency)
8 Version 3 - 1995 Mar 8 (requires LL 3.0 or higher)
9 (Uses Jim Eggert's Quicksort routine)
10
11 This report counts occurrences of all first (given) names in the
12 database. Individuals with only surnames are not counted. If the
13 surname is listed first, the next word is taken as the given name.
14
15 The output file is normally sorted in order of decreasing frequency,
16 but the sort order can be altered by changing func "compare", e.g.,
17 comment out the existing "set" and uncomment the one for alphabetical
18 order.
19
20 This program works only with LifeLines.
21
22 */
23 global(name_counts) /* used by comparison in sorting by frequency */
24
25 /* Comparison function for sorting. Same convention as strcmp. */
26 func compare(astring,bstring) {
27 /* alphabetical:
28 return(strcmp(astring,bstring)) */
29 /* decreasing frequency: */
30 if(ret,sub(lookup(name_counts,bstring),lookup(name_counts,astring))){
31 return(ret)
32 }
33 return(strcmp(astring,bstring))
34 }
35
36 /*
37 quicksort: Sort an input list by generating a permuted index list
38 Input: alist - list to be sorted
39 Output: ilist - list of index pointers into "alist" in sorted order
40 Needed: compare- external function of two arguments to return -1,0,+1
41 according to relative order of the two arguments
42 */
43 proc quicksort(alist,ilist) {
44 set(len,length(alist))
45 set(index,len)
46 while(index) {
47 setel(ilist,index,index)
48 decr(index)
49 }
50 call qsort(alist,ilist,1,len)
51 }
52
53 /* recursive core of quicksort */
54 proc qsort(alist,ilist,left,right) {
55 if(pcur,getpivot(alist,ilist,left,right)) {
56 set(pivot,getel(alist,getel(ilist,pcur)))
57 set(mid,partition(alist,ilist,left,right,pivot))
58 call qsort(alist,ilist,left,sub(mid,1))
59 call qsort(alist,ilist,mid,right)
60 }
61 }
62
63 /* partition around pivot */
64 func partition(alist,ilist,left,right,pivot) {
65 while(1) {
66 set(tmp,getel(ilist,left))
67 setel(ilist,left,getel(ilist,right))
68 setel(ilist,right,tmp)
69 while(lt(compare(getel(alist,getel(ilist,left)),pivot),0)) {
70 incr(left)
71 }
72 while(ge(compare(getel(alist,getel(ilist,right)),pivot),0)) {
73 decr(right)
74 }
75 if(gt(left,right)) { break() }
76 }
77 return(left)
78 }
79
80 /* choose pivot */
81 func getpivot(alist,ilist,left,right) {
82 set(pivot,getel(alist,getel(ilist,left)))
83 set(left0,left)
84 incr(left)
85 while(le(left,right)) {
86 set(rel,compare(getel(alist,getel(ilist,left)),pivot))
87 if (gt(rel,0)) { return(left) }
88 if (lt(rel,0)) { return(left0) }
89 incr(left)
90 }
91 return(0)
92 }
93
94 proc main ()
95 {
96 list(namelist)
97 table(name_counts)
98 list(names)
99 list(ilist)
100
101 forindi (indi, num) {
102 if(not(mod(num,20))) {print(".")}
103 extractnames(inode(indi), namelist, ncomp, sindx)
104 set(gindx,1) if(eq(sindx,1)) { set(gindx,2) }
105 set(fname, save(getel(namelist, gindx)))
106 if( or( gt(sindx,1), gt(ncomp,sindx))) {
107 if(nmatch, lookup(name_counts, fname)) {
108 set(nmatch, add(nmatch, 1))
109 }
110 else {
111 enqueue(names, fname)
112 set(nmatch, 1)
113 }
114 insert(name_counts, fname, nmatch)
115 }
116 }
117 "Frequency of given names (first only) in the database\n\n"
118 "Name Occurrences\n\n"
119
120 call quicksort(names,ilist)
121 forlist(ilist, index, num) {
122 set(fname,getel(names,index))
123 fname
124 set(nmatch, lookup(name_counts, fname))
125 col(sub(25, strlen(d(nmatch))))
126 d(nmatch) "\n"
127 }
128 }