Advertisement
CybEl

Trivia Bot Code

Mar 24th, 2019
2,224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.62 KB | None | 0 0
  1. /*
  2. Coded in C# using Forms
  3.  
  4. Cyborg Elf 2019
  5. https://www.youtube.com/c/cyborgelf
  6. https://www.youtube.com/channel/UC_bMnu_fYu9-2_EY7GUfclQ
  7. This code is intended for an educational source to learn from and understand how Optical Charector Recognition (OCR) works.
  8. I am not responsiple if you use this code for malicious purposes; This is not my intent in sharing it.
  9.  
  10. This code is very poor in effeciency; I created this as one of my first projects and haven't touched it in many months.
  11. It still has lots of work to be done to make it a full "application". When I stopped this projected it was still a work in progress
  12. and in its demo phase of development.
  13.  
  14. I have decided to make the code open source, so other may learn and improve it! If you improve my code please show me what you make (I'd love to see)!
  15. I'd love to contact you on Discord preferably!
  16. Discord username (may change): cyborg elf#7462 or Cyborg Elf#7462
  17. Preferably you will join my coding Disord with lots of active users!: https://discord.gg/kDUHPVD
  18.  
  19. Here is a video of me doing a demo of the project: https://www.youtube.com/watch?v=H1d8a3lrSyk&index=12&t=0s&list=PLzBukBmD3GxsxNv2AFV8Ik9kj50CTKEW-
  20. **/
  21.  
  22. using System;
  23. using System.Drawing;
  24. using System.Windows.Forms;
  25. using System.Diagnostics;
  26. using System.Drawing.Imaging;
  27. using System.IO;
  28. using System.Net;
  29. using Tesseract;
  30. using System.Text.RegularExpressions;
  31. //You may need to go into NuGet and add some of these packages
  32.  
  33.  
  34.  
  35. /*
  36. These are not being used (I think), but I will include them here in case you need to add them (if not just delete all of it).
  37. using Google.Apis.Services;
  38. using Google.Apis.Customsearch.v1;
  39. using System.Collections.Specialized;
  40. using System.Collections;
  41. using System.Text;
  42. using System.Collections.Generic;
  43. using System.ComponentModel;
  44. using System.Data;
  45. using System.Linq;
  46. using System.Threading.Tasks;
  47. using System.Threading;
  48. */
  49.  
  50.  
  51.  
  52. namespace v3_OCR_HQ
  53. {
  54. public partial class Form1 : Form
  55. {
  56. //Don't get mad at how I just declared these... It was my first project and in the testing phase...
  57. //and I don't feel like fixing all the bad coding I did...
  58. public string text;
  59. public string Question;
  60. public string Answer1;
  61. public string Answer2;
  62. public string Answer3;
  63. public string[] lines;
  64. public string[] ListAll;
  65. public string QuestionNew;
  66. public int q1;
  67. public int q2;
  68. public int q3;
  69.  
  70.  
  71. public Form1()
  72. {
  73. InitializeComponent();
  74. }
  75.  
  76. public void Screenshot() //This takes a screenshot of the box that pops up
  77. {
  78. string DeleteDir = @"C:\Users\rainb\source\repos\V3 OCR HQ Trivia\v3 OCR HQ\bin\Debug\OCR.png"; //You must obviously change this to get to the image file path on your PC
  79. File.Delete(DeleteDir);
  80. Point picpoint = answerBox1.PointToScreen(new Point(0, 0));
  81. var bmpScreenshot = new Bitmap(answerBox1.Size.Width,
  82. answerBox1.Size.Height,
  83. PixelFormat.Format32bppArgb);
  84. var gfxScreenshot = Graphics.FromImage(bmpScreenshot);
  85. gfxScreenshot.CopyFromScreen(picpoint.X,
  86. picpoint.Y,
  87. 0,
  88. 0,
  89. answerBox1.Size,
  90. CopyPixelOperation.SourceCopy);
  91. bmpScreenshot.Save("OCR.png", System.Drawing.Imaging.ImageFormat.Png);
  92. }
  93.  
  94. public void Tesseract() //Tesseract is a OCR NuGet package, it scans the imahe and turns it into a string (text)
  95. {
  96. //The OCR/Screnshot mechanics were always fidgety, so there is a lot of room to improve here!
  97. const string tessDataDir = @"C:\Users\rainb\source\repos\V3 OCR HQ Trivia\v3 OCR HQ\tessdata"; //You must obviously change this to get to the image file path on your PC
  98. const string imageDir = @"C:\Users\rainb\source\repos\v3 OCR HQ\V3 OCR HQ Trivia\bin\Debug\OCR.png"; //You must obviously change this to get to the image file path on your PC
  99.  
  100. using (var engine = new TesseractEngine(tessDataDir, "eng", EngineMode.Default))
  101. using (var image = Pix.LoadFromFile(imageDir))
  102. using (var page = engine.Process(image))
  103. {
  104. text = page.GetText();
  105. }
  106. }
  107.  
  108. public void Seperate() //I added this as a way to seperate the image into a Question, and Answer 1-3. It was primitive at the time, but it worked.
  109. {
  110. lines = text.Split(new[] { "\n\n" }, StringSplitOptions.None); //You might need to optimize this based on the thing your scanning.
  111.  
  112. Question = lines[0];
  113. Answer1 = lines[1];
  114. Answer2 = lines[2];
  115. Answer3 = lines[3];
  116. }
  117.  
  118. public void HTMLparse() //This code googles the question using a custom search engine and Regex matches for the most occuring keywords (answers).
  119. {
  120. //string correctString = Question.Replace(" ", "+").Replace("\n", "-").Replace("'", "").Replace("?", ""); //I didn't use this in my code, but I'll leave it in case you want it.
  121. string link = "https://www.googleapis.com/customsearch/ YOU MUST ENTER YOUR OWN SEARCH ENGINE API" + Question; //I use a custom google search engine api, you must make your own account with google and put your custom search engine API link in. Creating the custom search engine is super easy on their site... https://developers.google.com/custom-search/
  122. string pageContent = null;
  123. HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create(link);
  124. HttpWebResponse myres = (HttpWebResponse)myReq.GetResponse();
  125. if (myres.StatusCode != HttpStatusCode.OK)
  126. {
  127. Debug.WriteLine("\r\nResponse Status Code is NOT and StatusDescription is: {0}",
  128. myres.StatusDescription);
  129. }
  130. else
  131. {
  132. richTextBox1.ResetText();
  133. using (StreamReader sr = new StreamReader(myres.GetResponseStream()))
  134. {
  135. pageContent = sr.ReadToEnd();
  136. }
  137. int q1 = Regex.Matches(pageContent, Answer1).Count;
  138. int q2 = Regex.Matches(pageContent, Answer2).Count;
  139. int q3 = Regex.Matches(pageContent, Answer3).Count;
  140. richTextBox1.AppendText(Question + "\n" + Answer1 + ": " + q1 + "\n" + Answer2 + ": " + q2 + "\n" + Answer3 + ": " + q3 + "\n");
  141.  
  142. if (q1 > q2 && q1 > q3)
  143. {
  144. richTextBox1.AppendText("ANSWER:" + Answer1 + "\n");
  145. }
  146. else if (q2 > q1 && q2 > q3)
  147. {
  148. richTextBox1.AppendText("ANSWER: " + Answer2 + "\n");
  149. }
  150. else
  151. {
  152. richTextBox1.AppendText("ANSWER: " + Answer3 + "\n");
  153. }
  154.  
  155. }
  156. }
  157. /*public void QuoraSearch() //I never went with this for the final, but this parses a website/search engine (this is an alternative to using googles API if you don't want to use their trial/pay)
  158. {
  159. //You may need to add some NuGet packages if you use this!
  160. string correctString = Question.Replace(" ", "+").Replace("\n", "-").Replace("'", "").Replace("?", "");
  161. string link = "https://duckduckgo.com/?q=" + correctString;
  162. HtmlAgilityPack.HtmlWeb web = new HtmlAgilityPack.HtmlWeb();
  163. //Debug.WriteLine(link);
  164. HtmlAgilityPack.HtmlDocument doc = web.Load(link);
  165. var HeaderNames = doc.DocumentNode.SelectNodes("//div[@class='result__snippet']").ToList();
  166. foreach (var item in HeaderNames)
  167. {
  168. Debug.WriteLine(item.InnerText);
  169. }
  170. ListAll = HeaderNames.ConvertAll(x => x.ToString()).ToArray();
  171. Debug.WriteLine(ListAll);
  172. }*/
  173.  
  174. private void pictureBox1_Click(object sender, EventArgs e)
  175. {
  176.  
  177. }
  178.  
  179. private void button1_Click(object sender, EventArgs e)
  180. {
  181. Screenshot();
  182. Tesseract();
  183. Seperate();
  184. HTMLparse();
  185.  
  186. }
  187.  
  188. private void Form1_Load(object sender, EventArgs e)
  189. {
  190.  
  191. }
  192.  
  193. private void questionBox_Click(object sender, EventArgs e)
  194. {
  195.  
  196. }
  197. }
  198. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement