Inheritance: ObservableObject
Esempio n. 1
0
 public EditMeaningViewModel(IKeyedCollection<string, Meaning> meanings, Meaning meaning)
 {
     _title = "Edit Meaning";
     _meanings = meanings;
     _meaning = meaning;
     _gloss = meaning.Gloss;
     _category = meaning.Category;
 }
Esempio n. 2
0
 private void AddNewMeaning()
 {
     var vm = new EditMeaningViewModel(_projectService.Project.Meanings);
     if (_dialogService.ShowModalDialog(this, vm) == true)
     {
         var newMeaning = new Meaning(vm.Gloss, vm.Category);
         _projectService.Project.Meanings.Add(newMeaning);
         Messenger.Default.Send(new DomainModelChangedMessage(true));
         SelectedMeaning = _meanings.Single(s => s.DomainMeaning == newMeaning);
     }
 }
Esempio n. 3
0
 public void SetUp()
 {
     _busyService = Substitute.For<IBusyService>();
     _analysisService = Substitute.For<IAnalysisService>();
     _words = new ObservableList<WordViewModel>
     {
         new WordViewModel(_busyService, _analysisService, new Word("valid", _meaning)) {IsValid = true},
         new WordViewModel(_busyService, _analysisService, new Word("invalid", _meaning)) {IsValid = false}
     };
     _wordsViewModel = new WordsViewModel(_busyService, new ReadOnlyBindableList<WordViewModel>(_words));
     _meaning = new Meaning("gloss", "category");
 }
        public WordListsVarietyMeaningViewModel(IBusyService busyService, IAnalysisService analysisService, WordViewModel.Factory wordFactory, WordListsVarietyViewModel variety, Meaning meaning)
            : base(meaning)
        {
            _busyService = busyService;
            _analysisService = analysisService;
            _variety = variety;

            _domainWords = new ObservableList<Word>(variety.DomainVariety.Words[meaning]);
            _words = new MirroredBindableList<Word, WordViewModel>(_domainWords, word => wordFactory(word), vm => vm.DomainWord);
            _domainWords.CollectionChanged += DomainWordsChanged;
            _strRep = string.Join(",", _domainWords.Select(word => word.StrRep));
            _showInVarietiesCommand = new RelayCommand(ShowInVarieties, () => _domainWords.Count > 0);
        }
Esempio n. 5
0
        public static IEnumerable<Cluster<Word>> GenerateCognateSets(this CogProject project, Meaning meaning)
        {
            var words = new HashSet<Word>();
            var noise = new HashSet<Word>();
            foreach (VarietyPair vp in project.VarietyPairs)
            {
                WordPair wp;
                if (vp.WordPairs.TryGetValue(meaning, out wp))
                {
                    if (wp.Cognacy)
                    {
                        words.Add(wp.Word1);
                        words.Add(wp.Word2);
                        noise.Remove(wp.Word1);
                        noise.Remove(wp.Word2);
                    }
                    else
                    {
                        if (!words.Contains(wp.Word1))
                            noise.Add(wp.Word1);
                        if (!words.Contains(wp.Word2))
                            noise.Add(wp.Word2);
                    }
                }
            }

            double min = double.MaxValue;
            var distanceMatrix = new Dictionary<UnorderedTuple<Word, Word>, double>();
            Word[] wordArray = words.ToArray();
            for (int i = 0; i < wordArray.Length; i++)
            {
                for (int j = i + 1; j < wordArray.Length; j++)
                {
                    Word w1 = wordArray[i];
                    Word w2 = wordArray[j];
                    double score = 0;
                    WordPair wp;
                    if (w1.Variety != w2.Variety && w1.Variety.VarietyPairs[w2.Variety].WordPairs.TryGetValue(meaning, out wp) && wp.Cognacy
                        && wp.GetWord(w1.Variety) == w1 && wp.GetWord(w2.Variety) == w2)
                    {
                        score = wp.PredictedCognacyScore;
                    }
                    double distance = 1.0 - score;
                    min = Math.Min(min, distance);
                    distanceMatrix[UnorderedTuple.Create(w1, w2)] = distance;
                }
            }

            var clusterer = new FlatUpgmaClusterer<Word>((w1, w2) => distanceMatrix[UnorderedTuple.Create(w1, w2)], (1.0 + min) / 2);
            return clusterer.GenerateClusters(words).Concat(new Cluster<Word>(noise, true));
        }
