summaryrefslogtreecommitdiffstats
path: root/tests/storage/test_parser2.cpp
blob: b0a178728386f79fff86b0fcbea03110b910e34f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/* 
 *  libpinyin
 *  Library to deal with pinyin.
 *  
 *  Copyright (C) 2011 Peng Wu <alexepico@gmail.com>
 *  
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 * 
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 *  GNU General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */


#include "timer.h"
#include <errno.h>
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include "pinyin_internal.h"


static const gchar * parsername = "";
static gboolean incomplete = FALSE;
static const gchar * schemename = "";

static GOptionEntry entries[] =
{
    {"parser", 'p', 0, G_OPTION_ARG_STRING, &parsername, "parser", "fullpinyin doublepinyin zhuyin pinyindirect zhuyindirect"},
    {"incomplete", 'i', 0, G_OPTION_ARG_NONE, &incomplete, "incomplete pinyin", NULL},
    {"scheme", 's', 0, G_OPTION_ARG_STRING, &schemename, "scheme", "standard hsu dachen26"},
    {NULL}
};


size_t bench_times = 1000;

using namespace pinyin;


int main(int argc, char * argv[]) {
    GError * error = NULL;
    GOptionContext * context;

    context = g_option_context_new("- test pinyin parser");
    g_option_context_add_main_entries(context, entries, NULL);
    if (!g_option_context_parse(context, &argc, &argv, &error)) {
        g_print("option parsing failed:%s\n", error->message);
        exit(EINVAL);
    }

    pinyin_option_t options = PINYIN_CORRECT_ALL | USE_TONE | USE_RESPLIT_TABLE;
    if (incomplete)
        options |= PINYIN_INCOMPLETE | ZHUYIN_INCOMPLETE;

    PhoneticParser2 * parser = NULL;
    ChewingKeyVector keys = g_array_new(FALSE, FALSE, sizeof(ChewingKey));
    ChewingKeyRestVector key_rests =
        g_array_new(FALSE, FALSE, sizeof(ChewingKeyRest));

    /* create the parser */
    if (strcmp("fullpinyin", parsername) == 0) {
        parser = new FullPinyinParser2();
    } else if (strcmp("doublepinyin", parsername) == 0) {
        parser = new DoublePinyinParser2();
    } else if (strcmp("zhuyin", parsername) == 0) {
        if (strcmp("standard", schemename) == 0) {
            parser = new ZhuyinSimpleParser2();
        } else if (strcmp("hsu", schemename) == 0) {
            parser = new ZhuyinDiscreteParser2();
        } else if (strcmp("dachen26", schemename) == 0) {
            parser = new ZhuyinDaChenCP26Parser2();
        }
    } else if (strcmp("pinyindirect", parsername) == 0) {
        parser = new PinyinDirectParser2();
    } else if (strcmp("zhuyindirect", parsername) == 0) {
        parser = new ZhuyinDirectParser2();
    }


    if (!parser)
        parser = new FullPinyinParser2();

    char* linebuf = NULL; size_t size = 0; ssize_t read;
    while( (read = getline(&linebuf, &size, stdin)) != -1 ){
        if ( '\n' == linebuf[strlen(linebuf) - 1] ) {
            linebuf[strlen(linebuf) - 1] = '\0';
        }

        if ( strcmp ( linebuf, "quit" ) == 0)
            break;

#if 0
        ChewingKey key;
        bool success = parser->parse_one_key(options, key,
                                             linebuf, strlen(linebuf));
        if (success) {
            gchar * pinyins = key.get_pinyin_string();
            printf("pinyin:%s\n", pinyins);
            g_free(pinyins);
        }
#endif

#if 1
        int len = 0;
        guint32 start_time = record_time();
        for ( size_t i = 0; i < bench_times; ++i)
            len = parser->parse(options, keys, key_rests,
                                linebuf, strlen(linebuf));

        print_time(start_time, bench_times);

        printf("parsed %d chars, %d keys.\n", len, keys->len);

        assert(keys->len == key_rests->len);

        for (size_t i = 0; i < keys->len; ++i) {
            ChewingKey * key =
                &g_array_index(keys, ChewingKey, i);
            ChewingKeyRest * key_rest =
                &g_array_index(key_rests, ChewingKeyRest, i);

            gchar * pinyins = key->get_pinyin_string();
            printf("%s %d %d\t", pinyins,
                   key_rest->m_raw_begin, key_rest->m_raw_end);
            g_free(pinyins);
        }
        printf("\n");
#endif

    }

    if (linebuf)
        free(linebuf);

    delete parser;

    g_array_free(key_rests, TRUE);
    g_array_free(keys, TRUE);

    return 0;
}