Ejemplo n.º 1
0
        protected override void OnDataContextChanged(EventArgs e)
        {
            base.OnDataContextChanged(e);

            if (_prevDataContext != null)
            {
                _prevDataContext.Suggestions.CollectionChanged -= SuggestionsChanged;
                _prevDataContext.PropertyChanged -= ViewModelPropertyChanged;
                _prevDataContext.UnapprovedTargetSegmentRanges.CollectionChanged -= UnapprovedTargetSegmentRangesChanged;
                _prevDataContext.AlignedSourceWords.CollectionChanged            -= AlignedSourceWordsChanged;
            }

            var vm = DataContext as TextViewModel;

            if (vm != null)
            {
                vm.Suggestions.CollectionChanged += SuggestionsChanged;
                vm.PropertyChanged += ViewModelPropertyChanged;
                vm.UnapprovedTargetSegmentRanges.CollectionChanged += UnapprovedTargetSegmentRangesChanged;
                vm.AlignedSourceWords.CollectionChanged            += AlignedSourceWordsChanged;

                UpdateSuggestions(vm);
                UpdateTargetTextBackground(vm);
                UpdateSourceSegmentBackground(vm);
                UpdateTextForeground(SourceTextArea, vm.CurrentSourceSegmentRange);
                UpdateTextForeground(TargetTextArea, vm.CurrentTargetSegmentRange);
                TargetSegmentTextArea.Focus();
            }
            _prevDataContext = vm;
        }
Ejemplo n.º 2
0
 private void UpdateTargetTextBackground(TextViewModel vm)
 {
     TargetTextArea.Buffer.SetBackground(new Range <int>(0, TargetTextArea.Text.Length), Colors.White);
     foreach (Range <int> range in vm.UnapprovedTargetSegmentRanges)
     {
         TargetTextArea.Buffer.SetBackground(FixRichTextAreaInputRange(TargetTextArea, range), Colors.LightYellow);
     }
 }
Ejemplo n.º 3
0
 public MainFormViewModel()
 {
     _tokenizer           = new LatinWordTokenizer();
     _openProjectCommand  = new RelayCommand(OpenProject);
     _saveProjectCommand  = new RelayCommand(SaveProject, CanSaveProject);
     CloseCommand         = new RelayCommand(Close, CanClose);
     _hcTraceManager      = new TraceManager();
     _confidenceThreshold = 20;
     _texts       = new BulkObservableList <TextViewModel>();
     Texts        = new ReadOnlyObservableList <TextViewModel>(_texts);
     _currentText = new TextViewModel(_tokenizer);
     RebuildTask  = new TaskViewModel(RebuildAsync, () => _hybridEngine != null);
 }
Ejemplo n.º 4
0
 private void UpdateSuggestions(TextViewModel vm)
 {
     SuggestionsContainer.Items.Clear();
     foreach (SuggestionViewModel suggestion in vm.Suggestions)
     {
         var button = new LinkButton {
             DataContext = suggestion
         };
         button.TextBinding.BindDataContext((SuggestionViewModel svm) => svm.Text);
         button.BindDataContext(b => b.Command, (SuggestionViewModel svm) => svm.Command);
         SuggestionsContainer.Items.Add(new StackLayoutItem(button));
     }
 }
Ejemplo n.º 5
0
 private void CloseProject()
 {
     CurrentText = null;
     _texts.Clear();
     CurrentText   = new TextViewModel(_tokenizer);
     _sourceCorpus = null;
     _targetCorpus = null;
     if (_hybridEngine != null)
     {
         _hybridEngine.Dispose();
         _hybridEngine = null;
     }
     if (_smtModel != null)
     {
         _smtModel.Dispose();
         _smtModel = null;
     }
     _saveProjectCommand.UpdateCanExecute();
     RebuildTask.UpdateCanExecute();
 }
Ejemplo n.º 6
0
        private void UpdateSourceSegmentBackground(TextViewModel vm)
        {
            SourceSegmentTextArea.Buffer.SetBackground(new Range <int>(0, SourceSegmentTextArea.Text.Length), Colors.White);
            foreach (AlignedWordViewModel alignedWord in vm.AlignedSourceWords)
            {
                Color c = Colors.White;
                switch (alignedWord.Level)
                {
                case WordTranslationLevel.Transfer:
                    c = Colors.DarkGray;
                    break;

                case WordTranslationLevel.LowConfidence:
                    c = Colors.SkyBlue;
                    break;

                case WordTranslationLevel.HighConfidence:
                    c = Colors.Orange;
                    break;
                }
                SourceSegmentTextArea.Buffer.SetBackground(FixRichTextAreaInputRange(SourceSegmentTextArea, alignedWord.Range), c);
            }
        }
Ejemplo n.º 7
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);
        }
Ejemplo n.º 8
0
 public SuggestionViewModel(TextViewModel textViewModel, string text)
 {
     Text           = text;
     _textViewModel = textViewModel;
     _command       = new RelayCommand(InsertSuggestion);
 }