示例#1
0
        /// <summary>
        /// Extract data out from page object. If enableCache is true, this method
        /// will save the data in cache for later fast access.
        /// </summary>
        /// <param name="page">The page object</param>
        private void retrivePageContent(Page page)
        {
            //retrieve page data from cache
            if (enableCache && pageTextCached.ContainsKey(page.GetPageNumber()))
            {
                pageTextCached.TryGetValue(page.GetPageNumber(), out allSpeechText);
                pageTextHTMLCached.TryGetValue(page.GetPageNumber(), out allSpeechTextHTML);
                annotationArrayCached.TryGetValue(page.GetPageNumber(), out annotationArray);
                syllableArrayCached.TryGetValue(page.GetPageNumber(), out syllableArray);
            }
            else
            {
                List <string[]> pageText   = page.GetListOfTextArray();
                List <string>   annotation = page.GetListOfAnnotations();
                List <string>   annArray   = new List <string>();
                //cover list to array
                string whole     = "";
                string wholeHTML = "";
                //obtain the text and the text in HTML format for the current page
                for (int i = 0; i < pageText.Count; i++)
                {
                    if (pageText.ElementAt(i) == null)
                    {
                        wholeHTML = wholeHTML.TrimEnd() + "<br> ";
                    }
                    else
                    {
                        foreach (string str in pageText.ElementAt(i))
                        {
                            whole += str + " ";
                            if (str.Trim().Length == 0)
                            {
                                wholeHTML = wholeHTML.TrimEnd() + " ";
                            }
                            else
                            {
                                wholeHTML += str + " ";
                            }
                            annArray.Add(annotation.ElementAt(i));
                        }
                        //wholeHTML = wholeHTML.TrimEnd() + "<br> ";
                    }
                }
                whole             = whole.Replace("\"", "");
                allSpeechText     = whole.Split(default(string[]), StringSplitOptions.RemoveEmptyEntries);
                allSpeechTextHTML = wholeHTML.Split(default(string[]), StringSplitOptions.RemoveEmptyEntries);
                annotationArray   = annArray.ToArray();
                syllableArray     = EBookUtil.CountSyllables(allSpeechText);


                //save the data to hash map, we can simply retrieve the page data when
                //the page get revisit again
                if (enableCache)
                {
                    pageTextCached.Add(page.GetPageNumber(), allSpeechText);
                    pageTextHTMLCached.Add(page.GetPageNumber(), allSpeechTextHTML);
                    annotationArrayCached.Add(page.GetPageNumber(), annotationArray);
                    syllableArrayCached.Add(page.GetPageNumber(), syllableArray);
                }
            }
        }
示例#2
0
        /*
         * If Mode == REAL TIME, enable jump to different sentence. otherwise, disable jump to different sentence
         */
        public void process(Page page, Mode mode)
        {
            this.mode   = mode;
            currentPage = page;

            //reset parameters when process a new page
            resetParameters();

            //retrieve data from cache
            if (enableCache && pageTextCached.ContainsKey(page.GetPageNumber()))
            {
                pageTextCached.TryGetValue(page.GetPageNumber(), out allSpeechText);
                pageTextHTMLCached.TryGetValue(page.GetPageNumber(), out allSpeechTextHTML);
                annotationArrayCached.TryGetValue(page.GetPageNumber(), out annotationArray);
                syllableArrayCached.TryGetValue(page.GetPageNumber(), out syllableArray);
            }
            else
            {
                List <string[]> pageText   = page.GetListOfTextArray();
                List <string>   annotation = page.GetListOfAnnotations();
                List <string>   annArray   = new List <string>();
                //cover list to array
                string whole     = "";
                string wholeHTML = "";
                //obtain the text and the text in HTML format for the current page
                for (int i = 0; i < pageText.Count; i++)
                {
                    if (pageText.ElementAt(i) == null)
                    {
                        wholeHTML = wholeHTML.TrimEnd() + "<br> ";
                    }
                    else
                    {
                        foreach (string str in pageText.ElementAt(i))
                        {
                            whole += str + " ";
                            if (str.Trim().Length == 0)
                            {
                                wholeHTML = wholeHTML.TrimEnd() + " ";
                            }
                            else
                            {
                                wholeHTML += str + " ";
                            }
                            annArray.Add(annotation.ElementAt(i));
                        }
                        //wholeHTML = wholeHTML.TrimEnd() + "<br> ";
                    }
                }
                whole             = whole.Replace("\"", "");
                allSpeechText     = whole.Split(default(string[]), StringSplitOptions.RemoveEmptyEntries);
                allSpeechTextHTML = wholeHTML.Split(default(string[]), StringSplitOptions.RemoveEmptyEntries);
                annotationArray   = annArray.ToArray();
                syllableArray     = EBookUtil.CountSyllables(allSpeechText);


                //save the data to hash map, we can simply retrieve the page data when
                //the page get revisit again
                if (enableCache)
                {
                    pageTextCached.Add(page.GetPageNumber(), allSpeechText);
                    pageTextHTMLCached.Add(page.GetPageNumber(), allSpeechTextHTML);
                    annotationArrayCached.Add(page.GetPageNumber(), annotationArray);
                    syllableArrayCached.Add(page.GetPageNumber(), syllableArray);
                }
            }



            if (mode != Mode.REPLAY)
            {
                defaultStartIndexes.Clear();
                List <SrgsDocument> srgsDocs = EBookUtil.GenerateGrammars(allSpeechText, annotationArray);
                Debug.WriteLine("generated " + srgsDocs.Count + " grammar files");

                //load grammars
                List <Grammar> gs = new List <Grammar>();
                //loop from srgsDocs.Count to 0 will give the early sentence the priority.
                for (int i = srgsDocs.Count; --i >= 0;)
                {
                    string cfgPath = storyDirectory + "\\" + GRAMMAR_TMP_DIR + "\\" + page.GetPageNumber() + "_" + i + ".cfg";
                    Directory.CreateDirectory(storyDirectory + "\\" + GRAMMAR_TMP_DIR);
                    CompileGrammar(srgsDocs.ElementAt(i), cfgPath);
                    if (mode == Mode.REALTIME)
                    {
                        if (i == 0 || (i > 0 && (allSpeechText[i - 1].Contains("?") || allSpeechText[i - 1].Contains(".") ||
                                                 allSpeechText[i - 1].Contains("!"))))
                        {
                            defaultStartIndexes.Add(i);
                            Debug.WriteLine("loading grammar:" + cfgPath);
                            Grammar storyG = new Grammar(cfgPath);
                            storyG.Weight   = EBookConstant.DEFAULT_WEIGHT;
                            storyG.Priority = EBookConstant.DEFAULT_PRIORITY;

                            gs.Add(storyG);
                        }
                    }
                    else
                    {
                        if (i == 0)
                        {
                            defaultStartIndexes.Add(i);
                            Debug.WriteLine("loading grammar:" + cfgPath);
                            Grammar storyG = new Grammar(cfgPath);
                            storyG.Weight   = EBookConstant.DEFAULT_WEIGHT;
                            storyG.Priority = EBookConstant.DEFAULT_PRIORITY;

                            ActivityExecutor.add(new InternalReloadOnGoingGrammarActivity(storyG));
                        }
                    }
                }
                if (gs.Count > 0)
                {
                    ActivityExecutor.add(new InternalReloadStoryGrammarActivity(gs));
                }
            }
            string pageTextStr = constructPageText();

            ActivityExecutor.add(new InternalUpdatePageTextActivity(pageTextStr, page.pageNumber));
        }