Example #1
0
        /// <summary>
        /// Remove all slice boundaries in the source script file
        /// And save it to target script file.
        /// </summary>
        /// <param name="sourceScriptFilePath">Source script file.</param>
        /// <param name="targetScriptFilePath">Target script file.</param>
        /// <returns>Data error set found.</returns>
        public static ErrorSet RemoveSliceBoundary(string sourceScriptFilePath,
            string targetScriptFilePath)
        {
            // Parameters validation
            if (string.IsNullOrEmpty(sourceScriptFilePath))
            {
                throw new ArgumentNullException("sourceScriptFilePath");
            }

            if (string.IsNullOrEmpty(targetScriptFilePath))
            {
                throw new ArgumentNullException("targetScriptFilePath");
            }

            XmlScriptFile script = new XmlScriptFile();

            // Keep comments in XmlScript file
            XmlScriptFile.ContentControler controler = new XmlScriptFile.ContentControler();
            controler.LoadComments = true;
            script.Load(sourceScriptFilePath, controler);

            foreach (ScriptItem item in script.Items)
            {
                foreach (ScriptWord word in item.AllWords)
                {
                    if (!string.IsNullOrEmpty(word.Pronunciation))
                    {
                        word.Pronunciation = Pronunciation.RemoveUnitBoundary(word.Pronunciation);
                    }
                }
            }

            // Save comments in XmlScript file
            script.Save(targetScriptFilePath, Encoding.Unicode);

            return script.ErrorSet;
        }
Example #2
0
        /// <summary>
        /// Slice the pronunciation of one script file.
        /// </summary>
        /// <param name="scriptFilePath">Source file.</param>
        /// <param name="targetFilePath">Target file.</param>
        /// <returns>Data error set found.</returns>
        public ErrorSet Slice(string scriptFilePath, string targetFilePath)
        {
            XmlScriptFile script = new XmlScriptFile();
            XmlScriptFile.ContentControler controler = new XmlScriptFile.ContentControler();
            controler.LoadComments = true;
            script.Load(scriptFilePath, controler);

            ErrorSet errorSet = Slice(script);
            script.Save(targetFilePath);

            errorSet.Merge(script.ErrorSet);

            return errorSet;
        }
        /// <summary>
        /// Load script and check it.
        /// </summary>
        /// <param name="scriptFile">File to be loaded.</param>
        /// <param name="validateSetting">Validation data set.</param>
        /// <returns>Script loaded.</returns>
        public static XmlScriptFile LoadWithValidation(string scriptFile, XmlScriptValidateSetting validateSetting)
        {
            if (string.IsNullOrEmpty(scriptFile))
            {
                throw new ArgumentNullException("scriptFile");
            }

            if (validateSetting == null)
            {
                throw new ArgumentNullException("validateSetting");
            }

            validateSetting.VerifySetting();

            XmlScriptFile script = new XmlScriptFile();
            script.Load(scriptFile);

            script.PhoneSet = validateSetting.PhoneSet;
            script.PosSet = validateSetting.PosSet;
            script.Validate(validateSetting);

            return script;
        }
Example #4
0
        /// <summary>
        /// Load all the script items from a folder
        /// Note: Here don't validate the content, But duplicate item ID is not allowed.
        /// </summary>
        /// <param name="sourceDir">Script dir.</param>
        /// <param name="errors">Errors happened.</param>
        /// <returns>Loaded items collection.</returns>
        public static Collection<ScriptItem> LoadScriptsWithoutValidation(string sourceDir, ErrorSet errors)
        {
            if (string.IsNullOrEmpty(sourceDir))
            {
                throw new ArgumentNullException("sourceDir");
            }

            if (errors == null)
            {
                throw new ArgumentNullException("errors");
            }

            Collection<ScriptItem> items = new Collection<ScriptItem>();
            Dictionary<string, string> ids = new Dictionary<string, string>();
            string pattern = @"*" + XmlScriptFile.Extension;
            Language language = Language.Neutral;
            foreach (string file in Directory.GetFiles(sourceDir, pattern, SearchOption.AllDirectories))
            {
                XmlScriptFile script = new XmlScriptFile();
                XmlScriptFile.ContentControler controler = new XmlScriptFile.ContentControler();
                controler.LoadComments = true;
                script.Load(file, controler);
                if (language == Language.Neutral)
                {
                    language = script.Language;
                }
                else if (language != script.Language)
                {
                    throw new InvalidDataException(Helper.NeutralFormat(
                        "The language name in File [{0}] is different from other files.", file));
                }

                errors.Merge(script.ErrorSet);
                foreach (ScriptItem item in script.Items)
                {
                    if (ids.ContainsKey(item.Id))
                    {
                        errors.Add(ScriptError.DuplicateItemId, item.Id);
                    }
                    else
                    {
                        item.ScriptFile = null;
                        items.Add(item);
                    }
                }
            }

            return items;
        }
