| 1 |
/* |
| 2 |
* @progname names_freq.ll |
| 3 |
* @version 3.0 |
| 4 |
* @author Eggert |
| 5 |
* @category |
| 6 |
* @output Text |
| 7 |
* @description |
| 8 |
|
| 9 |
Tabulate frequency of names in database. Like namefreq (by John |
| 10 |
Chandler), but it computes frequencies for the first five given names, |
| 11 |
the surname, and the first two post-surnames. |
| 12 |
|
| 13 |
The output can be sorted by frequency or by alphabet, or not at all. |
| 14 |
|
| 15 |
names_freq - a LifeLines names frequency calculation program |
| 16 |
by Jim Eggert ([email protected]) |
| 17 |
Version 1, 8 November 1993 (initial release) |
| 18 |
listsort code by John Chandler ([email protected]) |
| 19 |
Version 2, 10 April 1995 changed listsort to quicksort |
| 20 |
Version 3, 15 January 2000 quicksort bug fix |
| 21 |
*/ |
| 22 |
|
| 23 |
global(indices) /* table for indexing into the various lists */ |
| 24 |
global(top_index) /* number of elements in table and lists */ |
| 25 |
global(sort_type) /* 0=none, 1=frequency, 2=alphabet */ |
| 26 |
global(names) /* list of all names */ |
| 27 |
global(givens1) /* list of counts of names in each position */ |
| 28 |
global(givens2) |
| 29 |
global(givens3) |
| 30 |
global(givens4) |
| 31 |
global(givens5) |
| 32 |
global(surs) |
| 33 |
global(posts1) |
| 34 |
global(posts2) |
| 35 |
global(totals) |
| 36 |
|
| 37 |
func compare(a,b) { |
| 38 |
if (eq(sort_type,1)) { |
| 39 |
/* decreasing frequency: */ |
| 40 |
if (lt(a,b)) { return(1) } |
| 41 |
if (eq(a,b)) { return(0) } |
| 42 |
return(neg(1)) |
| 43 |
} |
| 44 |
else { |
| 45 |
return(strcmp(a,b)) |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
/* |
| 50 |
quicksort: Sort an input list by generating a permuted index list |
| 51 |
Input: alist - list to be sorted |
| 52 |
Output: ilist - list of index pointers into "alist" in sorted order |
| 53 |
Needed: compare- external function of two arguments to return -1,0,+1 |
| 54 |
according to relative order of the two arguments |
| 55 |
*/ |
| 56 |
proc quicksort(alist,ilist) { |
| 57 |
set(index,1) |
| 58 |
set(len,length(alist)) |
| 59 |
while(le(index,len)) { |
| 60 |
setel(ilist,index,index) |
| 61 |
incr(index) |
| 62 |
} |
| 63 |
if (ge(len,2)) { call qsort(alist,ilist,1,len) } |
| 64 |
} |
| 65 |
|
| 66 |
/* recursive core of quicksort */ |
| 67 |
proc qsort(alist,ilist,left,right) { |
| 68 |
if(pcur,getpivot(alist,ilist,left,right)) { |
| 69 |
set(pivot,getel(alist,getel(ilist,pcur))) |
| 70 |
set(mid,partition(alist,ilist,left,right,pivot)) |
| 71 |
call qsort(alist,ilist,left,sub(mid,1)) |
| 72 |
call qsort(alist,ilist,mid,right) |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
/* partition around pivot */ |
| 77 |
func partition(alist,ilist,left,right,pivot) { |
| 78 |
while(1) { |
| 79 |
set(tmp,getel(ilist,left)) |
| 80 |
setel(ilist,left,getel(ilist,right)) |
| 81 |
setel(ilist,right,tmp) |
| 82 |
while(lt(compare(getel(alist,getel(ilist,left)),pivot),0)) { |
| 83 |
incr(left) |
| 84 |
} |
| 85 |
while(ge(compare(getel(alist,getel(ilist,right)),pivot),0)) { |
| 86 |
decr(right) |
| 87 |
} |
| 88 |
if(gt(left,right)) { break() } |
| 89 |
} |
| 90 |
return(left) |
| 91 |
} |
| 92 |
|
| 93 |
/* choose pivot */ |
| 94 |
func getpivot(alist,ilist,left,right) { |
| 95 |
set(pivot,getel(alist,getel(ilist,left))) |
| 96 |
set(left0,left) |
| 97 |
incr(left) |
| 98 |
while(le(left,right)) { |
| 99 |
set(next,getel(alist,getel(ilist,left))) |
| 100 |
set(rel,compare(next,pivot)) |
| 101 |
if (gt(rel,0)) { return(left) } |
| 102 |
if (lt(rel,0)) { return(left0) } |
| 103 |
incr(left) |
| 104 |
} |
| 105 |
return(0) |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
proc count_name(name,thiscount) { |
| 110 |
if(index, lookup(indices, name)) { |
| 111 |
setel(thiscount,index,add(getel(thiscount,index),1)) |
| 112 |
} else { |
| 113 |
/* print("(") print(name) print(")") */ |
| 114 |
incr(top_index) |
| 115 |
set(sname,save(name)) |
| 116 |
enqueue(names,sname) |
| 117 |
insert(indices,sname,top_index) |
| 118 |
enqueue(givens1,0) |
| 119 |
enqueue(givens2,0) |
| 120 |
enqueue(givens3,0) |
| 121 |
enqueue(givens4,0) |
| 122 |
enqueue(givens5,0) |
| 123 |
enqueue(surs,0) |
| 124 |
enqueue(posts1,0) |
| 125 |
enqueue(posts2,0) |
| 126 |
enqueue(totals,0) |
| 127 |
setel(thiscount,top_index,add(getel(thiscount,top_index),1)) |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
proc main () |
| 132 |
{ |
| 133 |
table(indices) |
| 134 |
list(namelist) |
| 135 |
list(names) |
| 136 |
list(givens1) |
| 137 |
list(givens2) |
| 138 |
list(givens3) |
| 139 |
list(givens4) |
| 140 |
list(givens5) |
| 141 |
list(surs) |
| 142 |
list(posts1) |
| 143 |
list(posts2) |
| 144 |
list(totals) |
| 145 |
list(ilist) |
| 146 |
|
| 147 |
set(top_index,0) |
| 148 |
set(next_num,0) |
| 149 |
|
| 150 |
print("Counting names...") |
| 151 |
forindi (indi, num) { |
| 152 |
extractnames(inode(indi), namelist, ncomp, sindx) |
| 153 |
forlist(namelist,name,ni) { |
| 154 |
call count_name(name,totals) |
| 155 |
} |
| 156 |
if (and(ge(ncomp,1),or(eq(sindx,0),gt(sindx,1)))) { |
| 157 |
call count_name(getel(namelist,1),givens1) |
| 158 |
} |
| 159 |
if (and(ge(ncomp,2),or(eq(sindx,0),gt(sindx,2)))) { |
| 160 |
call count_name(getel(namelist,2),givens2) |
| 161 |
} |
| 162 |
if (and(ge(ncomp,3),or(eq(sindx,0),gt(sindx,3)))) { |
| 163 |
call count_name(getel(namelist,3),givens3) |
| 164 |
} |
| 165 |
if (and(ge(ncomp,4),or(eq(sindx,0),gt(sindx,4)))) { |
| 166 |
call count_name(getel(namelist,4),givens4) |
| 167 |
} |
| 168 |
if (and(ge(ncomp,5),or(eq(sindx,0),gt(sindx,5)))) { |
| 169 |
call count_name(getel(namelist,5),givens5) |
| 170 |
} |
| 171 |
if (sindx) { |
| 172 |
call count_name(getel(namelist,sindx),surs) |
| 173 |
} |
| 174 |
if (gt(ncomp,sindx)) { |
| 175 |
call count_name(getel(namelist,add(sindx,1)),posts1) |
| 176 |
} |
| 177 |
if (gt(ncomp,add(sindx,1))) { |
| 178 |
call count_name(getel(namelist,add(sindx,2)),posts2) |
| 179 |
} |
| 180 |
if (ge(num,next_num)) { |
| 181 |
print(d(num)) print(" ") |
| 182 |
set(next_num,add(next_num,100)) |
| 183 |
} |
| 184 |
} |
| 185 |
print(d(num)) |
| 186 |
|
| 187 |
getintmsg(sort_type,"Sort method (0=no sort, 1=frequency, 2=alphabet)") |
| 188 |
if (sort_type) { |
| 189 |
print("\nSorting ") print(d(top_index)) print(" names...") |
| 190 |
} |
| 191 |
if (eq(sort_type,1)) { |
| 192 |
call quicksort(totals,ilist) |
| 193 |
} |
| 194 |
elsif (eq(sort_type,2)) { |
| 195 |
call quicksort(names,ilist) |
| 196 |
} |
| 197 |
else { |
| 198 |
forlist(names,name,index) { enqueue(ilist,index) } |
| 199 |
} |
| 200 |
|
| 201 |
print("\nWriting results...") |
| 202 |
|
| 203 |
"______Frequency of names in the database______\n\n" |
| 204 |
"Name 1st 2nd 3rd 4th 5th sur post1 post2 total" |
| 205 |
"\n\n" |
| 206 |
forlist(ilist, index, num) { |
| 207 |
getel(names,index) |
| 208 |
|
| 209 |
set(nmatch, getel(givens1,index)) |
| 210 |
col(sub(30, strlen(d(nmatch)))) |
| 211 |
d(nmatch) |
| 212 |
set(nmatch, getel(givens2,index)) |
| 213 |
col(sub(36, strlen(d(nmatch)))) |
| 214 |
d(nmatch) |
| 215 |
set(nmatch, getel(givens3,index)) |
| 216 |
col(sub(42, strlen(d(nmatch)))) |
| 217 |
d(nmatch) |
| 218 |
set(nmatch, getel(givens4,index)) |
| 219 |
col(sub(48, strlen(d(nmatch)))) |
| 220 |
d(nmatch) |
| 221 |
set(nmatch, getel(givens5,index)) |
| 222 |
col(sub(54, strlen(d(nmatch)))) |
| 223 |
d(nmatch) |
| 224 |
set(nmatch, getel(surs,index)) |
| 225 |
col(sub(60, strlen(d(nmatch)))) |
| 226 |
d(nmatch) |
| 227 |
set(nmatch, getel(posts1,index)) |
| 228 |
col(sub(66, strlen(d(nmatch)))) |
| 229 |
d(nmatch) |
| 230 |
set(nmatch, getel(posts2,index)) |
| 231 |
col(sub(72, strlen(d(nmatch)))) |
| 232 |
d(nmatch) |
| 233 |
set(nmatch, getel(totals,index)) |
| 234 |
col(sub(78, strlen(d(nmatch)))) |
| 235 |
d(nmatch) "\n" |
| 236 |
} |
| 237 |
} |