public static ITranslationEngine GetTranslationEngine(
            Project project)
        {
            if (_translationEngine == null)
            {
                if (project != null)
                {
                    var uid = project.TranslationEngineUniqueInternalName;
                    if (!StringExtensionMethods.IsNullOrWhiteSpace(uid))
                    {
                        foreach (var engine in Engines)
                        {
                            if (string.Compare(uid, engine.UniqueInternalName, StringComparison.OrdinalIgnoreCase) == 0)
                            {
                                _translationEngine = engine;
                                return(engine);
                            }
                        }
                    }
                }

                foreach (var engine in Engines)
                {
                    if (engine.IsDefault)
                    {
                        _translationEngine = engine;
                        return(engine);
                    }
                }

                throw new Exception();
            }

            return(_translationEngine);
        }
        public static ITranslationEngine GetTranslationEngine(
            Project project)
        {
            if (_translationEngine == null)
            {
                if (project != null)
                {
                    var uid = project.TranslationEngineUniqueInternalName;
                    if (!StringExtensionMethods.IsNullOrWhiteSpace(uid))
                    {
                        foreach (var engine in Engines)
                        {
                            if (uid.EqualsNoCase(engine.UniqueInternalName))
                            {
                                _translationEngine = engine;
                                return(engine);
                            }
                        }
                    }
                }

                foreach (var engine in Engines)
                {
                    if (engine.IsDefault)
                    {
                        _translationEngine = engine;
                        return(engine);
                    }
                }

                throw new Exception();
            }

            return(_translationEngine);
        }
 public static void ResetSelectedEngine()
 {
     _translationEngine   = null;
     _translationAppIDSet = false;
     _translationAppID    = null;
     _translationAppID2   = null;
 }
Beispiel #4
0
        public static IReadOnlyList <string> TranslateWord(this ITranslationEngine engine, string sourceWord)
        {
            TranslationResult result = engine.Translate(new[] { sourceWord });

            if (result.WordSources.Any(s => s == TranslationSources.None))
            {
                return(new string[0]);
            }
            return(result.TargetSegment);
        }
Beispiel #5
0
        public LanguageViewModel(ITranslationEngine translationEngine, string name, string twoLetter, bool isEnabled)
        {
            this.translationEngine = translationEngine;
            this.name = name;
            TwoLetter = twoLetter;
            IsEnabled = isEnabled;

            translationEngine.LanguageChanged += LanguageChanged;
            LanguageChanged();
        }
Beispiel #6
0
        public MenuItemViewModel(
            ITranslationEngine translationEngine,
            string label,
            PackIconFontAwesomeKind icon,
            Action navigate)
        {
            this.translationEngine = translationEngine;
            this.navigate          = navigate;
            this.label             = label;
            Icon = icon;

            translationEngine.LanguageChanged += LanguageChanged;

            LanguageChanged();
        }
Beispiel #7
0
        private void Load()
        {
            if (_isLoaded)
            {
                return;
            }

            _smtModel  = _smtModelFactory.Create(this);
            _smtEngine = _smtModel.CreateInteractiveEngine();

            _ruleEngine = _ruleEngineFactory.Create(this);

            _hybridEngine = new HybridTranslationEngine(_smtEngine, _ruleEngine);
            _isLoaded     = true;
        }
Beispiel #8
0
        public MainViewModel(ITranslationEngine translationEngine)
        {
            this.translationEngine = translationEngine;
            Menu = new[]
            {
                new MenuItemViewModel(translationEngine, "Home", PackIconFontAwesomeKind.HomeSolid, () => Navigate(new HomeViewModel(this))),
                new MenuItemViewModel(translationEngine, "Settings", PackIconFontAwesomeKind.CogSolid, () => Navigate(new SettingsViewModel(this)))
            };

            Languages = new[]
            {
                new LanguageViewModel(translationEngine, "Italian", "it", true),
                new LanguageViewModel(translationEngine, "English", "en", false)
            };

            SelectedMenuItem = Menu.First();

            Property(() => Content).OnChanged(x => DisplayMode = x is LoginViewModel ? SplitViewDisplayMode.Overlay : SplitViewDisplayMode.CompactOverlay);
            Property(() => SelectedMenuItem).OnChanged(x => x.Navigate());

            Navigate(new LoginViewModel(this));
        }
