Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Decompiled with JetBrains decompiler
- // Type: ImageEditor.util.SCKolokvijum2014
- // Assembly: ImageEditor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
- // MVID: 898CD58A-25C2-45D2-B6C8-07901E3DDD0B
- // Assembly location: F:\Downloads\SC vezba7 - priprema za kolokvijum-Dabic\SC vezba7 - priprema za kolokvijum-Dabic\SC vezba7 - priprema2\src\lib\ImageEditor.dll
- using Newtonsoft.Json;
- using Newtonsoft.Json.Linq;
- using StringMatching;
- using System;
- using System.Collections.Generic;
- using System.Drawing;
- using System.Drawing.Drawing2D;
- using System.IO;
- using System.Net;
- using System.Windows.Forms;
- namespace ImageEditor.util
- {
- public class SCKolokvijum2014
- {
- private int w = 1680;
- private int h = 480;
- private string _test = "";
- private int _duzinaReda = 10;
- private int _minBrojReci = 2;
- private int _maxBrojReci = 3;
- private int _minBrojRedova = 2;
- private int _maxBrojRedova = 3;
- private double prag = 0.0;
- private int uspeh = 0;
- private DateTime start = DateTime.Now;
- private Dictionary<string, string> _alfabeti = new Dictionary<string, string>();
- private Dictionary<string, HashSet<string>> _recnici = new Dictionary<string, HashSet<string>>();
- private Dictionary<string, List<string>> _recenice = new Dictionary<string, List<string>>();
- private Dictionary<string, bool> _inverzno = new Dictionary<string, bool>();
- private Dictionary<string, int> _brojRedova = new Dictionary<string, int>();
- private Dictionary<string, Color> _bojaSlova = new Dictionary<string, Color>();
- private Dictionary<string, Color> _bojaPozadine = new Dictionary<string, Color>();
- private string _serverUrl = "http://localhost/Mvc4WebApi/";
- private Font f1 = new Font("Verdana", 30f, FontStyle.Bold);
- private Font f2 = new Font("Comic Sans MS", 25f, FontStyle.Bold);
- private Font f3 = new Font("Courier New", 35f, FontStyle.Bold);
- private Random rnd = new Random();
- private bool _generateRandom = false;
- public List<SCKolokvijum2014Rez> rezultati = new List<SCKolokvijum2014Rez>();
- private FrmKolokvijumRezultati frm = (FrmKolokvijumRezultati) null;
- private string _putanjaObucavajuciSkup;
- private string _putanjaTestiranje;
- public int Uspeh
- {
- get
- {
- return this.uspeh;
- }
- }
- public Dictionary<string, string> Alfabeti
- {
- get
- {
- return this._alfabeti;
- }
- }
- public Dictionary<string, HashSet<string>> Recnici
- {
- get
- {
- return this._recnici;
- }
- }
- public SCKolokvijum2014(string putanjaObucavajuciSkup, string putanjaTestiranje, string serverUrl)
- {
- this._putanjaObucavajuciSkup = putanjaObucavajuciSkup;
- this._putanjaTestiranje = putanjaTestiranje;
- this._serverUrl = serverUrl;
- string str1 = System.IO.File.ReadAllText(this._putanjaObucavajuciSkup).Trim();
- string[] strArray1 = System.IO.File.ReadAllLines(this._putanjaTestiranje);
- string[] strArray2 = new string[1]
- {
- "1"
- };
- foreach (string index in strArray2)
- {
- int num = 0;
- List<string> list = new List<string>();
- string[] strArray3 = (string[]) null;
- switch (index)
- {
- case "1":
- strArray3 = new string[2]
- {
- "a",
- "รค"
- };
- this._inverzno.Add(index, false);
- num = 0;
- this._brojRedova.Add(index, 2);
- break;
- }
- if (!this._inverzno[index])
- this._bojaPozadine.Add(index, Color.FromArgb((int) byte.MaxValue, 180, 180, 180));
- if (num == 0)
- this._bojaSlova.Add(index, !this._inverzno[index] ? Color.FromArgb((int) byte.MaxValue, 240, 170, 110) : Color.FromArgb((int) byte.MaxValue, 140, 30, 10));
- string str2 = str1.Replace(", ", "").Replace(strArray3[0], strArray3[1]);
- this._alfabeti.Add(index, str2);
- foreach (string str3 in strArray1)
- {
- string str4 = str3.Substring(str3.LastIndexOf('}') + 1).ToLower().Replace(strArray3[0], strArray3[1]).Replace("!", "").Replace("?", "").Replace(".", "").Replace(",", "").Replace("'", "").Replace("-", "").Replace(":", "");
- list.Add(str4);
- }
- this._recenice.Add(index, list);
- this._recnici.Add(index, this.GenerisiRecnik(index));
- }
- }
- private HashSet<string> GenerisiRecnik(string sifra)
- {
- HashSet<string> hashSet = new HashSet<string>();
- foreach (string str1 in this._recenice[sifra])
- {
- string str2 = str1.Replace("|", " ");
- char[] chArray = new char[1]
- {
- ' '
- };
- foreach (string str3 in str2.Split(chArray))
- {
- if (!hashSet.Contains(str3))
- {
- if (str3.Contains(Environment.NewLine))
- Console.WriteLine(str3);
- hashSet.Add(str3);
- }
- }
- }
- return hashSet;
- }
- public string GetAlfabetServer(string sifra)
- {
- WebRequest webRequest = WebRequest.Create(this._serverUrl + "api/alphabets?sifra=" + sifra);
- webRequest.Method = "GET";
- WebResponse response = webRequest.GetResponse();
- Console.WriteLine(((HttpWebResponse) response).StatusDescription);
- Stream responseStream = response.GetResponseStream();
- StreamReader streamReader = new StreamReader(responseStream);
- JObject jobject = (JObject) JsonConvert.DeserializeObject(streamReader.ReadToEnd());
- streamReader.Close();
- responseStream.Close();
- response.Close();
- return (string) jobject.GetValue("Alphabet");
- }
- public string GetAlfabet(string sifra)
- {
- return this._alfabeti[sifra];
- }
- public HashSet<string> GetRecnikServer(string sifra)
- {
- WebRequest webRequest = WebRequest.Create(this._serverUrl + "api/dictionaries?sifra=" + sifra);
- webRequest.Method = "GET";
- WebResponse response = webRequest.GetResponse();
- Console.WriteLine(((HttpWebResponse) response).StatusDescription);
- Stream responseStream = response.GetResponseStream();
- StreamReader streamReader = new StreamReader(responseStream);
- JObject jobject = (JObject) JsonConvert.DeserializeObject(streamReader.ReadToEnd());
- streamReader.Close();
- responseStream.Close();
- response.Close();
- HashSet<string> hashSet = new HashSet<string>();
- foreach (JValue jvalue in (IEnumerable<JToken>) Extensions.AsJEnumerable((IEnumerable<JToken>) jobject.GetValue("Dictionary")))
- hashSet.Add(((object) jvalue).ToString());
- return hashSet;
- }
- public HashSet<string> GetRecnik(string sifra)
- {
- return this._recnici[sifra];
- }
- public Bitmap SlikaZaObucavanjeServer(string indeks, string ime, string prezime, ZaOcenu zaOcenu, string sifra)
- {
- using (WebClient webClient = new WebClient())
- {
- int num = zaOcenu == ZaOcenu.Sest ? 0 : (zaOcenu == ZaOcenu.Sedam ? 1 : 2);
- Stream stream = webClient.OpenRead(this._serverUrl + (object) "api/images?indeks=" + indeks + "&ime=" + ime + "&prezime=" + prezime + "&tezina=" + (string) (object) num + "&sifra=" + sifra + "&obuka=true");
- Bitmap bitmap = new Bitmap(stream);
- stream.Flush();
- stream.Close();
- Console.WriteLine(webClient.ResponseHeaders.Get("uuid"));
- webClient.Dispose();
- return bitmap;
- }
- }
- public Bitmap SlikaZaObucavanje(string sifra)
- {
- Bitmap bitmap = new Bitmap(this.w, this.h);
- Graphics gr = Graphics.FromImage((Image) bitmap);
- Color[] colorArray = this.BojeSlova(sifra);
- if (!this._inverzno[sifra])
- {
- this.Pozadina(gr, sifra);
- }
- else
- {
- colorArray = this.BojeSlovaInv(sifra);
- this.PozadinaInv(gr, sifra);
- }
- int num1 = 30;
- int num2 = 30;
- int num3 = 0;
- SizeF sizeF;
- for (int index = 0; index < this._alfabeti[sifra].Length; ++index)
- {
- string text = string.Concat((object) this._alfabeti[sifra][index]);
- sizeF = gr.MeasureString(text, this.f1);
- int num4 = (int) sizeF.Width;
- num3 = (int) sizeF.Height;
- SolidBrush solidBrush = new SolidBrush(colorArray[index % 30]);
- gr.DrawString(text ?? "", this.f1, (Brush) solidBrush, (float) num1, (float) num2);
- num1 += num4 + 5;
- }
- int num5 = 30;
- int num6 = num2 + (num3 + 20);
- for (int index = 0; index < this._alfabeti[sifra].Length; ++index)
- {
- string text = string.Concat((object) this._alfabeti[sifra][index]);
- sizeF = gr.MeasureString(text, this.f2);
- int num4 = (int) sizeF.Width;
- num3 = (int) sizeF.Height;
- SolidBrush solidBrush = new SolidBrush(colorArray[index % 30]);
- gr.DrawString(text ?? "", this.f2, (Brush) solidBrush, (float) num5, (float) num6);
- num5 += num4 + 5;
- }
- int num7 = 30;
- int num8 = num6 + (num3 + 20);
- for (int index = 0; index < this._alfabeti[sifra].Length; ++index)
- {
- string text = string.Concat((object) this._alfabeti[sifra][index]);
- sizeF = gr.MeasureString(text, this.f3);
- int num4 = (int) sizeF.Width;
- SolidBrush solidBrush = new SolidBrush(colorArray[index % 30]);
- gr.DrawString(text ?? "", this.f3, (Brush) solidBrush, (float) num7, (float) num8);
- num7 += num4 + 5;
- }
- this.UbaciShum(gr, sifra);
- gr.Dispose();
- return bitmap;
- }
- protected void Pozadina(Graphics gr, string sifra)
- {
- int alpha = (int) byte.MaxValue;
- Color color = this._bojaPozadine[sifra];
- int red = (int) color.R + this.rnd.Next(30) - 15;
- color = this._bojaPozadine[sifra];
- int green = (int) color.G + this.rnd.Next(30) - 15;
- color = this._bojaPozadine[sifra];
- int blue = (int) color.B + this.rnd.Next(30) - 15;
- Color color2 = Color.FromArgb(alpha, red, green, blue);
- LinearGradientBrush linearGradientBrush = new LinearGradientBrush(new Point(0, 0), new Point(this.w, this.h), this._bojaPozadine[sifra], color2);
- Pen pen = new Pen((Brush) linearGradientBrush);
- gr.FillRectangle((Brush) linearGradientBrush, 0, 0, this.w, this.h);
- }
- protected void PozadinaInv(Graphics gr, string sifra)
- {
- int alpha = (int) byte.MaxValue;
- Color color = this._bojaPozadine[sifra];
- int red = (int) color.R + this.rnd.Next(30) - 15;
- color = this._bojaPozadine[sifra];
- int green = (int) color.G + this.rnd.Next(30) - 15;
- color = this._bojaPozadine[sifra];
- int blue = (int) color.B + this.rnd.Next(30) - 15;
- Color color2 = Color.FromArgb(alpha, red, green, blue);
- LinearGradientBrush linearGradientBrush = new LinearGradientBrush(new Point(0, 0), new Point(this.w, this.h), this._bojaPozadine[sifra], color2);
- Pen pen = new Pen((Brush) linearGradientBrush);
- gr.FillRectangle((Brush) linearGradientBrush, 0, 0, this.w, this.h);
- }
- protected Color[] BojeSlova(string sifra)
- {
- Color[] colorArray = new Color[30];
- for (int index = 0; index < 30; ++index)
- {
- this.rnd.Next(100);
- int num = index % 3;
- int alpha = (int) byte.MaxValue;
- Color color1 = this._bojaSlova[sifra];
- int red = (int) color1.R + this.rnd.Next(20) - 10;
- color1 = this._bojaSlova[sifra];
- int green = (int) color1.G + this.rnd.Next(20) - 10;
- color1 = this._bojaSlova[sifra];
- int blue = (int) color1.B + this.rnd.Next(20) - 10;
- Color color2 = Color.FromArgb(alpha, red, green, blue);
- colorArray[index] = color2;
- }
- return colorArray;
- }
- protected Color[] BojeSlovaInv(string sifra)
- {
- return this.BojeSlova(sifra);
- }
- protected void UbaciShum(Graphics gr, string sifra)
- {
- Random random = new Random();
- int num = 200;
- for (int index = 0; index < num; ++index)
- {
- int x = random.Next(this.w - 1);
- int y = random.Next(this.h - 1);
- if (!this._inverzno[sifra])
- gr.DrawRectangle(Pens.Brown, x, y, 1, 1);
- else
- gr.DrawRectangle(Pens.Yellow, x, y, 1, 1);
- }
- }
- public Bitmap SlikaZaTestServer(string indeks, string ime, string prezime, ZaOcenu zaOcenu, string sifra, out string uuid)
- {
- using (WebClient webClient = new WebClient())
- {
- int num = zaOcenu == ZaOcenu.Sest ? 0 : (zaOcenu == ZaOcenu.Sedam ? 1 : 2);
- Stream stream = webClient.OpenRead(this._serverUrl + (object) "api/images?indeks=" + indeks + "&ime=" + ime + "&prezime=" + prezime + "&tezina=" + (string) (object) num + "&sifra=" + sifra + "&obuka=false");
- Bitmap bitmap = new Bitmap(stream);
- stream.Flush();
- stream.Close();
- webClient.Dispose();
- uuid = webClient.ResponseHeaders.Get("uuid");
- return bitmap;
- }
- }
- public Bitmap SlikaZaTest(string indeks, string ime, string prezime, string sifra, ZaOcenu tezina)
- {
- Bitmap bitmap = new Bitmap(this.w, this.h);
- Graphics gr = Graphics.FromImage((Image) bitmap);
- gr.InterpolationMode = InterpolationMode.High;
- Color[] colorArray = this.BojeSlova(sifra);
- if (!this._inverzno[sifra])
- {
- this.Pozadina(gr, sifra);
- }
- else
- {
- colorArray = this.BojeSlovaInv(sifra);
- this.PozadinaInv(gr, sifra);
- }
- Font font = this.f1;
- switch (this.rnd.Next(3))
- {
- case 0:
- font = this.f1;
- break;
- case 1:
- font = this.f2;
- break;
- case 2:
- font = this.f3;
- break;
- }
- int num1 = 20;
- float angle = 0.5f + (float) this.rnd.Next(2);
- gr.RotateTransform(angle);
- int num2 = 1;
- int num3 = 70;
- if (!this._generateRandom)
- {
- string str1 = "";
- while (true)
- {
- string str2;
- string str3;
- char[] chArray;
- do
- {
- int num4;
- if (str1.Length >= 25)
- num4 = str1.Split('|').Length < this._brojRedova[sifra] ? 1 : 0;
- else
- num4 = 1;
- if (num4 != 0)
- {
- str2 = this._recenice[sifra][this.rnd.Next(this._recenice[sifra].Count - 1)].Split('|')[0];
- str3 = str2;
- chArray = new char[1]
- {
- ' '
- };
- }
- else
- goto label_15;
- }
- while (str3.Split(chArray).Length < 3);
- str1 = !(str1 == "") ? str1 + "|" + str2 : str2;
- }
- label_15:
- string str4 = "";
- string[] strArray1 = str1.Split('|');
- int num5 = strArray1.Length;
- for (int index = 0; index < this._brojRedova[sifra]; ++index)
- str4 = str4 + strArray1[index] + "|";
- string str5 = str4.Substring(0, str4.Length - 1);
- if (tezina == ZaOcenu.Sest)
- {
- this._test = str5.Split('|')[0].Replace("|", " ").Replace(" ", "");
- num5 = 1;
- }
- else if (tezina == ZaOcenu.Sedam)
- {
- this._test = str5.Split('|')[0].Replace("|", " ");
- num5 = 1;
- }
- else if (tezina == ZaOcenu.Osam)
- {
- this._test = str5;
- num5 = this._test.Split("|".ToCharArray()).Length;
- }
- string[] strArray2 = this._test.Split("|".ToCharArray());
- for (int index1 = 0; index1 < num5; ++index1)
- {
- int num4 = 30;
- int num6 = index1 * num3;
- string str2 = strArray2[index1].Trim();
- int num7 = this.rnd.Next(str2.Length);
- string[] strArray3 = str2.Split(' ');
- int num8 = 6;
- int num9 = -1;
- for (int index2 = 0; index2 < strArray3.Length; ++index2)
- {
- if (strArray3[index2].Length > num8)
- {
- num9 = str2.IndexOf(strArray3[index2]);
- num8 = strArray3[index2].Length;
- }
- }
- for (int index2 = 0; index2 < str2.Length; ++index2)
- {
- char ch = str2[index2];
- if (tezina == ZaOcenu.Osam && (int) str2[index2] != 32 && (num9 != -1 && index2 > num9) && index2 < num9 + num8 && this.rnd.Next(15) == 1)
- ch = this._alfabeti[sifra][this.rnd.Next(this._alfabeti[sifra].Length - 1)];
- string text = string.Concat((object) ch);
- SizeF sizeF = gr.MeasureString(text, font);
- int num10 = (int) sizeF.Width;
- int num11 = (int) sizeF.Height;
- SolidBrush solidBrush = new SolidBrush(colorArray[index2 % 30]);
- gr.DrawString(text ?? "", font, (Brush) solidBrush, (float) num4, (float) (num1 + num6));
- if (index2 == num7)
- gr.DrawLine(new Pen(this._bojaSlova[sifra]), new Point(num4, num1 + num6), new Point(num4 + num10, num1 + num11 + num6));
- if ((int) str2[index2] == 32)
- num4 += num10 / 2 + 35;
- else
- num4 += num10 / 2 + 20;
- }
- int x = this.rnd.Next(num4);
- gr.DrawEllipse(new Pen(this._bojaSlova[sifra]), x, num1 + num6, 65, 30);
- }
- }
- else
- {
- if (tezina == ZaOcenu.Osam)
- num2 = this.rnd.Next(this._minBrojRedova, this._maxBrojRedova + 1);
- for (int index1 = 0; index1 < num2; ++index1)
- {
- int num4 = 30;
- this._test = "";
- int num5 = index1 * num3;
- for (int index2 = 0; index2 < this._duzinaReda; ++index2)
- {
- int index3 = this.rnd.Next(this._alfabeti[sifra].Length);
- SCKolokvijum2014 scKolokvijum2014 = this;
- string str = scKolokvijum2014._test + (object) this._alfabeti[sifra][index3];
- scKolokvijum2014._test = str;
- }
- int num6 = this.rnd.Next(this._duzinaReda);
- List<int> list = new List<int>();
- if (tezina == ZaOcenu.Sedam || tezina == ZaOcenu.Osam)
- {
- int num7 = this.rnd.Next(this._minBrojReci, this._maxBrojReci + 1);
- for (int index2 = 0; index2 < num7; ++index2)
- {
- int num8 = this.rnd.Next(1, this._duzinaReda);
- if (list.Contains(num8))
- --index2;
- else
- list.Add(num8);
- }
- }
- for (int index2 = 0; index2 < this._duzinaReda; ++index2)
- {
- string text = string.Concat((object) this._test[index2]);
- SizeF sizeF = gr.MeasureString(text, font);
- int num7 = (int) sizeF.Width;
- int num8 = (int) sizeF.Height;
- SolidBrush solidBrush = new SolidBrush(colorArray[index2 % 30]);
- gr.DrawString(text ?? "", font, (Brush) solidBrush, (float) num4, (float) (num1 + num5));
- if (index2 == num6)
- gr.DrawLine(Pens.Green, new Point(num4, num1 + num5), new Point(num4 + num7, num1 + num8 + num5));
- if (list.Contains(index2))
- num4 += num7 / 2 + 30;
- else
- num4 += num7 / 2 + 5;
- }
- int x = this.rnd.Next(num4);
- if (!this._inverzno[sifra])
- gr.DrawEllipse(Pens.Green, x, num1 + num5, 65, 30);
- else
- gr.DrawEllipse(Pens.YellowGreen, x, num1 + num5, 65, 30);
- }
- }
- this.UbaciShum(gr, sifra);
- gr.Dispose();
- this.start = DateTime.Now;
- return bitmap;
- }
- public double ProveraServer(string uuid, string text)
- {
- WebRequest webRequest = WebRequest.Create(this._serverUrl + "api/images?uuid=" + uuid + "&text=" + text);
- webRequest.Method = "POST";
- webRequest.ContentLength = 0L;
- WebResponse response = webRequest.GetResponse();
- Console.WriteLine(((HttpWebResponse) response).StatusDescription);
- Stream responseStream = response.GetResponseStream();
- StreamReader streamReader = new StreamReader(responseStream);
- JObject jobject = (JObject) JsonConvert.DeserializeObject(streamReader.ReadToEnd());
- double num1 = (double) jobject.GetValue("Success");
- double num2 = (double) jobject.GetValue("Time");
- string str = (string) jobject.GetValue("Text");
- streamReader.Close();
- responseStream.Close();
- response.Close();
- this.rezultati.Add(new SCKolokvijum2014Rez()
- {
- test = str,
- prepoznato = text,
- totalms = num2,
- r = num1
- });
- ++this.uspeh;
- if (this.frm == null)
- {
- this.frm = new FrmKolokvijumRezultati(this);
- ((Control) this.frm).Show();
- }
- else if (this.frm.IsDisposed)
- {
- this.frm = new FrmKolokvijumRezultati(this);
- ((Control) this.frm).Show();
- }
- else
- this.frm.osveziRezultate();
- return num1;
- }
- public double Provera(string pp, ZaOcenu tezina)
- {
- pp = pp.Replace("\n", "|").Replace("\r", "");
- if (tezina == ZaOcenu.Sest)
- pp = pp.Replace(" ", "");
- double totalMilliseconds = (DateTime.Now - this.start).TotalMilliseconds;
- double num = 1.0 - (double) Levenshtein.LevenshteinDistance(this._test, pp) / (double) Math.Max(this._test.Length, pp.Length);
- if (num >= this.prag)
- {
- this.rezultati.Add(new SCKolokvijum2014Rez()
- {
- test = this._test,
- prepoznato = pp,
- totalms = totalMilliseconds,
- r = num
- });
- ++this.uspeh;
- if (this.frm == null)
- {
- this.frm = new FrmKolokvijumRezultati(this);
- ((Control) this.frm).Show();
- }
- else if (this.frm.IsDisposed)
- {
- this.frm = new FrmKolokvijumRezultati(this);
- ((Control) this.frm).Show();
- }
- else
- this.frm.osveziRezultate();
- }
- return num;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment