コード例 #1
0
        public static SpellerInteropBase CreateInstance()
        {
            SpellerInteropBase result = null;
            bool flag = false;

            try
            {
                result = new WinRTSpellerInterop();
                flag   = true;
            }
            catch (PlatformNotSupportedException)
            {
                flag = false;
            }
            catch (NotSupportedException)
            {
                flag = true;
            }
            if (!flag)
            {
                try
                {
                    result = new NLGSpellerInterop();
                }
                catch (Exception ex) when(ex is DllNotFoundException || ex is EntryPointNotFoundException)
                {
                    return(null);
                }
                return(result);
            }
            return(result);
        }
コード例 #2
0
 public SpellerSegment(WordSegment segment, SpellChecker spellChecker, WinRTSpellerInterop owner)
 {
     _segment      = segment;
     _spellChecker = spellChecker;
     _suggestions  = null;
     _owner        = owner;
 }
コード例 #3
0
 public SpellerSentence(string sentence, WordsSegmenter wordBreaker, SpellChecker spellChecker, WinRTSpellerInterop owner)
 {
     _sentence = sentence;
     _wordBreaker = wordBreaker;
     _spellChecker = spellChecker;
     _segments = null;
     _owner = owner;
 }
コード例 #4
0
            public SpellerSegment(string sourceString, ITextRange textRange, SpellChecker spellChecker, WinRTSpellerInterop owner)
            {
                _spellChecker = spellChecker;
                _suggestions = null;
                Owner = owner;

                SourceString = sourceString;
                TextRange = textRange;
            }
コード例 #5
0
ファイル: SpellerInteropBase.cs プロジェクト: z2516305651/wpf
        public static SpellerInteropBase CreateInstance()
        {
            SpellerInteropBase spellerInterop = null;

            bool winRTSupport = false;

            try
            {
                spellerInterop = new WinRTSpellerInterop();
                winRTSupport   = true;
            }
            catch (PlatformNotSupportedException)
            {
                winRTSupport = false;
            }
            catch (NotSupportedException)
            {
                // Any other exception besides PlatformNotSupportedException
                // indicates that WinRT API's are supportable on this OS
                // platform, but failed to initialize for some reason.
                winRTSupport = true;
            }

            if (!winRTSupport)
            {
                try
                {
                    spellerInterop = new NLGSpellerInterop();
                }
                catch (Exception ex) when
                    (ex is DllNotFoundException || ex is EntryPointNotFoundException)
                {
                    return(null);
                }
            }

            return(spellerInterop);
        }
