OSDN Git Service

Bumped version 2.4.4.
[chasen-legacy/chasen.git] / chasen / chasen.c
1 /*
2  * Copyright (c) 2003 Nara Institute of Science and Technology
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *   notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. The name Nara Institute of Science and Technology may not be used to
15  *    endorse or promote products derived from this software without
16  *    specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY Nara Institute of Science and Technology 
19  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 
21  * PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE Nara Institute
22  * of Science and Technology BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
24  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
25  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 
26  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 
27  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  *
30  * $Id: chasen.c,v 1.1.1.1 2007/03/13 07:40:10 masayu-a Exp $
31  */
32
33 #include "config.h"
34
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38
39 #ifdef HAVE_UNISTD_H
40 #include <unistd.h>
41 #else
42 #if defined _WIN32
43 #include <io.h>
44 #define isatty(handle) _isatty(handle)
45 #endif /* _WIN32 */
46 #endif /* HAVE_UNISTD_H */
47
48 #include "chalib.h"
49
50 static char *output_file = NULL;
51 static int is_partial = 0;
52
53 #define CHA_NAME       "ChaSen"
54 #define CHA_PROG       "chasen"
55
56 /*
57  * opt_form_usage()
58  */
59 static void
60 opt_form_usage(FILE * fp)
61 {
62     static char *message[] = {
63         "Conversion characters of -F option:\n",
64         "  %m      surface form (inflected form)\n",
65         "  %M      surface form (base form)\n",
66         "  %y,%y1  first candidate of reading (inflected form)\n",
67         "  %Y,%Y1  first candidate of reading (base form)\n",
68         "  %y0     reading (inflected form)\n",
69         "  %Y0     reading (base form)\n",
70         "  %a,%a1  first candidate of pronounciation (inflected form)\n",
71         "  %A,%A1  first candidate of pronounciation (base form)\n",
72         "  %a0     pronounciation (inflected form)\n",
73         "  %A0     pronounciation (base form)\n",
74         "  %rABC   surface form with ruby (the format is \"AkanjiBkanaC\")\n",
75         "  %i,%i1  first candidate of semantic information\n",
76         "  %i0     semantic information\n",
77         "  %Ic     semantic information (if NIL, print character 'c'.)\n",
78         "  %Pc     part of speech separated by character 'c'\n",
79         "  %Pnc    part of speech separated by character 'c'\n",
80         "  %h      part of speech (code)\n",
81         "  %H      part of speech (name)\n",
82         "  %Hn     the part of speech (name) at the n-th layer\n",
83         "          (if NIL, the part of speech at the most specific layer)\n",
84         "  %b      sub-part of speech (code)\n",
85         "  %BB     sub-part of speech (name)(if NIL, print part of speech)\n",
86         "  %Bc     sub-part of speech (name)(if NIL, print character 'c')\n",
87         "  %t      inflection type (code)\n",
88         "  %Tc     inflection type (name)(if NIL, print character 'c')\n",
89         "  %f      inflected form (code)\n",
90         "  %Fc     inflected form (name)(if NIL, print character 'c')\n",
91         "  %c      cost value of the morpheme\n",
92         "  %S      the input sentence\n",
93         "  %pb     if the best path, '*', otherwise, ' '\n",
94         "  %pi     the index of the path of the output lattice\n",
95         "  %ps     the starting position of the morpheme\n",
96         "          at the path of the output lattice\n",
97         "  %pe     the ending position of the morpheme\n",
98         "          at the path of the output lattice\n",
99         "  %pc     the cost of the path of the output lattice\n",
100         "  %ppiC   the indices of the preceding paths,\n",
101         "          concatenated with the character 'C'\n",
102         "  %ppcC   the costs of the preceding paths,\n",
103         "          concatenated with the character 'C'\n",
104         "  %?B/STR1/STR2/\n",
105         "          if sub-part of speech exists, STR1, otherwise, STR2\n",
106         "  %?I/STR1/STR2/\n",
107         "          unless the semantic information is NIL and \"\", STR1,\n",
108         "          otherwise, STR2\n",
109         "  %?T/STR1/STR2/\n",
110         "          if conjugative, STR1, otherwise, STR2\n",
111         "  %?F/STR1/STR2/\n",
112         "          same as %?T/STR1/STR2/\n",
113         "  %?U/STR1/STR2/\n",
114         "          if unknown word, STR1, otherwise, STR2\n",
115         "  %U/STR/\n",
116         "          if unknown word, \"UNKNOWN\", otherwise, STR\n",
117         "  %%      '%'\n",
118         "  .       specify the field width\n",
119         "  -       specify the field width\n",
120         "  1-9     specify the field width\n",
121         "  \\n      carriage return\n",
122         "  \\t      tab\n",
123         "  \\\\      back slash\n",
124         "  \\'      single quotation mark\n",
125         "  \\\"      double quotation mark\n",
126         "\n",
127         "Examples:\n",
128         "  \"%m \"         split words by space (wakachi-gaki)\n",
129         "  \"%y\"          Kanji to Kana conversion\n",
130         "  \"%r ()\"       print surface form with ruby as \"kanji(kana)\"\n",
131         "  \"%m\\t%y\\t%M\\t%U(%P-)\\t%T \\t%F \\n\"           same as -f option (default)\n",
132         "  \"%m\\t%U(%y)\\t%M\\t%P- %h %T* %t %F* %f\\n\"    same as -e option\n",
133         "\n",
134         "Note:\n",
135         "  If the format ends with `\\n' then outputs `EOS',\n",
136         "  otherwise outputs newline every sentence.\n",
137         NULL
138     };
139     char **mes;
140
141     if (fp)
142         for (mes = message; *mes; mes++)
143             fputs(*mes, fp);
144 }
145
146 /*
147  *  usage()
148  */
149 static void
150 usage(FILE * fp)
151 {
152     static char *message[] = {
153         "Usage: ", CHA_PROG, " [options] [file...]\n",
154         "    -s             partial analyzing mode\n",
155         "  (how to print ambiguous results)\n",
156         "    -b             show the best path (default)\n",
157         "    -m             show all morphemes\n",
158         "    -p             show all paths\n",
159         "  (output format)\n",
160         "    -f             show formatted morpheme data (default)\n",
161         "    -e             show entire morpheme data\n",
162         "    -c             show coded morpheme data\n",
163         "    -d             show detailed morpheme data for Prolog\n",
164         "    -v             show detailed morpheme data for VisualMorphs\n",
165         "    -F format      show morpheme with formatted output\n",
166         "    -Fh            print help of -F option\n",
167         "  (miscellaneous)\n",
168         "    -i encoding    character encoding.\n",
169         "                   e: EUC-JP, s: Shift_JIS, ",
170         "w: UTF-8, a: ISO-8859-1\n",
171         "    -j             Japanese sentence mode\n",
172         "    -o file        write output to `file'\n",
173         "    -w width       specify the cost width\n",
174         "    -C             use command mode\n",
175         "    -r rc-file     use rc-file as a ", CHA_PROG,
176             "rc file other than the default\n",
177         "    -R             with -D, do not read ", CHA_PROG,
178             "rc file, without -D, read the\n",
179         "                   default chasenrc file `", RCPATH, "'\n",
180         "    -L lang        specify languages\n",
181         "    -O[c|s]        output with compound words or their segments\n",
182         "    -lp            print the list of parts of speech\n",
183         "    -lt            print the list of conjugation types\n",
184         "    -lf            print the list of conjugation forms\n",
185         "    -h             print this help\n",
186         "    -V             print ", CHA_NAME, " version number\n",
187         NULL
188     };
189     char **mes;
190
191     cha_version(fp);
192     if (fp)
193         for (mes = message; *mes; mes++)
194             fputs(*mes, fp);
195 }
196
197 /*
198  * getopt_argv()
199  */
200 static void
201 getopt_argv(char **argv)
202 {
203     int c;
204
205     Cha_optind = 0;
206     while ((c = cha_getopt_chasen(argv, stderr)) != EOF) {
207         switch (c) {
208         case 's':
209             is_partial = 1;
210             break;
211         case 'r':               /* chasenrc file */
212             cha_set_rcpath(Cha_optarg);
213             break;
214         case 'R':               /* don't read chasenrc file */
215             cha_set_rcpath("*");
216             break;
217         case 'o':
218             output_file = Cha_optarg;
219             break;
220         case 'F':
221             /*
222              * -Fh: print help of -F 
223              */
224             if (Cha_optarg[0] == 'h' && Cha_optarg[1] == '\0') {
225                 opt_form_usage(stdout);
226                 exit(0);
227             }
228             break;
229         case 'V':
230             cha_version(stdout);
231             exit(0);
232         case 'h':
233             usage(stdout);
234             exit(0);
235         case '?':
236             fprintf(stderr, "Try `%s -h' for more information.\n",
237                     CHA_PROG);
238             exit(1);
239         }
240     }
241 }
242
243 /*
244  * do_chasen_standalone()
245  */
246 static void
247 do_chasen_standalone(FILE * ifp, FILE * ofp)
248 {
249     int istty;
250
251     /*
252      * output: whether `stdout' or not 
253      */
254     istty = ofp == stdout && isatty(fileno(stdout));
255
256     if (is_partial) {
257         chasen_parse_segments(ifp, ofp);
258     } else {
259         while (!chasen_fparse(ifp, ofp))
260             if (!istty)
261                 fflush(ofp);
262     }
263 }
264
265 /*
266  * chasen_standalone()
267  *
268  * return: exit code
269  */
270 static int
271 chasen_standalone(char **argv, FILE * output)
272 {
273     /*
274      * standalone 
275      */
276     if (chasen_getopt_argv(argv, stderr))
277         return 1;
278     argv += Cha_optind;
279
280     if (*argv == NULL)
281         do_chasen_standalone(stdin, output);
282     else
283         for (; *argv; argv++)
284             do_chasen_standalone(cha_fopen(*argv, "r", 1), output);
285
286     return 0;
287 }
288
289 /*
290  * main()
291  */
292 int
293 main(int argc, char *argv[])
294 {
295     int rc;
296     FILE *output;
297
298     cha_set_progpath(argv[0]);
299
300     getopt_argv(argv);
301
302     output = output_file ? cha_fopen(output_file, "w", 1) : stdout;
303     rc = chasen_standalone(argv, output);
304     if (output != stdout)
305         fclose(output);
306     return rc;
307 }