comparison Wnn/jutil/atof.c @ 0:bbc77ca4def5

initial import
author Yoshiki Yazawa <yaz@cc.rim.or.jp>
date Thu, 13 Dec 2007 04:30:14 +0900
parents
children a7ccf412ba02
comparison
equal deleted inserted replaced
-1:000000000000 0:bbc77ca4def5
1 /*
2 * $Id: atof.c,v 1.7 2002/07/14 04:26:57 hiroo Exp $
3 */
4
5 /*
6 * FreeWnn is a network-extensible Kana-to-Kanji conversion system.
7 * This file is part of FreeWnn.
8 *
9 * Copyright Kyoto University Research Institute for Mathematical Sciences
10 * 1987, 1988, 1989, 1990, 1991, 1992
11 * Copyright OMRON Corporation. 1987, 1988, 1989, 1990, 1991, 1992, 1999
12 * Copyright ASTEC, Inc. 1987, 1988, 1989, 1990, 1991, 1992
13 * Copyright FreeWnn Project 1999, 2000, 2002
14 *
15 * Maintainer: FreeWnn Project <freewnn@tomo.gr.jp>
16 *
17 * This program is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation; either version 2 of the License, or
20 * (at your option) any later version.
21 *
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 *
27 * You should have received a copy of the GNU General Public License
28 * along with this program; if not, write to the Free Software
29 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30 */
31
32 /*
33 Fuzokugo file convert command.
34 Standard input UJIS format.
35 Standard output jserver format.
36 */
37
38 #ifndef lint
39 static char *rcs_id = "$Id: atof.c,v 1.7 2002/07/14 04:26:57 hiroo Exp $";
40 #endif /* lint */
41
42 /*
43 UJIS FORMAT
44
45 \attribute
46 {属性名}
47 {属性名}
48 ..........
49 ..........
50 \jiritugo-attr
51 {自立語品詞の属性定義}
52 {自立語品詞の属性定義}
53 ..........
54 ..........
55 \fuzokugo-id
56 {品詞の定義}
57 {品詞の定義}
58 ..........
59 ..........
60 \jiritugo
61 {自立語の品詞名}/{接続ベクトル}
62 {自立語の品詞名}/{接続ベクトル}
63 ..........
64 ..........
65 \fuzokugo
66 {同じ品詞の付属語の定義}
67 {同じ品詞の付属語の定義}
68 ..........
69 ..........
70 \syuutenv
71 \svkanren/{接続ベクトル}
72 \svkantan/{接続ベクトル}
73 \svbunsetsu/{接続ベクトル}
74 \bye
75
76
77 品詞の定義 :==
78 {品詞名} |
79 {品詞名}@{属性名},{属性名},.....
80
81 自立語品詞の属性定義 :==
82 {自立語品詞名} |
83 {自立語品詞名}@{属性名},{属性名},.....
84
85 注意)「自立語品詞の名前」は、品詞ファイルの中で定義されたものでないと
86 いけません。それに対し、「属性名」「品詞の名前」は、自分で勝手に名前を
87 定義してかまいません。
88
89
90 同じ品詞の付属語の定義 :==
91 \{付属語の品詞名}
92 {付属語の読み}/{コメント}/{接続ベクトル}
93 {付属語の読み}/{コメント}/{接続ベクトル}
94 ..........
95 ..........
96
97
98 接続ベクトル :==
99 {品詞名}:{品詞名}:{品詞名}:......
100
101 品詞名 :==
102 {品詞ファイルで定義された幹語品詞} |
103 {品詞の定義で定義された品詞名} |
104 @{品詞名}.{その品詞に属する付属語の読み} |
105 @{属性の定義で定義された属性} |
106 @{属性の定義で定義された属性}&{属性の定義で定義された属性}&... |
107 @svkanren. | @svkantan. | @svbunsetsu.
108
109 */
110
111 #ifdef HAVE_CONFIG_H
112 # include <config.h>
113 #endif
114
115 #include <stdio.h>
116 #if STDC_HEADERS
117 # include <stdlib.h>
118 # include <string.h>
119 #else
120 # if HAVE_STRINGS_H
121 # include <strings.h>
122 # endif
123 #endif /* STDC_HEADERS */
124 #if HAVE_UNISTD_H
125 # include <unistd.h>
126 #endif
127
128 #include "commonhd.h"
129 #include "hinsi.h"
130 #include "jslib.h"
131 #include "wnn_os.h"
132 #include "wnn_string.h"
133
134 #define FUZOKUGO_LEN 8
135 #define OUT_INT_KOSUU 20
136 #ifndef NO_FZK
137 #define FUZOKUGO_KOSUU 600 /* fuzokugo kosuu */
138 #endif /* NO_FZK */
139 #define ATTR_KOSUU 20
140 #define ID_HEIKIN_LEN 15
141 #define ID_MAX_LEN 128
142 #define SYUUTANV_KOSUU 3
143 #define F_COMMENT_LEN 80
144 #define ID_KOSUU (OUT_INT_KOSUU * sizeof(int) * 8)
145 /* fuzokugo hinsi kosuu */
146 #define HEAP_LEN (ID_KOSUU * ID_HEIKIN_LEN)
147
148 #define ATTR "\\attribute"
149 #ifndef NO_FZK
150 #define FUZOKUGO_ID "\\fuzokugo-id"
151 #endif /* NO_FZK */
152 #define JIRITUGO_ATTR "\\jiritugo-attr"
153 #define JIRITUGO "\\jiritugo"
154 #ifndef NO_FZK
155 #define FUZOKUGO "\\fuzokugo"
156 #endif /* NO_FZK */
157 #define END_ID "\\bye"
158 #define SYUUTANV "\\syuutanv"
159 #define SV0 "\\svkanren"
160 #define SV1 "\\svkantan"
161 #define SV2 "\\svbunsetsu"
162
163 char *SV[] = { "svkanren", "svkantan", "svbunsetsu" };
164
165 int wnnerror;
166
167 #define TCSEP '.'
168 #define ATTR_CHAR '@'
169 #define ATTR_START '@'
170 #define ATTR_NEXT ','
171 #define ATTR_AND '&'
172
173 #define ESCAPE_CHAR '\\'
174 #define COMMENT_CHAR ';'
175 #define DEVIDE_CHAR '/'
176 #define IGNORE_CHAR1 ' '
177 #define IGNORE_CHAR2 '\t'
178
179 #define UJIS_TEN 0xa1a2
180 #define UJIS_MARU 0xa1a3
181
182 #define LINE_SIZE 1024
183
184 extern int mhinsi;
185
186 int line_no = 0;
187
188 char outfile[LINE_SIZE];
189 char *com_name;
190
191 char *hinsi_file_name = NULL;
192
193 struct attr_struct
194 {
195 int id_set[OUT_INT_KOSUU];
196 char *name;
197 }
198 attr[ATTR_KOSUU];
199 int attr_num = 0;
200
201
202 struct id_struct
203 {
204 int bit;
205 char *str;
206 }
207 id[ID_KOSUU];
208 int id_num = 0;
209 int idn = 0; /* 品詞の数 */
210
211 char heap[HEAP_LEN];
212 char *hp = heap;
213
214 struct vector
215 {
216 int sv[OUT_INT_KOSUU];
217 struct vector *tc; /* for including other fuzokugo entries */
218 char *tc_name;
219 };
220
221 #ifndef NO_FZK
222 struct fuzokugo
223 {
224 w_char y[FUZOKUGO_LEN + 1];
225 char cy[FUZOKUGO_LEN * 2 + 1];
226 int hinsi;
227 char comment[F_COMMENT_LEN];
228 struct vector vect;
229 }
230 fz[FUZOKUGO_KOSUU];
231 int fz_num;
232 #endif /* NO_FZK */
233
234 struct jiritugov
235 {
236 struct vector v;
237 int vnum;
238 }
239 jiritugov[MAXHINSI];
240
241 int actjv[MAXHINSI];
242
243 int mactjv;
244
245 int out_int_kosuu = 0;
246
247 struct vector syuutanv[SYUUTANV_KOSUU];
248
249 #define SIZE 1024
250 char buf[SIZE];
251
252 static int find_id (), bsch (), tcp (),
253 #ifndef NO_FZK
254 count_yomi (), count_fz_num (), count_same_yomi (),
255 #endif
256 find_id_name (), search_attr (), is_same_jiritugov ();
257 static void init (), read_attr (), read_id (), read_jiritugov (),
258 #ifndef NO_FZK
259 make_fuzokugo_table (), check_fuzokugo (), sort (),
260 #endif
261 read_syuutanv (), set_tc_from_name (), manipulate_tc (),
262 print_out (), set_heap (), get_id_part (), check_attrs (), read_a_vector (), set_attribute (), bit_or (), set_id (), pre_clear_jiritugo_v (), usage (), sort_id ();
263 extern int wnn_find_hinsi_by_name (), wnn_loadhinsi (), create_file_header ();
264
265 static void
266 error_format (s, d1, d2, d3, d4, d5)
267 char *s;
268 int d1, d2, d3, d4, d5;
269 {
270 fprintf (stderr, "Bad format near line %d \"%s\".\n", line_no, buf);
271 fprintf (stderr, s, d1, d2, d3, d4, d5);
272 exit (1);
273 }
274
275 static void
276 error_exit_long ()
277 {
278 error_format ("Too Long Line.\n");
279 exit (1);
280 }
281
282 static void
283 error_eof ()
284 {
285 fprintf (stderr, "Error. File must end with %s\n", END_ID);
286 exit (1);
287 }
288
289 static void
290 error_no_heap ()
291 {
292 fprintf (stderr, "Error. Heap area is exhausted\n");
293 fprintf (stderr, "Please compile it again.");
294 exit (1);
295 }
296
297 static void
298 error_tc (c)
299 char *c;
300 {
301 fprintf (stderr, "No fuzokugo entry %s.\n", c);
302 exit (1);
303 }
304
305 int
306 main (argc, argv)
307 int argc;
308 char **argv;
309 {
310 com_name = argv[0];
311 init (argc, argv);
312 read_attr ();
313 read_id ();
314 sort_id ();
315 out_int_kosuu = (idn - 1) / 32 + 1;
316 read_jiritugov ();
317 #ifndef NO_FZK
318 make_fuzokugo_table ();
319 #endif /* NO_FZK */
320 read_syuutanv ();
321 set_tc_from_name ();
322 manipulate_tc ();
323 #ifndef NO_FZK
324 sort ();
325 check_fuzokugo ();
326 #endif /* NO_FZK */
327 print_out ();
328 exit (0);
329 }
330
331
332 static int
333 get_char0 ()
334 {
335 int c;
336
337 for (; (c = getchar ()) == COMMENT_CHAR || c == IGNORE_CHAR1 || c == IGNORE_CHAR2;)
338 {
339 if (c == COMMENT_CHAR)
340 {
341 for (;;)
342 {
343 if ((c = getchar ()) == EOF)
344 {
345 return (EOF);
346 }
347 if (c == '\n')
348 {
349 ungetc (c, stdin);
350 break;
351 }
352 }
353 }
354 }
355 if (c == '\n')
356 line_no += 1;
357 return (c);
358 }
359
360 static int
361 get_char1 () /* remove null lines "\\n" */
362 {
363 static int c = -1;
364 int d;
365 static int fufufu = 0;
366
367
368 if (c != -1)
369 {
370 d = c;
371 c = -1;
372 return (d);
373 }
374 else
375 {
376 if (fufufu == 0)
377 { /* remove all new lines in the head of the file */
378 for (; (d = get_char0 ()) == '\n';);
379 fufufu = 1;
380 }
381 else
382 {
383 d = get_char0 ();
384 }
385 if (d == '\n')
386 {
387 while ((c = get_char0 ()) == '\n');
388 }
389 return (d);
390 }
391 }
392
393 static int
394 get_char ()
395 {
396
397 static int hold = -1;
398 int c, d;
399
400 if (hold != -1)
401 {
402 c = hold;
403 hold = -1;
404 return (c);
405 }
406 else
407 {
408 start:
409 c = get_char1 ();
410 if (c == '\\')
411 {
412 if ((d = get_char1 ()) == EOF)
413 {
414 return (EOF);
415 }
416 if (d == '\n')
417 {
418 goto start;
419 }
420 else
421 {
422 hold = d;
423 return (c);
424 }
425 }
426 return (c);
427 }
428 }
429
430
431 /* get one phrase and return the separater */
432 static int
433 get_phrase (s0, size)
434 char *s0;
435 int size;
436 {
437 char *s = s0;
438 int c;
439 static int eof = 0;
440
441 if (eof)
442 error_eof ();
443
444 while ((c = get_char ()) != '\n' && c != '/' && c != ':' && c != EOF)
445 {
446 if (s - s0 >= size)
447 {
448 error_exit_long ();
449 }
450 *s++ = c;
451 }
452 if (c == EOF)
453 eof = 1;
454 if (s - s0 >= size - 1)
455 {
456 error_exit_long ();
457 }
458 *s++ = '\0';
459 return (c);
460 }
461
462 static void
463 read_attr ()
464 {
465 int k, j;
466
467 if (get_phrase (buf, SIZE) != '\n' || strcmp (ATTR, buf) != 0)
468 {
469 error_format ("File must start with %s\n", ATTR);
470 }
471
472 for (attr_num = 0;;)
473 {
474 if (get_phrase (buf, SIZE) != '\n')
475 {
476 error_format ("Newline is expected\n");
477 }
478 if (strcmp (JIRITUGO_ATTR, buf) == 0)
479 break;
480 set_heap (&attr[attr_num].name, buf);
481 if ((++attr_num) >= ATTR_KOSUU)
482 {
483 error_format ("Too many ATTR's\n");
484 }
485 }
486
487 for (k = 0; k < attr_num; k++)
488 {
489 for (j = 0; j < OUT_INT_KOSUU; j++)
490 {
491 attr[k].id_set[j] = 0;
492 }
493 }
494 }
495
496 extern char *wnn_get_hinsi_name ();
497
498 static void
499 read_id ()
500 {
501 char *d;
502 char buf1[SIZE];
503 int n;
504
505 /*
506 * jiritugo
507 */
508 if (wnn_loadhinsi (hinsi_file_name) < 0)
509 error_format ("Cannot open hinsi.data\n");
510 for (idn = 0; idn < mhinsi; idn++)
511 {
512 if (d = wnn_get_hinsi_name (idn))
513 {
514 set_heap (&id[id_num].str, d);
515 id[id_num++].bit = idn;
516 }
517 if (id_num >= ID_KOSUU)
518 {
519 error_format ("Too many ID's\n");
520 }
521 }
522 idn = (idn + 31) & ~31;
523 /*
524 * set attribute to jiritugo
525 */
526
527 for (;;)
528 {
529 get_phrase (buf, SIZE);
530 #ifdef NO_FZK
531 if (strcmp (JIRITUGO, buf) == 0)
532 break;
533 #else
534 if (strcmp (FUZOKUGO_ID, buf) == 0)
535 break;
536 #endif /* NO_FZK */
537 get_id_part (buf1, buf);
538 n = wnn_find_hinsi_by_name (buf1);
539 if (n == -1)
540 error_format ("Hinsi which is not defined in Hinsifile.\n");
541 strcpy (buf1, buf); /* in order to use it in check_attrs */
542 check_attrs (n, buf1);
543 }
544
545 #ifndef NO_FZK
546 /*
547 * fuzokugo
548 */
549 for (;; idn++)
550 {
551 get_phrase (buf, SIZE);
552 if (strcmp (JIRITUGO, buf) == 0)
553 break;
554 get_id_part (buf1, buf);
555 set_heap (&(id[id_num].str), buf1);
556 strcpy (buf1, buf); /* in order to use it in check_attrs */
557 id[id_num].bit = idn;
558 check_attrs (idn, buf1);
559 if ((++id_num) >= ID_KOSUU)
560 {
561 error_format ("Too many ID's\n");
562 }
563 }
564 #endif /* NO_FZK */
565 }
566
567
568 static int
569 sort_func_id (a, b)
570 char *a, *b;
571 {
572 return (strcmp (((struct id_struct *) a)->str, ((struct id_struct *) b)->str));
573 }
574
575 static void
576 sort_id ()
577 {
578 qsort ((char *) &id[0], id_num, sizeof (struct id_struct), sort_func_id);
579 }
580
581 #ifdef nodef
582 /* s is subseq of t */
583 static int
584 subseq (s, t)
585 char *s, *t;
586 {
587 while (*s)
588 {
589 if (*s++ != *t++)
590 {
591 return (-1); /* false */
592 }
593 }
594 return (0); /* true */
595 }
596 #endif
597
598 #ifndef NO_FZK
599 static void
600 make_fuzokugo_table ()
601 {
602 int id_num;
603 char d;
604 int i;
605 w_char tmp[FUZOKUGO_LEN + 1];
606
607
608 fz_num = 0;
609 d = get_phrase (buf, SIZE);
610 if (strcmp (SYUUTANV, buf) == 0)
611 return;
612 if (*buf != '\\')
613 {
614 error_format ("Fuzokugo_id is expected\n");
615 }
616 if ((id_num = find_id (buf + 1)) < 0)
617 {
618 error_format ("Unknown identifier %s\n", buf + 1);
619 }
620 for (; fz_num < FUZOKUGO_KOSUU;)
621 {
622 d = get_phrase (buf, SIZE);
623 if (strcmp (SYUUTANV, buf) == 0)
624 return;
625 if (*buf == '\\')
626 {
627 if ((id_num = find_id (buf + 1)) < 0)
628 {
629 error_format ("Unknown identifier %s\n", buf + 1);
630 }
631 continue;
632 }
633 if (d != '/')
634 {
635 error_format ("'/' and definition of fuzokugo is expected.\n");
636 }
637
638 if (strlen (buf) >= FUZOKUGO_LEN * 2)
639 {
640 error_format ("Too long fuzokugo \"%s\"\n", buf);
641 }
642
643 strcpy (fz[fz_num].cy, buf);
644 wnn_Sstrcpy (tmp, buf);
645 wnn_Sreverse (fz[fz_num].y, tmp);
646 fz[fz_num].hinsi = id_num;
647
648 d = get_phrase (buf, SIZE);
649 if (strlen (buf) >= F_COMMENT_LEN)
650 {
651 error_format ("Too long comment\n");
652 }
653 strcpy (fz[fz_num].comment, buf);
654
655 if (d != '/')
656 {
657 error_format ("'/' is expected.\n");
658 }
659
660 for (i = 0; i < OUT_INT_KOSUU; i++)
661 {
662 fz[fz_num].vect.sv[i] = 0;
663 }
664 fz[fz_num].vect.tc = NULL;
665 fz[fz_num].vect.tc_name = NULL;
666 read_a_vector (&fz[fz_num].vect);
667 fz_num += 1;
668 }
669 }
670 #endif /* NO_FZK */
671
672 static int
673 find_id (c)
674 char *c;
675 {
676 int k;
677 if ((k = bsch (c, 0, id_num - 1)) < 0)
678 {
679 return (-1);
680 }
681 return (id[k].bit);
682 }
683
684 static int
685 bsch (c, st, end)
686 char *c;
687 int st;
688 int end;
689 {
690 int tmp;
691 int m = (st + end) / 2;
692 if (st >= end)
693 {
694 if (strcmp (c, id[m].str) == 0)
695 {
696 return (st);
697 }
698 else
699 {
700 return (-1);
701 }
702 }
703
704 tmp = strcmp (c, id[m].str);
705 if (tmp == 0)
706 return (m);
707 if (tmp < 0)
708 return (bsch (c, st, m - 1));
709 return (bsch (c, m + 1, end));
710 }
711
712 #ifndef NO_FZK
713 static int
714 sort_func_fz (a, b)
715 char *a, *b;
716 {
717 int c;
718
719 if ((c = wnn_Strcmp (((struct fuzokugo *) a)->y, ((struct fuzokugo *) b)->y)) == 0)
720 {
721 if (((struct fuzokugo *) a)->hinsi > ((struct fuzokugo *) b)->hinsi)
722 {
723 return (1);
724 }
725 else if (((struct fuzokugo *) a)->hinsi < ((struct fuzokugo *) b)->hinsi)
726 {
727 return (-1);
728 }
729 return (0);
730 }
731 return (c);
732 }
733
734 static void
735 sort ()
736 {
737 qsort ((char *) &fz[0], fz_num, sizeof (struct fuzokugo), sort_func_fz);
738 }
739 #endif /* NO_FZK */
740
741 static void
742 read_a_vector (vect)
743 struct vector *vect;
744 {
745 int *v = vect->sv;
746 int d;
747 int id_num;
748 int finish = 0;
749
750 for (; finish == 0 && ((d = get_phrase (buf, SIZE)) == ':' || d == '\n');)
751 {
752 if (d == '\n')
753 {
754 finish = 1;
755 }
756 if (*buf == '\0')
757 continue;
758 if (*buf == ATTR_CHAR)
759 {
760 if (tcp (buf + 1))
761 {
762 set_heap (&vect->tc_name, buf + 1);
763 }
764 else
765 {
766 set_attribute (buf + 1, vect);
767 }
768 }
769 else
770 {
771 if ((id_num = find_id (buf)) < 0)
772 {
773 error_format ("Unknown identifier in vector %s\n", buf);
774 }
775 v[id_num / (sizeof (int) * 8)] |= 1 << id_num % (sizeof (int) * 8);
776 /* 1 << (((sizeof(int) * 8 - 1) - id_num % (sizeof(int) * 8))); */
777 }
778 }
779 if (finish == 0)
780 {
781 error_format ("%c is not permitted here.\n", d);
782 }
783 }
784
785 static void
786 read_syuutanv ()
787 {
788 int d;
789 /*
790 int k,j;
791 for(k = 0 ; k < SYUUTANV_KOSUU;k++){
792 for(j = 0 ; j < OUT_INT_KOSUU ;j++){
793 syuutanv[k].sv[j] = 0;
794 }
795 syuutanv[k].tc = NULL;
796 }
797 */
798 for (;;)
799 {
800 d = get_phrase (buf, SIZE);
801 if (strcmp (buf, END_ID) == 0)
802 {
803 break;
804 }
805 if (d != '/')
806 {
807 error_format ("");
808 }
809 if (strcmp (buf, SV0) == 0)
810 {
811 read_a_vector (&syuutanv[0]);
812 }
813 if (strcmp (buf, SV1) == 0)
814 {
815 read_a_vector (&syuutanv[1]);
816 }
817 if (strcmp (buf, SV2) == 0)
818 {
819 read_a_vector (&syuutanv[2]);
820 }
821 }
822 }
823
824 static void
825 print_out ()
826 {
827 int k, j, i;
828 int count = 0;
829 char *c;
830 FILE *ofpter;
831
832 if ((ofpter = fopen (outfile, "w")) == NULL)
833 {
834 fprintf (stderr, "Can't open the output file %s.\n", outfile);
835 perror ("");
836 exit (1);
837 }
838
839 create_file_header (ofpter, WNN_FT_FUZOKUGO_FILE, NULL);
840
841 for (k = 0; k < idn; k++)
842 {
843 if ((k % 32) == 0)
844 {
845 fprintf (ofpter, "\n");
846 }
847 for (j = 0; j < id_num; j++)
848 {
849 if (id[j].bit == k)
850 {
851 fprintf (ofpter, ";%d\t%s\n", id[j].bit, id[j].str);
852 break;
853 }
854 }
855 }
856
857 #ifndef NO_FZK
858 fprintf (ofpter, "\n%d ;付属語の個数\n", count_yomi ());
859 #endif /* NO_FZK */
860 fprintf (ofpter, "%d ;接続ベクタの長さ\n", out_int_kosuu);
861 #ifndef NO_FZK
862 fprintf (ofpter, "%d ;付属語ベクタの個数\n", count_fz_num ());
863 #endif /* NO_FZK */
864 fprintf (ofpter, "%d ;幹語品詞のワード数\n", ((mhinsi + 31) >> 5));
865 fprintf (ofpter, "%d ;幹語ベクトルの数\n", mactjv);
866 fprintf (ofpter, "%d ;幹語の品詞数\n", mhinsi);
867 #ifndef NO_FZK
868 for (k = 0; (count = count_same_yomi (k)) > 0;)
869 {
870 char tmp[FUZOKUGO_LEN * 2 + 1];
871 if (fz[k].y[0] == 0)
872 {
873 fprintf (ofpter, ";%s\n", fz[k].comment);
874 k++;
875 continue;
876 }
877 wnn_sStrcpy (tmp, fz[k].y);
878 fprintf (ofpter, "%s %d\n", tmp, count);
879 for (; count > 0; k++)
880 {
881 if (fz[k].y[0])
882 {
883 fprintf (ofpter, "%4d ", fz[k].hinsi);
884 for (i = 0; i < out_int_kosuu; i++)
885 {
886 fprintf (ofpter, "%8x ", fz[k].vect.sv[i]);
887 }
888 fprintf (ofpter, ";%s\n", fz[k].comment);
889 count--;
890 }
891 else
892 {
893 fprintf (ofpter, ";%s\n", fz[k].comment);
894 }
895 }
896 }
897 #endif /* NO_FZK */
898 fprintf (ofpter, "\n\n");
899 fprintf (ofpter, ";\n");
900 fprintf (ofpter, ";syuutan vectors\n");
901 fprintf (ofpter, ";\n");
902 for (k = 0; k < SYUUTANV_KOSUU; k++)
903 {
904 for (i = 0; i < out_int_kosuu; i++)
905 {
906 fprintf (ofpter, "%8x ", syuutanv[k].sv[i]);
907 }
908 fprintf (ofpter, "\n");
909 }
910 fprintf (ofpter, "; 幹語接続ベクタNo. 幹語接続ベクタ\n");
911 for (k = 0; k < mactjv; k++)
912 {
913 fprintf (ofpter, "\n%4d ", k);
914 for (i = 0; i < out_int_kosuu; i++)
915 {
916 fprintf (ofpter, "%8x ", jiritugov[k].v.sv[i]);
917 }
918 }
919 fprintf (ofpter, "\n\n; 幹語接続ベクタ\n");
920 for (k = 0; k < mhinsi; k++)
921 {
922 c = wnn_get_hinsi_name (k);
923 if (c)
924 {
925 fprintf (ofpter, "\t%d\t%d\t;%s\n", k, actjv[k], c);
926 }
927 else
928 {
929 #ifdef CHINESE
930 fprintf (ofpter, "\t%d\t-1\t;隆協吶\n", k);
931 #else
932 # ifdef KOREAN
933 fprintf (ofpter, "\t%d\t-1\t;擶鑷譎\n", k);
934 # else
935 fprintf (ofpter, "\t%d\t-1\t;未定義\n", k);
936 # endif
937 #endif
938 }
939 }
940 }
941
942 #ifndef NO_FZK
943 static int
944 count_same_yomi (start)
945 int start;
946 {
947 int count = 1;
948 w_char yomi[FUZOKUGO_LEN + 1];
949 int i;
950
951 if (start >= fz_num)
952 return (0);
953 wnn_Strcpy (yomi, fz[start].y);
954 for (i = start + 1; i < fz_num; i++)
955 {
956 if (fz[i].y[0])
957 {
958 if (wnn_Strcmp (yomi, fz[i].y) == 0)
959 {
960 count += 1;
961 }
962 else
963 {
964 return (count);
965 }
966 }
967 }
968 return (count);
969 }
970
971 static int
972 count_yomi ()
973 {
974 int k;
975 w_char yomi[FUZOKUGO_LEN + 1];
976 int count = 0;
977
978 yomi[0] = 0;
979 for (k = 0; k < fz_num; k++)
980 {
981 if (fz[k].y[0])
982 {
983 if (wnn_Strcmp (yomi, fz[k].y) != 0)
984 {
985 count += 1;
986 wnn_Strcpy (yomi, fz[k].y);
987 }
988 }
989 }
990 return (count);
991 }
992 #endif /* NO_FZK */
993
994 /* reverse for char as w_char */
995 #ifdef nodef
996 static void
997 reverse (d, s)
998 char *d, *s;
999 {
1000 int k;
1001 int len = strlen (s);
1002 if (len % 2)
1003 {
1004 error_format ("length of yomi string is odd\n");
1005 }
1006 len /= 2;
1007 for (k = 0; k < len; k++)
1008 {
1009 d[k * 2] = s[(len - 1 - k) * 2];
1010 d[k * 2 + 1] = s[(len - 1 - k) * 2 + 1];
1011 }
1012 d[len * 2] = 0;
1013 d[len * 2 + 1] = 0;
1014 }
1015
1016
1017 static int
1018 w_str_cmp (a, b)
1019 w_char *a, *b;
1020 {
1021 w_char wa, wb;
1022
1023 for (; *a != 0 && *b != 0;)
1024 {
1025 wa = *a;
1026 wb = *b;
1027 if (wa != wb)
1028 {
1029 return (w_char_cmp (wa, wb));
1030 }
1031 }
1032 if (*a == 0 && *b == 0)
1033 return (0);
1034 if (*a == 0)
1035 return (-1);
1036 return (1);
1037
1038 }
1039
1040 static int
1041 w_char_cmp (wa, wb)
1042 unsigned short wa, wb;
1043 {
1044 if (wa == wb)
1045 return (0);
1046 if (wa == UJIS_TEN)
1047 {
1048 if (wb == UJIS_MARU)
1049 return (-1);
1050 return (1);
1051 }
1052 if (wa == UJIS_MARU)
1053 return (1);
1054 if (wb == UJIS_MARU || wb == UJIS_TEN)
1055 return (-1);
1056
1057 if (wb > wa)
1058 return (-1);
1059 return (1);
1060 }
1061 #endif
1062
1063 #ifndef NO_FZK
1064 static void
1065 check_fuzokugo ()
1066 {
1067 int k;
1068 int point = 0;
1069
1070 for (k = 1; k < fz_num; k++)
1071 {
1072 if (wnn_Strcmp (fz[point].y, fz[k].y) == 0 && fz[point].hinsi == fz[k].hinsi)
1073 {
1074 fprintf (stderr, "Entries with same yomi \"%s\"and same hinsi \"%s\" are merged.\n", fz[k].cy, id[find_id_name (fz[k].hinsi)].str);
1075 bit_or (fz[point].vect.sv, fz[k].vect.sv);
1076 fz[k].y[0] = 0;
1077 }
1078 else
1079 {
1080 point = k;
1081 }
1082 }
1083 }
1084 #endif /* NO_FZK */
1085
1086 static void
1087 bit_or (bit1, bit2)
1088 int bit1[];
1089 int bit2[];
1090 {
1091 int i;
1092 for (i = 0; i < out_int_kosuu; i++)
1093 {
1094 bit1[i] |= bit2[i];
1095 }
1096 }
1097
1098 static int
1099 find_id_name (i)
1100 int i;
1101 {
1102 int k;
1103 for (k = 0; k < id_num; k++)
1104 {
1105 if (id[k].bit == i)
1106 {
1107 return (k);
1108 }
1109 }
1110 return -1;
1111 }
1112
1113 #ifndef NO_FZK
1114 static int
1115 count_fz_num ()
1116 {
1117 int k;
1118 int count = 0;
1119 for (k = 0; k < fz_num; k++)
1120 {
1121 if (fz[k].y[0])
1122 {
1123 count += 1;
1124 }
1125 }
1126 return (count);
1127 }
1128 #endif /* NO_FZK */
1129
1130 static void
1131 get_id_part (buf1, buf)
1132 char *buf1;
1133 char *buf;
1134 {
1135 char *c = buf1;
1136
1137 strcpy (buf1, buf);
1138 for (; *c; c++)
1139 {
1140 if (*c == ATTR_START)
1141 {
1142 *c = '\0';
1143 return;
1144 }
1145 }
1146 return;
1147 }
1148
1149 static char *
1150 attr_start (c)
1151 char *c;
1152 {
1153 for (; *c; c++)
1154 {
1155 if (*c == ATTR_START)
1156 {
1157 return (c + 1);
1158 }
1159 }
1160 return (NULL);
1161 }
1162
1163 static char *
1164 get_attr_part (c, intp)
1165 char *c;
1166 int *intp;
1167 {
1168 char tmp[ID_MAX_LEN];
1169 char *c1 = tmp;
1170
1171 strcpy (tmp, c);
1172 for (; *c1; c1++)
1173 {
1174 if (*c1 == ATTR_NEXT)
1175 {
1176 *c1 = '\0';
1177 *intp = search_attr (tmp);
1178 return (c + 1 + (c1 - tmp));
1179 }
1180 }
1181 *intp = search_attr (tmp);
1182 return (NULL);
1183 }
1184
1185 static int
1186 search_attr (c)
1187 char *c;
1188 {
1189 int k;
1190
1191 for (k = 0; k < attr_num; k++)
1192 {
1193 if (strcmp (attr[k].name, c) == 0)
1194 {
1195 return (k);
1196 }
1197 }
1198 error_format ("Not defined attribute %s.\n", c);
1199 return -1;
1200 }
1201
1202 static void
1203 check_attrs (id_n, c)
1204 int id_n;
1205 char *c;
1206 {
1207 int attr_n;
1208
1209 for (c = attr_start (c); c;)
1210 {
1211 c = get_attr_part (c, &attr_n);
1212 set_id (attr_n, id_n);
1213 }
1214 }
1215
1216 static void
1217 set_id (attr_n, id_n)
1218 int attr_n, id_n;
1219 {
1220 attr[attr_n].id_set[id_n / (sizeof (int) * 8)] |= (1 << id_n % (sizeof (int) * 8));
1221 }
1222
1223 static void
1224 vector_or (sv, attrv)
1225 int sv[];
1226 int attrv[];
1227 {
1228 int k;
1229
1230 for (k = 0; k < OUT_INT_KOSUU; k++)
1231 {
1232 sv[k] |= attrv[k];
1233 }
1234 }
1235
1236 static void
1237 vector_and (sv, attrv)
1238 int sv[];
1239 int attrv[];
1240 {
1241 int k;
1242
1243 for (k = 0; k < OUT_INT_KOSUU; k++)
1244 {
1245 sv[k] &= attrv[k];
1246 }
1247 }
1248
1249 static void
1250 manipulate_tc_vector (vectp)
1251 struct vector *vectp;
1252 {
1253 if (vectp->tc)
1254 {
1255 manipulate_tc_vector (vectp->tc);
1256 vector_or (vectp->sv, (vectp->tc)->sv);
1257 vectp->tc = NULL;
1258 }
1259 }
1260
1261
1262 static void
1263 manipulate_tc ()
1264 {
1265 int k;
1266
1267 #ifndef NO_FZK
1268 for (k = 0; k < fz_num; k++)
1269 {
1270 manipulate_tc_vector (&fz[k].vect);
1271 }
1272 #endif /* NO_FZK */
1273 for (k = 0; k < SYUUTANV_KOSUU; k++)
1274 {
1275 manipulate_tc_vector (&syuutanv[k]);
1276 }
1277 }
1278
1279 static void
1280 set_heap (cp, str)
1281 char **cp;
1282 char *str;
1283 {
1284 int len = strlen (str);
1285
1286 if (hp + len + 1 >= heap + HEAP_LEN)
1287 {
1288 error_no_heap ();
1289 }
1290 *cp = hp;
1291 strcpy (hp, str);
1292 hp += len + 1;
1293 }
1294
1295 char svstr[] = "sv*";
1296
1297 static struct vector *
1298 find_tc_vect (c)
1299 char *c;
1300 {
1301 int k;
1302 char tmp[ID_MAX_LEN + FUZOKUGO_LEN + 1];
1303 #ifndef NO_FZK
1304 w_char tmp1[FUZOKUGO_LEN + 1];
1305 w_char tmp2[FUZOKUGO_LEN + 1];
1306 int id_num;
1307 #endif
1308 char *cc;
1309
1310 strcpy (tmp, c);
1311 for (cc = tmp; *cc; cc++)
1312 {
1313 if (*cc == TCSEP)
1314 {
1315 *cc = 0;
1316 cc += 1;
1317 break;
1318 }
1319 }
1320
1321
1322 if (strlen (tmp) == strlen (c))
1323 {
1324 error_tc (c);
1325 }
1326
1327 for (k = 0; k < SYUUTANV_KOSUU; k++)
1328 {
1329 if (strcmp (tmp, SV[k]) == 0)
1330 {
1331 return (&syuutanv[k]);
1332 }
1333 }
1334
1335 #ifndef NO_FZK
1336 if ((id_num = find_id (tmp)) < 0)
1337 {
1338 error_tc (c);
1339 }
1340
1341 wnn_Sstrcpy (tmp1, cc);
1342 wnn_Sreverse (tmp2, tmp1);
1343 for (k = 0; k < fz_num; k++)
1344 {
1345 if ((wnn_Strcmp (fz[k].y, tmp2) == 0) && (fz[k].hinsi == id_num))
1346 {
1347 return (&fz[k].vect);
1348 }
1349 }
1350 #endif /* NO_FZK */
1351 error_tc (c);
1352 return (NULL);
1353 }
1354
1355 static int
1356 tcp (c)
1357 char *c;
1358 {
1359 for (; *c; c++)
1360 {
1361 if (*c == TCSEP)
1362 return (1);
1363 }
1364 return (0);
1365 }
1366
1367 static void
1368 set_tc_from_name ()
1369 {
1370 int k;
1371 #ifndef NO_FZK
1372 for (k = 0; k < fz_num; k++)
1373 {
1374 if (fz[k].vect.tc_name)
1375 {
1376 fz[k].vect.tc = find_tc_vect (fz[k].vect.tc_name);
1377 }
1378 }
1379 #endif /* NO_FZK */
1380 for (k = 0; k < SYUUTANV_KOSUU; k++)
1381 {
1382 if (syuutanv[k].tc_name)
1383 {
1384 syuutanv[k].tc = find_tc_vect (syuutanv[k].tc_name);
1385 }
1386 }
1387 }
1388
1389
1390 static void
1391 set_attribute (c, vect)
1392 char *c;
1393 struct vector *vect;
1394 {
1395
1396 int tmp_v[OUT_INT_KOSUU];
1397 char tmp[ID_MAX_LEN];
1398 char *d = tmp;
1399 int k;
1400
1401 for (k = 0; k < OUT_INT_KOSUU; k++)
1402 {
1403 tmp_v[k] = 0xffffffff;
1404 }
1405
1406 for (; *c; c++)
1407 {
1408 if (*c == ATTR_AND)
1409 {
1410 *d = 0;
1411 vector_and (tmp_v, attr[search_attr (tmp)].id_set);
1412 d = tmp;
1413 }
1414 else
1415 {
1416 *d++ = *c;
1417 }
1418 }
1419 *d++ = 0;
1420 vector_and (tmp_v, attr[search_attr (tmp)].id_set);
1421
1422 vector_or (vect->sv, tmp_v);
1423 }
1424
1425 /* 自立語ベクタの読み込み */
1426 static void
1427 read_jiritugov ()
1428 {
1429 char d;
1430 register int i, n;
1431 int vno;
1432
1433 for (mactjv = 0, i = 0; i < MAXHINSI; i++)
1434 {
1435 d = get_phrase (buf, SIZE);
1436 #ifdef NO_FZK
1437 if (strcmp (SYUUTANV, buf) == 0)
1438 return;
1439 #else
1440 if (strcmp (FUZOKUGO, buf) == 0)
1441 return;
1442 #endif /* NO_FZK */
1443 n = wnn_find_hinsi_by_name (buf);
1444 if (n == -1)
1445 error_format ("Hinsi which is not defined in Hinsifile.\n");
1446 if (d != '/')
1447 {
1448 error_format ("'/' and setuzoku vector is expected.\n");
1449 }
1450 pre_clear_jiritugo_v (mactjv);
1451 read_a_vector (&jiritugov[mactjv].v);
1452 if ((vno = is_same_jiritugov (mactjv)) == -1)
1453 {
1454 vno = mactjv;
1455 mactjv++;
1456 }
1457 actjv[n] = vno;
1458 }
1459 }
1460
1461 /* 自立語ベクタ領域をクリアーする */
1462 static void
1463 pre_clear_jiritugo_v (n)
1464 int n;
1465 {
1466 int i;
1467
1468 for (i = 0; i < OUT_INT_KOSUU; i++)
1469 jiritugov[n].v.sv[i] = 0;
1470 }
1471
1472 /* 同じ自立語ベクタあったか */
1473 static int
1474 is_same_jiritugov (n)
1475 int n;
1476 {
1477 register int i, j;
1478
1479 for (i = 0; i < n; i++)
1480 {
1481 for (j = 0; j < OUT_INT_KOSUU; j++)
1482 {
1483 if (jiritugov[i].v.sv[j] != jiritugov[n].v.sv[j])
1484 break;
1485 }
1486 if (j == OUT_INT_KOSUU)
1487 return (i); /* 同じベクタ */
1488 }
1489 return (-1); /* 同じものがなかった */
1490 }
1491
1492 /*
1493 static void
1494 classify_jiritugov()
1495 {
1496 int k,j;
1497 int mac;
1498 for(k = 0 ; k < mhinsi; k++){
1499 for(j = 0 ; j < mactjv; j++){
1500 if(eqv(jiritugov[k].v.sv,actjv[j].sv)){
1501 jiritugov[k].vnum = j;
1502 break;
1503 }
1504 }
1505 if(j == mactjv){
1506 vcp(actjv[mactjv++].sv, jiritugov[k].v.sv);
1507 }
1508 }
1509 }
1510
1511 static void
1512 eqv(a,b)
1513 int a[],b[];
1514 {
1515 int k;
1516 for(k = 0 ; k < OUT_INT_KOSUU; k++){
1517 if(!(a[k] == b[k]))return(0);
1518 }
1519 return(1);
1520 }
1521
1522 static void
1523 vcp(a,b)
1524 int a[],b[];
1525 {
1526 int k;
1527 for(k = 0 ; k < OUT_INT_KOSUU; k++){
1528 a[k] = b[k];
1529 }
1530 }
1531 */
1532
1533 static void
1534 init (argc, argv)
1535 int argc;
1536 char **argv;
1537 {
1538
1539 int c;
1540 extern int optind;
1541 extern char *optarg;
1542
1543 while ((c = getopt (argc, argv, "h:")) != EOF)
1544 {
1545 switch (c)
1546 {
1547 case 'h':
1548 hinsi_file_name = optarg;
1549 break;
1550 }
1551 }
1552 if (optind)
1553 {
1554 optind--;
1555 argc -= optind;
1556 argv += optind;
1557 }
1558
1559 if (argc != 2)
1560 {
1561 usage ();
1562 exit (1);
1563 }
1564
1565 strcpy (outfile, argv[1]);
1566
1567 }
1568
1569 static void
1570 usage ()
1571 {
1572 fprintf (stderr, "Usage : %s [-h <hinsi filename>] <fzk.data filename>\n", com_name);
1573 }