~ubuntu-branches/ubuntu/trusty/presage/trusty-proposed

« back to all changes in this revision

Viewing changes to src/tools/presageDemoText.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Matteo Vescovi
  • Date: 2011-08-06 09:26:15 UTC
  • Revision ID: james.westby@ubuntu.com-20110806092615-0wvhajaht9974ncx
Tags: upstream-0.8.6
ImportĀ upstreamĀ versionĀ 0.8.6

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
/******************************************************
 
3
 *  Presage, an extensible predictive text entry system
 
4
 *  ---------------------------------------------------
 
5
 *
 
6
 *  Copyright (C) 2008  Matteo Vescovi <matteo.vescovi@yahoo.co.uk>
 
7
 
 
8
    This program is free software; you can redistribute it and/or modify
 
9
    it under the terms of the GNU General Public License as published by
 
10
    the Free Software Foundation; either version 2 of the License, or
 
11
    (at your option) any later version.
 
12
 
 
13
    This program is distributed in the hope that it will be useful,
 
14
    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
15
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
16
    GNU General Public License for more details.
 
17
 
 
18
    You should have received a copy of the GNU General Public License along
 
19
    with this program; if not, write to the Free Software Foundation, Inc.,
 
20
    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 
21
                                                                             *
 
22
                                                                **********(*)*/
 
23
 
 
24
 
 
25
#include "presage.h"
 
26
 
 
27
#ifdef HAVE_CONFIG_H
 
28
# include "config.h"
 
29
#endif
 
30
 
 
31
#ifdef HAVE_STDLIB_H
 
32
# include <stdlib.h>
 
33
#endif
 
34
 
 
35
#include <getopt.h>
 
36
 
 
37
#include <iostream>
 
38
#include <sstream>
 
39
 
 
40
const char PROGRAM_NAME[] = "presage_demo_text";
 
41
 
 
42
void disclaimer ();
 
43
void parse_cmd_line_args (int argc, char** argv);
 
44
void print_version ();
 
45
void print_usage ();
 
46
void print_prediction (const std::vector<std::string>&);
 
47
 
 
48
std::string config;
 
49
int suggestions = 0;
 
50
 
 
51
int main(int argc, char** argv)
 
52
{
 
53
    parse_cmd_line_args (argc, argv);
 
54
    disclaimer ();
 
55
 
 
56
    // magic starts here...
 
57
    LegacyPresageCallback callback;
 
58
    Presage presage (&callback, config);
 
59
 
 
60
 
 
61
    if (suggestions) {
 
62
        // convert int to string using a stringstream
 
63
        std::stringstream ss;
 
64
        ss << suggestions;
 
65
        presage.config("Presage.Selector.SUGGESTIONS", ss.str());
 
66
    }
 
67
 
 
68
    // buffer to read user input
 
69
    const int BUFFER_SIZE = 80;
 
70
    char buffer[ BUFFER_SIZE ];
 
71
 
 
72
    for (;;) {
 
73
        std::cout << "> ";                       // prompt the user
 
74
        std::cin.getline (buffer, BUFFER_SIZE);  // read in string (if any)
 
75
 
 
76
        callback.update(buffer);                 // update internal buffer
 
77
        print_prediction(
 
78
                         presage.predict()       // request new prediction
 
79
                         );
 
80
        std::cout << "-- Context: " << presage.context() << '|' << std::endl;
 
81
        if (presage.context_change()) {
 
82
            std::cout << "-- Context changed" << std::endl;
 
83
        }
 
84
    }
 
85
 
 
86
    return 0;
 
87
}
 
88
 
 
89
 
 
90
void disclaimer ()
 
91
{
 
92
    std::cout <<
 
93
        "Presage Textual Demo\n"
 
94
        "--------------------\n"
 
95
        "\n"
 
96
        "This program is intended as a demonstration of Presage ONLY.\n"
 
97
        "\n"
 
98
        "The Presage project aims to provide an intelligent predictive text entry platform.\n"
 
99
        "\n"
 
100
        "Its intent is NOT to provide a predictive text entry user interface.\n"
 
101
        "Think of Presage as the predictive backend that sits behind a shiny user interface and does all the predictive heavy lifting.\n"
 
102
        "\n" << std::endl;
 
103
}
 
104
 
 
105
void parse_cmd_line_args (int argc, char* argv[])
 
106
{
 
107
    int next_option;
 
108
 
 
109
    // getopt structures
 
110
    const char* const short_options = "c:s:hv";
 
111
 
 
112
    const struct option long_options[] = {
 
113
        { "config",       required_argument, 0, 'c' },
 
114
        { "suggestions",  required_argument, 0, 's' },
 
115
        { "help",         no_argument,       0, 'h' },
 
116
        { "version",      no_argument,       0, 'v' },
 
117
        { 0, 0, 0, 0 }
 
118
    };
 
119
 
 
120
    do {
 
121
        next_option = getopt_long( argc, argv, 
 
122
                                   short_options, long_options, NULL );
 
123
 
 
124
        switch( next_option ) {
 
125
        case 'c': // --config or -c option
 
126
            config = optarg;
 
127
            break;
 
128
        case 's': // --suggestions or -s option
 
129
            suggestions = atoi(optarg);
 
130
            break;
 
131
        case 'h': // --help or -h option
 
132
            print_usage ();
 
133
            exit (0);
 
134
            break;
 
135
        case 'v': // --version or -v option
 
136
            print_version ();
 
137
            exit (0);
 
138
            break;
 
139
        case '?': // unknown option
 
140
            print_usage();
 
141
            exit (0);
 
142
            break;
 
143
        case -1:
 
144
            break;
 
145
        default:
 
146
            abort ();
 
147
            break;
 
148
        }
 
149
 
 
150
    } while (next_option != -1);
 
151
}
 
152
 
 
153
void print_prediction (const std::vector<std::string>& words)
 
154
{
 
155
    for( std::vector<std::string>::const_iterator i = words.begin();
 
156
         i != words.end();
 
157
         i++ ) {
 
158
        std::cout << *i << std::endl;
 
159
    }
 
160
}
 
161
 
 
162
void print_version ()
 
163
{
 
164
    std::cout << PROGRAM_NAME << " (" << PACKAGE << ") version " << VERSION << std::endl
 
165
              << "Copyright (C) 2004 Matteo Vescovi." << std::endl
 
166
              << "This is free software; see the source for copying conditions.  There is NO" << std::endl
 
167
              << "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE," << std::endl
 
168
              << "to the extent permitted by law." << std::endl;
 
169
}
 
170
 
 
171
void print_usage ()
 
172
{
 
173
    std::cout << "Usage: " << PROGRAM_NAME << " [OPTION]..." << std::endl
 
174
              << std::endl
 
175
              << "At the prompt, type in some text. Hit enter to generate a prediction." << std::endl
 
176
              << "Any text input is valid, including no text, a single character, or a long string." << std::endl
 
177
              << std::endl
 
178
              << "  -c, --config CONFIG  use config file CONFIG" << std::endl
 
179
              << "  -s, --suggestions N  set prediction size to N suggestions" << std::endl
 
180
              << "  -h, --help           display this help and exit" << std::endl
 
181
              << "  -v, --version        output version information and exit" << std::endl
 
182
              << std::endl
 
183
              << "Direct your bug reports to: " << PACKAGE_BUGREPORT << std::endl;
 
184
}