Inheritance: ObservableObject
Exemple #1
0
 public VarietyPairSurrogate(VarietyPair vp)
 {
     Variety1 = vp.Variety1.Name;
     Variety2 = vp.Variety2.Name;
     var wordPairSurrogates = new Dictionary<WordPair, WordPairSurrogate>();
     _wordPairs = vp.WordPairs.Select(wp => wordPairSurrogates.GetValue(wp, () => new WordPairSurrogate(wp))).ToList();
     PhoneticSimilarityScore = vp.PhoneticSimilarityScore;
     LexicalSimilarityScore = vp.LexicalSimilarityScore;
     DefaultSoundCorrespondenceProbability = vp.DefaultSoundCorrespondenceProbability;
     _cognateSoundCorrespondenceFrequencyDistribution = new Dictionary<SoundContextSurrogate, Tuple<string[], int>[]>();
     foreach (SoundContext lhs in vp.CognateSoundCorrespondenceFrequencyDistribution.Conditions)
     {
         FrequencyDistribution<Ngram<Segment>> freqDist = vp.CognateSoundCorrespondenceFrequencyDistribution[lhs];
         _cognateSoundCorrespondenceFrequencyDistribution[new SoundContextSurrogate(lhs)] = freqDist.ObservedSamples.Select(ngram => Tuple.Create(ngram.Select(seg => seg.StrRep).ToArray(), freqDist[ngram])).ToArray();
     }
     _cognateSoundCorrespondenceByPosition = new Dictionary<string, List<SoundCorrespondenceSurrogate>>();
     foreach (KeyValuePair<FeatureSymbol, SoundCorrespondenceCollection> kvp in vp.CognateSoundCorrespondencesByPosition)
     {
         string pos;
         if (kvp.Key == CogFeatureSystem.Onset)
             pos = "onset";
         else if (kvp.Key == CogFeatureSystem.Nucleus)
             pos = "nucleus";
         else
             pos = "coda";
         _cognateSoundCorrespondenceByPosition[pos] = kvp.Value.Select(corr => new SoundCorrespondenceSurrogate(wordPairSurrogates, corr)).ToList();
     }
 }
Exemple #2
0
 public bool ExportVarietyPair(object ownerViewModel, VarietyPair varietyPair)
 {
     FileDialogResult result = _dialogService.ShowSaveFileDialog(ownerViewModel, "Export Variety Pair", VarietyPairExporters.Keys);
     if (result.IsValid)
         return Export(ownerViewModel, result.FileName, stream => VarietyPairExporters[result.SelectedFileType].Export(stream,
             _projectService.Project.WordAligners[ComponentIdentifiers.PrimaryWordAligner], varietyPair));
     return false;
 }
 public SimilarityMatrixVarietyPairViewModel(SimilarityMetric similarityMetric, Variety thisVariety, VarietyPair varietyPair)
 {
     _varietyPair = varietyPair;
     _thisVariety = thisVariety;
     _otherVariety = _varietyPair.GetOtherVariety(_thisVariety);
     _switchToVarietyPairCommand = new RelayCommand(SwitchToVarietyPair);
     _similarityMetric = similarityMetric;
 }
Exemple #4
0
 public void Compare(VarietyPair varietyPair)
 {
     _busyService.ShowBusyIndicatorUntilFinishDrawing();
     Messenger.Default.Send(new PerformingComparisonMessage(varietyPair));
     var pipeline = new Pipeline<VarietyPair>(GetCompareProcessors());
     pipeline.Process(varietyPair.ToEnumerable());
     Messenger.Default.Send(new ComparisonPerformedMessage(varietyPair));
 }
Exemple #5
0
        public VarietyPairViewModel(SegmentPool segmentPool, IProjectService projectService, WordPairsViewModel.Factory wordPairsFactory,
			WordPairViewModel.Factory wordPairFactory, VarietyPair varietyPair, bool areVarietiesInOrder)
            : base(varietyPair)
        {
            _segmentPool = segmentPool;
            _projectService = projectService;
            _varietyPair = varietyPair;
            _areVarietiesInOrder = areVarietiesInOrder;
            _wordPairsFactory = wordPairsFactory;
            _wordPairFactory = wordPairFactory;

            Messenger.Default.Register<ComparisonPerformedMessage>(this, HandleComparisonPerformed);

            UpdateVarietyPair();
        }
