Exemplo n.º 1
0
 /// <summary>
 /// Load Lexicon Data object.
 /// </summary>
 /// <param name="errorSet">ErrorSet.</param>
 /// <returns>Lexicon Data object.</returns>
 internal override object LoadDataObject(ErrorSet errorSet)
 {
     Lexicon lexicon = new Lexicon(this.Language);
     Lexicon.ContentControler lexiconControler = new Lexicon.ContentControler();
     lexiconControler.IsCaseSensitive = true;
     lexicon.Load(this.Path, lexiconControler);
     return lexicon;
 }
Exemplo n.º 2
0
        /// <summary>
        /// Initialize the validator.
        /// </summary>
        public void EnsureInitialized()
        {
            Debug.Assert(LexiconFilePath != null || Lexicon != null);
            Debug.Assert(PhoneSetFilePath != null || PhoneSet != null);

            if (_lexicon == null)
            {
                _lexicon = new Lexicon();
                _lexicon.Load(LexiconFilePath);
            }

            if (_phoneset == null)
            {
                _phoneset = new TtsPhoneSet();
                _phoneset.Load(PhoneSetFilePath);
            }

            if (_phoneset.Language != _lexicon.Language)
            {
                string message = Utility.Helper.NeutralFormat(
                    "phoneset and lexicon language should match");
                throw new InvalidDataException(message);
            }

            _language = _lexicon.Language;
        }
Exemplo n.º 3
0
        /// <summary>
        /// Read all lexicon items from XML lexicon file.
        /// </summary>
        /// <param name="lexiconFilePath">XML lexicon filepath.</param>
        /// <returns>Lexicon.</returns>
        public static Lexicon ReadAllData(string lexiconFilePath)
        {
            if (string.IsNullOrEmpty(lexiconFilePath))
            {
                throw new ArgumentNullException("lexiconFilePath");
            }

            Lexicon lexicon = new Lexicon();
            lexicon.Load(lexiconFilePath);
            return lexicon;
        }
Exemplo n.º 4
0
        /// <summary>
        /// Load XML file.
        /// </summary>
        /// <param name="xmlDoc">XmlDoc.</param>
        /// <param name="nsmgr">Nsmgr.</param>
        /// <param name="contentController">Content controller.</param>
        protected override void Load(XmlDocument xmlDoc, XmlNamespaceManager nsmgr, object contentController)
        {
            ContentControler lexiconContentController = contentController as ContentControler;
            Debug.Assert(contentController == null || lexiconContentController != null);
            if (lexiconContentController == null)
            {
                lexiconContentController = new ContentControler();
            }

            Language language = Localor.StringToLanguage(xmlDoc.DocumentElement.Attributes["lang"].InnerText);
            if (!Language.Equals(Language.Neutral) && !language.Equals(Language))
            {
                ErrorSet.Add(CommonError.NotConsistentLanguage,
                    Language.ToString(), "initial one", language.ToString(), "lexicon");
            }

            Language = language;
            if (xmlDoc.DocumentElement.Attributes["domain"] != null)
            {
                string domainTag = xmlDoc.DocumentElement.Attributes["domain"].InnerText;
                if (!string.IsNullOrEmpty(domainTag))
                {
                    DomainTag = domainTag;
                }
            }

            // Load current lexicon
            _items.Clear();
            XmlNodeList wordNodes = xmlDoc.DocumentElement.SelectNodes("tts:w", nsmgr);
            foreach (XmlNode wordNode in wordNodes)
            {
                LoadLexicalItem(this, wordNode, nsmgr, lexiconContentController);
            }

            // Get baseline lexicon file path
            string baseLexiconFilePath = string.Empty;
            if (xmlDoc.DocumentElement.FirstChild != null &&
                xmlDoc.DocumentElement.FirstChild.LocalName == "include" &&
                xmlDoc.DocumentElement.FirstChild.Attributes["href"] != null)
            {
                BaseLexiconRelativeFilePath = xmlDoc.DocumentElement.FirstChild.Attributes["href"].InnerText;
                if (!string.IsNullOrEmpty(BaseLexiconRelativeFilePath))
                {
                    baseLexiconFilePath = Helper.GetFullPath(Path.GetDirectoryName(this.FilePath), BaseLexiconRelativeFilePath);
                }
            }

            if (!string.IsNullOrEmpty(baseLexiconFilePath) && File.Exists(baseLexiconFilePath))
            {
                Lexicon baseLexicon = new Lexicon();
                baseLexicon._isBaseline = true;

                // Load baseline lexicon
                baseLexicon.Load(baseLexiconFilePath, lexiconContentController);

                // Merge current lexicon and baseline lexicon
                foreach (var baseItem in baseLexicon.Items)
                {
                    // We drop those items if they have "deleted" status when LoadLexicalItem(),
                    // so there's no deleted words in both lexicons.

                    // if this item isn't in current lexicon, add it into current lexicon
                    if (!_items.ContainsKey(baseItem.Key))
                    {
                        _items.Add(baseItem.Key, baseItem.Value);
                    }
                    //// if this item is already in current lexicon, keep current word item
                    /*else
                    {

                    } */
                }
            }
        }