Esempio n. 6
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);
        }
Esempio n. 7
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);
        }
        private CogProject CreateProject()
        {
            var project = new CogProject(_spanFactory);
            var variety1 = new Variety("variety1");
            project.Varieties.Add(variety1);
            var variety2 = new Variety("variety2");
            project.Varieties.Add(variety2);
            var variety3 = new Variety("variety3");
            project.Varieties.Add(variety3);
            var meaning1 = new Meaning("meaning1", null);
            project.Meanings.Add(meaning1);
            var meaning2 = new Meaning("meaning2", null);
            project.Meanings.Add(meaning2);
            var meaning3 = new Meaning("meaning3", null);
            project.Meanings.Add(meaning3);

            variety1.Words.Add(new Word("word1", meaning1));
            variety1.Words.Add(new Word("word2", meaning2));
            variety1.Words.Add(new Word("word3", meaning3));

            variety2.Words.Add(new Word("word4", meaning1));
            variety2.Words.Add(new Word("word5", meaning2));
            variety2.Words.Add(new Word("word6", meaning3));

            variety3.Words.Add(new Word("word7", meaning1));
            variety3.Words.Add(new Word("word8", meaning2));
            variety3.Words.Add(new Word("word9", meaning3));

            var vpGenerator = new VarietyPairGenerator();
            vpGenerator.Process(project);

            double score = 1.0;
            foreach (VarietyPair vp in project.VarietyPairs)
            {
                foreach (Meaning meaning in project.Meanings)
                {
                    Word w1 = vp.Variety1.Words[meaning].First();
                    Word w2 = vp.Variety2.Words[meaning].First();
                    WordPair wp = vp.WordPairs.Add(w1, w2);
                    wp.PredictedCognacyScore = score;
                    wp.PredictedCognacy = true;
                    score -= 0.1;
                }
            }
            return project;
        }
Esempio n. 9
0
 public void RemoveAll(Meaning meaning)
 {
     _words.Remove(meaning);
 }
Esempio n. 10
0
 public MeaningViewModel(Meaning meaning)
     : base(meaning)
 {
     _meaning = meaning;
 }
Esempio n. 11
0
 protected Word ParseWordOnce(string wordText, Meaning meaning, CogProject project)
 {
     Word word;
     // We expect to see a lot of duplicates in our input text; save time by memoizing
     if (_parsedWords.TryGetValue(wordText, out word))
         return word;
     try
     {
         word = ParseWord(wordText, meaning);
     }
     catch (FormatException e)
     {
         Errors.Add(e.Message);
         return null;
     }
     project.Segmenter.Segment(word);
     _parsedWords.Add(wordText, word);
     return word;
 }
Esempio n. 12
0
 protected Word ParseWord(string wordText, Meaning meaning)
 {
     int stemStartIdx = wordText.IndexOf("|", StringComparison.Ordinal);
     int stemEndIdx = wordText.LastIndexOf("|", StringComparison.Ordinal) - 1; // -1 because we're going to remove the leading |
     if (stemStartIdx != -1 && stemEndIdx < stemStartIdx)
     {
         // Only way this can happen is if there was only a single "|" in the word
         throw new FormatException(String.Format("Words should have either 0 or 2 pipe characters representing word stems. Offending word: {0}", wordText));
     }
     var word = (stemStartIdx == -1) ?
         new Word(wordText, meaning) :
         new Word(wordText.Replace("|", ""), stemStartIdx, stemEndIdx - stemStartIdx, meaning);
     return word;
 }