コード例 #6
0
        /// <summary>
        ///     Actual implementation of loading a dictionary
        /// </summary>
        /// <param name="lexiconFilePath"></param>
        /// <param name="dictionaryLoadedCallback"></param>
        /// <param name="callbackParam"></param>
        /// <returns>
        ///     A tuple of cultureinfo detected from <paramref name="lexiconFilePath"/> and
        ///     a temp file path which holds a copy of <paramref name="lexiconFilePath"/>
        ///
        ///     If no culture is specified in the first line of <paramref name="lexiconFilePath"/>
        ///     in the format #LID nnnn (where nnnn = decimal LCID of the culture), then invariant
        ///     culture is returned.
        /// </returns>
        /// <remarks>
        ///     At the end of this method, we guarantee that <paramref name="lexiconFilePath"/>
        ///     can be reclaimed (i.e., potentially deleted) by the caller.
        /// </remarks>
        private Tuple<string, string> LoadDictionaryImpl(string lexiconFilePath)
        {
            if (_isDisposed)
            {
                return new Tuple<string, string>(null, null);
            }

            if (!File.Exists(lexiconFilePath))
            {
                throw new ArgumentException(SR.Get(SRID.CustomDictionaryFailedToLoadDictionaryUri, lexiconFilePath));
            }

            bool fileCopied = false;
            string lexiconPrivateCopyPath = null;

            try
            {
                CultureInfo culture = null;

                // Read the first line of the file and detect culture, if specified
                using (FileStream stream = new FileStream(lexiconFilePath, FileMode.Open, FileAccess.Read))
                {
                    string line = null;
                    using (StreamReader reader = new StreamReader(stream))
                    {
                        line = reader.ReadLine();
                        culture = WinRTSpellerInterop.TryParseLexiconCulture(line);
                    }
                }

                string ietfLanguageTag = culture.IetfLanguageTag;

                // Make a temp file and copy the original file over.
                // Ensure that the copy has Unicode (UTF16-LE) encoding
                using (FileStream lexiconPrivateCopyStream = FileHelper.CreateAndOpenTemporaryFile(out lexiconPrivateCopyPath, extension: "dic"))
                {
                    WinRTSpellerInterop.CopyToUnicodeFile(lexiconFilePath, lexiconPrivateCopyStream);
                    fileCopied = true;
                }

                // Add the temp file (with .dic extension) just created to a cache,
                // then pass it along to IUserDictionariesRegistrar

                if (!_customDictionaryFiles.ContainsKey(ietfLanguageTag))
                {
                    _customDictionaryFiles[ietfLanguageTag] = new List<string>();
                }

                _customDictionaryFiles[ietfLanguageTag].Add(lexiconPrivateCopyPath);

                using (new SpellerCOMActionTraceLogger(this, SpellerCOMActionTraceLogger.Actions.RegisterUserDictionary))
                {
                    SpellCheckerFactory.RegisterUserDictionary(lexiconPrivateCopyPath, ietfLanguageTag);
                }

                return new Tuple<string, string>(ietfLanguageTag, lexiconPrivateCopyPath);
            }
            catch (Exception e) when ((e is ArgumentException) || !fileCopied)
            {
                // IUserDictionariesRegistrar.RegisterUserDictionary can
                // throw ArgumentException on failure. Cleanup the temp file if
                // we successfully created one.
                if (lexiconPrivateCopyPath != null)
                {
                    FileHelper.DeleteTemporaryFile(lexiconPrivateCopyPath);
                }

                throw new ArgumentException(SR.Get(SRID.CustomDictionaryFailedToLoadDictionaryUri, lexiconFilePath), e);
            }
        }
