/// <summary>
        /// This method saved the specified scoreboard to the appropriate user's scoreboard file. it writes the data of the scoreboard
        /// using a similar notation to html, where the different tags refer to different fields of the scoreboard hierarchy. Items
        /// are nested inside sessions, which are in turn nested inside a scoreboard.
        /// </summary>
        /// <param name="scoreboard"></param>
        public static void SaveScoreboard(MPAiSpeakScoreBoard scoreboard)
        {
            if (scoreboard.IsEmpty())
            {
                return;
            }

            if (File.Exists(SpeakScoreboardFileAddress(scoreboard.User)))
            {
                File.Delete(SpeakScoreboardFileAddress(scoreboard.User));
            }
            using (FileStream fs = new FileStream(SpeakScoreboardFileAddress(scoreboard.User), FileMode.Create))
            {
                using (StreamWriter sw = new StreamWriter(fs))
                {
                    sw.WriteLine("<Scoreboard>");
                    foreach (MPAiSpeakScoreBoardSession session in scoreboard.Sessions)
                    {
                        if (session.IsEmpty())
                        {
                            continue;
                        }
                        sw.WriteLine("<Session>");
                        sw.WriteLine("<Date>");
                        sw.WriteLine(session.DateAndTime);
                        sw.WriteLine("</Date>");
                        sw.WriteLine("<Content>");
                        foreach (MPAiSpeakScoreBoardItem item in session.Content)
                        {
                            sw.WriteLine("<Expected>");
                            sw.WriteLine(item.ExpectedText);
                            sw.WriteLine("</Expected>");
                            sw.WriteLine("<Recognised>");
                            sw.WriteLine(item.RecognisedText);
                            sw.WriteLine("</Recognised>");
                            sw.WriteLine("<Analysis>");
                            sw.WriteLine(item.Analysis);
                            sw.WriteLine("</Analysis>");
                            sw.WriteLine("<RecordingName>");
                            sw.WriteLine(item.RecordingName);
                            sw.WriteLine("</RecordingName>");
                        }
                        sw.WriteLine("</Content>");
                        sw.WriteLine("</Session>");
                    }
                    sw.WriteLine("</Scoreboard>");
                }
            }
            File.SetAttributes(SpeakScoreboardFileAddress(scoreboard.User), File.GetAttributes(SpeakScoreboardFileAddress(scoreboard.User)) | FileAttributes.Hidden);
        }
        /// <summary>
        /// This method loads the scoreboard for the specified user. It does this by reading the html-style file created by SaveScoreboard.
        /// The nested nature of the file is why this method is also deeply nested.
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public static MPAiSpeakScoreBoard LoadScoreboard(MPAiUser user)
        {
            MPAiSpeakScoreBoard scoreboard = new MPAiSpeakScoreBoard(user);

            if (File.Exists(SpeakScoreboardFileAddress(scoreboard.User)))
            {
                using (FileStream fs = new FileStream(SpeakScoreboardFileAddress(scoreboard.User), FileMode.Open))
                {
                    using (StreamReader sr = new StreamReader(fs))
                    {
                        string line;
                        line = sr.ReadLine();
                        //MPAiMessageBoxFactory.Show(line + ": <Scoreboard> expected");
                        if (line.Equals("<Scoreboard>"))
                        {
                            //MPAiMessageBoxFactory.Show("Success, entered <Scoreboard>");
                            while (!line.Equals("</Scoreboard>"))
                            {
                                line = sr.ReadLine();
                                while (line.Equals("<Session>"))
                                {
                                    while (!line.Equals("</Session>"))
                                    {
                                        DateTime dateAndTime = new DateTime();;
                                        line = sr.ReadLine();
                                        if (line.Equals("<Date>"))
                                        {
                                            line = sr.ReadLine();
                                            while (!line.Equals("</Date>"))
                                            {
                                                dateAndTime = new DateTime();
                                                if (!DateTime.TryParse(line, out dateAndTime))
                                                {
                                                    throw new FileLoadException("Date could not be read");
                                                }
                                                line = sr.ReadLine();
                                            }
                                            line = sr.ReadLine();
                                        }

                                        List <MPAiSpeakScoreBoardItem> content = new List <MPAiSpeakScoreBoardItem>();
                                        if (line.Equals("<Content>"))
                                        {
                                            line = sr.ReadLine();
                                            while (!line.Equals("</Content>"))
                                            {
                                                string expected      = "";
                                                string recognised    = "";
                                                string analysis      = "";
                                                string recordingName = "";

                                                if (line.Equals("<Expected>"))
                                                {
                                                    bool firstline = true;
                                                    line = sr.ReadLine();
                                                    while (!line.Equals("</Expected>"))
                                                    {
                                                        if (firstline)
                                                        {
                                                            firstline = false;
                                                            expected += line;
                                                        }
                                                        else
                                                        {
                                                            expected += String.Format(@"{0}", Environment.NewLine) + line;
                                                        }
                                                        line = sr.ReadLine();
                                                    }
                                                    line = sr.ReadLine();
                                                }

                                                if (line.Equals("<Recognised>"))
                                                {
                                                    bool firstline = true;
                                                    line = sr.ReadLine();
                                                    while (!line.Equals("</Recognised>"))
                                                    {
                                                        if (firstline)
                                                        {
                                                            firstline   = false;
                                                            recognised += line;
                                                        }
                                                        else
                                                        {
                                                            recognised += String.Format(@"{0}", Environment.NewLine) + line;
                                                        }
                                                        line = sr.ReadLine();
                                                    }
                                                    line = sr.ReadLine();
                                                }

                                                if (line.Equals("<Analysis>"))
                                                {
                                                    bool firstline = true;
                                                    line = sr.ReadLine();
                                                    while (!line.Equals("</Analysis>"))
                                                    {
                                                        if (firstline)
                                                        {
                                                            firstline = false;
                                                            analysis += line;
                                                        }
                                                        else
                                                        {
                                                            analysis += String.Format(@"{0}", Environment.NewLine) + line;
                                                        }
                                                        line = sr.ReadLine();
                                                    }
                                                    line = sr.ReadLine();
                                                }

                                                if (line.Equals("<RecordingName>"))
                                                {
                                                    bool firstline = true;
                                                    line = sr.ReadLine();
                                                    while (!line.Equals("</RecordingName>"))
                                                    {
                                                        if (firstline)
                                                        {
                                                            firstline      = false;
                                                            recordingName += line;
                                                        }
                                                        else
                                                        {
                                                            recordingName += String.Format(@"{0}", Environment.NewLine) + line;
                                                        }
                                                        line = sr.ReadLine();
                                                    }
                                                    line = sr.ReadLine();
                                                }
                                                content.Add(new MPAiSpeakScoreBoardItem(expected, recognised, analysis, recordingName));
                                            }
                                            line = sr.ReadLine();
                                        }
                                        scoreboard.NewScoreBoardSession(dateAndTime, content);
                                    }
                                    line = sr.ReadLine();
                                }
                            }
                        }
                    }
                }
            }
            return(scoreboard);
        }