Esempio n. 13
0
 // Should this be protected? But the unit test needs to call it.
 public void SetUpProject()
 {
     if (ConfigData != null && ConfigFilename != null)
     {
         Warnings.Add("WARNING: options --config-data and --config-file were both specified. Ignoring --config-file.");
         ConfigFilename = null;
     }
     if (ConfigData == null && ConfigFilename == null)
     {
         ConfigFilename = FindConfigFilename();
         // If ConfigFilename is STILL null at this point, it's because no config files were found at all, so we'll use the default one from the resource.
     }
     SpanFactory = new ShapeSpanFactory();
     SegmentPool = new SegmentPool();
     Variety1 = new Variety("variety1");
     Variety2 = new Variety("variety2");
     Meaning = new Meaning("gloss1", "cat1");
     if (ConfigData == null && ConfigFilename == null)
         Project = GetProjectFromResource(SpanFactory, SegmentPool);
     else if (ConfigData != null)
         Project = GetProjectFromXmlString(SpanFactory, SegmentPool, ConfigData);
     else if (ConfigFilename != null)
         Project = GetProjectFromFilename(SpanFactory, SegmentPool, ConfigFilename);
     else // Should never get here given checks above, but let's be safe and write the check anyway
         Project = GetProjectFromResource(SpanFactory, SegmentPool);
     Project.Meanings.Add(Meaning);
     Project.Varieties.Add(Variety1);
     Project.Varieties.Add(Variety2);
     Project.VarietyPairs.Add(new VarietyPair(Variety1, Variety2));
 }
Esempio n. 14
0
 public Word(string strRep, Meaning meaning)
     : this(strRep, 0, strRep.Length, meaning)
 {
 }
Esempio n. 15
0
        public void Import(object importSettingsViewModel, Stream stream, CogProject project)
        {
            XDocument doc = XDocument.Load(stream, LoadOptions.SetLineInfo);
            XElement root = doc.Element("survey");
            if (root == null)
                throw new ImportException("No survey element.");

            var varietyNames = new HashSet<string>();
            var varieties = new Dictionary<string, Tuple<Variety, List<Word>>>();
            foreach (XElement wordListElem in root.Elements("word_lists").Elements("word_list"))
            {
                var id = (string) wordListElem.Attribute("id");
                if (id == null)
                    throw new ImportException(string.Format("A \"word_list\" element is missing an \"id\" attribute. Line: {0}", ((IXmlLineInfo) wordListElem).LineNumber));
                if (varieties.ContainsKey(id))
                    throw new ImportException(string.Format("The ID of a \"word_list\" element is not unique. Line: {0}", ((IXmlLineInfo) wordListElem).LineNumber));
                XElement nameElem = wordListElem.Element("name");
                if (nameElem == null)
                    throw new ImportException(string.Format("A \"word_list\" element is missing a \"name\" element. Line: {0}", ((IXmlLineInfo) wordListElem).LineNumber));
                var name = ((string) nameElem).Trim();
                if (string.IsNullOrEmpty(name))
                    throw new ImportException(string.Format("A blank variety name is not allowed. Line: {0}", ((IXmlLineInfo) nameElem).LineNumber));
                if (varietyNames.Contains(name))
                    throw new ImportException(string.Format("The variety name, \"{0}\", is not unique. Line: {1}", name, ((IXmlLineInfo) wordListElem).LineNumber));
                varietyNames.Add(name);
                var variety = new Variety(name);

                varieties[id] = Tuple.Create(variety, new List<Word>());
            }

            var meanings = new Dictionary<string, Meaning>();
            foreach (XElement glossElem in root.Elements("glosses").Elements("gloss"))
            {
                XElement nameElem = glossElem.Element("name");
                if (nameElem == null)
                    throw new ImportException(string.Format("A \"gloss\" element is missing a \"name\" element. Line: {0}", ((IXmlLineInfo) glossElem).LineNumber));
                var gloss = ((string) nameElem).Trim();
                if (string.IsNullOrEmpty(gloss))
                    throw new ImportException(string.Format("A blank gloss is not allowed. Line: {0}", ((IXmlLineInfo) nameElem).LineNumber));
                var pos = (string) glossElem.Element("part_of_speech");
                if (meanings.ContainsKey(gloss))
                    throw new ImportException(string.Format("The gloss, \"{0}\", is not unique. Line: {1}", gloss, ((IXmlLineInfo) nameElem).LineNumber));
                var meaning = new Meaning(gloss, pos);
                meanings[gloss] = meaning;
                foreach (XElement transElem in glossElem.Elements("transcriptions").Elements("transcription"))
                {
                    XElement wordListIdElem = transElem.Element("word_list_id");
                    if (wordListIdElem == null)
                        throw new ImportException(string.Format("A \"transcription\" element is missing a \"word_list_id\" element. Line: {0}", ((IXmlLineInfo) transElem).LineNumber));
                    var varietyID = (string) wordListIdElem;
                    var wordform = (string) transElem.Element("name");
                    if (wordform != null)
                    {
                        wordform = wordform.Trim();
                        if (!string.IsNullOrEmpty(wordform))
                        {
                            Tuple<Variety, List<Word>> variety;
                            if (varieties.TryGetValue(varietyID, out variety))
                            {
                                foreach (string w in wordform.Split(','))
                                {
                                    string str = w.Trim();
                                    variety.Item2.Add(new Word(str, meaning));
                                }
                            }
                        }
                    }
                }
            }

            project.Meanings.ReplaceAll(meanings.Values);
            using (project.Varieties.BulkUpdate())
            {
                project.Varieties.Clear();
                foreach (Tuple<Variety, List<Word>> variety in varieties.Values)
                {
                    variety.Item1.Words.AddRange(variety.Item2);
                    project.Varieties.Add(variety.Item1);
                }
            }
        }
