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