Exemple #3
0
        /// <summary>
        /// Generates an HTML score report based on an input MPAiSpeak scoreboard.
        /// </summary>
        /// <param name="scoreboard">The scoreboard to generate an HTML report of.</param>
        public static void GenerateMPAiSpeakScoreHTML(MPAiSpeakScoreBoard scoreboard)
        {
            if (scoreboard.IsEmpty())
            {
                return;
            }

            scoreboard.SaveScoreBoardToFile();

            if (!File.Exists(ScoreboardReportCSSAddress))
            {
                generateScoreboardCSS();
            }
            using (FileStream fs = new FileStream(MPAiSpeakScoreReportHTMLAddress, FileMode.Create))
            {
                using (StreamWriter sw = new StreamWriter(fs))
                {
                    using (HtmlTextWriter htw = new HtmlTextWriter(sw))
                    {
                        htw.RenderBeginTag(HtmlTextWriterTag.Html);
                        // Table settings
                        htw.RenderBeginTag(HtmlTextWriterTag.Head);
                        htw.AddAttribute("charset", "UTF-8");
                        htw.RenderBeginTag(HtmlTextWriterTag.Meta);
                        htw.AddAttribute(HtmlTextWriterAttribute.Rel, "stylesheet");
                        htw.AddAttribute(HtmlTextWriterAttribute.Type, "text/css");
                        htw.AddAttribute(HtmlTextWriterAttribute.Href, "Scoreboard.css");
                        htw.RenderBeginTag(HtmlTextWriterTag.Link);
                        htw.RenderEndTag();
                        htw.RenderEndTag();
                        htw.RenderEndTag();
                        //Scoreboard Title
                        htw.RenderBeginTag(HtmlTextWriterTag.Body);
                        htw.AddAttribute(HtmlTextWriterAttribute.Class, "title");
                        htw.RenderBeginTag(HtmlTextWriterTag.Div);
                        htw.RenderBeginTag(HtmlTextWriterTag.H3);
                        htw.Write(scoreboard.User.GetCorrectlyCapitalisedName() + "'s MPAi Speak Pronunciation Scoreboard");
                        htw.RenderEndTag();
                        htw.RenderEndTag();

                        foreach (MPAiSpeakScoreBoardSession session in scoreboard.Sessions)
                        {
                            if (session.IsEmpty())
                            {
                                continue;
                            }
                            //Table Title
                            htw.AddAttribute(HtmlTextWriterAttribute.Class, "table-title");
                            htw.RenderBeginTag(HtmlTextWriterTag.Div);
                            htw.RenderBeginTag(HtmlTextWriterTag.H3);
                            htw.Write(session.DateAndTime.ToString("dd MMMM yyyy, h:mm tt"));
                            htw.RenderEndTag();
                            htw.RenderEndTag();
                            // Header row of the table
                            htw.AddAttribute(HtmlTextWriterAttribute.Class, "table-fill");
                            htw.RenderBeginTag(HtmlTextWriterTag.Table);
                            htw.RenderBeginTag(HtmlTextWriterTag.Tr);
                            htw.RenderBeginTag(HtmlTextWriterTag.Th);
                            htw.Write("Expecting Word");
                            htw.RenderEndTag();
                            htw.RenderBeginTag(HtmlTextWriterTag.Th);
                            htw.Write("Recognized Word");
                            htw.RenderEndTag();
                            htw.RenderBeginTag(HtmlTextWriterTag.Th);
                            htw.Write("Similarity Score");
                            htw.RenderEndTag();
                            htw.RenderBeginTag(HtmlTextWriterTag.Th);
                            htw.Write("Analysis Tips");
                            htw.RenderEndTag();
                            htw.RenderEndTag();
                            // Table rows
                            foreach (MPAiSpeakScoreBoardItem item in session.Content)
                            {
                                htw.RenderBeginTag(HtmlTextWriterTag.Tr);
                                htw.RenderBeginTag(HtmlTextWriterTag.Td);
                                htw.Write(item.ExpectedText);
                                htw.RenderEndTag();
                                htw.RenderBeginTag(HtmlTextWriterTag.Td);
                                htw.Write(item.RecognisedText);
                                htw.RenderEndTag();
                                htw.RenderBeginTag(HtmlTextWriterTag.Td);
                                htw.Write(item.Similarity.ToString("0.0%"));
                                htw.RenderEndTag();
                                htw.RenderBeginTag(HtmlTextWriterTag.Td);
                                htw.Write(item.Analysis);
                                htw.RenderEndTag();
                                htw.RenderEndTag();
                            }
                            // Correctness score
                            float correctness = session.OverallCorrectnessPercentage;
                            if (correctness >= 0.8)
                            {
                                htw.AddAttribute(HtmlTextWriterAttribute.Class, "good-colour");
                            }
                            else if (correctness >= 0.5)
                            {
                                htw.AddAttribute(HtmlTextWriterAttribute.Class, "medium-colour");
                            }
                            else
                            {
                                htw.AddAttribute(HtmlTextWriterAttribute.Class, "bad-colour");
                            }
                            htw.RenderBeginTag(HtmlTextWriterTag.Tr);
                            htw.AddAttribute(HtmlTextWriterAttribute.Colspan, "4");
                            htw.RenderBeginTag(HtmlTextWriterTag.Td);
                            htw.Write("Pronunciation is " + correctness.ToString("0.0%") + " Correct");
                            htw.RenderEndTag();
                            htw.RenderEndTag();
                            htw.RenderEndTag();
                        }
                        htw.RenderEndTag();
                        htw.RenderEndTag();
                    }
                }
            }
        }