public ActionResult Download()
        {
            try
            {
                ArticleGradingInfo articleGradingInfo = TempData["articleInfo"] as ArticleGradingInfo;

                string name, path;
                name = articleGradingInfo.Name == null ? "Article" : articleGradingInfo.Name;
                path = HttpContext.Server.MapPath(string.Format(@"~\{0}.docx", name));

                DocX doc = CreateDocX(articleGradingInfo, path);

                FileStream   docFile   = new FileStream(path, FileMode.Open, FileAccess.Read);
                MemoryStream docMemory = new MemoryStream();
                docFile.Seek(0, SeekOrigin.Begin);
                docFile.CopyTo(docMemory);
                docFile.Close();

                System.IO.File.Delete(path);

                return(File(docMemory.ToArray(), "application/vnd.openxmlformats-officedocument.wordprocessingml.document", string.Format("{0} - Result.docx", name)));
            }
            catch (Exception e)
            {
                Logger.Instance.WriteLine("An error occurred while trying to download Article {0}", e.Message);
                return(RedirectToAction("Error", "HomeController"));
            }
        }
        private DocX CreateDocX(ArticleGradingInfo i_ArticleGradingInfo, string i_Path)
        {
            using (DocX doc = DocX.Create(i_Path))
            {
                Paragraph stat = doc.InsertParagraph("Result:").Font(new System.Drawing.FontFamily("Arial")).FontSize(15D).Bold().UnderlineStyle(UnderlineStyle.singleLine);
                stat.AppendLine(string.Format("Common: {0}%, {1}  \nNormal: {2}%, {3} \nRare: {4}%, {5} \nScore: {6} \nNumber Of Words: {7}\n\n\n"
                                              , Math.Round((double)i_ArticleGradingInfo.CommonWords.Count / i_ArticleGradingInfo.CleanedWords.Count * 100), i_ArticleGradingInfo.CommonWords.Count
                                              , Math.Round((double)i_ArticleGradingInfo.NormalWords.Count / i_ArticleGradingInfo.CleanedWords.Count * 100), i_ArticleGradingInfo.NormalWords.Count
                                              , Math.Round((double)i_ArticleGradingInfo.RareWords.Count / i_ArticleGradingInfo.CleanedWords.Count * 100), i_ArticleGradingInfo.RareWords.Count
                                              , Math.Round(100 - ((i_ArticleGradingInfo.NormalWords.Count * 0.5f + i_ArticleGradingInfo.RareWords.Count) * 100 / i_ArticleGradingInfo.CleanedWords.Count)).ToString()
                                              , i_ArticleGradingInfo.CleanedWords.Count.ToString()));

                stat.FontSize(13D);

                Paragraph wordParagraph = doc.InsertParagraph();

                foreach (string word in i_ArticleGradingInfo.Words)
                {
                    string cleanedWord = TextGrading.CleanWord(word).ToLower();

                    wordParagraph.Append(word);
                    wordParagraph.Font(new System.Drawing.FontFamily("Arial"));
                    wordParagraph.FontSize(12D);
                    wordParagraph.Bold();

                    if (i_ArticleGradingInfo.RareWords.Contains(cleanedWord))
                    {
                        wordParagraph.Color(System.Drawing.Color.Red);
                    }
                    else if (i_ArticleGradingInfo.NormalWords.Contains(cleanedWord))
                    {
                        wordParagraph.Color(System.Drawing.Color.Orange);
                    }
                    else
                    {
                        wordParagraph.Color(System.Drawing.Color.Black);
                    }
                }

                doc.Save();

                return(doc);
            }
        }
        public ActionResult Index(string ContentTA, HttpPostedFileBase ArticleFU)
        {
            ArticleGradingInfo articleGradingInfo = null;
            bool   fileCantBeGraded = false;
            string text             = null;

            if (ArticleFU != null)
            {
                if (ArticleFU.VerifyArticleType())
                {
                    try
                    {
                        string extension = ArticleFU.FileName.ToLower().Substring(ArticleFU.FileName.LastIndexOf('.') + 1);
                        text = TextGrading.LoadTextFromFile(ArticleFU.InputStream, extension);
                        articleGradingInfo      = TextGrading.AnalyzeSingleText(text);
                        articleGradingInfo.Name = ArticleFU.FileName.Substring(0, ArticleFU.FileName.LastIndexOf('.'));

                        Logger.UpdateNumberOfUses(1);
                    }
                    catch
                    {
                        fileCantBeGraded = true;
                    }
                }
                else
                {
                    fileCantBeGraded = true;
                }
            }
            else
            {
                articleGradingInfo = TextGrading.AnalyzeSingleText(ContentTA);
                Logger.UpdateNumberOfUses(1);
            }


            if (fileCantBeGraded)
            {
                articleGradingInfo       = new ArticleGradingInfo();
                articleGradingInfo.Error = "File can not be graded.";
            }

            return(View(articleGradingInfo));
        }
        private List <ArticleGradingInfo> getArticlesGradingInfoList(IEnumerable <HttpPostedFileBase> i_Articles)
        {
            List <ArticleGradingInfo> articlesGradingInfo = new List <ArticleGradingInfo>();
            string text = null, name;

            if (i_Articles != null)
            {
                foreach (var article in i_Articles)
                {
                    ArticleGradingInfo currentArticleInfo = null;

                    if (article.VerifyArticleType())
                    {
                        try
                        {
                            string extension = article.FileName.ToLower().Substring(article.FileName.LastIndexOf('.') + 1);
                            name = article.FileName.ToLower().Substring(0, article.FileName.LastIndexOf('.'));
                            text = TextGrading.LoadTextFromFile(article.InputStream, extension);

                            currentArticleInfo      = TextGrading.AnalyzeSingleText(text);
                            currentArticleInfo.Name = name + extension;
                        }
                        catch
                        {
                            articlesGradingInfo.Clear();
                            break;
                        }
                    }
                    else
                    {
                        articlesGradingInfo.Clear();
                        break;
                    }

                    if (!string.IsNullOrEmpty(text) && currentArticleInfo.Error == null)
                    {
                        articlesGradingInfo.Add(currentArticleInfo);
                    }
                }
            }

            return(articlesGradingInfo);
        }
