00001
00002
00003
00004
00005
00006
00007 #include "openLogger.h"
00008 #include "connectstring"
00009 #include "cgiClass.h"
00010 #include "ocXML.h"
00011
00012 string baseUrl;
00013 #include "page.hpp"
00014 #include "page_control.hpp"
00015 #include <time.h>
00016 #include <vector>
00017 using namespace std;
00018
00019 typedef vector<string> words;
00020 class rant
00021 {
00022 public:
00023 string buf;
00024 words attributes;
00025 words predicates;
00026 words nouns;
00027 words adverbs;
00028 words hedge;
00029 words clause;
00030 words prep;
00031 words pred_prep;
00032 enum tense { Present, Past } Tense;
00033 int nsentences;
00034 int Lpos;
00035 bool Intitle, Inclause, Inpred;
00036 rant():Tense(present),nsentences(50),
00037
00038 {
00039 ifstream grammer("grammer.xml");
00040 xmlParser gparse(grammer);
00041 gparse.parse();
00042 gparse.states.nodeMap["attributes"];
00043
00044 time_t lt;
00045 time(<);
00046 srand(lt);
00047 }
00048 ~rant(){;}
00049
00050
00051 inline string & pick(words & ary)
00052 {
00053 return ary[rand()%(ary.length())];
00054 }
00055
00056 void generate( ostream & away )
00057 {
00058 title(away);
00059
00060 }
00061
00062 void title (ostream & away)
00063 {
00064 Intitle = 1;
00065 buf = "The ";
00066 buf += pick(nouns);
00067 buf += "."
00068 away << output ();
00069 buf = "";
00070 attribute();
00071 buf += " ";
00072 buf += pick(nouns);
00073 buf += " ";
00074 buf += pick(prep));
00075 buf += " ";
00076 nounphr();
00077 Intitle = 0;
00078 }
00079 void sentence (ostream & away)
00080 {
00081 if (rand() % 3) put_clause (buf);
00082 nounphr (buf);
00083 buf += " ";
00084 be (buf);
00085 predicate (buf);
00086 away << output();
00087 }
00088 void nounphr (away)
00089 {
00090 string noun = pick(nouns);
00091 if (index(noun, ' ') || Intitle || !(random() % 4))
00092 buf = !(random() % 2)? "the ": (isvowel(noun[0]))? "an ": "a ";
00093 else
00094 {
00095 attribute (buf);
00096 buf += " ";
00097 }
00098 buf += noun;
00099 if (!Intitle && !Inpred && !(random() % 3))
00100 {
00101 buf += " ";
00102 buf += pick(prep);
00103 buf += " ";
00104 nounphr (buf);
00105 }
00106 }
00107 attribute (char *buf)
00108 {
00109 register char *nextw, *attr, *adv = NULL;
00110
00111 nextw = attr = PICK1(attributes);
00112 if (index(attr, ' ') == NULL && !(random() % 3))
00113 nextw = adv = PICK1(adverbs);
00114 strcat (buf, !Intitle && !(random() % 2)? "the ":
00115 (isvowel(*nextw))? "an ": "a ");
00116 if (adv) {
00117 strcat (buf, adv);
00118 strcat (buf, " ");
00119 }
00120 strcat (buf, attr);
00121 }
00122
00123 be (char *buf)
00124 {
00125 register char *verb;
00126
00127 verb = Tense == PRESENT? "is ":
00128 Tense == PAST? "was ":
00129 random() % 3? "is " :
00130 "was ";
00131 Tense = *verb == 'i'? PRESENT: PAST;
00132 strcat (buf, verb);
00133 if (!(random() % 3))
00134 strcat (buf, "not ");
00135 }
00136
00137 predicate (char *buf)
00138 {
00139 register char *pred;
00140
00141 Inpred = 1;
00142 if (random() % 3) {
00143 if (!(random() % 10))
00144 strcat (buf, "known to be ");
00145 pred = PICK1(predicates);
00146 if (index(pred, ' ') == NULL && !(random() % 2)) {
00147 strcat (buf, PICK1(adverbs));
00148 strcat (buf, " ");
00149 }
00150 strcat (buf, pred);
00151 }
00152 else {
00153 strcat (buf, PICK1(pred_prep));
00154 strcat (buf, " ");
00155 nounphr (buf);
00156 }
00157 Inpred = 0;
00158 }
00159
00160 put_clause (char *buf)
00161 {
00162 if (!Inclause) {
00163 Inclause = 1;
00164 if (random() % 3) {
00165 strcat (buf, PICK1(hedge));
00166 strcat (buf, " ");
00167 Tense = PRESENT;
00168 }
00169 else {
00170 strcat (buf, PICK1(clause));
00171 strcat (buf, " ");
00172 sentence (buf);
00173 strcat (buf, ", ");
00174 }
00175 Inclause = 0;
00176 }
00177 }
00178
00179 output( char *bufbuf)
00180 {
00181 register char *sp, *p;
00182
00183 if (*buf == '\1')
00184 buf++;
00185 if (islower(*buf))
00186 *buf = toupper (*buf);
00187 if (!Intitle)
00188 strcat (buf, ".");
00189 for (sp = buf; *sp; sp++) {
00190 if (*sp == '\1')
00191 continue;
00192 if (sp == buf || *sp == ' ') {
00193 for (p = sp + 1; *p && *p != ' '; p++);
00194 if (Lpos + (p - sp) + (sp == buf? 2: 0) >= 72) {
00195 putchar('\n');
00196 Lpos = 0;
00197 if (*sp == ' ')
00198 continue;
00199 }
00200 else if (sp == buf && Lpos > 0) {
00201 putchar(' ');
00202 if (!Intitle)
00203 putchar(' ');
00204 }
00205 }
00206 putchar(*sp);
00207 Lpos++;
00208 }
00209 }
00210
00211 } ;
00212
00213
00214 int main(int argc, char ** argv)
00215 {
00216 baseUrl = "pages.meta";
00217 writelog( "instance of script" );
00218 cgiScript script;
00219 writelog( "instance of page" );
00220 page pg(script);
00221 writelog( "load page" );
00222 if( pg.load() )
00223 {
00224 writelog( "instance of page control" );
00225 page_control ctrl(pg);
00226 writelog( "page control emit" );
00227 ctrl.emit ();
00228 }
00229 else
00230 {
00231 script << "<html><head><title>Page Unavailable</title></head>" << endl
00232 << "<body><h1>Sorry</h1>" << endl
00233 << "<p>The page you requested is not available</p></body></html>";
00234 }
00235 return 0;
00236 }