public SegmentMappingsTableViewModel(IProjectService projectService, SegmentMappingsTableSegmentPairViewModel.Factory segmentPairFactory, SegmentMappingViewModel.Factory mappingFactory, IEnumerable<SegmentMappingViewModel> mappings, SoundType soundType, int threshold) { _threshold = threshold; _soundType = soundType; FeatureSymbol segmentType; switch (_soundType) { case SoundType.Consonant: segmentType = CogFeatureSystem.ConsonantType; break; case SoundType.Vowel: segmentType = CogFeatureSystem.VowelType; break; default: throw new InvalidEnumArgumentException(); } var segmentComparer = new SegmentComparer(); var categoryComparer = new SegmentCategoryComparer(); _segments = new ReadOnlyList<SegmentMappingsTableSegmentViewModel>(projectService.Project.Varieties.SelectMany(v => v.SegmentFrequencyDistribution.ObservedSamples) .Where(s => s.Type == segmentType).Distinct().OrderBy(s => s.Category(), categoryComparer).ThenBy(s => s, segmentComparer) .Select(s => new SegmentMappingsTableSegmentViewModel(s, _soundType)).Concat(new SegmentMappingsTableSegmentViewModel(null, _soundType)).ToArray()); _categories = new ReadOnlyList<SegmentCategoryViewModel>(_segments.GroupBy(s => s.DomainSegment == null ? string.Empty : s.DomainSegment.Category()) .OrderBy(g => g.Key, categoryComparer).Select(g => new SegmentCategoryViewModel(g.Key, g)).ToArray()); var mappingLookup = new Dictionary<UnorderedTuple<string, string>, HashSet<UnorderedTuple<string, string>>>(); foreach (SegmentMappingViewModel mapping in mappings) { string seg1, seg2; FeatureSymbol leftEnv1, rightEnv1, leftEnv2, rightEnv2; if (ListSegmentMappings.Normalize(projectService.Project.Segmenter, mapping.Segment1, out seg1, out leftEnv1, out rightEnv1) && ListSegmentMappings.Normalize(projectService.Project.Segmenter, mapping.Segment2, out seg2, out leftEnv2, out rightEnv2)) { UnorderedTuple<string, string> key = UnorderedTuple.Create(seg1, seg2); HashSet<UnorderedTuple<string, string>> m = mappingLookup.GetValue(key, () => new HashSet<UnorderedTuple<string, string>>()); m.Add(UnorderedTuple.Create(mapping.Segment1, mapping.Segment2)); } } IWordAligner aligner = projectService.Project.WordAligners[ComponentIdentifiers.PrimaryWordAligner]; foreach (SegmentMappingsTableSegmentViewModel segment1 in _segments) { bool isEnabled = true; foreach (SegmentMappingsTableSegmentViewModel segment2 in _segments) { if (EqualityComparer<Segment>.Default.Equals(segment1.DomainSegment, segment2.DomainSegment)) isEnabled = false; int delta = segment1.DomainSegment == null || segment2.DomainSegment == null ? -1 : aligner.Delta(segment1.DomainSegment.FeatureStruct, segment2.DomainSegment.FeatureStruct); SegmentMappingsTableSegmentPairViewModel segmentPair = segmentPairFactory(segment1, segment2, delta, isEnabled); segmentPair.MeetsThreshold = delta != -1 && delta <= _threshold; HashSet<UnorderedTuple<string, string>> pairMappings; if (mappingLookup.TryGetValue(UnorderedTuple.Create(segment1.StrRep, segment2.StrRep), out pairMappings)) segmentPair.Mappings.Mappings.AddRange(pairMappings.Select(m => mappingFactory(m.Item1, m.Item2))); segment1.SegmentPairs.Add(segmentPair); } } }
public SegmentMappingsTableViewModel(IProjectService projectService, SegmentMappingsTableSegmentPairViewModel.Factory segmentPairFactory, SegmentMappingViewModel.Factory mappingFactory, IEnumerable <SegmentMappingViewModel> mappings, SoundType soundType, int threshold) { _threshold = threshold; _soundType = soundType; FeatureSymbol segmentType; switch (_soundType) { case SoundType.Consonant: segmentType = CogFeatureSystem.ConsonantType; break; case SoundType.Vowel: segmentType = CogFeatureSystem.VowelType; break; default: throw new InvalidEnumArgumentException(); } var segmentComparer = new SegmentComparer(); var categoryComparer = new SegmentCategoryComparer(); _segments = new ReadOnlyList <SegmentMappingsTableSegmentViewModel>(projectService.Project.Varieties.SelectMany(v => v.SegmentFrequencyDistribution.ObservedSamples) .Where(s => s.Type == segmentType).Distinct().OrderBy(s => s.Category(), categoryComparer).ThenBy(s => s, segmentComparer) .Select(s => new SegmentMappingsTableSegmentViewModel(s, _soundType)).Concat(new SegmentMappingsTableSegmentViewModel(null, _soundType)).ToArray()); _categories = new ReadOnlyList <SegmentCategoryViewModel>(_segments.GroupBy(s => s.DomainSegment == null ? string.Empty : s.DomainSegment.Category()) .OrderBy(g => g.Key, categoryComparer).Select(g => new SegmentCategoryViewModel(g.Key, g)).ToArray()); var mappingLookup = new Dictionary <UnorderedTuple <string, string>, HashSet <UnorderedTuple <string, string> > >(); foreach (SegmentMappingViewModel mapping in mappings) { string seg1, seg2; FeatureSymbol leftEnv1, rightEnv1, leftEnv2, rightEnv2; if (ListSegmentMappings.Normalize(projectService.Project.Segmenter, mapping.Segment1, out seg1, out leftEnv1, out rightEnv1) && ListSegmentMappings.Normalize(projectService.Project.Segmenter, mapping.Segment2, out seg2, out leftEnv2, out rightEnv2)) { UnorderedTuple <string, string> key = UnorderedTuple.Create(seg1, seg2); HashSet <UnorderedTuple <string, string> > m = mappingLookup.GetOrCreate(key, () => new HashSet <UnorderedTuple <string, string> >()); m.Add(UnorderedTuple.Create(mapping.Segment1, mapping.Segment2)); } } IWordAligner aligner = projectService.Project.WordAligners[ComponentIdentifiers.PrimaryWordAligner]; foreach (SegmentMappingsTableSegmentViewModel segment1 in _segments) { bool isEnabled = true; foreach (SegmentMappingsTableSegmentViewModel segment2 in _segments) { if (EqualityComparer <Segment> .Default.Equals(segment1.DomainSegment, segment2.DomainSegment)) { isEnabled = false; } int delta = segment1.DomainSegment == null || segment2.DomainSegment == null ? -1 : aligner.Delta(segment1.DomainSegment.FeatureStruct, segment2.DomainSegment.FeatureStruct); SegmentMappingsTableSegmentPairViewModel segmentPair = segmentPairFactory(segment1, segment2, delta, isEnabled); segmentPair.MeetsThreshold = delta != -1 && delta <= _threshold; HashSet <UnorderedTuple <string, string> > pairMappings; if (mappingLookup.TryGetValue(UnorderedTuple.Create(segment1.StrRep, segment2.StrRep), out pairMappings)) { segmentPair.Mappings.Mappings.AddRange(pairMappings.Select(m => mappingFactory(m.Item1, m.Item2))); } segment1.SegmentPairs.Add(segmentPair); } } }