コード例 #7
0
        /// <summary>
        /// Tokenizes <paramref name="text"/> using <paramref name="segmenter"/>, and then identifies fixes-up
        /// the tokens to account for any missed text "in-between" those tokens.
        /// </summary>
        /// <param name="segmenter">Word-breaker instance</param>
        /// <param name="text">The text being tokenized</param>
        /// <param name="spellChecker">The spell-checker instance used to augment the tokenizing process</param>
        /// <param name="owner">Calling <see cref="WinRTSpellerInterop"/> instance</param>
        /// <returns></returns>
        /// <remarks>
        /// Windows.Data.Text.WordsSegmenter tends to drop punctuation characters like period ('.')
        /// when tokenizing text. Though this behavior is compatible with a vast majority of text-processing
        /// scenarios (like word-counting), it is not ideal for spell-checking.
        ///
        /// In this method, the following <paramref name="spellChecker"/> augmented heuristic is applied to update the token-list generated by
        /// <paramref name="segmenter"/>.
        ///
        ///  - Identify if any text 'missingFragment' has been dropped by the <paramref name="segmenter"/>
        ///  - If the token immediately preceding 'missingFragment', previousToken, has a spelling error, then attempt to
        ///     create new candiate tokens in the following order:
        ///
        ///             previousToken + missingFragment[0..0]
        ///             previousToken + missingFragment[0..1]
        ///             previousToken + missingFragment[0..2]
        ///             ...
        ///             ...
        ///             previousToken + missingFragment[0..LEN-1], where LEN = LEN(missingFragment)
        ///
        ///  - Select the first candidate token that is free of spelling errors, and replace 'previousToken' with it.
        ///  - For performance reasons, we choose a constant MAXLEN = 4 such that when LEN > MAXLEN, only MAXLEN
        ///     tokens are considered.
        ///     - MAXLEN = 4 is a somewhat arbitrary choice, though it seems more than sufficient to address common
        ///       problems this heuristic is intended to help with.
        ///
        ///     - Typical word-breaking problems that have been observed empirically involve only one missed character,
        ///       for which MAXLEN=1 would be sufficient. MAXLEN=4 is chosen as a sufficiently-large tradeoff between
        ///       correctness and performance.
        ///
        ///     - Also see https://github.com/dotnet/wpf/pull/2753#issuecomment-602120768 for a discussion related to this.
        /// </remarks>
        public static IReadOnlyList <SpellerSegment> ComprehensiveGetTokens(
            this WordsSegmenter segmenter,
            string text,
            SpellChecker spellChecker,
            WinRTSpellerInterop owner)
        {
            IReadOnlyList <WordSegment> tokens = segmenter?.GetTokens(text) ?? Array.Empty <WordSegment>();

            if (tokens.Count == 0)
            {
                return(Array.Empty <SpellerSegment>());
            }

            var allTokens = new List <SpellerSegment>();
            int predictedNextTokenStartPosition = 0;

            for (int i = 0; i < tokens.Count; i++)
            {
                int nextTokenStartPosition = (int)tokens[i].SourceTextSegment.StartPosition;
                int nextTokenLength        = (int)tokens[i].SourceTextSegment.Length;

                if (spellChecker != null)
                {
                    if (nextTokenStartPosition > predictedNextTokenStartPosition)
                    {
                        // There is a "gap" between the last recorded token and the current token.
                        // Identify the missing token and add it as a "supplementary word segment" - but only if the token
                        // turns out to be a substantial one (i.e., if the string is non-blank/non-empty).
                        var missingFragment =
                            new SpellerSegment(
                                text,
                                new WinRTSpellerInterop.TextRange(
                                    predictedNextTokenStartPosition,
                                    nextTokenStartPosition - predictedNextTokenStartPosition),
                                spellChecker,
                                owner);
                        if (allTokens.Count > 0)
                        {
                            var substToken = GetSpellCheckCleanSubstitutionToken(spellChecker, text, allTokens[allTokens.Count - 1], missingFragment);
                            if (substToken != null)
                            {
                                allTokens[allTokens.Count - 1] = new SpellerSegment(text, substToken.Value, spellChecker, owner);
                            }
                        }
                    }
                }

                allTokens.Add(
                    new SpellerSegment(
                        text,
                        new WinRTSpellerInterop.TextRange(
                            nextTokenStartPosition,
                            nextTokenLength),
                        spellChecker,
                        owner));
                predictedNextTokenStartPosition = nextTokenStartPosition + nextTokenLength;
            }

            if (tokens.Count > 0 &&
                spellChecker != null &&
                spellChecker.HasErrors(tokens[tokens.Count - 1].Text) &&
                predictedNextTokenStartPosition < text.Length)
            {
                // There is a token possibly missing at the end of the string
                var missingFragment =
                    new SpellerSegment(
                        text,
                        new WinRTSpellerInterop.TextRange(
                            predictedNextTokenStartPosition,
                            text.Length - predictedNextTokenStartPosition),
                        spellChecker,
                        owner);

                if (allTokens.Count > 0)
                {
                    var substToken = GetSpellCheckCleanSubstitutionToken(spellChecker, text, allTokens[allTokens.Count - 1], missingFragment);
                    if (substToken != null)
                    {
                        allTokens[allTokens.Count - 1] = new SpellerSegment(text, substToken.Value, spellChecker, owner);
                    }
                }
            }

            return(allTokens.AsReadOnly());
        }
        // Token: 0x06003F1A RID: 16154 RVA: 0x00120230 File Offset: 0x0011E430
        public static IReadOnlyList <WinRTSpellerInterop.SpellerSegment> ComprehensiveGetTokens(this WordsSegmenter segmenter, string text, SpellChecker spellChecker, WinRTSpellerInterop owner)
        {
            IReadOnlyList <WordSegment> readOnlyList       = ((segmenter != null) ? segmenter.GetTokens(text) : null) ?? new List <WordSegment>().AsReadOnly();
            List <WinRTSpellerInterop.SpellerSegment> list = new List <WinRTSpellerInterop.SpellerSegment>();

            if (readOnlyList.Count == 0)
            {
                return(list.AsReadOnly());
            }
            int num = 0;

            for (int i = 0; i < readOnlyList.Count; i++)
            {
                int startPosition = (int)readOnlyList[i].SourceTextSegment.StartPosition;
                int length        = (int)readOnlyList[i].SourceTextSegment.Length;
                if (spellChecker != null && startPosition > num)
                {
                    WinRTSpellerInterop.SpellerSegment missingFragment = new WinRTSpellerInterop.SpellerSegment(text, new WinRTSpellerInterop.TextRange(num, startPosition - num), spellChecker, owner);
                    if (list.Count > 0)
                    {
                        WinRTSpellerInterop.TextRange?spellCheckCleanSubstitutionToken = WinRTSpellerInteropExtensions.GetSpellCheckCleanSubstitutionToken(spellChecker, text, list[list.Count - 1], missingFragment);
                        if (spellCheckCleanSubstitutionToken != null)
                        {
                            list[list.Count - 1] = new WinRTSpellerInterop.SpellerSegment(text, spellCheckCleanSubstitutionToken.Value, spellChecker, owner);
                        }
                    }
                }
                list.Add(new WinRTSpellerInterop.SpellerSegment(text, new WinRTSpellerInterop.TextRange(startPosition, length), spellChecker, owner));
                num = startPosition + length;
            }
            if (readOnlyList.Count > 0)
            {
                bool flag;
                if (spellChecker == null)
                {
                    flag = true;
                }
                else
                {
                    List <SpellChecker.SpellingError> list2 = spellChecker.ComprehensiveCheck(readOnlyList[readOnlyList.Count - 1].Text, true);
                    flag = (((list2 != null) ? new int?(list2.Count) : null) != 0);
                }
                if (flag && num < text.Length)
                {
                    WinRTSpellerInterop.SpellerSegment missingFragment2 = new WinRTSpellerInterop.SpellerSegment(text, new WinRTSpellerInterop.TextRange(num, text.Length - num), spellChecker, owner);
                    if (list.Count > 0)
                    {
                        WinRTSpellerInterop.TextRange?spellCheckCleanSubstitutionToken2 = WinRTSpellerInteropExtensions.GetSpellCheckCleanSubstitutionToken(spellChecker, text, list[list.Count - 1], missingFragment2);
                        if (spellCheckCleanSubstitutionToken2 != null)
                        {
                            list[list.Count - 1] = new WinRTSpellerInterop.SpellerSegment(text, spellCheckCleanSubstitutionToken2.Value, spellChecker, owner);
                        }
                    }
                }
            }
            return(list.AsReadOnly());
        }