Exemplo n.º 5
0
        public static Lexicon GetLexicon(Language language)
        {
            Lexicon lexicon = null;
            if (_ttsLexiconMap.ContainsKey(language))
            {
                lexicon = _ttsLexiconMap[language];
            }
            else
            {
                using (StreamReader reader = Localor.LoadResource(language, Localor.LexiconFileName))
                {
                    if (reader != null)
                    {
                        lexicon = new Lexicon(language);
                        lexicon.Load(reader);
                        _ttsLexiconMap[language] = lexicon;
                    }
                }
            }

            return lexicon;
        }
Exemplo n.º 6
0
        private ErrorSet CompileLexicon(Stream outputStream)
        {
            if (outputStream == null)
            {
                throw new ArgumentNullException("outputStream");
            }

            ErrorSet errorSet = new ErrorSet();

            ErrorSet subErrorSet = new ErrorSet();
            LexicalAttributeSchema schema = (LexicalAttributeSchema)GetObject(
                RawDataName.LexicalAttributeSchema, subErrorSet);
            MergeDependencyError(errorSet, subErrorSet, _schemaFullName);

            subErrorSet.Clear();
            TtsPhoneSet phoneSet = (TtsPhoneSet)GetObject(RawDataName.PhoneSet, subErrorSet);
            MergeDependencyError(errorSet, subErrorSet, RawDataName.PhoneSet);

            if (!errorSet.Contains(ErrorSeverity.MustFix))
            {
                Microsoft.Tts.Offline.Core.Lexicon lexicon = (Microsoft.Tts.Offline.Core.Lexicon)GetObject(RawDataName.Lexicon, errorSet);
                errorSet.Merge(lexicon.ErrorSet);

                // Change to case insensitive lexicon
                MemoryStream lexiconStream = new MemoryStream();
                using (XmlWriter xmlWriter = XmlWriter.Create(lexiconStream))
                {
                    Microsoft.Tts.Offline.Core.Lexicon.ContentControler lexiconControler = 
                        new Microsoft.Tts.Offline.Core.Lexicon.ContentControler();
                    lexiconControler.IsCaseSensitive = true;
                    lexicon.Save(xmlWriter, lexiconControler);
                }

                lexiconStream.Seek(0, SeekOrigin.Begin);
                Microsoft.Tts.Offline.Core.Lexicon caseInsensitiveLexicon = new Microsoft.Tts.Offline.Core.Lexicon();
                using (StreamReader sr = new StreamReader(lexiconStream))
                {
                    caseInsensitiveLexicon.Load(sr);
                }

                if (caseInsensitiveLexicon != null && !errorSet.Contains(ErrorSeverity.MustFix))
                {
                    caseInsensitiveLexicon.LexicalAttributeSchema = schema;

                    caseInsensitiveLexicon.PhoneSet = phoneSet;
                    caseInsensitiveLexicon.Validate();

                    // Set severity of errors only in case-insensitive lexicon to NoError for they're not treated as real error
                    caseInsensitiveLexicon.ErrorSet.SetSeverity(ErrorSeverity.NoError);

                    string vendorLexiconPath = Helper.GetTempFileName();

                    caseInsensitiveLexicon.SaveToVendorLexicon(vendorLexiconPath);

                    string toolFileName = ToolName.BldVendor2;
                    string binaryLexiconPath = Helper.GetTempFileName();

                    string compilingArguments = Helper.NeutralFormat("-v {0} V2 \"{1}\" \"{2}\" \"{3}\" TTS",
                        (int)_language, _dataHandlerList.Datas[RawDataName.LexicalAttributeSchema].Path,
                        vendorLexiconPath, binaryLexiconPath);
                    string toolPath = Path.Combine(ToolDir, toolFileName);

                    CheckToolExists(toolPath, errorSet);
                    if (!errorSet.Contains(ErrorSeverity.MustFix))
                    {
                        HandleCommandLine(ModuleDataName.Lexicon, toolPath, compilingArguments,
                            binaryLexiconPath, outputStream, errorSet);
                    }

                    File.Delete(vendorLexiconPath);

                    errorSet.Merge(caseInsensitiveLexicon.ErrorSet);
                }
                else if (lexicon == null)
                {
                    errorSet.Add(DataCompilerError.RawDataError, "Lexicon");
                }
                else
                {
                    errorSet.Merge(caseInsensitiveLexicon.ErrorSet);
                }
            }

            return errorSet;
        }