0

我目前正在使用 Unity 和 NetSpell 开发一个 Android 应用程序,在我的桌面上运行良好,但由于无法访问字典文本文件,因此无法在平板电脑上运行。我知道我可以在 Unity 中使用 TextAsset 加载文件,但是我在 NetSpell 中看到的唯一方法是按文件夹和文件路径加载。如何加载字典数据并将其作为字符串传递给 NetSpell?有什么方法可以做到这一点吗?

编辑:添加代码......我应该如何修改以下内容,以便它可以接受字典文件作为字符串?

// open dictionary file
        FileStream fs = new FileStream(dictionaryPath, FileMode.Open, FileAccess.Read, FileShare.Read);
        StreamReader sr = new StreamReader(fs, Encoding.UTF8);

        // read line by line
        while (sr.Peek() >= 0) 
        {
            string tempLine = sr.ReadLine().Trim();
            if (tempLine.Length > 0)
            {
                // check for section flag
                switch (tempLine)
                {
                    case "[Copyright]" :
                    case "[Try]" : 
                    case "[Replace]" : 
                    case "[Prefix]" :
                    case "[Suffix]" :
                    case "[Phonetic]" :
                    case "[Words]" :
                        // set current section that is being parsed
                        currentSection = tempLine;
                        break;
                    default :       
                        // parse line and place in correct object
                    switch (currentSection)
                    {
                        case "[Copyright]" :
                            this.Copyright += tempLine + "\r\n";
                            break;
                        case "[Try]" : // ISpell try chars
                            this.TryCharacters += tempLine;
                            break;
                        case "[Replace]" : // ISpell replace chars
                            this.ReplaceCharacters.Add(tempLine);
                            break;
                        case "[Prefix]" : // MySpell prefix rules
                        case "[Suffix]" : // MySpell suffix rules

                            // split line by white space
                            partMatches = _spaceRegx.Matches(tempLine);

                            // if 3 parts, then new rule  
                            if (partMatches.Count == 3)
                            {
                                currentRule = new AffixRule();

                                // part 1 = affix key
                                currentRule.Name = partMatches[0].Value;
                                // part 2 = combine flag
                                if (partMatches[1].Value == "Y") currentRule.AllowCombine = true;
                                // part 3 = entry count, not used

                                if (currentSection == "[Prefix]")
                                {
                                    // add to prefix collection
                                    this.PrefixRules.Add(currentRule.Name, currentRule);
                                }
                                else 
                                {
                                    // add to suffix collection
                                    this.SuffixRules.Add(currentRule.Name, currentRule);
                                }
                            }
                                //if 4 parts, then entry for current rule
                            else if (partMatches.Count == 4)
                            {
                                // part 1 = affix key
                                if (currentRule.Name == partMatches[0].Value)
                                {
                                    AffixEntry entry = new AffixEntry();

                                    // part 2 = strip char
                                    if (partMatches[1].Value != "0") entry.StripCharacters = partMatches[1].Value;
                                    // part 3 = add chars
                                    entry.AddCharacters = partMatches[2].Value;
                                    // part 4 = conditions
                                    AffixUtility.EncodeConditions(partMatches[3].Value, entry);

                                    currentRule.AffixEntries.Add(entry);
                                }
                            }   
                            break;
                        case "[Phonetic]" : // ASpell phonetic rules
                            // split line by white space
                            partMatches = _spaceRegx.Matches(tempLine);
                            if (partMatches.Count >= 2)
                            {
                                PhoneticRule rule = new PhoneticRule();
                                PhoneticUtility.EncodeRule(partMatches[0].Value, ref rule);
                                rule.ReplaceString = partMatches[1].Value;
                                _phoneticRules.Add(rule);
                            }
                            break;
                        case "[Words]" : // dictionary word list
                            // splits word into its parts
                            string[] parts = tempLine.Split('/');
                            Word tempWord = new Word();
                            // part 1 = base word
                            tempWord.Text = parts[0];
                            // part 2 = affix keys
                            if (parts.Length >= 2) tempWord.AffixKeys = parts[1];
                            // part 3 = phonetic code
                            if (parts.Length >= 3) tempWord.PhoneticCode = parts[2];

                            this.BaseWords.Add(tempWord.Text, tempWord);
                            break;
                    } // currentSection swith
                        break;
                } //tempLine switch
            } // if templine
        } // read line
        // close files
        sr.Close();
        fs.Close();
4

0 回答 0