context: /*
      mpcreate
      Copyright (C) 2008 Bob Mottram
      fuzzgun@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 3 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, see <http://www.gnu.org/licenses/>.
  */
  
  using System;
  using System.Drawing;
  using System.Collections.Generic;
  using System.IO;
  using System.Collections;
  using sluggish.utilities;
  using System.Text;
  using MySql.Data;	
  using MySql.Data.MySqlClient;
  using ca.guitard.jeff.utility;
  
  namespace mpcreate
  {
  	class MainClass
  	{
  		public static void Main(string[] args)
  		{	
  			//verbs vrb = new verbs();
  			//vrb.LoadFromPropBank("/home/motters/develop/propbank");
  			//vrb.LoadFromVerbNet("/home/motters/develop/verbnet-3.0");
  			//vrb.SaveArray("/home/motters/Desktop/verb_classes.txt");
  			//Console.WriteLine(verbs.GetVerbClasses("does a chair have legs?"));
  			
  			/*
  			Console.WriteLine("105 = " + phoneme.ConvertNumber(105));
  			Console.WriteLine(phoneme.ConvertText("Alan's  psychedelic breakfast"));
  			byte[] formants = phoneme.FormantsNormalisedSimple("This is a test", 16);
  			Console.WriteLine(phoneme.StringFromFormants(formants));
  			for (int i = 0; i < formants.Length; i++)
  				Console.Write(formants[i].ToString() + " ");
  			Console.WriteLine("");
  			
  			int diff = phoneme.Difference("Cat food", "Dog loop", 16, 20);
  			Console.WriteLine("diff = " + diff.ToString());
  			diff = phoneme.Difference("Cake", "Bake", 16, 20);
  			Console.WriteLine("diff = " + diff.ToString());
  			Console.WriteLine(phoneme.ToNgramStandardised("This is a test",3));
  			Console.WriteLine(Soundex.ToSoundexStandardised("This is a test"));
  			*/
  			
  			
  			Console.WriteLine("mpcreate: A utility for creating mindpixels");
  			Console.WriteLine("Version 0.4");
  			
              // default parameters for the database
              string server_name = "localhost";
              string database_name = "mindpixel";
              string user_name = "testuser";
              string password = "password";
              string mp_table_name = "mindpixels";
              string users_table_name = "users";
  			//string soundex_table_name = "wordsoundex";
  			
              string[] mp_fields = {
  				"Id", "BINARY(16) NOT NULL", // that's 16 bytes, not 16 bits!
                  "Hash", "INT",
                  "Question", "TEXT",
                  "YesVotes", "INT",
                  "NoVotes", "INT",
                  "Coherence", "FLOAT",
  				"Ngram3", "FLOAT",
  				"Soundex", "FLOAT",
  				"Verbs", "FLOAT",
  				"NYSIIS", "FLOAT",
  				"MetaphonePrimary", "FLOAT",
  				"MetaphoneSecondary", "FLOAT",
  				"Connections", "FLOAT",
  				"Emotion", "FLOAT"
              };
  
  			string[] users_fields = {
                  "TimeStamp", "DATETIME",
  				"Username", "VARCHAR(100)",
  				"Hash", "INT",
                  "Question", "TEXT",
                  "Answer", "TINYINT"
              };
  
  			string[] soundex_fields = {
  				"SoundexHash", "INT",
                  "MindpixelHash", "INT",
              };
  			
              // the character used to indicate that what follows is a parameter name
              const string switch_character = "-";
  			
              // extract command line parameters
              ArrayList parameters = commandline.ParseCommandLineParameters(args, switch_character, GetValidParameters());
  
              // is help required ?
              string show_help = commandline.GetParameterValue("help", parameters);
              if (show_help == "true")
              {
                  ShowHelp();
              }
              else
              {
  				string conceptnet_rdf = commandline.GetParameterValue("cn", parameters);
  				if (conceptnet_rdf != "")
  				{
  			        conceptnet grab = new conceptnet();
  			        grab.ProcessConceptNet(conceptnet_rdf, "conceptnet.txt");					
  				}
  				else
  				{				
  					string wikipedia_directory = commandline.GetParameterValue("wp", parameters);
  					if (wikipedia_directory != "")
  					{
  				        WikipediaFactGrabber grab = new WikipediaFactGrabber();
  				        grab.ProcessWikipedia(wikipedia_directory, "wikipedia.txt");					
  					}
  					else
  					{				
  						string freebase_directory = commandline.GetParameterValue("fb", parameters);
  						if (freebase_directory != "")
  						{
  							freebase fb = new freebase();
  							fb.ProccessFreebase(freebase_directory, "freebase.txt");
  						}
  						else
  						{
  							CreateEmotionalWords();
  							
  							string server_name_str = commandline.GetParameterValue("server", parameters);
  							if (server_name_str != "") server_name = server_name_str;
  							
  							string user_name_str = commandline.GetParameterValue("username", parameters);
  							if (user_name_str != "") user_name = user_name_str;
  			
  							string password_str = commandline.GetParameterValue("password", parameters);
  							if (password_str != "") password = password_str;						
  							
  							string database_str = commandline.GetParameterValue("db", parameters);
  							if (database_str != "") database_name = database_str;
  														
  			                string load_filename = commandline.GetParameterValue("load", parameters);
  							if (load_filename != "")
  							{
  								
  								//List<string> word = new List<string>();
  								//List<string> word_cooccurrence = new List<string>();
  								
  								//loadGACWordFrequencies(
  					                //server_name,
  					                //database_name,
  					                //user_name,
  					                //password,
  								    //load_filename, 
  								    //"Mind Hack",
  								    //word,
  									//word_cooccurrence);
  								
  								//Console.WriteLine(word.Count.ToString() + " words");
  								//Console.WriteLine(word_cooccurrence.Count.ToString() + " word cooccurrences");								
  								
  								
  								List<string> differential = new List<string>();
  								List<int> differential_frequency = new List<int>();
  								List<string> semantic_differential = new List<string>();
  								
  								loadGACSemanticDifferentials(
  								    server_name,
  								    database_name,
  								    user_name,
  								    password,
  									load_filename, 
  								    "Mind Hack",
  									differential,
  									differential_frequency,
  								    semantic_differential);
  								    
  								
  							    loadGAC(
  					                load_filename, 
  			                        "Mind Hack",
  					                mp_fields,
  					                users_fields,
  								    soundex_fields,
  					                server_name,
  					                database_name, 
  					                user_name, 
  					                password, 
  					                mp_table_name,
  					                users_table_name);
  								    //soundex_table_name);
  
  								
  								//UpdateConnections(
  								    //server_name,
  								    //database_name,
  								    //user_name,
  								    //password,
  								    //mp_table_name,
  									//soundex_table_name);
  									
  							}
  							else
  							{
  				                string random_pixels_filename = commandline.GetParameterValue("random", parameters);
  								if (random_pixels_filename != "")
  								{					
  									int no_of_random_pixels = 1;
  								    SaveRandomMindpixels(random_pixels_filename, server_name, database_name, user_name, password, mp_table_name, no_of_random_pixels);
  								}
  								else
  								{				
  					                string user_pixels_filename = commandline.GetParameterValue("userpixels", parameters);
  									if (user_pixels_filename != "")
  									{
  									    SaveUserPixels(user_pixels_filename, server_name, database_name, user_name, password, users_table_name);
  									}
  									else
  									{				
  						                string save_filename = commandline.GetParameterValue("save", parameters);
  										if (save_filename != "")
  										{
  											SaveMindpixels(save_filename, server_name, database_name, user_name, password, mp_table_name);
  										}
  										else
  										{
  							                string question = commandline.GetParameterValue("q", parameters);
  											if (question != "")
  											{
  							                    string answer = commandline.GetParameterValue("a", parameters);
  												if (answer != "")
  												{
  													bool answer_value = false;
  													answer = answer.ToLower();
  													if ((answer == "yes") || 
  													    (answer == "y") ||
  													    (answer == "true") ||
  													    (answer == "t"))
  														answer_value = true;
  																			
  													int question_hash = GetHashCode(question);
  																														
  										            // insert the field names into a list so that we can easily search it
  										            List<string> mp_fields_to_be_inserted = new List<string>();
  										            List<string> mp_field_type = new List<string>();
  										            for (int i = 0; i < mp_fields.Length; i += 2)
  										            {
  										                mp_fields_to_be_inserted.Add(mp_fields[i]);
  										                mp_field_type.Add(mp_fields[i + 1]);
  										            }
  							
  										            List<string> users_fields_to_be_inserted = new List<string>();
  										            List<string> users_field_type = new List<string>();
  										            for (int i = 0; i < users_fields.Length; i += 2)
  										            {
  										                users_fields_to_be_inserted.Add(users_fields[i]);
  										                users_field_type.Add(users_fields[i + 1]);
  										            }
  
  													List<string> soundex_fields_to_be_inserted = new List<string>();
  										            List<string> soundex_field_type = new List<string>();
  										            for (int i = 0; i < soundex_fields.Length; i += 2)
  										            {
  										                soundex_fields_to_be_inserted.Add(soundex_fields[i]);
  										                soundex_field_type.Add(soundex_fields[i + 1]);
  										            }
  													
  										            // create tables if necessary
  										            CreateTable(
  										                server_name,
  										                database_name,
  										                user_name,
  										                password,
  										                mp_table_name,
  										                mp_fields_to_be_inserted,
  										                mp_field_type, 0,1,5);
  																			
  										            CreateTable(
  										                server_name,
  										                database_name,
  										                user_name,
  										                password,
  										                users_table_name,
  										                users_fields_to_be_inserted,
  										                users_field_type,-1,0,1);
  
  													
  													//CreateTable(
  										                //server_name,
  										                //database_name,
  										                //user_name,
  										                //password,
  										                //soundex_table_name,
  										                //soundex_fields_to_be_inserted,
  										                //soundex_field_type,-1,0,1);
  
  											        //InsertWordsIntoMySql(
  													    //question_hash,
  											            //question,
  											            //server_name,
  											            //database_name,
  											            //user_name,
  											            //password,
  											            //soundex_table_name,
  											            //soundex_fields_to_be_inserted,
  											            //soundex_field_type);
  													
  									                InsertMindpixelIntoMySql(
  													    question_hash,
  									                    question,
  									                    answer_value,
  									                    server_name,
  									                    database_name,
  									                    user_name,
  									                    password,
  									                    mp_table_name,
  									                    mp_fields_to_be_inserted,
  									                    mp_field_type);
  																			
  									                InsertMindpixelUserDataIntoMySql(
  													    question_hash,
  									                    question,
  									                    answer_value,
  									                    server_name,
  									                    database_name,
  									                    user_name,
  									                    password,
  									                    users_table_name,
  									                    users_fields_to_be_inserted,
  									                    users_field_type);
  													
  													Console.WriteLine("Mindpixel added");
  												}
  												else
  												{
  							  					    Console.WriteLine("Please specify a yes/no answer using the -a option");
  												}
  											}
  											else
  											{
  												//Console.WriteLine("Please specify a question using the -q option");
  											}
  										}
  									}
  								}
  							}
  							
  							int plot_type = 0;
  							string plot_type_str = commandline.GetParameterValue("plot", parameters);
  							if (plot_type_str != "")
  							{
  								plot_type = Convert.ToInt32(plot_type_str);
  							}
  							
  							string map_filename = commandline.GetParameterValue("map", parameters);
  							if (map_filename != "")
  							{
  								float[] coherence = null;
  								int[] hash1 = null;
  								int[] hash2 = null;
  								int[] hash3 = null;
  								float[] index1 = null;
  								float[] index2 = null;
  								float[] index3 = null;
  								Console.WriteLine("Saving map...");
  								ShowPlot(
  					                server_name,
  					                database_name, 
  					                user_name, 
  					                password,
  								    1000,
  								    map_filename,
  								    false,
  								    plot_type,
  								    ref coherence,
  								    ref hash1,
  								    ref hash2,
  								    ref hash3,
  								    ref index1,
  								    ref index2,
  								    ref index3);
  								Console.WriteLine("Saved " + map_filename);
  							}
  							
  							string map_mono_filename = commandline.GetParameterValue("mapmono", parameters);
  							if (map_mono_filename != "")
  							{
  								float[] coherence = null;
  								int[] hash1 = null;
  								int[] hash2 = null;
  								int[] hash3 = null;
  								float[] index1 = null;
  								float[] index2 = null;
  								float[] index3 = null;
  								Console.WriteLine("Saving map...");
  								ShowPlot(
  					                server_name,
  					                database_name, 
  					                user_name, 
  					                password,
  								    1000,
  								    map_mono_filename,
  								    false,
  								    plot_type,
  								    ref coherence,
  								    ref hash1,
  								    ref hash2,
  								    ref hash3,
  								    ref index1,
  								    ref index2,
  								    ref index3);
  								Console.WriteLine("Saved " + map_filename);
  							}
  							
  							string lookup_tables_filename = commandline.GetParameterValue("lookup", parameters);
  							if (lookup_tables_filename != "")
  							{
  								float[] coherence = null;
  								int[] hash1 = null;
  								int[] hash2 = null;
  								int[] hash3 = null;
  								float[] index1 = null;
  								float[] index2 = null;
  								float[] index3 = null;
  								Console.Write("Generating map...");
  								ShowPlot(
  					                server_name,
  					                database_name, 
  					                user_name, 
  					                password,
  								    1000,
  								    "",
  								    true,
  								    plot_type,
  								    ref coherence,
  								    ref hash1,
  								    ref hash2,
  								    ref hash3,
  								    ref index1,
  								    ref index2,
  								    ref index3);
  								Console.WriteLine("Done");
  								
  								SaveLookupTables(
  								    lookup_tables_filename,
  								    index1, index2, index3,
  								    coherence);
  								Console.WriteLine("Saved lookup tables to " + lookup_tables_filename);
  							}
  							
  							
  						}
  					}
  				}			
  			}
  		}
  		
  		#region "saving lookup tables"
  		
  		private static void SaveLookupTables(
              string filename,
  			float[] index1, 
  		    float[] index2,
  		    float[] index3,
  			float[] coherence)
  		{			
  			StreamWriter oWrite = null;
  			bool file_open = false;
  			try
  			{
  			    oWrite = File.CreateText(filename);
  				file_open = true;
  			}
  			catch
  			{
  			}
  			if (file_open)
  			{
  				oWrite.WriteLine(index1.Length.ToString());
  				for (int i = 0; i < index1.Length; i++)
  				{
  					oWrite.WriteLine(index1[i].ToString());
  					oWrite.WriteLine(index2[i].ToString());
  					oWrite.WriteLine(index3[i].ToString());
  				}
  				oWrite.Close();
  			}			
  		}		
  		
  		#endregion
  		
  		#region "generate image"
  		
  		//static int img_width = 1000;
  		static int plot_ctr;
  		
  		#endregion
  		
  		/// <summary>
  		/// Generates a hash code indicative of the text string
  		/// </summary>
  		/// <param name="s">string</param>
  		/// <returns>hash code</returns>
  		private static int GetHashCode(string s)
  		{
  			s = s.Trim();
  			s = s.ToLower();
  			
  			char[] ch = s.ToCharArray();
  			
  			int hash = 0;
              for (int i = 0; i < ch.Length; i++) {
  				if (((ch[i] >= 'a') &&
  				     (ch[i] <= 'z')) ||
  				    ((ch[i] >= '0') &&
  				     (ch[i] <= '9')))
  				{
                      hash = 31*hash + ch[i];
  				}
              }
  
  			return(hash);
  		}
  				
  		#region "mysql database"
  		
          private static void CreateTable(
              string server_name,
              string database_name,
              string user_name,
              string password,
              string table_name,
              List<string> fields_to_be_inserted,
              List<string> field_type,
  		    int primary_key_field,
  		    int index_field1,
  		    int index_field2)
          {
              MySqlConnection connection = new MySqlConnection();
  
              if ((server_name == "") ||
                  (server_name == null))
                  server_name = "localhost";
  
              connection.ConnectionString =
                  "server=" + server_name + ";"
                  + "database=" + database_name + ";"
                  + "uid=" + user_name + ";"
                  + "password=" + password + ";";
  			
  			//Console.WriteLine("connection string: " + connection.ConnectionString);
  
              bool connected = false;
              string exception_str = "";
              try
              {
                  connection.Open();
                  connected = true;
              }
              catch (Exception ex)
              {
                  Console.WriteLine("Connection error: " + ex.Message);
              }
  
              if (connected)
              {
                  string Query = "CREATE TABLE IF NOT EXISTS " + table_name + "(";
  
                  for (int i = 0; i < fields_to_be_inserted.Count; i++)
                  {
                      Query += fields_to_be_inserted[i] + " " + field_type[i] + ",";					
                  }
  			    if (primary_key_field> -1) Query += "PRIMARY KEY (" + fields_to_be_inserted[primary_key_field] + ")";
  				if (index_field1 > -1)
  				{
  					if (primary_key_field> -1) Query += ",";
  					Query += "INDEX (" + fields_to_be_inserted[index_field1] + ")";
  					if (index_field2 > -1) Query += ",INDEX (" + fields_to_be_inserted[index_field2] + ")";
  				}
  				if (table_name == "mindpixels")
  				{
  					Query += ",INDEX (Ngram3),INDEX (Soundex),INDEX (Verbs),INDEX (Connections),INDEX (NYSIIS),INDEX (MetaphonePrimary),INDEX (MetaphoneSecondary),INDEX (Emotion)";
  				}
  				Query += ")";
  				
                  MySqlCommand addxml = new MySqlCommand(Query, connection);
  
                  //Console.WriteLine("Running query:");
                  //Console.WriteLine(Query);
  
                  try
                  {
                      addxml.ExecuteNonQuery();
                  }
                  catch (Exception ex)
                  {
  					Console.WriteLine("Can't create table " + ex.Message);
                  }
  
                  connection.Close();
              }
              else
              {
                  Console.WriteLine("CreateTable: Couldn't connect to database " + database_name);
                  Console.WriteLine(exception_str);
              }
          }
  		
  		private static ArrayList RunMySqlCommand(
  		    string commandText, 
  		    string connectionString,
  		    int no_of_fields)
  	    {
  			ArrayList rows = new ArrayList();
  	        using (MySqlConnection connection = new MySqlConnection(connectionString))
  	        {
  	            try
  	            {
  					//Console.WriteLine("commandtext: " + commandText);
  	                MySqlCommand command = new MySqlCommand(commandText, connection);
  	
  	                connection.Open();
  	                IAsyncResult result = command.BeginExecuteReader();
  	
  	                int count = 0;
  	                while (!result.IsCompleted)
  	                {
  	                    count += 1;
  	                    //Console.WriteLine("Waiting ({0})", count);
  	                    System.Threading.Thread.Sleep(100);
  	                }
  	
  	                MySqlDataReader query_result = command.EndExecuteReader(result);
  					while (query_result.Read())
  					{
  						ArrayList row = new ArrayList();
  						for (int i = 0; i < no_of_fields; i++)
  						{
  							row.Add(query_result.GetValue(i));
  						}
  						rows.Add(row);
  					}
  					
  					connection.Close();
  	            }
  	            catch (MySqlException ex)
  	            {
  	                Console.WriteLine("Error ({0}): {1}", ex.Number, ex.Message);
  					connection.Close();
  	            }
  	            catch (InvalidOperationException ex)
  	            {
  	                Console.WriteLine("Error: {0}", ex.Message);
  					connection.Close();
  	            }
  	            catch (Exception ex)
  	            {
  	                Console.WriteLine("Error: {0}", ex.Message);
  					connection.Close();
  	            }
  	        }
  			return(rows);
  	    }
  		
          static int[] emotional_word_rating;
          static string[] emotional_words;
  		static int no_of_emotional_words;
  		
  		private static void addEmotionalWord(string word, int rating)
  		{
  			emotional_word_rating[no_of_emotional_words] = rating;
  			emotional_words[no_of_emotional_words] = word;
  			no_of_emotional_words++;
  		}
  		
  		private static void CreateEmotionalWords()
  		{	
  			emotional_word_rating = new int[300];
  			emotional_words = new string[300];
  			no_of_emotional_words = 0;
  			
              //words associated with positive emotions
              addEmotionalWord("love", 20);
              addEmotionalWord("loved", 20);
              addEmotionalWord("like", 2);
              addEmotionalWord("enjoy", 5);
              addEmotionalWord("pleasurable", 5);
              addEmotionalWord("pleasure", 5);
              addEmotionalWord("easy", 5);
              addEmotionalWord("enjoys", 5);
              addEmotionalWord("amused", 5);
              addEmotionalWord("amusing", 5);
              addEmotionalWord("entertained", 5);
              addEmotionalWord("entertaining", 5);
              addEmotionalWord("enjoyed", 5);
              addEmotionalWord("treasure", 5);
              addEmotionalWord("treasured", 5);
              addEmotionalWord("pleasant", 5);
              addEmotionalWord("joy", 5);
              addEmotionalWord("happy", 5);
              addEmotionalWord("good", 5);
              addEmotionalWord("good at", 5);
              addEmotionalWord("happiness", 5);
              addEmotionalWord("popular", 5);
              addEmotionalWord("esteem", 5);
              addEmotionalWord("worthy", 5);
              addEmotionalWord("worthwhile", 5);
              addEmotionalWord("loving", 5);
              addEmotionalWord("positive", 5);
              addEmotionalWord("protect", 5);
              addEmotionalWord("enjoyable", 10);
              addEmotionalWord("exciting", 10);
              addEmotionalWord("pleasing", 10);
              addEmotionalWord("pleased", 10);
              addEmotionalWord("encouraging", 5);
              addEmotionalWord("encouraged", 5);
              addEmotionalWord("encourage", 5);
              addEmotionalWord("invest", 4);
              addEmotionalWord("investment", 4);
              addEmotionalWord("growth", 4);
              addEmotionalWord("increase", 10);
              addEmotionalWord("increased", 10);
              addEmotionalWord("joyous", 10);
              addEmotionalWord("tidy", 2);
              addEmotionalWord("neat", 2);
              addEmotionalWord("confident", 10);
              addEmotionalWord("confidence", 10);
              addEmotionalWord("peace", 10);
              addEmotionalWord("peacefull", 10);
              addEmotionalWord("calm", 10);
              addEmotionalWord("bright", 10);
              addEmotionalWord("rosy", 10);
              addEmotionalWord("kindness", 10);
              addEmotionalWord("goodness", 10);
              addEmotionalWord("better", 2);
              addEmotionalWord("welcome", 2);
              addEmotionalWord("amusing", 2);
              addEmotionalWord("lucky", 5);
              addEmotionalWord("correct", 5);
              addEmotionalWord("hired", 5);
              addEmotionalWord("replenish", 5);
              addEmotionalWord("respect", 5);
              addEmotionalWord("respects", 5);
              addEmotionalWord("respected", 5);
              addEmotionalWord("working", 5);
              addEmotionalWord("fascinate", 5);
              addEmotionalWord("fascinated", 5);
              addEmotionalWord("fascinating", 5);
              addEmotionalWord("poetic", 5);
              addEmotionalWord("interested", 5);
              addEmotionalWord("interesting", 5);
              addEmotionalWord("divine", 10);
              addEmotionalWord("divinely", 10);
              addEmotionalWord("fantastic", 10);
              addEmotionalWord("fabulous", 10);
              addEmotionalWord("funny", 10);
              addEmotionalWord("initiative", 2);
              addEmotionalWord("genuis", 5);
              addEmotionalWord("freedom", 5);
              addEmotionalWord("devoted", 10);
              addEmotionalWord("refreshing", 10);
              addEmotionalWord("refreshed", 10);
              addEmotionalWord("enthusiast", 10);
              addEmotionalWord("enthusiastic", 10);
              addEmotionalWord("enthusiastically", 10);
              addEmotionalWord("enthused", 10);
              addEmotionalWord("enthusiasm", 10);
              
  
              //words associated with negative emotions
              addEmotionalWord("hate", -10);
              addEmotionalWord("hated", -10);
              addEmotionalWord("never", -2);
              addEmotionalWord("hates", -10);
              addEmotionalWord("harm", -10);
              addEmotionalWord("kill", -10);
              addEmotionalWord("kills", -10);
              addEmotionalWord("killed", -10);
              addEmotionalWord("killing", -10);
              addEmotionalWord("death", -10);
              addEmotionalWord("grave", -10);
              addEmotionalWord("victim", -10);
              addEmotionalWord("stole", -5);
              addEmotionalWord("steal", -5);
              addEmotionalWord("stolen", -5);
              addEmotionalWord("surrender", -1);
              addEmotionalWord("killing", -10);
              addEmotionalWord("offensive", -10);
              addEmotionalWord("disslike", -5);
              addEmotionalWord("frustrate", -5);
              addEmotionalWord("frustration", -5);
              addEmotionalWord("exclusion", -5);
              addEmotionalWord("empty", -5);
              addEmotionalWord("sad", -5);
              addEmotionalWord("robbed", -8);
              addEmotionalWord("crooked", -5);
              addEmotionalWord("scam", -5);
              addEmotionalWord("slave", -5);
              addEmotionalWord("evil", -10);
              addEmotionalWord("hysterical", -10);
              addEmotionalWord("hysteria", -10);
              addEmotionalWord("imposed", -5);
              addEmotionalWord("violent", -5);
              addEmotionalWord("violence", -5);
              addEmotionalWord("shot", -10);
              addEmotionalWord("genocide", -10);
              addEmotionalWord("fighting", -5);
              addEmotionalWord("paranoia", -5);
              addEmotionalWord("paranoid", -5);
              addEmotionalWord("propaganda", -5);
              addEmotionalWord("fraud", -5);
              addEmotionalWord("liar", -10);
              addEmotionalWord("lies", -10);
              addEmotionalWord("misslead", -1);
              addEmotionalWord("lied", -10);
              addEmotionalWord("hoax", -1);
              addEmotionalWord("scary", -5);
              addEmotionalWord("scared", -5);
              addEmotionalWord("scare", -5);
              addEmotionalWord("frightening", -10);
              addEmotionalWord("frightened", -10);
              addEmotionalWord("criticism", -5);
              addEmotionalWord("criticise", -5);
              addEmotionalWord("horror", -8);
              addEmotionalWord("disturbing", -8);
              addEmotionalWord("horrible", -8);
              addEmotionalWord("criticised", -5);
              addEmotionalWord("criticising", -5);
              addEmotionalWord("spam", -10);
              addEmotionalWord("damn", -10);
              addEmotionalWord("darn", -10);
              addEmotionalWord("fired", -5);
              addEmotionalWord("sacked", -5);
              addEmotionalWord("redundant", -5);
              addEmotionalWord("bad", -10);
              addEmotionalWord("too bad", -10);
              addEmotionalWord("scream", -10);
              addEmotionalWord("screamed", -10);
              addEmotionalWord("screaming", -10);
              addEmotionalWord("worse", -2);
              addEmotionalWord("stumble", -2);
              addEmotionalWord("stumbling", -2);
              addEmotionalWord("frantic", -2);
              addEmotionalWord("arrested", -10);
              addEmotionalWord("worse than", -2);
              addEmotionalWord("virus", -10);
              addEmotionalWord("bomb", -10);
              addEmotionalWord("boring", -10);
              addEmotionalWord("dumb", -2);
              addEmotionalWord("unnecessary", -2);
              addEmotionalWord("unsophisticated", -2);
              addEmotionalWord("fuck", -10);
              addEmotionalWord("fucked", -10);
              addEmotionalWord("shoot", -10);
              addEmotionalWord("incorrect", -10);
              addEmotionalWord("shot", -10);
              addEmotionalWord("shooter", -10);
              addEmotionalWord("messy", -2);
              addEmotionalWord("untidy", -2);
              addEmotionalWord("shooting", -10);
              addEmotionalWord("fucker", -10);
              addEmotionalWord("fucking", -10);
              addEmotionalWord("shit", -10);
              addEmotionalWord("bitch", -10);
              addEmotionalWord("dont like", -10);
              addEmotionalWord("bitchy", -10);
              addEmotionalWord("bitching", -10);
              addEmotionalWord("gun", -10);
              addEmotionalWord("guns", -10);
              addEmotionalWord("weapon", -10);
              addEmotionalWord("weapons", -10);
              addEmotionalWord("war", -10);
              addEmotionalWord("rifle", -10);
              addEmotionalWord("lonely", -10);
              addEmotionalWord("loser", -10);
              addEmotionalWord("invade", -10);
              addEmotionalWord("invasion", -10);
              addEmotionalWord("disease", -10);
              addEmotionalWord("sick", -10);
              addEmotionalWord("ill", -10);
              addEmotionalWord("illness", -10);
              addEmotionalWord("sickness", -10);
              addEmotionalWord("jealous", -5);
              addEmotionalWord("fear", -5);
              addEmotionalWord("feared", -5);
              addEmotionalWord("fearfull", -5);
              addEmotionalWord("nowhere", -5);
              addEmotionalWord("unpopular", -5);
              addEmotionalWord("angry", -5);
              addEmotionalWord("deluded", -5);
              addEmotionalWord("delusion", -5);
              addEmotionalWord("deserted", -5);
              addEmotionalWord("worthless", -5);
              addEmotionalWord("difficult", -2);
              addEmotionalWord("hard", -2);
              addEmotionalWord("trouble", -2);
              addEmotionalWord("troublesome", -4);
              addEmotionalWord("bicker", -5);
              addEmotionalWord("argue", -5);
              addEmotionalWord("decline", -5);
              addEmotionalWord("reduce", -5);
              addEmotionalWord("cut", -5);
              addEmotionalWord("unlucky", -5);
              addEmotionalWord("wrong", -5);
              addEmotionalWord("go wrong", -5);
              addEmotionalWord("gone wrong", -5);
              addEmotionalWord("going wrong", -5);
              addEmotionalWord("not working", -5);
  		}
  		
  		private static float GetEmotionRating(string text)
  		{
  			float rating = 0;
  			
  			text = text.ToLower();
  			for (int i = 0; i < no_of_emotional_words; i++)
  			{
  				if (text.Contains(emotional_words[i]))
  					rating += emotional_word_rating[i];
  			}
  			
  			rating /= 40.0f;
  			
  			return(rating);
  		}
  
  		private static float GetNgramIndex(string idx, int max_length)
  		{
  			double index = 0;
  			idx = idx.ToLower();
  			if (idx.Length > max_length)
  				idx = idx.Substring(0, max_length);
  			while (idx.Length < max_length)
  				idx += "0";
  			
  			char[] ch = idx.ToCharArray();
  			
  			double mult = 1.0/3600000000.0;
  			double max = 0;
              for (int i = 0; i < ch.Length; i++) {
  				if (((ch[i] >= 'a') &&
  				     (ch[i] <= 'z')) ||
  				    ((ch[i] >= '0') &&
  				     (ch[i] <= '9')))
  				{
  					if (ch[i] <= '9')
  						index = 2*index + ((ch[i] - (int)'0' + 1)*mult);
  					else
                          index = 2*index + ((ch[i] - (int)'a' + 11)*mult);
  					
  					max = 2*max + (36*mult);
  				}
              }
  			index /= max;
  			//Console.WriteLine("index = " + index.ToString());
  			
  			return((float)index);
  		}
  		
          public static void InsertMindpixelIntoMySql(
  		    int hash,
              string question,
              bool answer,
              string server_name,
              string database_name,
              string user_name,
              string password,
              string table_name,
              List<string> fields_to_be_inserted,
              List<string> field_type)
          {
  			int plot_type = 0;
              if ((server_name == "") ||
                  (server_name == null))
                  server_name = "localhost";
  
  			float coherence = 0.0f;
  			if (answer == true) coherence = 1.0f;
  			
  			string index_ngram3 = phoneme.ToNgramStandardised(question, 3, false);
  			string index_soundex = Soundex.ToSoundexStandardised(question, false, false);
  			string index_verbs = verbs.GetVerbClasses(question);
  			string index_metaphone_primary="", index_metaphone_secondary="";
  			Metaphone.ToMetaphoneStandardised(question, false, ref index_metaphone_primary, ref index_metaphone_secondary);
  			string index_nysiis = NYSIIS.ToNYSIISStandardised(question, false);
  
  			float coordinate_ngram3 = GetNgramIndex(index_ngram3, 80);
  			float coordinate_soundex = GetNgramIndex(index_soundex, 80);
  			float coordinate_verbs = GetNgramIndex(index_verbs, 80);
  			float coordinate_nysiis = GetNgramIndex(index_nysiis, 80);
  			float coordinate_metaphone_primary = GetNgramIndex(index_metaphone_primary, 80);
  			float coordinate_metaphone_secondary = GetNgramIndex(index_metaphone_secondary, 80);
  			float coordinate_emotion = GetEmotionRating(question);
  			
  			plot_ctr++;
  			if (plot_ctr > 10000)
  			{
  				float[] coherence2 = null;
  				int[] hash1 = null;
  				int[] hash2 = null;
  				int[] hash3 = null;
  				float[] index1 = null;
  				float[] index2 = null;
  				float[] index3 = null;
                  ShowPlot(
                      server_name,
                      database_name,
                      user_name,
                      password,
  		            1000,
  		            "mindpixels.bmp",
  				    false,
  				    plot_type,
  				    ref coherence2,
  				    ref hash1,
  				    ref hash2,
  				    ref hash3,
  				    ref index1,
  				    ref index2,
  				    ref index3);
  					
  				plot_ctr = 0;
  			}
  						
  			string connection_str = 
                  "server=" + server_name + ";"
                  + "database=" + database_name + ";"
                  + "uid=" + user_name + ";"
                  + "password=" + password + ";";
  			
  			string Query = "SELECT * FROM " + table_name + " WHERE Hash = " + hash.ToString() + ";";
  			//Console.WriteLine(Query);
  			
  			int YesVotes = 0;
  			int NoVotes = 0;
  			Guid best_result = Guid.Empty;
  			ArrayList pixels = RunMySqlCommand(Query, connection_str, 5);
  			if (pixels.Count > 0)
  			{
  				char[] template = Soundex.ToSoundexCode(question).ToCharArray();
  				int max_score = -1;
  				// update existing pixel
                  for (int ctr = 0; ctr < pixels.Count; ctr++)
  				{
  					ArrayList row = (ArrayList)pixels[ctr];
  					string result_question = (string)row[2];
    				    char[] s = Soundex.ToSoundexCode(result_question).ToCharArray();
  					int score = 0;
  					for (int i = 0; i < s.Length; i++)
  					{						
  						score += Math.Abs((int)s[i] - (int)template[i]);
  					}
  					if (score > max_score)
  					{
  						best_result = (Guid)row[0];
  						max_score = score;
  						YesVotes = Convert.ToInt32(row[3]);
  						NoVotes = Convert.ToInt32(row[4]);
  					}
  					
  					//Guid result_Id = (Guid)pixels.GetValue(0);
  					//int result_hash = pixels.GetInt32(1);
  					//int result_yes = pixels.GetInt32(3);
  					//int result_no = pixels.GetInt32(4);
                  }
  				
  				if (best_result != Guid.Empty)
  				{
  					// update the mindpixel coherence
  					if (answer == true)
  						YesVotes++;
  					else
  						NoVotes++;
  					coherence = YesVotes / (float)(YesVotes + NoVotes);
  					
  				    Query = "UPDATE " + table_name + 
  						" SET YesVotes='" + YesVotes.ToString() + "'," +
  						" NoVotes='" + NoVotes.ToString() + "'," +
  						" Coherence='" + coherence.ToString() + "'," +
  					    " Ngram3='" + coordinate_ngram3.ToString() + "'," +
  					    " Soundex='" + coordinate_soundex.ToString() + "'," +
  						" Verbs='" + coordinate_verbs.ToString() + "'," +
  					    " NYSIIS='" + coordinate_nysiis.ToString() + "'," +
  					    " MetaphonePrimary='" + coordinate_metaphone_primary.ToString() + "'," +
  					    " MetaphoneSecondary='" + coordinate_metaphone_secondary.ToString() + "'," +
  					    " Emotion='" + coordinate_emotion.ToString() + "'" +
  					    " WHERE Id=CAST(?get_id as BINARY(16));";
  					
                      MySqlConnection connection = new MySqlConnection();
  
                      connection.ConnectionString = connection_str;
  
                      bool connected = false;
                      try
                      {
                          connection.Open();
                          connected = true;
                      }
                      catch (Exception ex)
                      {
                          Console.WriteLine(ex.Message);
  						connection.Close();
                      }
  
                      if (connected)
                      {
  		                MySqlCommand update = new MySqlCommand(Query, connection);
  						update.Parameters.Add("get_id", MySqlDbType.Binary).Value = best_result.ToByteArray();
  		
  		                //Console.WriteLine("Running update query:");
  		                //Console.WriteLine(Query);
  		
  		                try
  		                {
  		                    update.ExecuteNonQuery();
  		                }
  		                catch (Exception excp)
  		                {
  		                    Exception myExcp = new Exception("Could not update mindpixel. Error: " + excp.Message, excp);
  		                    throw (myExcp);
  		                }
  		
  		                connection.Close();					
  				    }	
  				}
  			}
  			
  			if (best_result == Guid.Empty)
  			{
  	            List<string> field_name = new List<string>();
  	            List<string> field_value = new List<string>();
  				
  				if (answer == true)
  				{
  					YesVotes = 1;
  					coherence = 1;
  				}
  				else
  				{
  					NoVotes = 1;
  					coherence = 0;
  				}
  	
  				field_name.Add("Id");
  				field_value.Add(System.Guid.NewGuid().ToString());
  				field_name.Add("Hash");
  				field_value.Add(hash.ToString());
  				field_name.Add("Question");
  				field_value.Add(question);
  				field_name.Add("YesVotes");
  				field_value.Add(YesVotes.ToString());				
  				field_name.Add("NoVotes");
  				field_value.Add(NoVotes.ToString());				
  				field_name.Add("Coherence");
  				field_value.Add(coherence.ToString());
  			    field_name.Add("Ngram3");
  			    field_value.Add(coordinate_ngram3.ToString());
  			    field_name.Add("Soundex");
  			    field_value.Add(coordinate_soundex.ToString());
  			    field_name.Add("Verbs");
  			    field_value.Add(coordinate_verbs.ToString());
  			    field_name.Add("Connections");
  			    field_value.Add("0");
  			    field_name.Add("NYSIIS");
  			    field_value.Add(coordinate_nysiis.ToString());
  			    field_name.Add("MetaphonePrimary");
  			    field_value.Add(coordinate_metaphone_primary.ToString());
  			    field_name.Add("MetaphoneSecondary");
  			    field_value.Add(coordinate_metaphone_secondary.ToString());
  			    field_name.Add("Emotion");
  			    field_value.Add(coordinate_emotion.ToString());
  				
  				// add new pixel
  	            MySqlConnection connection = new MySqlConnection();
  				
  				connection.ConnectionString = connection_str;
  		
  	            bool connected = false;
  	            string exception_str = "";
  	            try
  	            {
  	                connection.Open();
  	                connected = true;
  	            }
  	            catch (Exception ex)
  	            {
  	                exception_str = ex.Message;
  					connection.Close();
  	            }
  	
  	            if (connected)
  	            {
  	                Query = "INSERT INTO " + table_name + "(";
  	
  	                for (int i = 0; i < field_name.Count; i++)
  	                {
  	                    if ((fields_to_be_inserted.Count == 0) ||
  	                        (fields_to_be_inserted.Contains(field_name[i])))
  	                    {
                              Query += field_name[i];
  	                        if (i < field_name.Count - 1) Query += ",";
  	                    }
  	                }
  	                Query += ") values(";
  	                for (int i = 0; i < field_value.Count; i++)
  	                {
  	                    int idx = fields_to_be_inserted.IndexOf(field_name[i]);
  	
  	                    if ((fields_to_be_inserted.Count == 0) ||
  	                        (fields_to_be_inserted.Contains(field_name[i])))
  	                    {
  	                        if (fields_to_be_inserted.Count > 0)
  	                        {
  	                            if (idx > -1)
  	                            {
  									if (field_type[idx].Contains("BINARY"))
  									{
  										field_value[i] = "?Id";
  									}
  									
  	                                if (field_type[idx] == "DATETIME")
  	                                {
  	                                    DateTime d = DateTime.Parse(field_value[i]);
  	                                    string d_str = d.Year.ToString() + "-" + d.Month.ToString() + "-" + d.Day.ToString() + " " + d.Hour.ToString() + ":" + d.Minute.ToString() + ":" + d.Second.ToString();
  	                                    field_value[i] = d_str;
  	                                }
  	                            }
  	                        }
  	
  							if (!field_type[idx].Contains("BINARY"))
  	                            Query += "'" + field_value[i] + "'";
  							else
  								Query += field_value[i];
  	                        if (i < field_value.Count - 1) Query += ",";
  	                    }
  	                }
  	                Query += ")";
  	
  	                MySqlCommand addxml = new MySqlCommand(Query, connection);
  	
  	                //Console.WriteLine("Running insert query:");
  	                //Console.WriteLine(Query);
  	
  	                try
  	                {
  						addxml.Parameters.Add("Id", MySqlDbType.Binary).Value = System.Guid.NewGuid().ToByteArray();
  	                    addxml.ExecuteNonQuery();
  	                }
  	                catch (Exception excp)
  	                {
  	                    Exception myExcp = new Exception("Could not add new mindpixel. Error: " + excp.Message, excp);
  	                    throw (myExcp);
  	                }
  	
  	                connection.Close();
  	            }
  	            else
  	            {
  	                Console.WriteLine("InsertMindpixel: Couldn't connect to database " + database_name);
  	                Console.WriteLine(exception_str);
  	            }
  				
  			}
          }
  
          public static void UpdateConnections(
              string server_name,
              string database_name,
              string user_name,
              string password,
              string mp_table_name,
  		    string soundex_table_name)
  		{
              if ((server_name == "") ||
                  (server_name == null))
                  server_name = "localhost";			
  						
  			string connection_str = 
                  "server=" + server_name + ";"
                  + "database=" + database_name + ";"
                  + "uid=" + user_name + ";"
                  + "password=" + password + ";";
  			
  			string Query = "SELECT Hash,Question FROM " + mp_table_name + ";";
  			ArrayList mps = RunMySqlCommand(Query, connection_str, 2);
  			for (int i = 0; i < mps.Count; i++)
  			{
  				ArrayList row = (ArrayList)mps[i];
  				int hash = Convert.ToInt32(Convert.ToString(row[0]));
  				string question = Convert.ToString(row[1]);
  				
  				UpdateConnections(
  				    hash,
  				    question,
  				    server_name,
  				    database_name,
  				    user_name,
  				    password,
  				    mp_table_name,
  					soundex_table_name);
  				Console.WriteLine(i.ToString());
  			}
  		}
  		
          public static void UpdateConnections(
  		    int hash,
              string question,
              string server_name,
              string database_name,
              string user_name,
              string password,
              string mp_table_name,
  		    string soundex_table_name)
          {
              if ((server_name == "") ||
                  (server_name == null))
                  server_name = "localhost";			
  						
  			string connection_str = 
                  "server=" + server_name + ";"
                  + "database=" + database_name + ";"
                  + "uid=" + user_name + ";"
                  + "password=" + password + ";";
  			
  			string Query = "SELECT SoundexHash FROM " + soundex_table_name + " WHERE MindpixelHash = " + hash.ToString() + " GROUP BY SoundexHash;";
  			ArrayList soundex_hashes = RunMySqlCommand(Query, connection_str, 1);
  			if (soundex_hashes.Count > 0)
  			{
  				List<int> hashes = new List<int>();
  				for (int i = 0; i < soundex_hashes.Count; i++)
  				{
  					ArrayList row = (ArrayList)soundex_hashes[i];
  					string shash = Convert.ToString(row[0]);
  			        Query = "SELECT MindpixelHash FROM " + soundex_table_name + " WHERE SoundexHash = " + shash + " GROUP BY MindpixelHash;";
  			        ArrayList mindpixel_hashes = RunMySqlCommand(Query, connection_str, 1);
  					for (int j = 0; j < mindpixel_hashes.Count; j++)
  					{
  						ArrayList row2 = (ArrayList)mindpixel_hashes[j];
  						string mphash = Convert.ToString(row2[0]);
  						int mphash2 = Convert.ToInt32(mphash);
  						if (mphash2 != hash)
  						{
  							if (hashes.IndexOf(mphash2) == -1)
  								hashes.Add(mphash2);
  						}
  					}					
  				}
  				hashes.Sort();
  				string hashes_str = "";
  				for (int i = 0; i < hashes.Count; i++)
  				{
  					hashes_str += hashes[i] + " ";
  				}
  				hashes_str = hashes_str.Trim();
  				float hashes_index = GetNgramIndex(hashes_str.Trim(), 100);
  								
  				Query = "UPDATE " + mp_table_name + 
  						" SET Connections='" + hashes_index.ToString() + "'" +
  					    " WHERE Hash=" + hash.ToString() + ";";
  					
                  MySqlConnection connection = new MySqlConnection();
  
                  connection.ConnectionString = connection_str;
  
                  bool connected = false;
                  try
                  {
                      connection.Open();
                      connected = true;
                  }
                  catch (Exception ex)
                  {
                      Console.WriteLine(ex.Message);
  				    connection.Close();
                  }
  
                  if (connected)
                  {
  		            MySqlCommand update = new MySqlCommand(Query, connection);
  		
  	                try
  	                {
  	                    update.ExecuteNonQuery();
  	                }
  	                catch (Exception excp)
  	                {
  	                    Exception myExcp = new Exception("Could not update mindpixel. Error: " + excp.Message, excp);
  	                    throw (myExcp);
  	                }
  		
  	                connection.Close();					
  			    }	
  			}
  			
          }
  				
          public static void InsertWordsIntoMySql(
  		    int hash,
              string question,
              string server_name,
              string database_name,
              string user_name,
              string password,
              string table_name,
              List<string> fields_to_be_inserted,
              List<string> field_type)
          {
  			
              if ((server_name == "") ||
                  (server_name == null))
                  server_name = "localhost";
  
  			string connection_str = 
                  "server=" + server_name + ";"
                  + "database=" + database_name + ";"
                  + "uid=" + user_name + ";"
                  + "password=" + password + ";";
  			
  			string question_str = RemoveCommonWords(question);
  			string[] str2 = question_str.Split(' ');
              List<string> field_name = new List<string>();
              List<string> field_value = new List<string>();
  			
  			for (int wrd = 0; wrd < str2.Length; wrd++)
  			{
  				if (str2[wrd].Length > 2)
  				{
  					//Console.WriteLine(str2[wrd]);
  					int word_sound_hash = GetHashCode(Soundex.ToSoundexCode(str2[wrd]));
  
  		            field_name.Clear();
  		            field_value.Clear();
  					
  					field_name.Add("SoundexHash");
  					field_value.Add(word_sound_hash.ToString());
  					field_name.Add("MindpixelHash");
  					field_value.Add(hash.ToString());
  					
  					// add new pixel
  		            MySqlConnection connection = new MySqlConnection();
  					
  					connection.ConnectionString = connection_str;
  			
  		            bool connected = false;
  		            string exception_str = "";
  		            try
  		            {
  		                connection.Open();
  		                connected = true;
  		            }
  		            catch (Exception ex)
  		            {
  		                exception_str = ex.Message;
  						connection.Close();
  		            }
  		
  		            if (connected)
  		            {
  		                string Query = "INSERT INTO " + table_name + "(";
  		
  		                for (int i = 0; i < field_name.Count; i++)
  		                {
  		                    if ((fields_to_be_inserted.Count == 0) ||
  		                        (fields_to_be_inserted.Contains(field_name[i])))
  		                    {
  		                        Query += field_name[i];
  		                        if (i < field_name.Count - 1) Query += ",";
  		                    }
  		                }
  		                Query += ") values(";
  		                for (int i = 0; i < field_value.Count; i++)
  		                {
  		                    int idx = fields_to_be_inserted.IndexOf(field_name[i]);
  		
  		                    if ((fields_to_be_inserted.Count == 0) ||
  		                        (fields_to_be_inserted.Contains(field_name[i])))
  		                    {
  		                        if (fields_to_be_inserted.Count > 0)
  		                        {
  		                            if (idx > -1)
  		                            {
  										if (field_type[idx].Contains("BINARY"))
  										{
  											field_value[i] = "?Id";
  										}
  										
  		                                if (field_type[idx] == "DATETIME")
  		                                {
  		                                    DateTime d = DateTime.Parse(field_value[i]);
  		                                    string d_str = d.Year.ToString() + "-" + d.Month.ToString() + "-" + d.Day.ToString() + " " + d.Hour.ToString() + ":" + d.Minute.ToString() + ":" + d.Second.ToString();
  		                                    field_value[i] = d_str;
  		                                }
  		                            }
  		                        }
  		
  								if (!field_type[idx].Contains("BINARY"))
  		                            Query += "'" + field_value[i] + "'";
  								else
  									Query += field_value[i];
  		                        if (i < field_value.Count - 1) Query += ",";
  		                    }
  		                }
  		                Query += ")";
  								
  		                MySqlCommand addxml = new MySqlCommand(Query, connection);
  		
  		                //Console.WriteLine("Running insert query:");
  		                //Console.WriteLine(Query);
  		
  		                try
  		                {
  		                    addxml.ExecuteNonQuery();
  		                }
  		                catch (Exception excp)
  		                {
  		                    Exception myExcp = new Exception("Could not add new mindpixel. Error: " + excp.Message, excp);
  		                    throw (myExcp);
  		                }
  		
  		                connection.Close();
  		            }
  		            else
  		            {
  		                Console.WriteLine("InsertMindpixel: Couldn't connect to database " + database_name);
  		                Console.WriteLine(exception_str);
  		            }
  				
  				}
  			}
  			
          }		
  		
  		private static int GCD(int a, int b)
  		{
  		     while (a != 0 && b != 0)
  		     {
  		         if (a > b)
  		            a %= b;
  		         else
  		            b %= a;
  		     }
  		
  		     if (a == 0)
  		         return b;
  		     else
  		         return a;
  		}		
  		
          private static void InsertMindpixelWithCoherence(
  		    int hash,
              string question,
              float coherence,
              string server_name,
              string database_name,
              string user_name,
              string password,
              string table_name,
              List<string> fields_to_be_inserted,
              List<string> field_type)
          {
  			int plot_type = 0;
              if ((server_name == "") ||
                  (server_name == null))
                  server_name = "localhost";
  			
  			string index_ngram3 = phoneme.ToNgramStandardised(question, 3, false);
  			string index_soundex = Soundex.ToSoundexStandardised(question, false, false);
  			string index_verbs = verbs.GetVerbClasses(question);
  			string index_metaphone_primary="", index_metaphone_secondary="";
  			Metaphone.ToMetaphoneStandardised(question, false, ref index_metaphone_primary, ref index_metaphone_secondary);
  			string index_nysiis = NYSIIS.ToNYSIISStandardised(question, false);
  
  			float coordinate_ngram3 = GetNgramIndex(index_ngram3, 80);
  			float coordinate_soundex = GetNgramIndex(index_soundex, 80);
  			float coordinate_verbs = GetNgramIndex(index_verbs, 80);
  			float coordinate_nysiis = GetNgramIndex(index_nysiis, 80);
  			float coordinate_metaphone_primary = GetNgramIndex(index_metaphone_primary, 80);
  			float coordinate_metaphone_secondary = GetNgramIndex(index_metaphone_secondary, 80);
  			float coordinate_emotion = GetEmotionRating(question);
  			
  			plot_ctr++;
  			if (plot_ctr > 10000)
  			{
  				float[] coherence2 = null;
  				int[] hash1 = null;
  				int[] hash2 = null;
  				int[] hash3 = null;
  				float[] index1 = null;
  				float[] index2 = null;
  				float[] index3 = null;
                  ShowPlot(
                      server_name,
                      database_name,
                      user_name,
                      password,
  		            1000,
  		            "mindpixels.bmp",
  				    false,
  				    plot_type,
  				    ref coherence2,
  				    ref hash1,
  				    ref hash2,
  				    ref hash3,
  				    ref index1,
  				    ref index2,
  				    ref index3);
  				
  				plot_ctr = 0;
  			}
  			
  			string connection_str = 
                  "server=" + server_name + ";"
                  + "database=" + database_name + ";"
                  + "uid=" + user_name + ";"
                  + "password=" + password + ";";
  			
              List<string> field_name = new List<string>();
              List<string> field_value = new List<string>();
  				
  			int YesVotes = 0;
  			int NoVotes = 1;
  			
  			if (coherence > 0)
  			{
  				YesVotes = (int)(coherence*100);
  				NoVotes = 100 - YesVotes;
  				int denom = GCD(YesVotes,100);
  
gt: 				if (denom > 0)