Exemple #6
0
        public bool?GetCognacy(VarietyPair varietyPair, Meaning meaning)
        {
            Dictionary <Meaning, CognacyDecision> decisions;

            if (_lookupDictionary.TryGetValue(UnorderedTuple.Create(varietyPair.Variety1, varietyPair.Variety2),
                                              out decisions))
            {
                CognacyDecision decision;
                if (decisions.TryGetValue(meaning, out decision))
                {
                    return(decision.Cognacy);
                }
            }

            return(null);
        }
Exemple #7
0
        public bool?GetCognacy(VarietyPair varietyPair, Meaning meaning)
        {
            Dictionary <Meaning, int> decisions;

            if (_lookupDictionary.TryGetValue(UnorderedTuple.Create(varietyPair.Variety1, varietyPair.Variety2),
                                              out decisions))
            {
                int index;
                if (decisions.TryGetValue(meaning, out index))
                {
                    return(Items[index].Cognacy);
                }
            }

            return(null);
        }
Exemple #8
0
        public VarietyPairViewModel(SegmentPool segmentPool, IProjectService projectService, WordPairsViewModel.Factory wordPairsFactory, VarietyPair varietyPair, bool areVarietiesInOrder)
        {
            _segmentPool = segmentPool;
            _projectService = projectService;
            _varietyPair = varietyPair;
            _areVarietiesInOrder = areVarietiesInOrder;

            IWordAligner aligner = projectService.Project.WordAligners[ComponentIdentifiers.PrimaryWordAligner];
            _cognates = wordPairsFactory();
            foreach (WordPair wp in _varietyPair.WordPairs.Where(wp => wp.AreCognatePredicted))
                _cognates.WordPairs.Add(new WordPairViewModel(aligner, wp, _areVarietiesInOrder));
            _noncognates = wordPairsFactory();
            foreach (WordPair wp in _varietyPair.WordPairs.Where(wp => !wp.AreCognatePredicted))
                _noncognates.WordPairs.Add(new WordPairViewModel(aligner, wp, _areVarietiesInOrder));

            _soundChanges = new ReadOnlyList<SoundChangeViewModel>(_varietyPair.SoundChangeProbabilityDistribution.Conditions.SelectMany(lhs => _varietyPair.SoundChangeProbabilityDistribution[lhs].Samples,
                (lhs, segment) => new SoundChangeViewModel(lhs, segment, _varietyPair.SoundChangeProbabilityDistribution[lhs][segment], _varietyPair.SoundChangeFrequencyDistribution[lhs][segment])).ToList());
        }
        public void Export(Stream stream, IWordAligner aligner, VarietyPair varietyPair)
        {
            using (var writer = new StreamWriter(new NonClosingStreamWrapper(stream)))
            {
                writer.WriteLine("Similarity");
                writer.WriteLine("----------");
                writer.WriteLine("Lexical: {0:p}", varietyPair.LexicalSimilarityScore);
                writer.WriteLine("Phonetic: {0:p}", varietyPair.PhoneticSimilarityScore);
                writer.WriteLine();

                writer.WriteLine("Likely cognates");
                writer.WriteLine("--------------");
                WriteWordPairs(writer, aligner, varietyPair.WordPairs.Where(wp => wp.Cognacy));
                writer.WriteLine();

                writer.WriteLine("Likely non-cognates");
                writer.WriteLine("-------------------");
                WriteWordPairs(writer, aligner, varietyPair.WordPairs.Where(wp => !wp.Cognacy));
                writer.WriteLine();

                writer.WriteLine("Sound correspondences");
                writer.WriteLine("---------------------");
                bool first = true;
                foreach (SoundContext lhs in varietyPair.CognateSoundCorrespondenceProbabilityDistribution.Conditions)
                {
                    if (!first)
                        writer.WriteLine();
                    IProbabilityDistribution<Ngram<Segment>> probDist = varietyPair.CognateSoundCorrespondenceProbabilityDistribution[lhs];
                    FrequencyDistribution<Ngram<Segment>> freqDist = varietyPair.CognateSoundCorrespondenceFrequencyDistribution[lhs];
                    writer.WriteLine(lhs.ToString());
                    foreach (var correspondence in freqDist.ObservedSamples.Select(corr => new {Segment = corr, Probability = probDist[corr], Frequency = freqDist[corr]}).OrderByDescending(corr => corr.Probability))
                        writer.WriteLine("{0}: {1:p}, {2}", correspondence.Segment, correspondence.Probability, correspondence.Frequency);
                    first = false;
                }
            }
        }