Beispiel #9
0
        private void Unload()
        {
            if (!_isLoaded)
            {
                return;
            }

            Save();

            _hybridEngine.Dispose();
            _hybridEngine = null;

            if (_ruleEngine != null)
            {
                _ruleEngine.Dispose();
                _ruleEngine = null;
            }

            _smtEngine.Dispose();
            _smtEngine = null;
            _smtModel.Dispose();
            _smtModel = null;
            _isLoaded = false;
        }
Beispiel #10
0
 public HybridTranslationEngine(IInteractiveSmtEngine smtEngine, ITranslationEngine ruleEngine = null)
 {
     SmtEngine  = smtEngine;
     RuleEngine = ruleEngine;
     _sessions  = new HashSet <HybridInteractiveTranslationSession>();
 }
		private int translateArray(
			string appID,
			string appID2,
			ITranslationEngine ti,
			DataTable table,
			string refLanguageCode,
			DataColumn column,
			int refValueIndex,
			string raw,
			BackgroundWorker bw,
			int delayMilliseconds,
			string prefixSuccess,
			int translationSuccessCount,
			ref int translationErrorCount,
			bool continueOnErrors,
			string prefixError,
			ref int translationCount)
		{
			var items = new List<TranslateItemInfo>();

			// --
			// From GUI to in-memory-list.

			if (column != null)
			{
				foreach (DataRow row in table.Rows)
				{
					items.Clear();
					if (row != null)
					{
						if (bw.CancellationPending)
						{
							throw new OperationCanceledException();
						}

						var text = row[column] as string;
						if (!string.IsNullOrEmpty(text) && text.Trim().Length > 0)
						{
							continue;
						}

						// http://www.codeproject.com/KB/aspnet/ZetaResourceEditor.aspx?msg=3367544#xx3367544xx
						if (FileGroup.IsInternalRow(row))
						{
							continue;
						}

						var sourceText =
							row[refValueIndex] as string;

						if (string.IsNullOrEmpty(sourceText))
						{
							continue;
						}

						items.Add(new TranslateItemInfo { Row = row, SourceText = sourceText });
					}

					// --
					// Pack into blocks.

					var blocks = new List<List<TranslateItemInfo>>();

					for (var index = 0; index < items.Count; index++)
					{
						if (index % ti.MaxArraySize == 0)
						{
							blocks.Add(new List<TranslateItemInfo>());
						}

						blocks[blocks.Count - 1].Add(items[index]);
					}

					// --
					// Translate each block in batch.

					foreach (var block in blocks)
					{
						if (block != null)
						{
							if (bw.CancellationPending)
							{
								throw new OperationCanceledException();
							}
							if (delayMilliseconds > 0)
							{
								Thread.Sleep(delayMilliseconds);
							}

							var sourceTexts = new List<string>();
							foreach (var itemInfo in block)
							{
								sourceTexts.Add(itemInfo.SourceText);
							}

							try
							{
								var destinationTexts =
									ti.TranslateArray(
										appID,
										appID2,
										sourceTexts.ToArray(),
										ti.MapCultureToSourceLanguageCode(
											appID, appID2,
											CultureHelper.CreateCultureErrorTolerant(refLanguageCode)),
										ti.MapCultureToDestinationLanguageCode(
											appID, appID2,
											CultureHelper.CreateCultureErrorTolerant(raw)),
										_project.TranslationWordsToProtect,
										_project.TranslationWordsToRemove);

								for (var index = 0; index < block.Count; ++index)
								{
									block[index].Row[column] = prefixSuccess + destinationTexts[index];

									translationSuccessCount++;
								}
							}
							catch (Exception x)
							{
								translationErrorCount++;

								if (continueOnErrors)
								{
									var destinationText =
										prefixError +
										x.Message;

									foreach (var t in block)
									{
										if (t != null && t.Row != null && t.Row[column] != null)
										{
											t.Row[column] = destinationText;
										}
									}
								}
								else
								{
									throw;
								}
							}

							translationCount++;
						}
					}
				}
			}

			return translationSuccessCount;
		}
 public static void AddListener(ITranslationEngine source, IWeakEventListener listener)
 {
     CurrentManager.ProtectedAddListener(source, listener);
 }
		public static ITranslationEngine GetTranslationEngine(
			Project project)
		{
			if (_translationEngine == null)
			{
				if (project != null)
				{
					var uid = project.TranslationEngineUniqueInternalName;
					if (!StringExtensionMethods.IsNullOrWhiteSpace(uid))
					{
						foreach (var engine in Engines)
						{
							if (string.Compare(uid, engine.UniqueInternalName, StringComparison.OrdinalIgnoreCase) == 0)
							{
								_translationEngine = engine;
								return engine;
							}
						}
					}
				}

				foreach (var engine in Engines)
				{
					if (engine.IsDefault)
					{
						_translationEngine = engine;
						return engine;
					}
				}

				throw new Exception();
			}

			return _translationEngine;
		}
		public static void ResetSelectedEngine()
		{
			_translationEngine = null;
			_translationAppIDSet = false;
			_translationAppID = null;
			_translationAppID2 = null;
		}
			public EngineHelper(
				ITranslationEngine engine)
			{
				Engine = engine;
			}
        private int translateSingle(
            string appID,
            ITranslationEngine ti,
            DataTable table,
            string refLanguageCode,
            DataColumn column,
            int refValueIndex,
            string raw,
            BackgroundWorker bw,
            int delayMilliseconds,
            string prefixSuccess,
            bool useExistingTranslations,
            bool useExistingTranslationsOnly,
            InMemoryTranslationSnapshot imss,
            int translationSuccessCount,
            ref int translationErrorCount,
            bool continueOnErrors,
            string prefixError,
            ref int translationCount,
            ref int internallyExistingTranslationTranslatedSuccessCount)
        {
            if (column != null)
            {
                foreach (DataRow row in table.Rows)
                {
                    if (row != null)
                    {
                        if (bw.CancellationPending)
                        {
                            throw new OperationCanceledException();
                        }

                        var text = row[column] as string;
                        if (!string.IsNullOrEmpty(text) && text.Trim().Length > 0)
                        {
                            continue;
                        }

                        // http://www.codeproject.com/KB/aspnet/ZetaResourceEditor.aspx?msg=3367544#xx3367544xx
                        if (FileGroup.IsInternalRow(row))
                        {
                            continue;
                        }

                        var sourceText = row[refValueIndex] as string;

                        if (string.IsNullOrEmpty(sourceText))
                        {
                            continue;
                        }

                        if (delayMilliseconds > 0)
                        {
                            Thread.Sleep(delayMilliseconds);
                        }

                        try
                        {
                            var sourceLanguageCode = ti.MapCultureToSourceLanguageCode(
                                appID,
                                CultureHelper.CreateCultureErrorTolerant(refLanguageCode));
                            var destinationLanguageCode = ti.MapCultureToDestinationLanguageCode(
                                appID,
                                CultureHelper.CreateCultureErrorTolerant(raw));

                            var doIt = true;

                            // Ggf. aus Cache holen.
                            if (useExistingTranslations)
                            {
                                var dstText = imss?.GetTranslation(refLanguageCode, raw, sourceText);
                                if (dstText != null)
                                {
                                    row[column] = dstText;

                                    translationSuccessCount++;
                                    internallyExistingTranslationTranslatedSuccessCount++;

                                    doIt = false;
                                }
                            }

                            if (!useExistingTranslationsOnly && doIt)
                            {
                                var destinationText =
                                    prefixSuccess +
                                    ti.Translate(
                                        appID,
                                        sourceText,
                                        sourceLanguageCode,
                                        destinationLanguageCode,
                                        _project.TranslationWordsToProtect,
                                        _project.TranslationWordsToRemove);

                                row[column] = destinationText;

                                // Merken für ggf. nächsten Durchlauf.
                                if (useExistingTranslations)
                                {
                                    imss?.AddTranslation(refLanguageCode, raw, sourceText, destinationText);
                                }

                                translationSuccessCount++;
                            }
                        }
                        catch (Exception x)
                        {
                            translationErrorCount++;

                            if (continueOnErrors)
                            {
                                var destinationText = prefixError + x.Message;

                                if (row[column] != null)
                                {
                                    row[column] = destinationText;
                                }
                            }
                            else
                            {
                                throw;
                            }
                        }

                        translationCount++;
                    }
                }
            }

            return(translationSuccessCount);
        }
        private int translateArray(
            string appID,
            ITranslationEngine ti,
            DataTable table,
            string refLanguageCode,
            DataColumn column,
            int refValueIndex,
            string raw,
            BackgroundWorker bw,
            int delayMilliseconds,
            string prefixSuccess,
            bool useExistingTranslations,
            bool useExistingTranslationsOnly,
            InMemoryTranslationSnapshot imss,
            int translationSuccessCount,
            ref int translationErrorCount,
            bool continueOnErrors,
            string prefixError,
            ref int translationCount,
            ref int internallyExistingTranslationTranslatedSuccessCount)
        {
            var items = new List <TranslateItemInfo>();

            // --
            // From GUI to in-memory-list.

            if (column != null)
            {
                foreach (DataRow row in table.Rows)
                {
                    items.Clear();
                    if (row != null)
                    {
                        if (bw.CancellationPending)
                        {
                            throw new OperationCanceledException();
                        }

                        var text = row[column] as string;
                        if (!string.IsNullOrEmpty(text) && text.Trim().Length > 0)
                        {
                            continue;
                        }

                        // http://www.codeproject.com/KB/aspnet/ZetaResourceEditor.aspx?msg=3367544#xx3367544xx
                        if (FileGroup.IsInternalRow(row))
                        {
                            continue;
                        }

                        var sourceText = row[refValueIndex] as string;

                        if (string.IsNullOrEmpty(sourceText))
                        {
                            continue;
                        }

                        items.Add(new TranslateItemInfo {
                            Row = row, SourceText = sourceText
                        });
                    }

                    // --
                    // Pack into blocks.

                    var blocks = new List <List <TranslateItemInfo> >();

                    for (var index = 0; index < items.Count; index++)
                    {
                        if (index % ti.MaxArraySize == 0)
                        {
                            blocks.Add(new List <TranslateItemInfo>());
                        }

                        blocks[blocks.Count - 1].Add(items[index]);
                    }

                    // --
                    // Translate each block in batch.

                    foreach (var block in blocks)
                    {
                        if (block != null)
                        {
                            if (bw.CancellationPending)
                            {
                                throw new OperationCanceledException();
                            }

                            if (delayMilliseconds > 0)
                            {
                                Thread.Sleep(delayMilliseconds);
                            }

                            var sourceTexts = block.Select(itemInfo => itemInfo.SourceText).ToList();

                            try
                            {
                                var sourceLanguageCode = ti.MapCultureToSourceLanguageCode(
                                    appID,
                                    CultureHelper.CreateCultureErrorTolerant(refLanguageCode));
                                var destinationLanguageCode = ti.MapCultureToDestinationLanguageCode(
                                    appID,
                                    CultureHelper.CreateCultureErrorTolerant(raw));

                                if (useExistingTranslations)
                                {
                                    // Alle die nehmen, die schon übersetzt sind. Also quasi "aus Cache".
                                    for (var index = 0; index < sourceTexts.Count; ++index)
                                    {
                                        var dstText = imss?.GetTranslation(refLanguageCode, raw, sourceTexts[index]);
                                        if (dstText != null)
                                        {
                                            block[index].Row[column] = prefixSuccess + dstText;

                                            translationSuccessCount++;
                                            internallyExistingTranslationTranslatedSuccessCount++;

                                            sourceTexts[index] = string.Empty; // Markieren als "erledigt".
                                        }
                                    }
                                }

                                if (!useExistingTranslationsOnly && sourceTexts.Any(s => !string.IsNullOrEmpty(s)))
                                {
                                    var destinationTexts =
                                        ti.TranslateArray(
                                            appID,
                                            sourceTexts.ToArray(),
                                            sourceLanguageCode,
                                            destinationLanguageCode,
                                            _project.TranslationWordsToProtect,
                                            _project.TranslationWordsToRemove);

                                    for (var index = 0; index < block.Count; ++index)
                                    {
                                        if (destinationTexts[index] != null)
                                        {
                                            block[index].Row[column] = prefixSuccess + destinationTexts[index];

                                            // Merken für ggf. nächsten Durchlauf.
                                            if (useExistingTranslations)
                                            {
                                                imss?.AddTranslation(
                                                    refLanguageCode,
                                                    raw,
                                                    sourceTexts[index],
                                                    destinationTexts[index]);
                                            }

                                            translationSuccessCount++;
                                        }
                                    }
                                }
                            }
                            catch (Exception x)
                            {
                                translationErrorCount++;

                                if (continueOnErrors)
                                {
                                    var destinationText = prefixError + x.Message;

                                    foreach (var t in block)
                                    {
                                        if (t?.Row?[column] != null)
                                        {
                                            t.Row[column] = destinationText;
                                        }
                                    }
                                }
                                else
                                {
                                    throw;
                                }
                            }

                            translationCount++;
                        }
                    }
                }
            }

            return(translationSuccessCount);
        }
