using System; using System.ComponentModel; using System.Diagnostics; using System.Drawing; using System.Collections; using System.Windows.Forms; using System.Data; using System.IO; using System.Net; using System.Text; using System.Text.RegularExpressions; using System.Web; using System.Web.SessionState; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls; namespace GPoem { /// /// Summary description for Form1. /// public class Form1 : System.Windows.Forms.Form { /// /// Required designer variable. /// protected const string my_license = "---edited out---"; protected const string FileName = "excite.txt"; protected const int MAX_RESULT = 10; private Hashtable hash = new Hashtable(); private System.Windows.Forms.Label lblRating; private System.Windows.Forms.Button button1; private System.Windows.Forms.TextBox txtPhrase; private System.Windows.Forms.Label lblTitle; private System.ComponentModel.Container components = null; private string poemname = ""; private System.Random myRandom; public Form1() { // // Required for Windows Form Designer support // InitializeComponent(); myRandom = new Random(); } /// /// Clean up any resources being used. /// protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows Form Designer generated code /// /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// private void InitializeComponent() { this.lblRating = new System.Windows.Forms.Label(); this.button1 = new System.Windows.Forms.Button(); this.txtPhrase = new System.Windows.Forms.TextBox(); this.lblTitle = new System.Windows.Forms.Label(); this.SuspendLayout(); // // lblRating // this.lblRating.Location = new System.Drawing.Point(16, 128); this.lblRating.Name = "lblRating"; this.lblRating.Size = new System.Drawing.Size(672, 16); this.lblRating.TabIndex = 0; // // button1 // this.button1.BackColor = System.Drawing.SystemColors.Control; this.button1.Location = new System.Drawing.Point(16, 72); this.button1.Name = "button1"; this.button1.Size = new System.Drawing.Size(104, 23); this.button1.TabIndex = 1; this.button1.Text = "Submit Phrase"; this.button1.Click += new System.EventHandler(this.button1_Click); // // txtPhrase // this.txtPhrase.Location = new System.Drawing.Point(128, 72); this.txtPhrase.Name = "txtPhrase"; this.txtPhrase.Size = new System.Drawing.Size(288, 20); this.txtPhrase.TabIndex = 2; this.txtPhrase.Text = ""; // // lblTitle // this.lblTitle.Font = new System.Drawing.Font("Cooper Black", 36F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0))); this.lblTitle.Location = new System.Drawing.Point(56, 8); this.lblTitle.Name = "lblTitle"; this.lblTitle.Size = new System.Drawing.Size(592, 56); this.lblTitle.TabIndex = 4; this.lblTitle.Text = "Exciting Google Poetry"; // // Form1 // this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.BackColor = System.Drawing.SystemColors.ActiveCaptionText; this.ClientSize = new System.Drawing.Size(704, 190); this.Controls.AddRange(new System.Windows.Forms.Control[] { this.lblTitle, this.txtPhrase, this.button1, this.lblRating}); this.Name = "Form1"; this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; this.Text = "Google Poetry"; this.Load += new System.EventHandler(this.Form1_Load); this.ResumeLayout(false); } #endregion /// /// The main entry point for the application. /// [STAThread] static void Main() { Application.Run(new Form1()); } private string addText(string URL) { string page = ""; string line; try { HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(URL); HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse(); System.IO.Stream responseStream = myResponse.GetResponseStream(); Encoding encode = System.Text.Encoding.GetEncoding("utf-8"); System.IO.StreamReader responseReader = new System.IO.StreamReader(responseStream, encode); while((line = responseReader.ReadLine()) != null) { page += " " + line; } myResponse.Close(); } catch(System.Exception) { } return page; } private int displayResults(Hashtable wf, string theWord) { lblRating.Visible = true; lblRating.Text = theWord + " has an excitement rating of "; int score = 0; int totalWords = 0; int unique = 0; foreach(string key in wf.Keys) { totalWords += (int)wf[key]; unique++; if(hash.ContainsKey(key)) { score += (int)hash[key]; } } lblRating.Text = lblRating.Text + score.ToString() + " from " + totalWords.ToString() + " words (" + unique.ToString() + " unique words)"; return score; } private void addValues(Hashtable myhash, string page) { bool skip = false; string word = ""; for(int i = 0; i < page.Length; i++) { if(page[i] == '<') skip = true; if (!skip) { if(Regex.IsMatch(page.Substring(i,1),"^[a-zA-Z]")) { word.Insert(word.Length, page.Substring(i,1)); word = word + page.Substring(i,1); } else if((word.Length > 0)&&(word != "nbsp")) { word = word.ToLower(); if(myhash.ContainsKey(word)) { int c = (int)myhash[word]; c++; myhash[word] = c; } else myhash.Add(word, 1); word = ""; } else if(word == "nbsp") { word = ""; } } if(page[i] == '>') skip = false; } } private void writePoem(string[] allPages, string theWord, int score) { System.IO.FileStream fw = new FileStream(theWord+".htm", FileMode.Create, FileAccess.Write, FileShare.None); string theLine = ""; bool gotLine = false; string poem = "\n"+theWord+"\n\n"; poem += "

"+theWord+" has an excitement rating of "+score.ToString()+"

"; poem += ""+theWord+"\n"; IDictionaryEnumerator enumerator = hash.GetEnumerator(); for(int i=0; i < allPages.Length; i++) { int lines = 0; int done = 0; poem += "

"; enumerator.MoveNext(); while((done < 2)&&(lines < 5)) { string line = ""; int keyIndex = allPages[i].IndexOf(" "+enumerator.Key.ToString()+" "); if(keyIndex > -1) { int end = 0; while(!Regex.IsMatch(allPages[i].Substring(keyIndex+end,1), "[.!?<(\"\n)]")) { end++; } line = allPages[i].Length < (keyIndex + end) ? allPages[i].Substring(keyIndex, (allPages[i].Length-1 - keyIndex)):allPages[i].Substring(keyIndex, end); poem += line + "
\n"; lines++; if(!gotLine) { if(myRandom.Next(10) == 1) { gotLine = true; theLine = line; } } } if(!enumerator.MoveNext()) { enumerator.Reset(); enumerator.MoveNext(); done++; } } poem += "

\n\n\n"; } byte[] poetry = System.Text.Encoding.ASCII.GetBytes(poem); fw.Write(poetry, 0, poetry.Length); fw.Close(); poemname = theWord+".htm"; Process.Start(poemname); if(gotLine) { DialogResult dr = MessageBox.Show("Generate another poem?", "Infinite Loop Prevention", MessageBoxButtons.YesNo); if(dr == DialogResult.Yes) { lblRating.Text = "Poem generation in progress."; lblRating.Refresh(); submitQuery(theLine); } } } private void handleResult(google.GoogleSearchResult gsr, string theWord) { string[] allPages = new string[gsr.endIndex-1]; int result; Hashtable wordFrequency = new Hashtable(); for(int i = 1; i < gsr.endIndex; i++) { allPages[i-1] = addText(gsr.resultElements[i].URL); addValues(wordFrequency, allPages[i-1]); } result = displayResults(wordFrequency, theWord); writePoem(allPages, theWord, result); } private void submitQuery(System.String phrase) { google.GoogleSearchService s = new google.GoogleSearchService(); google.GoogleSearchResult r = new google.GoogleSearchResult(); try { r = s.doGoogleSearch(my_license, phrase, 0, MAX_RESULT, false, "", false, "", "", ""); } catch (System.Web.Services.Protocols.SoapException ex) { System.Windows.Forms.MessageBox.Show(ex.Message); } if (r.endIndex == 0) { lblRating.Text = "Google knows not of what you speak"; lblRating.Visible = true; } else { handleResult(r, phrase); } } private void Form1_Load(object sender, System.EventArgs e) { if (File.Exists(FileName)) { string delstr = ","; char[] delim = delstr.ToCharArray(); StreamReader fileReader = new StreamReader(FileName); string line; while ((line = fileReader.ReadLine()) != null) { string[] entry = new string[2]; entry = line.Split(delim,2); hash.Add(entry[0], Int32.Parse(entry[1])); } fileReader.Close(); } else { lblRating.Text = "no file"; lblRating.Visible = true; } } private void button1_Click(object sender, System.EventArgs e) { lblRating.Text = "Poem generation in progress."; lblRating.Refresh(); submitQuery(txtPhrase.Text); } } }