Exemple #10
0
        public VarietyPair ToVarietyPair(SegmentPool segmentPool, CogProject project)
        {
            var vp = new VarietyPair(project.Varieties[Variety1], project.Varieties[Variety2])
                {
                    PhoneticSimilarityScore = PhoneticSimilarityScore,
                    LexicalSimilarityScore = LexicalSimilarityScore,
                    DefaultSoundCorrespondenceProbability = DefaultSoundCorrespondenceProbability
                };
            var wordPairs = new Dictionary<WordPairSurrogate, WordPair>();
            foreach (WordPairSurrogate wpSurrogate in _wordPairs)
            {
                WordPair wp = wpSurrogate.ToWordPair(project, vp);
                vp.WordPairs.Add(wp);
                project.CognacyDecisions.UpdateActualCognacy(wp);
                wordPairs[wpSurrogate] = wp;
            }
            var cognateCorrCounts = new ConditionalFrequencyDistribution<SoundContext, Ngram<Segment>>();
            foreach (KeyValuePair<SoundContextSurrogate, Tuple<string[], int>[]> fd in _cognateSoundCorrespondenceFrequencyDistribution)
            {
                SoundContext ctxt = fd.Key.ToSoundContext(project, segmentPool);
                FrequencyDistribution<Ngram<Segment>> freqDist = cognateCorrCounts[ctxt];
                foreach (Tuple<string[], int> sample in fd.Value)
                {
                    Ngram<Segment> corr = sample.Item1 == null ? new Ngram<Segment>() : new Ngram<Segment>(sample.Item1.Select(segmentPool.GetExisting));
                    freqDist.Increment(corr, sample.Item2);
                }
            }
            vp.CognateSoundCorrespondenceFrequencyDistribution = cognateCorrCounts;
            IWordAligner aligner = project.WordAligners[ComponentIdentifiers.PrimaryWordAligner];
            int segmentCount = vp.Variety2.SegmentFrequencyDistribution.ObservedSamples.Count;
            int possCorrCount = aligner.ExpansionCompressionEnabled ? (segmentCount * segmentCount) + segmentCount + 1 : segmentCount + 1;
            vp.CognateSoundCorrespondenceProbabilityDistribution = new ConditionalProbabilityDistribution<SoundContext, Ngram<Segment>>(cognateCorrCounts,
                (sc, freqDist) => new WittenBellProbabilityDistribution<Ngram<Segment>>(freqDist, possCorrCount));

            foreach (KeyValuePair<string, List<SoundCorrespondenceSurrogate>> kvp in _cognateSoundCorrespondenceByPosition)
            {
                if (kvp.Value != null)
                {
                    FeatureSymbol pos = null;
                    switch (kvp.Key)
                    {
                        case "onset":
                            pos = CogFeatureSystem.Onset;
                            break;
                        case "nucleus":
                            pos = CogFeatureSystem.Nucleus;
                            break;
                        case "coda":
                            pos = CogFeatureSystem.Coda;
                            break;
                    }
                    vp.CognateSoundCorrespondencesByPosition[pos].AddRange(kvp.Value.Select(surrogate => surrogate.ToSoundCorrespondence(segmentPool, wordPairs)));
                }
            }
            return vp;
        }
