Пример #1
0
        // SDL libraries reuse
        public static int GetWordsCount(this string text, string culture)
        {
            int      wordsNum       = 0;
            var      _Culture       = new CultureInfo(culture);
            var      _LinguaSegment = new Segment(_Culture);
            Assembly languageProcessingAssembly;
            Assembly translationMemoryToolsAssembly;

            // set values to the IText (used when calling builder.VisitText() method)
            var _textSDL = new Text(text);
            var txt      = new Common.Text(text);

            FileTypeSupport.Framework.BilingualApi.IText _iTxt = txt;
            var txtProperties = new Common.TextProperties(text);

            _iTxt.Properties = txtProperties;

            // make setups from Sdl.LanguagePlatform.TranslationMemoryTools.dll
            //get assembly
            if (_studioInstalledLocation.Contains(@"Program Files (x86)\SDL\SDL Trados Studio"))
            {
                // used in Trados Studio
                translationMemoryToolsAssembly = Assembly.LoadFrom(Path.Combine(_studioInstalledLocation, "Sdl.LanguagePlatform.TranslationMemoryTools.dll"));
            }
            else
            {
                // used in the separate app (Sdl.Community.SplitMergeUIWix) generated by the Wix installer
                translationMemoryToolsAssembly = Assembly.LoadFrom(Path.Combine(_studioInstalledLocation, "Sdl.LanguagePlatform.TranslationMemoryTools.dll"));
            }

            //get object type
            var linguaSegmentBuilderType =
                translationMemoryToolsAssembly.GetType("Sdl.LanguagePlatform.TranslationMemoryTools.LinguaSegmentBuilder");

            //create constructor type
            Type[] constructorArgumentTypes = { typeof(Segment), typeof(bool), typeof(bool) };

            //get constructor
            ConstructorInfo linguaSegmentConstrutor = linguaSegmentBuilderType.GetConstructor(constructorArgumentTypes);

            // invoke constructor with its arguments and call method/set values from builder object
            dynamic builder = linguaSegmentConstrutor.Invoke(new object[] { _LinguaSegment, false, false });

            builder.VisitText(_iTxt);
            builder.Result.Elements.Add(_textSDL);

            // make setups from Sdl.Core.LanguageProcessing.dll
            if (_studioInstalledLocation.Contains(@"Program Files (x86)\SDL\SDL Trados Studio"))
            {
                // used in Trados Studio
                languageProcessingAssembly = Assembly.LoadFrom(Path.Combine(_studioInstalledLocation, "Sdl.Core.LanguageProcessing.dll"));
            }
            else
            {
                // used in the separate app (Sdl.Community.SplitMergeUIWix) generated by the Wix installer
                languageProcessingAssembly = Assembly.LoadFrom(Path.Combine(_studioInstalledLocation, "Sdl.Core.LanguageProcessing.dll"));
            }

            //get object type
            var     tokenizationFactoryType = languageProcessingAssembly.GetType("Sdl.Core.LanguageProcessing.Tokenization.TokenizerSetupFactory");
            dynamic tokenizerFactory        = tokenizationFactoryType.GetMethods(BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Static);

            var createMethod = tokenizerFactory[0];

            if (createMethod != null)
            {
                dynamic setup = createMethod.Invoke(null, new object[] { _Culture });

                setup.CreateWhitespaceTokens = true;
                setup.BuiltinRecognizers     =
                    LanguagePlatform.Core.Tokenization.BuiltinRecognizers.RecognizeNumbers |
                    LanguagePlatform.Core.Tokenization.BuiltinRecognizers.RecognizeDates |
                    LanguagePlatform.Core.Tokenization.BuiltinRecognizers.RecognizeTimes;

                var             tokenizerType = languageProcessingAssembly.GetType("Sdl.Core.LanguageProcessing.Tokenization.Tokenizer");
                Type[]          constructorTokenizerArgumentTypes = { setup.GetType() };
                ConstructorInfo tokenizerConstructor = tokenizerType.GetConstructor(constructorTokenizerArgumentTypes);
                dynamic         tokenizer            = tokenizerConstructor.Invoke(new object[] { setup });

                IList <LanguagePlatform.Core.Tokenization.Token> _tokens = tokenizer.Tokenize(_LinguaSegment);
                foreach (LanguagePlatform.Core.Tokenization.Token _token in _tokens)
                {
                    if (_token.IsWord)
                    {
                        wordsNum++;
                    }
                }
            }
            return(wordsNum);
        }