コード例 #9
0
        private Tuple <CultureInfo, String> LoadDictionaryImpl(string lexiconFilePath)
        {
            if (_isDisposed)
            {
                return(new Tuple <CultureInfo, string>(null, null));
            }

            try
            {
                new FileIOPermission(FileIOPermissionAccess.Read, lexiconFilePath).Demand();
            }
            catch (SecurityException se)
            {
                throw new ArgumentException(SR.Get(SRID.CustomDictionaryFailedToLoadDictionaryUri, lexiconFilePath), se);
            }

            if (!File.Exists(lexiconFilePath))
            {
                throw new ArgumentException(SR.Get(SRID.CustomDictionaryFailedToLoadDictionaryUri, lexiconFilePath));
            }

            bool   fileCopied             = false;
            string lexiconPrivateCopyPath = null;

            try
            {
                CultureInfo culture = null;

                // Read the first line of the file and detect culture, if specified
                using (FileStream stream = new FileStream(lexiconFilePath, FileMode.Open, FileAccess.Read))
                {
                    string line = null;
                    using (StreamReader reader = new StreamReader(stream))
                    {
                        line    = reader.ReadLine();
                        culture = WinRTSpellerInterop.TryParseLexiconCulture(line);
                    }
                }

                // Make a temp file and copy the original file over.
                // Ensure that the copy has Unicode (UTF16-LE) encoding
                lexiconPrivateCopyPath = WinRTSpellerInterop.GetTempFileName(extension: "dic");

                new FileIOPermission(FileIOPermissionAccess.Read | FileIOPermissionAccess.Write, lexiconPrivateCopyPath).Assert();
                try
                {
                    WinRTSpellerInterop.CopyToUnicodeFile(lexiconFilePath, lexiconPrivateCopyPath);
                    fileCopied = true;
                }
                finally
                {
                    CodeAccessPermission.RevertAssert();
                }

                // Add the temp file (with .dic extension) just created to a cache,
                // then pass it along to IUserDictionariesRegistrar

                _customDictionaryFilesLock.WaitOne();
                try
                {
                    if (!_customDictionaryFiles.ContainsKey(culture))
                    {
                        _customDictionaryFiles[culture] = new List <string>();
                    }

                    _customDictionaryFiles[culture].Add(lexiconPrivateCopyPath);
                }
                finally
                {
                    _customDictionaryFilesLock.Release();
                }

                SpellCheckerFactory.RegisterUserDictionary(lexiconPrivateCopyPath, culture.IetfLanguageTag);

                return(new Tuple <CultureInfo, string>(culture, lexiconPrivateCopyPath));
            }
            catch (Exception e) when((e is SecurityException) || (e is ArgumentException) || !fileCopied)
            {
                // IUserDictionariesRegistrar.RegisterUserDictionary can
                // throw ArgumentException on failure. Cleanup the temp file if
                // we successfully created one.
                if (lexiconPrivateCopyPath != null)
                {
                    File.Delete(lexiconPrivateCopyPath);
                }

                throw new ArgumentException(SR.Get(SRID.CustomDictionaryFailedToLoadDictionaryUri, lexiconFilePath), e);
            }
        }