Beispiel #18
0
        private int translateSingle(
            string appID,
            string appID2,
            ITranslationEngine ti,
            DataTable table,
            string refLanguageCode,
            DataColumn column,
            int refValueIndex,
            string raw,
            BackgroundWorker bw,
            int delayMilliseconds,
            string prefixSuccess,
            int translationSuccessCount,
            ref int translationErrorCount,
            bool continueOnErrors,
            string prefixError,
            ref int translationCount)
        {
            if (column != null)
            {
                foreach (DataRow row in table.Rows)
                {
                    if (row != null)
                    {
                        if (bw.CancellationPending)
                        {
                            throw new OperationCanceledException();
                        }

                        var text = row[column] as string;
                        if (!string.IsNullOrEmpty(text) && text.Trim().Length > 0)
                        {
                            continue;
                        }

                        // http://www.codeproject.com/KB/aspnet/ZetaResourceEditor.aspx?msg=3367544#xx3367544xx
                        if (FileGroup.IsInternalRow(row))
                        {
                            continue;
                        }

                        var sourceText =
                            row[refValueIndex] as string;

                        if (string.IsNullOrEmpty(sourceText))
                        {
                            continue;
                        }

                        if (delayMilliseconds > 0)
                        {
                            Thread.Sleep(delayMilliseconds);
                        }

                        try
                        {
                            var destinationText =
                                prefixSuccess +
                                ti.Translate(
                                    appID, appID2,
                                    sourceText,
                                    ti.MapCultureToSourceLanguageCode(
                                        appID, appID2,
                                        CultureHelper.CreateCultureErrorTolerant(refLanguageCode)),
                                    ti.MapCultureToDestinationLanguageCode(
                                        appID, appID2,
                                        CultureHelper.CreateCultureErrorTolerant(raw)),
                                    _project.TranslationWordsToProtect,
                                    _project.TranslationWordsToRemove);

                            row[column] = destinationText;

                            translationSuccessCount++;
                        }
                        catch (Exception x)
                        {
                            translationErrorCount++;

                            if (continueOnErrors)
                            {
                                var destinationText = prefixError + x.Message;

                                if (row[column] != null)
                                {
                                    row[column] = destinationText;
                                }
                            }
                            else
                            {
                                throw;
                            }
                        }

                        translationCount++;
                    }
                }
            }
            return(translationSuccessCount);
        }
		private int translateSingle(
			string appID,
			string appID2,
			ITranslationEngine ti,
			DataTable table,
			string refLanguageCode,
			DataColumn column,
			int refValueIndex,
			string raw,
			BackgroundWorker bw,
			int delayMilliseconds,
			string prefixSuccess,
			int translationSuccessCount,
			ref int translationErrorCount,
			bool continueOnErrors,
			string prefixError,
			ref int translationCount)
		{
			if (column != null)
			{
				foreach (DataRow row in table.Rows)
				{
					if (row != null)
					{
						if (bw.CancellationPending)
						{
							throw new OperationCanceledException();
						}

						var text = row[column] as string;
						if (!string.IsNullOrEmpty(text) && text.Trim().Length > 0)
						{
							continue;
						}

						// http://www.codeproject.com/KB/aspnet/ZetaResourceEditor.aspx?msg=3367544#xx3367544xx
						if (FileGroup.IsInternalRow(row))
						{
							continue;
						}

						var sourceText =
							row[refValueIndex] as string;

						if (string.IsNullOrEmpty(sourceText))
						{
							continue;
						}

						if (delayMilliseconds > 0)
						{
							Thread.Sleep(delayMilliseconds);
						}

						try
						{
							var destinationText =
								prefixSuccess +
								ti.Translate(
									appID, appID2,
									sourceText,
									ti.MapCultureToSourceLanguageCode(
										appID, appID2,
										CultureHelper.CreateCultureErrorTolerant(refLanguageCode)),
									ti.MapCultureToDestinationLanguageCode(
										appID, appID2,
										CultureHelper.CreateCultureErrorTolerant(raw)),
									_project.TranslationWordsToProtect,
									_project.TranslationWordsToRemove);

							row[column] = destinationText;

							translationSuccessCount++;
						}
						catch (Exception x)
						{
							translationErrorCount++;

							if (continueOnErrors)
							{
								var destinationText = prefixError + x.Message;

								if (row[column] != null)
								{
									row[column] = destinationText;
								}
							}
							else
							{
								throw;
							}
						}

						translationCount++;
					}
				}
			}
			return translationSuccessCount;
		}
