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