Пример #2
0
        // SDL libraries reuse
        public static int GetWordsCount(this string text, string culture)
        {
            var studioInstalledLocation     = GetInstalledStudioVersion();
            var studioInstalledLocationPath = studioInstalledLocation != null?Path.GetDirectoryName(GetInstalledStudioVersion().InstallPath)
                                                  : string.Empty;

            if (string.IsNullOrEmpty(studioInstalledLocationPath))
            {
                MessageBox.Show("Studio location could not be found. Please ensure Trados Studio is installed in order to split files!",
                                "Informative message",
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Information);
                return(0);
            }
            else
            {
                int      wordsNum       = 0;
                var      _Culture       = new CultureInfo(culture);
                var      _LinguaSegment = new Segment(_Culture);
                Assembly languageProcessingAssembly;
                Assembly translationMemoryToolsAssembly;

                // set values to the IText (used when calling builder.VisitText() method)
                var _textSDL = new Text(text);
                var txt      = new Common.Text(text);
                FileTypeSupport.Framework.BilingualApi.IText _iTxt = txt;
                var txtProperties = new Common.TextProperties(text);
                _iTxt.Properties = txtProperties;

                translationMemoryToolsAssembly = Assembly.LoadFrom(Path.Combine(studioInstalledLocationPath, "Sdl.LanguagePlatform.TranslationMemoryTools.dll"));

                //get object type
                var linguaSegmentBuilderType =
                    translationMemoryToolsAssembly.GetType("Sdl.LanguagePlatform.TranslationMemoryTools.LinguaSegmentBuilder");

                //create constructor type
                Type[] constructorArgumentTypes = { typeof(Segment), typeof(bool), typeof(bool) };

                //get constructor
                ConstructorInfo linguaSegmentConstrutor = linguaSegmentBuilderType.GetConstructor(constructorArgumentTypes);

                // invoke constructor with its arguments and call method/set values from builder object
                dynamic builder = linguaSegmentConstrutor.Invoke(new object[] { _LinguaSegment, false, false });
                builder.VisitText(_iTxt);
                builder.Result.Elements.Add(_textSDL);

                languageProcessingAssembly = Assembly.LoadFrom(Path.Combine(studioInstalledLocationPath, "Sdl.Core.LanguageProcessing.dll"));

                //get object type
                var     tokenizationFactoryType = languageProcessingAssembly.GetType("Sdl.Core.LanguageProcessing.Tokenization.TokenizerSetupFactory");
                dynamic tokenizerFactory        = tokenizationFactoryType.GetMethods(BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Static);

                var createMethod = tokenizerFactory[0];
                if (createMethod != null)
                {
                    dynamic setup = createMethod.Invoke(null, new object[] { _Culture });

                    setup.CreateWhitespaceTokens = true;
                    setup.BuiltinRecognizers     =
                        LanguagePlatform.Core.Tokenization.BuiltinRecognizers.RecognizeNumbers |
                        LanguagePlatform.Core.Tokenization.BuiltinRecognizers.RecognizeDates |
                        LanguagePlatform.Core.Tokenization.BuiltinRecognizers.RecognizeTimes;

                    var             tokenizerType = languageProcessingAssembly.GetType("Sdl.Core.LanguageProcessing.Tokenization.Tokenizer");
                    Type[]          constructorTokenizerArgumentTypes = { setup.GetType() };
                    ConstructorInfo tokenizerConstructor = tokenizerType.GetConstructor(constructorTokenizerArgumentTypes);
                    dynamic         tokenizer            = tokenizerConstructor.Invoke(new object[] { setup });

                    IList <LanguagePlatform.Core.Tokenization.Token> _tokens = tokenizer.Tokenize(_LinguaSegment);
                    foreach (LanguagePlatform.Core.Tokenization.Token _token in _tokens)
                    {
                        if (_token.IsWord)
                        {
                            wordsNum++;
                        }
                    }
                }
                return(wordsNum);
            }
        }