xref: /plan9-contrib/sys/src/cmd/dict/roget.c (revision 119a69fa8ad92159a87063874a5e6ab47dcf1bcf)
1 #include <u.h>
2 #include <libc.h>
3 #include <bio.h>
4 #include <ctype.h>
5 #include "dict.h"
6 
7 /* Roget's Thesaurus from project Gutenberg */
8 
9 static long Last = 0;
10 
11 void
rogetprintentry(Entry e,int cmd)12 rogetprintentry(Entry e, int cmd)
13 {
14 	int spc;
15 	char c, *p;
16 
17 	spc = 0;
18 	p = e.start;
19 
20 	if(cmd == 'h'){
21 		while(!isspace(*p) && p < e.end)
22 			p++;
23 		while(strncmp(p, " -- ", 4) != 0 && p < e.end){
24 			while(isspace(*p) && p < e.end)
25 				p++;
26 			if (*p == '[' || *p == '{'){
27 				c = (*p == '[')? ']': '}';
28 				while(*p != c && p < e.end)
29 					p++;
30 				p++;
31 				continue;
32 			}
33 			if (isdigit(*p) || ispunct(*p)){
34 				while(!isspace(*p) && p < e.end)
35 					p++;
36 				continue;
37 			}
38 
39 
40 			if (isspace(*p))
41 				spc = 1;
42 			else
43 			if (spc){
44 				outchar(' ');
45 				spc = 0;
46 			}
47 
48 			while(!isspace(*p) && p < e.end)
49 				outchar(*p++);
50 		}
51 		return;
52 	}
53 
54 	while(p < e.end && !isspace(*p))
55 		p++;
56 	while(p < e.end && isspace(*p))
57 		p++;
58 
59 	while (p < e.end){
60 		if (p < e.end -4 && strncmp(p, " -- ", 4) == 0){	/* first line */
61 			outnl(2);
62 			p += 4;
63 			spc = 0;
64 		}
65 
66 		if (p < e.end -2 && strncmp(p, "[ ", 4) == 0){		/* twiddle layout */
67 			outchars(" [");
68 			continue;
69 		}
70 
71 		if (p < e.end -4 && strncmp(p, "&c (", 4) == 0){	/* usefull xref */
72 			if (spc)
73 				outchar(' ');
74 			outchar('/');
75 			while(p < e.end && *p != '(')
76 				p++;
77 			p++;
78 			while(p < e.end && *p != ')')
79 				outchar(*p++);
80 			p++;
81 			while(p < e.end && isspace(*p))
82 				p++;
83 			while(p < e.end && isdigit(*p))
84 				p++;
85 			outchar('/');
86 			continue;
87 		}
88 
89 		if (p < e.end -3 && strncmp(p, "&c ", 3) == 0){		/* less usefull xref */
90 			while(p < e.end && !isdigit(*p))
91 				p++;
92 			while(p < e.end && isdigit(*p))
93 				p++;
94 			continue;
95 		}
96 
97 		if (*p == '\n' && p < (e.end -1)){			/* their newlines */
98 			spc = 0;
99 			p++;
100 			if (isspace(*p)){				/* their continuation line */
101 				while (isspace(*p))
102 					p++;
103 				p--;
104 			}
105 			else{
106 				outnl(2);
107 			}
108 		}
109 		if (spc && *p != ';' && *p != '.' &&
110 		    *p != ',' && !isspace(*p)){				/* drop spaces before punct */
111 			spc = 0;
112 			outchar(' ');
113 		}
114 		if (isspace(*p))
115 			spc = 1;
116 		else
117 			outchar(*p);
118 		p++;
119 	}
120 	outnl(0);
121 }
122 
123 long
rogetnextoff(long fromoff)124 rogetnextoff(long fromoff)
125 {
126 	int i;
127 	vlong l;
128 	char *p;
129 
130 	Bseek(bdict, fromoff, 0);
131 	Brdline(bdict, '\n');
132 	while ((p = Brdline(bdict, '\n')) != nil){
133 		l = Blinelen(bdict);
134 		if (!isdigit(*p))
135 			continue;
136 		for (i = 0; i < l-4; i++)
137 			if (strncmp(p+i, " -- ", 4) == 0)
138 				return Boffset(bdict)-l;
139 	}
140 	return Boffset(bdict);
141 }
142 
143 void
rogetprintkey(void)144 rogetprintkey(void)
145 {
146 	Bprint(bout, "No pronunciation key.\n");
147 }
148