Source Code

  • July 2020
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Source Code as PDF for free.

More details

  • Words: 1,432
  • Pages: 21
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.IO; using Word = Microsoft.Office.Interop.Word; namespace Project2009 { public partial class Summarizer : Form { Double i; public Summarizer() { InitializeComponent(); } private void label1_Click(object sender, EventArgs e) {

} private void label3_Click(object sender, EventArgs e) {

} private void Form1_Load(object sender, EventArgs e) { button3.Visible = false; MessageBox.Show("This program was developed as Final Year Project by Nitish Raj, Parantap Das and Nishant of Dr. MGR University, CSE Dept, 20052009 batch under the guidence of Mrs. Sumathi Eswaran."); } private void button2_Click(object sender, EventArgs e) { String path = textBox1.Text; String keyword = textBox2.Text; String outdir = textBox3.Text; String summary_name = textBox4.Text; if (keyword.Length == 0) { MessageBox.Show("Oops! It seems you forgot to provide a keyword.Please provide a keyword."); goto Exit_error; } try { Word_to_Text_Converter.converter(path); Formatting_Module.Formatter(@"C:\Program Files\Sonu\Summarizer\convertedtext.txt"); path = @"C:\Program Files\Sonu\Summarizer\convertedtext.txt";

} catch { goto Error2; } if (outdir.Length == 0) { try { String related_word = Database_Retriever.ConnectAndQuery(keyword); if (related_word == null) { richTextBox1.Text = "Keyword not in database. Please enter the keyword along with related words"; } else if (related_word == "DNE") { related_word = textBox3.Text; Writer.Write(keyword, related_word); //Application.Exit(); } else { Scoring_Module.score(keyword, related_word);

} } catch { richTextBox1.Text = "Invalid format for related words. Please enter related words as shown: related_word1,related_word2,related_word3,related_word4,related_word5."; goto Error; } } else { try { Scoring_Module.score(keyword, outdir); String related_word = Database_Retriever.ConnectAndQuery(keyword); if (related_word == "DNE") Writer.Write(keyword, outdir); } catch { richTextBox1.Text = "Invalid format for related words. Please enter related words as shown: related_word1,related_word2,related_word3,related_word4,related_word5."; goto Error; }

} Double initial_count = Counter.line_count(path); Double level_of_summarization = Summary_Generator.Summary_Write(initial_count,0); Writer.final_summary(summary_name); String disp_text = @"C:\Program Files\Sonu\Summarizer\" + summary_name + ".txt"; richTextBox1.Text = File.ReadAllText(disp_text); goto Msg; Error: { MessageBox.Show("Sorry!There was an exception during the processing.Please try again."); Application.Exit(); goto X; } Error2: { MessageBox.Show("Unrecognised File Format.Please input a plaintext or Ms-Word file."); Application.Restart(); goto X; } Exit_error: { Application.Exit();

Application.Restart(); goto X; } Msg: { MessageBox.Show("Summarization is complete!"); button3.Visible = true; } X: { String s = "k"; }

} private void button1_Click(object sender, EventArgs e) { OpenFileDialog dig = new OpenFileDialog(); dig.ShowDialog(); String str = dig.FileName; textBox1.Text = str; } private void label5_Click(object sender, EventArgs e) {

}

private void textBox4_TextChanged(object sender, EventArgs e) {

} private void button3_Click(object sender, EventArgs e) { i += 0.75; //int i = 0; String path = textBox1.Text; String summary_name = textBox4.Text; Double initial_count = Counter.line_count(path); Double level_of_summarization = Summary_Generator.Summary_Write(initial_count, i); Writer.final_summary(summary_name); String disp_text = @"C:\Program Files\Sonu\Summarizer\" + summary_name + ".txt"; richTextBox1.Text = File.ReadAllText(disp_text); MessageBox.Show("Done"); } } internal class Formatting_Module { internal static void Formatter(String path) { String npath = path, noutdir = @"C:\Program Files\Sonu\Summarizer\Temp.txt";

String line = File.ReadAllText(@npath); using (StreamWriter sw = new StreamWriter(@noutdir)) { char[] delimiterChars = { '.' }; string[] words = line.Split(delimiterChars); foreach (string s in words) { if (s.Length != 0) sw.WriteLine(s); else sw.WriteLine(""); } } } } internal class Database_Retriever { static internal String ConnectAndQuery(String keyword) { String static_path = @"C:\Program Files\Sonu\Summarizer\keyword.txt", t_rel_words = "", line; String contents = File.ReadAllText(@static_path); if (contents.Contains(keyword)) { using (StreamReader sr = new StreamReader(@static_path))

{ while ((line = (sr.ReadLine())) != null) { t_rel_words = Search(keyword, line); } return (t_rel_words); } } else { return ("DNE"); } } static internal String Search(String keyword, String line) { int l = line.Length; int lk = keyword.Length; String t_contents = line.Substring(lk); if (System.Text.RegularExpressions.Regex.IsMatch(line, keyword, System.Text.RegularExpressions.RegexOptions.IgnoreCase)) { return (t_contents); } else return (null);

} } internal class Counter { internal static long line_count(String path) { long number = LineCount2(@path, true); return (number); } public static long LineCount2(string source, bool isFileName) { if (source != null) { string text = source; long numOfLines = 0; if (isFileName) { FileStream FS = new FileStream(source, FileMode.Open, FileAccess.Read, FileShare.Read); StreamReader SR = new StreamReader(FS); while (text != null) { text = SR.ReadLine(); if (text != null) {

++numOfLines; } } SR.Close(); FS.Close(); return (numOfLines); } else { System.Text.RegularExpressions.Regex RE = new System.Text.RegularExpressions.Regex("\n", System.Text.RegularExpressions.RegexOptions.Multiline); System.Text.RegularExpressions.MatchCollection theMatches = RE.Matches(text); return (theMatches.Count + 1); } } else { return (0); } } } internal class Scoring_Module { internal static void score(String keyword, String related_word)

{ String noutdir = @"C:\Program Files\Sonu\Summarizer\temp.txt"; String noutdir2 = @"C:\Program Files\Sonu\Summarizer\temp1.txt"; String k = keyword, k1, k2, k3, k4, k5; String list = related_word; char[] delimiterChars = { ',' }; string[] words = list.Split(delimiterChars); k1 = words[0]; k2 = words[1]; k3 = words[2]; k4 = words[3]; k5 = words[4]; using (StreamReader sr = new StreamReader(@noutdir)) using (StreamWriter sw = new StreamWriter(@noutdir2)) { String line; while ((line = sr.ReadLine()) != null) { double score = scorer(line, k, k1, k2, k3, k4, k5); String scoredtext = Convert.ToString(score) + " " + line; if (score == 0) scoredtext = null; sw.WriteLine(scoredtext); } }

} internal static double scorer(string line, String k, String k1, String k2, String k3, String k4, String k5) { String rk, rk1, rk2, rk3, rk4, rk5; rk = k; rk1 = k1; rk2 = k2; rk3 = k3; rk4 = k4; rk5 = k5; double score = 0.0; { if (System.Text.RegularExpressions.Regex.IsMatch(line, rk1, System.Text.RegularExpressions.RegexOptions.IgnoreCase)) { score = score + 0.75; } else { score = score + 0.0; } if (System.Text.RegularExpressions.Regex.IsMatch(line, rk2, System.Text.RegularExpressions.RegexOptions.IgnoreCase)) { score = score + 0.75;

} else { score = score + 0.0; } if (System.Text.RegularExpressions.Regex.IsMatch(line, rk3, System.Text.RegularExpressions.RegexOptions.IgnoreCase)) { score = score + 0.75; } else { score = score + 0.0; } if (System.Text.RegularExpressions.Regex.IsMatch(line, rk4, System.Text.RegularExpressions.RegexOptions.IgnoreCase)) { score = score + 0.75; } else { score = score + 0.0; } if (System.Text.RegularExpressions.Regex.IsMatch(line, rk5, System.Text.RegularExpressions.RegexOptions.IgnoreCase)) { score = score + 0.75; } else

{ score = score + 0.0; } if (System.Text.RegularExpressions.Regex.IsMatch(line, rk, System.Text.RegularExpressions.RegexOptions.IgnoreCase)) { score = score + 1.0; } else { score = score + 0.0; } return (score); } } } internal class Summary_Generator { internal static double min_score_calc(string path) { using (StreamReader sr = new StreamReader(@path)) { double min_score = 0, temp_score; String line; while ((line = sr.ReadLine()) != null)

{ temp_score = summa(line); if (temp_score < min_score) min_score = temp_score; } if (min_score == 0) min_score += 0.75; return (min_score); } } internal static double summa(string line) { String s; if (line.Length > 0) s = line.Substring(0, 3); else s = "0"; double j; try { j = Convert.ToDouble(s); } catch { s = line.Substring(0, 1);

j = Convert.ToDouble(s); } return (j); } internal static void min_score_eliminator(string inpath, double min_score) { string read_name = inpath; string write_name = @"C:\Program Files\Sonu\Summarizer\Temp_summary.txt"; using (StreamReader sr = new StreamReader(@read_name)) using (StreamWriter sw = new StreamWriter(@write_name)) { String line; double score; while ((line = sr.ReadLine()) != null) { score = summa(line); if (score <= min_score) line = null; sw.WriteLine(line); } } } internal static String swap(String a, String b) {

String temp = a; a = b; b = temp; return (a); } internal static Double Summary_Write(Double initial_count,Double new_var) { String in_path = @"C:\Program Files\Sonu\Summarizer\temp1.txt"; String pathname_of_temp_file = @"C:\Program Files\Sonu\Summarizer\Temp_summary.txt"; Double score_max = max_score_calc(in_path); Double score_min = min_score_calc(in_path); Double score_temp = (score_max - score_min) / 2 + new_var; min_score_eliminator(in_path, score_temp); Double count_temp = Counter.line_count(pathname_of_temp_file); Double level_of_summarization = (count_temp / initial_count) * 100; return (level_of_summarization); } internal static Double max_score_calc(String path) { using (StreamReader sr = new StreamReader(@path)) { double max_score = 0, temp_score; String line; while ((line = sr.ReadLine()) != null)

{ temp_score = summa(line); if (temp_score > max_score) max_score = temp_score; } return (max_score); } } } internal class Writer { internal static void Write(String keyword, String rel_words) { String path = @"C:\Program Files\Sonu\Summarizer\keyword.txt"; String file_text = File.ReadAllText(path); file_text = file_text + keyword + " " + rel_words; using (StreamWriter sw = File.CreateText(path)) sw.WriteLine(file_text); } internal static void final_summary(string path) { String write_path = @"C:\Program Files\Sonu\Summarizer\" + path + ".txt"; using (StreamReader sr = new StreamReader(@"C:\Program Files\Sonu\Summarizer\Temp_Summary.txt"))

using (StreamWriter sw = new StreamWriter(@write_path)) { String line; String text_write; while ((line = sr.ReadLine()) != null) { text_write = summ_wri(line); sw.WriteLine(text_write); } } } internal static String summ_wri(string line) { String s; if (line.Length >= 4) s = line.Substring(4); else s = ""; return (s); } } internal class Word_to_Text_Converter { internal static void converter(String path) {

object fileName = @path ; object oMissing = System.Reflection.Missing.Value; object oEndOfDoc = "\\endofdoc"; /* \endofdoc is a predefined bookmark */ //Start Word and create a new document. Word._Application oWord; Word._Document oDoc; oWord = new Word.Application(); oWord.Visible = false; oDoc = oWord.Documents.Open(ref fileName, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing); oDoc.ActiveWindow.Selection.WholeStory(); oDoc.ActiveWindow.Selection.Copy(); IDataObject data = Clipboard.GetDataObject(); String text = data.GetData(DataFormats.Text).ToString(); oDoc.Close(ref oMissing, ref oMissing, ref oMissing); oWord.Quit(ref oMissing, ref oMissing, ref oMissing); using (StreamWriter sw = new StreamWriter(@"C:\Program Files\Sonu\Summarizer\convertedtext.txt")) sw.WriteLine(text); } } }

Related Documents

Source Code
July 2020 13
Source Code
June 2020 19
Source Code
May 2020 17
Open Source Code Analysis
October 2019 32