xref: /original-bsd/usr.bin/diction/style3/part.l (revision f737e041)
1 %{
2 
3 /*-
4  * %sccs.include.proprietary.c%
5  */
6 
7 #ifndef lint
8 static char sccsid[] = "@(#)part.l	8.2 (Berkeley) 01/28/94";
9 #endif /* not lint */
10 
11 #include "style.h"
12 #include "names.h"
13 #include "conp.h"
14 
15 #undef yywrap
16 
17 FILE *deb;
18 int nosave = 1;
19 int part = 0;
20 int barebones = 0;
21 int topic = 0;
22 int style = 1;
23 int pastyle = 0;
24 int pstyle = 0;
25 int lstyle = 0;
26 int rstyle = 0;
27 int estyle = 0;
28 int nstyle = 0;
29 int Nstyle = 0;
30 int lthresh;
31 int rthresh;
32 int nomin;
33 char s[SCHAR];
34 char *sptr = s;
35 struct ss sent[SLENG];
36 struct ss *sentp = sent;
37 float wperc();
38 float sperc();
39 float typersent();
40 float vperc();
41 int numsent = 0;
42 int qcount = 0;
43 int icount = 0;
44 long vowel = 0;
45 long numwds = 0;
46 long twds = 0;
47 long numnonf = 0;
48 long letnonf = 0;
49 int maxsent = 0;
50 int maxindex = 0;
51 int minsent = 30;
52 int minindex = 0;
53 int simple = 0;
54 int compound = 0;
55 int compdx = 0;
56 int prepc = 0;
57 int conjc = 0;
58 int complex = 0;
59 int tobe = 0;
60 int adj = 0;
61 int infin = 0;
62 int pron = 0;
63 int passive = 0;
64 int aux = 0;
65 int adv = 0;
66 int verbc = 0;
67 int tverbc = 0;
68 int noun = 0;
69 long numlet = 0;
70 int beg[15]  = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 };
71 int sleng[50];
72 int nsleng = 0;
73 int j,jj,i;
74 int comma = 0;
75 int cflg;
76 int question;
77 int quote = 0;
78 char *st;
79 int initf = 0;
80 int over = 1;
81 int nroff = 0;
82 int nrofflg = 0;
83 int leng[MAXPAR];
84 int sentno= 0;
85 %}
86 C	[A-DF-Za-z]
87 %%
88 ^{C}:.+	{
89 collect:
90 	sentp->cc = sentp->ic = yytext[0];
91 	if(sentp->cc == NOM)
92 		sentp->cc = NOUN;
93 collect1:
94 	nsleng++;
95 	sentp->leng = yyleng-2;
96 	sentp++->sp = sptr;
97 	if(sentp >= &sent[SLENG-1]){
98 		if(over)fprintf(stderr,"sentence %d too many words\n",numsent+2);
99 		over=0;
100 		sentp--;
101 	}
102 	if(sptr+yyleng >= &s[SCHAR-1]){
103 		if(over)fprintf(stderr,"sentence %d too many characters\n",numsent+2);
104 		over=0;
105 	}
106 	else {
107 		for(i=2;i<yyleng;i++)*sptr++=yytext[i];
108 		*sptr++ = '\0';
109 	}
110 	}
111 ^";:".+	{
112 	sentp->cc=END;
113 	sentp->ic = ';';
114 	goto collect1;
115 	}
116 ^",:".+	{
117 	comma++;
118 	goto collect;
119 	}
120 [\n]	;
121 ^"\":".+	{
122 	goto collect;
123 	}
124 ^E:"/."	{
125 	cflg = 1;
126 	goto sdone;
127 	}
128 ^E:.+	{
129 	cflg = 0;
130 sdone:
131 	over=1;
132 	sentp->cc=sentp->ic=END;
133 	sentp++->sp = sptr;
134 	for(i=2;i<yyleng;i++)*sptr++=yytext[i];
135 	*sptr++='\0';
136 	if(yytext[2]=='?')question=1;
137 	else question=0;
138 
139 fragment:
140 	jj=0;
141 	if(quote == 1 && sent[jj].cc == ED){
142 		sent[jj].cc = VERB;
143 		quote = 0;
144 	}
145 	if(sent[jj].cc=='"')jj++;
146 	if(sent[jj].cc==SUBCONJ){
147 		if(sent[jj+1].cc == ','){
148 			sent[jj].cc=ADV;
149 			jj += 2;
150 			comma--;
151 		}
152 		else {
153 			jj=scan(1,',',0);
154 			if(jj != -1)jj++;
155 			comma--;
156 		}
157 	}
158 	if(jj != -1){
159 		if(sent[jj].cc==CONJ || sent[jj].cc=='"')jj++;
160 		while((jj=scan(jj,END,cflg)) != -1){
161 			jj++;
162 			if(sent[jj].cc == SUBCONJ && sent[jj+1].cc == ','){
163 				sent[jj].cc=ADV;
164 				jj += 2;
165 				comma--;
166 			}
167 		}
168 	}
169 	st = sent[i].sp;
170 	if(*(st+1) == '"')
171 		if(*st == '?' || *st == '!')quote = 1;
172 	outp();
173 	nsleng = 0;
174 	if(nroff){
175 		if(sentno > 0){
176 			printf(".SL \"");
177 			for(i=0;i<sentno;i++)
178 				printf(" %d",leng[i]);
179 			printf("\"\n");
180 			sentno = 0;
181 		}
182 		printf("%s",&yytext[1]);
183 		nroff = 0;
184 	}
185 	sptr=s;
186 	sentp=sent;
187 	comma=0;
188 	}
189 ;.+	{
190 	if(style){
191 		nomin = atoi(&yytext[1]);
192 	}
193 	}
194 ^:.+[\n]	{
195 	nrofflg=1;
196 	if(sentp != sent){
197 		sentp->cc = sentp->ic = END;
198 		sentp++->sp = sptr;
199 		*sptr++ = '.';
200 		*sptr++ = '\0';
201 		over = 1;
202 		nroff = 1;
203 		goto fragment;
204 	}
205 	if(sentno > 0){
206 		printf(".SL \"");
207 		for(i=0;i<sentno;i++)
208 			printf(" %d",leng[i]);
209 		printf("\"\n");
210 		sentno = 0;
211 	}
212 	printf("%s",&yytext[1]);
213 	}
214 %%
215 yywrap(){
216 	int ii;
217 	int ml,mg,lsum,gsum;
218 	float aindex, avl, avw;
219 	float cindex,kindex,findex,fgrad;
220 	float syl, avsy, adjs,snonf;
221 	FILE *io;
222 
223 	if(style){
224 	if(numwds == 0 || numsent == 0)exit(0);
225 	avw = (float)(numwds)/(float)(numsent);
226 	avl = (float)(numlet)/(float)(numwds);
227 	aindex = 4.71*avl + .5*avw -21.43;
228 	syl = .9981*vowel-.3432*twds;
229 	avsy = syl/twds;
230 	kindex = 11.8*avsy+.39*avw-15.59;
231 	findex = 206.835-84.6*avsy-1.015*avw;
232 	if(findex < 30.)fgrad = 17.;
233 	else if(findex > 100.) fgrad = 4.;
234 	else if(findex > 70.)fgrad=(100.-findex)/10 +5.;
235 	else if(findex > 60.)fgrad =(70.-findex)/10+8.;
236 	else if(findex >50.)fgrad=(60.-findex)/5+10;
237 	else fgrad=(50.-findex)/6.66 +13.;
238 	adjs = 100 * (float)numsent/numwds;
239 	cindex = 5.89*avl-.3*adjs-15.8;
240 	printf("readability grades:\n	(Kincaid) %4.1f  (auto) %4.1f  (Coleman-Liau) %4.1f  (Flesch) %4.1f (%4.1f)\n",kindex,aindex,cindex,fgrad,findex);
241 	printf("sentence info:\n");
242 	printf("	no. sent %d no. wds %ld\n",numsent,numwds);
243 	printf("	av sent leng %4.1f av word leng %4.2f\n",avw,avl);
244 	printf("	no. questions %d no. imperatives %d\n",qcount,icount);
245 	if(numnonf != 0){
246 		snonf = (float)(letnonf)/(float)(numnonf);
247 	}
248 	printf("	no. nonfunc wds %ld  %4.1f%%   av leng %4.2f\n",numnonf,(float)(numnonf)*100/numwds,snonf);
249 	mg = avw + 10.5;
250 	if(mg > 49)mg = 49;
251 	ml = avw - 4.5;
252 	if(ml <= 0)ml = 1;
253 	else if(ml > 49)ml=48;
254 	gsum = lsum = 0;
255 	for(ii=0;ii<50;ii++){
256 		if(ii < ml)lsum += sleng[ii];
257 		else if(ii > mg)gsum+= sleng[ii];
258 	}
259 	printf("	short sent (<%d)%3.0f%% (%d) long sent (>%d) %3.0f%% (%d)\n",ml,sperc(lsum),lsum,mg,sperc(gsum),gsum);
260 	printf("	longest sent %d wds at sent %d; shortest sent %d wds at sent %d\n",maxsent,maxindex,minsent,minindex);
261 	printf("sentence types:\n");
262 	printf("	simple %3.0f%% (%d) complex %3.0f%% (%d)\n",sperc(simple),simple,sperc(complex),complex);
263 	printf("	compound %3.0f%% (%d) compound-complex %3.0f%% (%d)\n",sperc(compound),compound,sperc(compdx),compdx);
264 	printf("word usage:\n");
265 	printf("	verb types as %% of total verbs\n");
266 	printf("	tobe %3.0f%% (%d) aux %3.0f%% (%d) inf %3.0f%% (%d)\n",vperc(tobe),tobe,vperc(aux),aux,vperc(infin),infin);
267 	if(verbc != 0)adjs = (float)(passive)*100/(float)(verbc);
268 	else adjs=0;
269 	printf("	passives as %% of non-inf verbs %3.0f%% (%d)\n",adjs,passive);
270 	printf("	types as %% of total\n");
271 	printf("	prep %3.1f%% (%d) conj %3.1f%% (%d) adv %3.1f%% (%d)\n",wperc(prepc),prepc,wperc(conjc),conjc,wperc(adv),adv);
272 	printf("	noun %3.1f%% (%d) adj %3.1f%% (%d) pron %3.1f%% (%d)\n",wperc(noun),noun,
273 		wperc(adj),adj,wperc(pron),pron);
274 	printf("	nominalizations %3.0f %% (%d)\n",wperc(nomin),nomin);
275 	printf("sentence beginnings:\n");
276 	ii=beg[0]+beg[7]+beg[6]+beg[3]+beg[8];
277 	printf("	subject opener: noun (%d) pron (%d) pos (%d) adj (%d) art (%d) tot %3.0f%%\n",
278 beg[0],beg[7],beg[6],beg[3],beg[8],sperc(ii));
279 	printf("	prep %3.0f%% (%d) adv %3.0f%% (%d) \n",sperc(beg[9]),beg[9],sperc(beg[4]),beg[4]);
280 	printf("	verb %3.0f%% (%d) ",sperc(beg[1]+beg[10]+beg[11]),beg[1]+beg[10]+beg[11]);
281 	printf(" sub_conj %3.0f%% (%d) conj %3.0f%% (%d)\n",sperc(beg[13]),beg[13],sperc(beg[5]),beg[5]);
282 	printf("	expletives %3.0f%% (%d)\n",sperc(beg[14]),beg[14]);
283 #ifdef SCATCH
284 	if(nosave && (fopen(SCATCH,"r")) != NULL){
285 	if(((io=fopen(SCATCH,"a")) != NULL)){
286 		fprintf(io," read %4.1f %4.1f %4.1f %4.1f %4.1f\n",kindex, aindex, cindex, findex, fgrad);
287 		fprintf(io," sentl %d %ld %4.2f %4.2f %d %d %ld %4.2f\n",numsent,numwds,avw,avl,qcount,icount,numnonf,snonf);
288 		fprintf(io," l var %d %d %d %d %d\n",ml,lsum,mg,gsum,maxsent);
289 		fprintf(io," t var %d %d %d %d\n",simple,complex,compound,compdx);
290 		fprintf(io," verbs %d %d %d %d %d %d\n",tverbc,verbc,tobe,aux,infin,passive);
291 		fprintf(io," ty %d %d %d %d %d %d %d\n",prepc,conjc,adv,noun,adj,pron,nomin);
292 		fprintf(io," beg %d %d %d %d %d %d\n",beg[0],beg[7],beg[6],beg[3],beg[8],ii);
293 		fprintf(io," sbeg %d %d %d %d %d %d\n",beg[9],beg[4],beg[1]+beg[10]+beg[11],beg[13],beg[5],beg[14]);
294 		}
295 	}
296 #endif
297 	}
298 	return(1);
299 }
300 float
301 wperc(a)
302 {
303 	return((float)(a)*100/numwds);
304 }
305 float
306 sperc(a)
307 {
308 	return((float)(a)*100/numsent);
309 }
310 float
311 typersent(a)
312 {
313 return((float)(a)/numsent);
314 }
315 float
316 vperc(a)
317 {
318 	if(tverbc == 0)return(0);
319 	return((float)(a)*100/tverbc);
320 }
321 main(argc,argv)
322 char **argv;
323 {
324 	while(--argc > 0 && (++argv)[0][0] == '-' ){
325 		switch(argv[0][1]){
326 		case 'd': nosave = 0;
327 			continue;
328 		case 's': style=1;
329 			continue;
330 		case 'p': pastyle=style=1;
331 			continue;
332 		case 'a': pstyle=style=1;
333 			continue;
334 		case 'e': estyle = style = 1;
335 			continue;
336 		case 'n': nstyle = style = 1;
337 			continue;
338 		case 'N': Nstyle = style = 1;
339 			continue;
340 		case 'l': style=lstyle=1;
341 			lthresh = atoi(*(++argv));
342 			argc--;
343 			continue;
344 		case 'r':
345 			style=rstyle=1;
346 			rthresh = atoi(*(++argv));
347 			argc--;
348 			continue;
349 		case 'P':
350 			part = 1;
351 			style = 0;
352 			continue;
353 		case 'b':		/* print bare bones info rje */
354 			barebones = 1;
355 			style = 0;
356 			continue;
357 		case 'T':		/*topic*/
358 			style = 0;
359 			topic = 1;
360 			continue;
361 		default:
362 			fprintf(stderr,"unknown flag to part %s\n",*argv);
363 			exit(1);
364 		}
365 		argv++;
366 	}
367 #ifdef SNOM
368 	if(fopen(SNOM,"r") != NULL){
369 		deb = fopen(SNOM,"a");	/* SAVE NOM*/
370 	}
371 #else
372 	deb = NULL;
373 #endif
374 	yylex();
375 	if(nrofflg && sentno > 0){
376 		printf(".SL \"");
377 		for(i=0;i<sentno;i++)
378 			printf(" %d",leng[i]);
379 		printf("\"\n");
380 	}
381 }
382