Beispiel #20
0
        private int translateArray(
            string appID,
            string appID2,
            ITranslationEngine ti,
            DataTable table,
            string refLanguageCode,
            DataColumn column,
            int refValueIndex,
            string raw,
            BackgroundWorker bw,
            int delayMilliseconds,
            string prefixSuccess,
            int translationSuccessCount,
            ref int translationErrorCount,
            bool continueOnErrors,
            string prefixError,
            ref int translationCount)
        {
            var items = new List <TranslateItemInfo>();

            // --
            // From GUI to in-memory-list.

            if (column != null)
            {
                foreach (DataRow row in table.Rows)
                {
                    items.Clear();
                    if (row != null)
                    {
                        if (bw.CancellationPending)
                        {
                            throw new OperationCanceledException();
                        }

                        var text = row[column] as string;
                        if (!string.IsNullOrEmpty(text) && text.Trim().Length > 0)
                        {
                            continue;
                        }

                        // http://www.codeproject.com/KB/aspnet/ZetaResourceEditor.aspx?msg=3367544#xx3367544xx
                        if (FileGroup.IsInternalRow(row))
                        {
                            continue;
                        }

                        var sourceText =
                            row[refValueIndex] as string;

                        if (string.IsNullOrEmpty(sourceText))
                        {
                            continue;
                        }

                        items.Add(new TranslateItemInfo {
                            Row = row, SourceText = sourceText
                        });
                    }

                    // --
                    // Pack into blocks.

                    var blocks = new List <List <TranslateItemInfo> >();

                    for (var index = 0; index < items.Count; index++)
                    {
                        if (index % ti.MaxArraySize == 0)
                        {
                            blocks.Add(new List <TranslateItemInfo>());
                        }

                        blocks[blocks.Count - 1].Add(items[index]);
                    }

                    // --
                    // Translate each block in batch.

                    foreach (var block in blocks)
                    {
                        if (block != null)
                        {
                            if (bw.CancellationPending)
                            {
                                throw new OperationCanceledException();
                            }
                            if (delayMilliseconds > 0)
                            {
                                Thread.Sleep(delayMilliseconds);
                            }

                            var sourceTexts = new List <string>();
                            foreach (var itemInfo in block)
                            {
                                sourceTexts.Add(itemInfo.SourceText);
                            }

                            try
                            {
                                var destinationTexts =
                                    ti.TranslateArray(
                                        appID,
                                        appID2,
                                        sourceTexts.ToArray(),
                                        ti.MapCultureToSourceLanguageCode(
                                            appID, appID2,
                                            CultureHelper.CreateCultureErrorTolerant(refLanguageCode)),
                                        ti.MapCultureToDestinationLanguageCode(
                                            appID, appID2,
                                            CultureHelper.CreateCultureErrorTolerant(raw)),
                                        _project.TranslationWordsToProtect,
                                        _project.TranslationWordsToRemove);

                                for (var index = 0; index < block.Count; ++index)
                                {
                                    block[index].Row[column] = prefixSuccess + destinationTexts[index];

                                    translationSuccessCount++;
                                }
                            }
                            catch (Exception x)
                            {
                                translationErrorCount++;

                                if (continueOnErrors)
                                {
                                    var destinationText =
                                        prefixError +
                                        x.Message;

                                    foreach (var t in block)
                                    {
                                        if (t != null && t.Row != null && t.Row[column] != null)
                                        {
                                            t.Row[column] = destinationText;
                                        }
                                    }
                                }
                                else
                                {
                                    throw;
                                }
                            }

                            translationCount++;
                        }
                    }
                }
            }

            return(translationSuccessCount);
        }
