public DokumentWindow(Dokument dok) { InitializeComponent(); this.tbTytulOryginal.Text = dok.NaglowekOryginal; this.tbTrescOryginal.Text = dok.TrescOryginal; this.tbTytulStem.Text = dok.NaglowekStemming; this.tbTrescStem.Text = dok.TrescStemming; }
/// <summary> /// Robi wszystko co potrzeba żeby przyporządkować miary prawdopodobieństwa do dokumentów /// </summary> /// <param name="zapytanie">Zapytanie usera</param> public void DajCzadu(string zapytanie, ProgressBar progBar) { progBar.Value = 0; Term[] termyZapytania = PrzerobNaTermy(zapytanie); // zapytanie traktujemy jak dokument Dokument zapDok = new Dokument { Termy = termyZapytania.ToList() }; zapDok.WypelnijWektory(this.termy, this.LiczIDF()); for (int i = 0; i < this.dokumenty.Count && this.dokumenty.Count > 0; i++) { progBar.Dispatcher.Invoke(DispatcherPriority.Render, new ThreadStart(delegate { progBar.Value = ((i + 1) * 100) / this.dokumenty.Count; progBar.InvalidateVisual(); progBar.UpdateLayout(); })); this.dokumenty[i].WypelnijWektory(this.termy, zapDok, this.LiczIDF()); } progBar.Value = 0; }
public bool WczytajDokumenty(string path) { if (path.Length > 0 && File.Exists(path)) { try { string documents = string.Empty; using (TextReader tr = new StreamReader(File.Open(path, FileMode.Open))) { documents = tr.ReadToEnd(); } string[] tmpDocs = documents.Split(new string[] { "\n\n" }, StringSplitOptions.RemoveEmptyEntries); if (tmpDocs != null && tmpDocs.Length > 0) { this.Dokumenty = new List<Dokument>(); PorterStemmer ps = new PorterStemmer(); foreach (string item in tmpDocs) { int index = item.IndexOf('\n'); if (index > 0 && index < item.Length) { Dokument newDoc = new Dokument { NaglowekOryginal = item.Substring(0, index), TrescOryginal = item.Substring(index + 1, item.Length - index - 1) }; // usuwamy interpunkcję string naglStem = Regex.Replace(newDoc.NaglowekOryginal, "(\\p{P})", string.Empty).ToLower(); string trescStem = Regex.Replace(newDoc.TrescOryginal, "(\\p{P})", string.Empty).ToLower().Replace('\n', ' '); // używamy algorytmu Portera dla nagłówka string[] splitted = naglStem.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries); naglStem = string.Empty; foreach (string s in splitted) { string st = ps.stemTerm(s.Trim()); naglStem += st + " "; newDoc.Termy.Add(new Term { TermStemming = st }); } naglStem = naglStem.Substring(0, naglStem.Length - 1); // używamy algorytmu Portera dla treści splitted = trescStem.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries); trescStem = string.Empty; foreach (string s in splitted) { string st = ps.stemTerm(s.Trim()); trescStem += st + " "; newDoc.Termy.Add(new Term { TermStemming = st }); } trescStem = trescStem.Substring(0, trescStem.Length - 1); newDoc.NaglowekStemming = naglStem; newDoc.TrescStemming = trescStem; this.Dokumenty.Add(newDoc); } } return true; } } catch (Exception ex) { return false; } } return false; }
private double LiczPrawdopodobienstwo(Dokument zapytanie) { double sumaIlocz = 0.0; foreach (var item in zapytanie.TFIDFVect) { sumaIlocz += zapytanie.TFIDFVect[item.Key] * this.TFIDFVect[item.Key]; } double dlugoscZap = this.LiczDlugoscWektora(zapytanie.TFIDFVect); double dlugoscDok = this.LiczDlugoscWektora(this.TFIDFVect); if (dlugoscDok == 0 || dlugoscZap == 0) { return -1; // not available } return sumaIlocz / (dlugoscZap * dlugoscDok); }
/// <summary> /// Wypełnia wektory BOW, TF i TFIDF na podstawie zapytania. Liczy też ostateczne prawdopodobienstwo. /// (Kolumny są w kolejności takiej jak termy w zapytaniu) /// </summary> public void WypelnijWektory(List<Term> wszystkieTermy, Dokument zapytanie, Dictionary<string, double> idfVect) { this.WypelnijWektory(wszystkieTermy, idfVect); // similarity todo this.MiaraPrawdopodobienstwa = this.LiczPrawdopodobienstwo(zapytanie); }