Example #5
0
        /// <summary>
        /// Convert XML script to two-line script.
        /// </summary>
        /// <param name="xmlScript">Input XML script.</param>
        /// <param name="targetFile">Output script.</param>
        /// <param name="phoneSet">
        /// Phone set used to convert pronunciation
        /// It can be null when you can directly get the word's pronunciation in the word's attribute.
        /// </param>
        /// <returns>Errors happened.</returns>
        public static ErrorSet ConvertXmlScriptToTwoLineScript(string xmlScript, 
            string targetFile, TtsPhoneSet phoneSet)
        {
            if (string.IsNullOrEmpty(xmlScript))
            {
                throw new ArgumentNullException("xmlScript");
            }

            if (string.IsNullOrEmpty(targetFile))
            {
                throw new ArgumentNullException("targetFile");
            }

            if (!Directory.Exists(Path.GetDirectoryName(targetFile)))
            {
                throw new DirectoryNotFoundException(targetFile);
            }

            ErrorSet errorSet = new ErrorSet();
            XmlScriptFile script = new XmlScriptFile();
            script.Load(xmlScript);
            ScriptFile oldScript = new ScriptFile(script.Language);
            foreach (ScriptItem item in script.Items)
            {
                ErrorSet itemErrors = new ErrorSet();
                ScriptItem oldItem = ConvertScriptItemToTwoLineFormat(item, phoneSet, itemErrors);
                if (itemErrors.Count != 0)
                {
                    errorSet.Merge(itemErrors);
                }
                else
                {
                    oldScript.Items.Add(oldItem.Id, oldItem);
                }
            }

            oldScript.Save(targetFile, true, true);

            return errorSet;
        }
Example #6
0
        /// <summary>
        /// Merge scripts in a folder into a script file.
        /// Error items are removed from the output file.
        /// </summary>
        /// <param name="scriptDir">Dir conataining script file.</param>
        /// <param name="errorSet">Error set.</param>
        /// <param name="resetId">True means resetting id.</param>
        /// <param name="validateSetting">Validation setting.</param>
        /// <param name="contentController">Contenct controller.</param>
        /// <returns>Xml script file.</returns>
        public static XmlScriptFile MergeScripts(string scriptDir, ErrorSet errorSet,
            bool resetId, XmlScriptValidateSetting validateSetting, object contentController)
        {
            if (string.IsNullOrEmpty(scriptDir))
            {
                throw new ArgumentNullException("scriptDir");
            }

            if (errorSet == null)
            {
                throw new ArgumentNullException("errorSet");
            }

            if (validateSetting == null)
            {
                throw new ArgumentNullException("validateSetting");
            }

            if (!Directory.Exists(scriptDir))
            {
                throw new DirectoryNotFoundException(scriptDir);
            }

            validateSetting.VerifySetting();
            
            XmlScriptValidationScope scope = validateSetting.ValidationScope;

            string[] subFiles = Directory.GetFiles(
                scriptDir, "*" + XmlScriptFile.Extension, SearchOption.AllDirectories);
            XmlScriptFile mergedScript = new XmlScriptFile();

            long id = 0;
            foreach (string file in subFiles)
            {
                XmlScriptFile script = new XmlScriptFile();
                script.Load(file, contentController);
                if (mergedScript.Language == Language.Neutral)
                {
                    mergedScript.Language = script.Language;
                }
                else if (mergedScript.Language != script.Language)
                {
                    throw new InvalidDataException(Helper.NeutralFormat("Inconsistent langage in {0}", file));
                }

                if (scope != XmlScriptValidationScope.None)
                {
                    script.PosSet = validateSetting.PosSet;
                    script.PhoneSet = validateSetting.PhoneSet;

                    script.Validate(validateSetting);
                    script.Remove(GetNeedDeleteItemIds(script.ErrorSet));
                }

                errorSet.Merge(script.ErrorSet);
                foreach (ScriptItem item in script.Items)
                {
                    item.Id = resetId ? Helper.NeutralFormat("{0:D10}", ++id) : item.Id;

                    ErrorSet addErrors = new ErrorSet();
                    if (!mergedScript.Add(item, addErrors, false))
                    {
                        // Added failed
                        errorSet.Merge(addErrors);
                    }
                }
            }

            return mergedScript;
        }