示例#5
0
        public static ArticleGradingInfo AnalyzeSingleText(string i_Text)
        {
            ArticleGradingInfo articleGradingInfo;

            if (string.IsNullOrWhiteSpace(i_Text))
            {
                articleGradingInfo       = new ArticleGradingInfo();
                articleGradingInfo.Error = "Article is empty.";
            }
            else
            {
                if (!m_WordsLoaded)
                {
                    loadWords();
                }

                articleGradingInfo = analyzeArticle(i_Text);
            }

            return(articleGradingInfo);
        }
示例#6
0
        private static ArticleGradingInfo analyzeArticle(string i_Article)
        {
            List <string> cleanedWords = getCleanedWords(i_Article);

            ArticleGradingInfo articleGradingInfo = new ArticleGradingInfo();

            articleGradingInfo.Content = i_Article;

            articleGradingInfo.CommonWords = getWordsInRange(cleanedWords, m_Common, int.MaxValue);
            articleGradingInfo.NormalWords = getWordsInRange(cleanedWords, m_Normal, m_Common - 1);
            articleGradingInfo.RareWords   = getWordsInRange(cleanedWords, 0, m_Normal - 1);

            foreach (string splitOption in sr_SplitOptions)
            {
                i_Article = i_Article.Replace(splitOption, "\0" + splitOption + "\0");
            }

            articleGradingInfo.CleanedWords = cleanedWords;
            articleGradingInfo.Words        = i_Article.Split(new char[] { '\0' }, StringSplitOptions.RemoveEmptyEntries);

            articleGradingInfo.Score = cleanedWords.Count;

            return(articleGradingInfo);
        }