Beispiel #21
0
 public EngineHelper(
     ITranslationEngine engine)
 {
     Engine = engine;
 }
Beispiel #22
0
        private bool LoadProject(string fileName)
        {
            XElement projectElem;

            try
            {
                projectElem = XElement.Load(fileName);
            }
            catch (Exception)
            {
                return(false);
            }

            XElement engineElem = projectElem.Element("TranslationEngine");

            if (engineElem == null)
            {
                return(false);
            }

            var smtConfig = (string)engineElem.Element("SmtConfig");

            if (smtConfig == null)
            {
                return(false);
            }

            var hcSrcConfig = (string)engineElem.Element("SourceAnalyzerConfig");
            var hcTrgConfig = (string)engineElem.Element("TargetGeneratorConfig");

            string configDir = Path.GetDirectoryName(fileName);

            Debug.Assert(configDir != null);

            ITranslationEngine transferEngine = null;

            if (hcSrcConfig != null && hcTrgConfig != null)
            {
                Language srcLang    = XmlLanguageLoader.Load(Path.Combine(configDir, hcSrcConfig));
                var      srcMorpher = new Morpher(_hcTraceManager, srcLang);

                Language trgLang    = XmlLanguageLoader.Load(Path.Combine(configDir, hcTrgConfig));
                var      trgMorpher = new Morpher(_hcTraceManager, trgLang);

                transferEngine = new TransferEngine(srcMorpher,
                                                    new SimpleTransferer(new GlossMorphemeMapper(trgMorpher)), trgMorpher);
            }

            _smtModel = new ThotSmtModel(Path.Combine(configDir, smtConfig));
            IInteractiveSmtEngine smtEngine = _smtModel.CreateInteractiveEngine();

            _hybridEngine = new HybridTranslationEngine(smtEngine, transferEngine);

            var sourceTexts          = new List <IText>();
            var targetTexts          = new List <IText>();
            var alignmentCollections = new List <ITextAlignmentCollection>();

            using (_texts.BulkUpdate())
            {
                foreach (XElement textElem in projectElem.Elements("Texts").Elements("Text"))
                {
                    var name = (string)textElem.Attribute("name");

                    var metadataFileName = (string)textElem.Element("MetadataFile");
                    if (metadataFileName == null)
                    {
                        return(false);
                    }
                    metadataFileName = Path.Combine(configDir, metadataFileName);

                    var srcTextFileName = (string)textElem.Element("SourceFile");
                    if (srcTextFileName == null)
                    {
                        return(false);
                    }
                    srcTextFileName = Path.Combine(configDir, srcTextFileName);

                    var trgTextFileName = (string)textElem.Element("TargetFile");
                    if (trgTextFileName == null)
                    {
                        return(false);
                    }
                    trgTextFileName = Path.Combine(configDir, trgTextFileName);

                    var alignmentsFileName = (string)textElem.Element("AlignmentsFile");
                    if (alignmentsFileName != null)
                    {
                        alignmentsFileName = Path.Combine(configDir, alignmentsFileName);
                    }

                    var text = new TextViewModel(_tokenizer, name, metadataFileName, srcTextFileName, trgTextFileName,
                                                 alignmentsFileName)
                    {
                        Engine = _hybridEngine
                    };
                    text.PropertyChanged += TextPropertyChanged;
                    _texts.Add(text);

                    Func <TextSegment, bool> segmentFilter = s => text.IsApproved((TextSegmentRef)s.SegmentRef);
                    sourceTexts.Add(new FilteredText(new TextFileText(_tokenizer, name, srcTextFileName),
                                                     segmentFilter));
                    targetTexts.Add(new FilteredText(new TextFileText(_tokenizer, name, trgTextFileName),
                                                     segmentFilter));
                    if (alignmentsFileName != null)
                    {
                        alignmentCollections.Add(new TextFileTextAlignmentCollection(name, alignmentsFileName));
                    }
                }
            }
            if (_texts.Count == 0)
            {
                return(false);
            }

            _sourceCorpus    = new DictionaryTextCorpus(sourceTexts);
            _targetCorpus    = new DictionaryTextCorpus(targetTexts);
            _alignmentCorpus = new DictionaryTextAlignmentCorpus(alignmentCollections);

            CurrentText = _texts[0];
            AcceptChanges();
            RebuildTask.UpdateCanExecute();
            return(true);
        }