Esempio n. 16
0
        public static IEnumerable <Cluster <Word> > GenerateCognateSets(this CogProject project, Meaning meaning)
        {
            var words = new HashSet <Word>();
            var noise = new HashSet <Word>();

            foreach (VarietyPair vp in project.VarietyPairs)
            {
                WordPair wp;
                if (vp.WordPairs.TryGet(meaning, out wp))
                {
                    if (wp.Cognacy)
                    {
                        words.Add(wp.Word1);
                        words.Add(wp.Word2);
                        noise.Remove(wp.Word1);
                        noise.Remove(wp.Word2);
                    }
                    else
                    {
                        if (!words.Contains(wp.Word1))
                        {
                            noise.Add(wp.Word1);
                        }
                        if (!words.Contains(wp.Word2))
                        {
                            noise.Add(wp.Word2);
                        }
                    }
                }
            }

            double min            = double.MaxValue;
            var    distanceMatrix = new Dictionary <UnorderedTuple <Word, Word>, double>();

            Word[] wordArray = words.ToArray();
            for (int i = 0; i < wordArray.Length; i++)
            {
                for (int j = i + 1; j < wordArray.Length; j++)
                {
                    Word     w1    = wordArray[i];
                    Word     w2    = wordArray[j];
                    double   score = 0;
                    WordPair wp;
                    if (w1.Variety != w2.Variety && w1.Variety.VarietyPairs[w2.Variety].WordPairs.TryGet(meaning, out wp) && wp.Cognacy &&
                        wp.GetWord(w1.Variety) == w1 && wp.GetWord(w2.Variety) == w2)
                    {
                        score = wp.PredictedCognacyScore;
                    }
                    double distance = 1.0 - score;
                    min = Math.Min(min, distance);
                    distanceMatrix[UnorderedTuple.Create(w1, w2)] = distance;
                }
            }

            var clusterer = new FlatUpgmaClusterer <Word>((w1, w2) => distanceMatrix[UnorderedTuple.Create(w1, w2)], (1.0 + min) / 2);

            return(clusterer.GenerateClusters(words).Concat(new Cluster <Word>(noise, true)));
        }