コード例 #10
0
 // Token: 0x06008615 RID: 34325 RVA: 0x0024B9A6 File Offset: 0x00249BA6
 public SpellerSegment(string sourceString, WordSegment segment, SpellChecker spellChecker, WinRTSpellerInterop owner) : this(sourceString, new WinRTSpellerInterop.TextRange(segment.SourceTextSegment), spellChecker, owner)
 {
 }
コード例 #11
0
 // Token: 0x06008614 RID: 34324 RVA: 0x0024B97A File Offset: 0x00249B7A
 public SpellerSegment(string sourceString, SpellerInteropBase.ITextRange textRange, SpellChecker spellChecker, WinRTSpellerInterop owner)
 {
     this._spellChecker = spellChecker;
     this._suggestions  = null;
     this.Owner         = owner;
     this.SourceString  = sourceString;
     this.TextRange     = textRange;
 }
コード例 #12
0
        private Tuple <string, string> LoadDictionaryImpl(string lexiconFilePath)
        {
            if (this._isDisposed)
            {
                return(new Tuple <string, string>(null, null));
            }
            try
            {
                new FileIOPermission(FileIOPermissionAccess.Read, lexiconFilePath).Demand();
            }
            catch (SecurityException innerException)
            {
                throw new ArgumentException(SR.Get("CustomDictionaryFailedToLoadDictionaryUri", new object[]
                {
                    lexiconFilePath
                }), innerException);
            }
            if (!File.Exists(lexiconFilePath))
            {
                throw new ArgumentException(SR.Get("CustomDictionaryFailedToLoadDictionaryUri", new object[]
                {
                    lexiconFilePath
                }));
            }
            bool   flag = false;
            string text = null;
            Tuple <string, string> result;

            try
            {
                CultureInfo cultureInfo = null;
                using (FileStream fileStream = new FileStream(lexiconFilePath, FileMode.Open, FileAccess.Read))
                {
                    using (StreamReader streamReader = new StreamReader(fileStream))
                    {
                        string line = streamReader.ReadLine();
                        cultureInfo = WinRTSpellerInterop.TryParseLexiconCulture(line);
                    }
                }
                string ietfLanguageTag = cultureInfo.IetfLanguageTag;
                using (FileStream fileStream2 = FileHelper.CreateAndOpenTemporaryFile(out text, FileAccess.Write, FileOptions.None, "dic", "WPF"))
                {
                    WinRTSpellerInterop.CopyToUnicodeFile(lexiconFilePath, fileStream2);
                    flag = true;
                }
                if (!this._customDictionaryFiles.ContainsKey(ietfLanguageTag))
                {
                    this._customDictionaryFiles[ietfLanguageTag] = new List <string>();
                }
                this._customDictionaryFiles[ietfLanguageTag].Add(text);
                using (new SpellerCOMActionTraceLogger(this, SpellerCOMActionTraceLogger.Actions.RegisterUserDictionary))
                {
                    SpellCheckerFactory.RegisterUserDictionary(text, ietfLanguageTag, true);
                }
                result = new Tuple <string, string>(ietfLanguageTag, text);
            }
            catch (Exception ex) when(ex is SecurityException || ex is ArgumentException || !flag)
            {
                if (text != null)
                {
                    FileHelper.DeleteTemporaryFile(text);
                }
                throw new ArgumentException(SR.Get("CustomDictionaryFailedToLoadDictionaryUri", new object[]
                {
                    lexiconFilePath
                }), ex);
            }
            return(result);
        }