presage
0.8.7
|
00001 00002 /****************************************************** 00003 * Presage, an extensible predictive text entry system 00004 * --------------------------------------------------- 00005 * 00006 * Copyright (C) 2008 Matteo Vescovi <matteo.vescovi@yahoo.co.uk> 00007 00008 This program is free software; you can redistribute it and/or modify 00009 it under the terms of the GNU General Public License as published by 00010 the Free Software Foundation; either version 2 of the License, or 00011 (at your option) any later version. 00012 00013 This program is distributed in the hope that it will be useful, 00014 but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00016 GNU General Public License for more details. 00017 00018 You should have received a copy of the GNU General Public License along 00019 with this program; if not, write to the Free Software Foundation, Inc., 00020 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 00021 * 00022 **********(*)*/ 00023 00024 00025 #include "presage.h" 00026 00027 #ifdef HAVE_CONFIG_H 00028 # include "config.h" 00029 #endif 00030 00031 #ifdef HAVE_STDLIB_H 00032 # include <stdlib.h> 00033 #endif 00034 00035 #include <getopt.h> 00036 00037 #include <iostream> 00038 #include <sstream> 00039 00040 const char PROGRAM_NAME[] = "presage_demo_text"; 00041 00042 void disclaimer (); 00043 void parse_cmd_line_args (int argc, char** argv); 00044 void print_version (); 00045 void print_usage (); 00046 void print_prediction (const std::vector<std::string>&); 00047 00048 std::string config; 00049 int suggestions = 0; 00050 00051 int main(int argc, char** argv) 00052 { 00053 parse_cmd_line_args (argc, argv); 00054 disclaimer (); 00055 00056 // magic starts here... 00057 LegacyPresageCallback callback; 00058 Presage presage (&callback, config); 00059 00060 00061 if (suggestions) { 00062 // convert int to string using a stringstream 00063 std::stringstream ss; 00064 ss << suggestions; 00065 presage.config("Presage.Selector.SUGGESTIONS", ss.str()); 00066 } 00067 00068 // buffer to read user input 00069 const int BUFFER_SIZE = 80; 00070 char buffer[ BUFFER_SIZE ]; 00071 00072 for (;;) { 00073 std::cout << "> "; // prompt the user 00074 std::cin.getline (buffer, BUFFER_SIZE); // read in string (if any) 00075 00076 callback.update(buffer); // update internal buffer 00077 print_prediction( 00078 presage.predict() // request new prediction 00079 ); 00080 std::cout << "-- Context: " << presage.context() << '|' << std::endl; 00081 if (presage.context_change()) { 00082 std::cout << "-- Context changed" << std::endl; 00083 } 00084 } 00085 00086 return 0; 00087 } 00088 00089 00090 void disclaimer () 00091 { 00092 std::cout << 00093 "Presage Textual Demo\n" 00094 "--------------------\n" 00095 "\n" 00096 "This program is intended as a demonstration of Presage ONLY.\n" 00097 "\n" 00098 "The Presage project aims to provide an intelligent predictive text entry platform.\n" 00099 "\n" 00100 "Its intent is NOT to provide a predictive text entry user interface.\n" 00101 "Think of Presage as the predictive backend that sits behind a shiny user interface and does all the predictive heavy lifting.\n" 00102 "\n" << std::endl; 00103 } 00104 00105 void parse_cmd_line_args (int argc, char* argv[]) 00106 { 00107 int next_option; 00108 00109 // getopt structures 00110 const char* const short_options = "c:s:hv"; 00111 00112 const struct option long_options[] = { 00113 { "config", required_argument, 0, 'c' }, 00114 { "suggestions", required_argument, 0, 's' }, 00115 { "help", no_argument, 0, 'h' }, 00116 { "version", no_argument, 0, 'v' }, 00117 { 0, 0, 0, 0 } 00118 }; 00119 00120 do { 00121 next_option = getopt_long( argc, argv, 00122 short_options, long_options, NULL ); 00123 00124 switch( next_option ) { 00125 case 'c': // --config or -c option 00126 config = optarg; 00127 break; 00128 case 's': // --suggestions or -s option 00129 suggestions = atoi(optarg); 00130 break; 00131 case 'h': // --help or -h option 00132 print_usage (); 00133 exit (0); 00134 break; 00135 case 'v': // --version or -v option 00136 print_version (); 00137 exit (0); 00138 break; 00139 case '?': // unknown option 00140 print_usage(); 00141 exit (0); 00142 break; 00143 case -1: 00144 break; 00145 default: 00146 abort (); 00147 break; 00148 } 00149 00150 } while (next_option != -1); 00151 } 00152 00153 void print_prediction (const std::vector<std::string>& words) 00154 { 00155 for( std::vector<std::string>::const_iterator i = words.begin(); 00156 i != words.end(); 00157 i++ ) { 00158 std::cout << *i << std::endl; 00159 } 00160 } 00161 00162 void print_version () 00163 { 00164 std::cout << PROGRAM_NAME << " (" << PACKAGE << ") version " << VERSION << std::endl 00165 << "Copyright (C) 2004 Matteo Vescovi." << std::endl 00166 << "This is free software; see the source for copying conditions. There is NO" << std::endl 00167 << "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE," << std::endl 00168 << "to the extent permitted by law." << std::endl; 00169 } 00170 00171 void print_usage () 00172 { 00173 std::cout << "Usage: " << PROGRAM_NAME << " [OPTION]..." << std::endl 00174 << std::endl 00175 << "At the prompt, type in some text. Hit enter to generate a prediction." << std::endl 00176 << "Any text input is valid, including no text, a single character, or a long string." << std::endl 00177 << std::endl 00178 << " -c, --config CONFIG use config file CONFIG" << std::endl 00179 << " -s, --suggestions N set prediction size to N suggestions" << std::endl 00180 << " -h, --help display this help and exit" << std::endl 00181 << " -v, --version output version information and exit" << std::endl 00182 << std::endl 00183 << "Direct your bug reports to: " << PACKAGE_BUGREPORT << std::endl; 00184 }