Exemple #11
0
 internal WordPairCollection(VarietyPair varietyPair)
 {
     _varietyPair = varietyPair;
 }
            public TestEnvironment(bool? actualCognacy, bool predictedCognacy)
            {
                DispatcherHelper.Initialize();

                var segmentPool = new SegmentPool();
                var spanFactory = new ShapeSpanFactory();
                var projectService = Substitute.For<IProjectService>();
                var analysisService = Substitute.For<IAnalysisService>();
                _project = TestHelpers.GetTestProject(spanFactory, segmentPool);
                _project.Varieties.AddRange(new[] {new Variety("variety1"), new Variety("variety2")});
                _project.Meanings.Add(new Meaning("meaning1", null));
                var word1 = new Word("wɜrd", _project.Meanings[0]);
                _project.Varieties[0].Words.Add(word1);
                _project.Segmenter.Segment(word1);
                var word2 = new Word("kɑr", _project.Meanings[0]);
                _project.Varieties[1].Words.Add(word2);
                _project.Segmenter.Segment(word2);

                var vp = new VarietyPair(_project.Varieties[0], _project.Varieties[1]);
                if (actualCognacy != null)
                    _project.CognacyDecisions.Add(new CognacyDecision(vp.Variety1, vp.Variety2, _project.Meanings[0], (bool) actualCognacy));

                _project.VarietyPairs.Add(vp);
                var wp = new WordPair(word1, word2) {PredictedCognacy = predictedCognacy, ActualCognacy = actualCognacy};
                _project.VarietyPairs[0].WordPairs.Add(wp);

                projectService.Project.Returns(_project);
                _wordPairViewModel = new WordPairViewModel(projectService, analysisService, wp, true);
            }
Exemple #13
0
 // Following code is from AnalysisService, tweaked just a little (e.g., not getting the project from ProjectService).
 // TODO: Refactor this, and/or AnalysisService, so that we don't have to have this code duplication.
 // (The code duplication is currently necessary because AnalysisService lives in Cog.Application, which references parts
 // of WPF like PresentationCore -- so we can't use Cog.Application with Mono on Linux. Moving AnalysisService to a
 // different assembly, or moving the WPF-dependent code to a different assembly, would be a good solution.) - 2015-09 RM
 public void Compare(VarietyPair varietyPair)
 {
     var pipeline = new Pipeline<VarietyPair>(GetCompareProcessors());
     pipeline.Process(varietyPair.ToEnumerable());
 }
 public PerformingComparisonMessage(VarietyPair varietyPair = null)
 {
     _varietyPair = varietyPair;
 }
Exemple #15
0
 public void Compare(VarietyPair varietyPair)
 {
     _busyService.ShowBusyIndicatorUntilFinishDrawing();
     var pipeline = new Pipeline<VarietyPair>(GetCompareProcessors());
     pipeline.Process(varietyPair.ToEnumerable());
 }
Exemple #16
0
        private void PerformComparison()
        {
            if (_varietyPairState == VarietyPairState.NotSelected || _selectedVarietyPair != null)
                return;

            _busyService.ShowBusyIndicatorUntilFinishDrawing();
            CogProject project = _projectService.Project;
            var pair = new VarietyPair(_selectedVariety1.DomainVariety, _selectedVariety2.DomainVariety);
            project.VarietyPairs.Add(pair);

            _analysisService.Compare(pair);

            SelectedVarietyPair = _varietyPairFactory(pair, true);
            VarietyPairState = VarietyPairState.SelectedAndCompared;
        }
Exemple #17
0
 public WordPair ToWordPair(CogProject project, VarietyPair vp)
 {
     Meaning meaning = project.Meanings[Meaning];
     Word word1 = vp.Variety1.Words[meaning].First(w => w.StrRep == Word1);
     Word word2 = vp.Variety2.Words[meaning].First(w => w.StrRep == Word2);
     var wp = new WordPair(word1, word2)
         {
             AreCognatePredicted = AreCognatePredicted,
             PhoneticSimilarityScore = PhoneticSimilarityScore,
             CognacyScore = CognacyScore
         };
     wp.AlignmentNotes.AddRange(_alignmentNotes);
     return wp;
 }
 public ComparisonPerformedMessage(VarietyPair varietyPair = null)
 {
     _varietyPair = varietyPair;
 }
Exemple #19
0
        private void PerformComparison()
        {
            CogProject project = _projectService.Project;
            VarietyPair pair;
            if (_selectedVarietyPair == null)
            {
                pair = new VarietyPair(_selectedVariety1.DomainVariety, _selectedVariety2.DomainVariety);
                project.VarietyPairs.Add(pair);
            }
            else
            {
                pair = _selectedVarietyPair.DomainVarietyPair;
            }

            _analysisService.Compare(pair);

            if (_selectedVarietyPair == null)
                SelectedVarietyPair = _varietyPairFactory(pair, true);
            VarietyPairState = VarietyPairState.SelectedAndCompared;
        }