private IEnumerable <UnorderedTuple <string, string> > ParseMappings(XElement elem) { foreach (XElement mappingElem in elem.Elements(ConfigManager.Cog + "Mapping")) { yield return(UnorderedTuple.Create((string)mappingElem.Attribute("segment1"), (string)mappingElem.Attribute("segment2"))); } }
private static void WriteLinks(XmlGraphData <TUIRawData, TEditorData> conversation, XElement root) { HashSet <UnorderedTuple2 <Tuple <Id <TConnector>, IConversationNodeData> > > links = new HashSet <UnorderedTuple2 <Tuple <Id <TConnector>, IConversationNodeData> > >(); foreach (var n in conversation.Nodes) { foreach (var c in n.GraphData.Connectors) { foreach (var cc in c.Connections) { var pair1 = Tuple.Create(c.Id, n.GraphData); var pair2 = Tuple.Create(cc.Id, cc.Parent); links.Add(UnorderedTuple.Make(pair1, pair2)); } } } foreach (var link in links) { var node1 = new XAttribute("node1", link.Item1.Item2.NodeId.Serialized()); var connector1 = new XAttribute("connector1", link.Item1.Item1.Serialized()); var node2 = new XAttribute("node2", link.Item2.Item2.NodeId.Serialized()); var connector2 = new XAttribute("connector2", link.Item2.Item1.Serialized()); root.Add(new XElement("Link", node1, connector1, node2, connector2)); } }
public void GetHashCode_SameTuplesDifferentTypesInDifferentOrder_HashCodesEqual() { var tuple1 = UnorderedTuple.Create("A", "B", 0); var tuple2 = UnorderedTuple.Create("B", "A", 0); Assert.That(tuple1.GetHashCode(), Is.EqualTo(tuple2.GetHashCode())); }
public void Equals_SameTuples_ReturnsTrue() { var tuple1 = UnorderedTuple.Create("A", "B", "C"); var tuple2 = UnorderedTuple.Create("A", "B", "C"); Assert.That(tuple1.Equals(tuple2), Is.True); }
public void GetHashCode_SameTuples_HashCodesEqual() { var tuple1 = UnorderedTuple.Create("A", "B", "C"); var tuple2 = UnorderedTuple.Create("A", "B", "C"); Assert.That(tuple1.GetHashCode(), Is.EqualTo(tuple2.GetHashCode())); }
public void GetHashCode_DifferentTuples_HashCodesNotEqual() { var tuple1 = UnorderedTuple.Create("A", "B", "D"); var tuple2 = UnorderedTuple.Create("A", "B", "C"); Assert.That(tuple1.GetHashCode(), Is.Not.EqualTo(tuple2.GetHashCode())); }
public void Equals_SameTuplesDifferentTypesInDifferentOrder_ReturnsTrue() { var tuple1 = UnorderedTuple.Create("A", "B", 0); var tuple2 = UnorderedTuple.Create("B", "A", 0); Assert.That(tuple1.Equals(tuple2), Is.True); }
public void Equals_DifferentTuples_ReturnsFalse() { var tuple1 = UnorderedTuple.Create("A", "B", "D"); var tuple2 = UnorderedTuple.Create("A", "B", "C"); Assert.That(tuple1.Equals(tuple2), Is.False); }
private void AddToLookupDictionary(int index, CognacyDecision item) { Dictionary <Meaning, int> lookup = _lookupDictionary.GetValue(UnorderedTuple.Create(item.Variety1, item.Variety2), () => new Dictionary <Meaning, int>()); lookup[item.Meaning] = index; }
private IEnumerable <Usage> ConnectionDefinitionUsages(IConversationNodeData data) { Id <TConnectorDefinition> connector1 = Id <TConnectorDefinition> .FromGuid(data.Parameters.Where(p => p.Id == DomainIDs.ConnectionDefinitionConnector1).OfType <IEnumParameter>().Single().Value); Id <TConnectorDefinition> connector2 = Id <TConnectorDefinition> .FromGuid(data.Parameters.Where(p => p.Id == DomainIDs.ConnectionDefinitionConnector2).OfType <IEnumParameter>().Single().Value); var reference = UnorderedTuple.Make(connector1, connector2); foreach (var conversationFile in m_project.Conversations) { HashSet <UnorderedTuple2 <Output> > connections = new HashSet <UnorderedTuple2 <Output> >(); foreach (var node in conversationFile.Nodes) { foreach (var connector in node.Data.Connectors) { if (connector.Definition.Id == connector1 || connector.Definition.Id == connector2) { foreach (var connection in connector.Connections) { var c = UnorderedTuple.Make(connector.Definition.Id, connection.Definition.Id); if (c == reference) { connections.Add(UnorderedTuple.Make(connector, connection)); } yield return(new Usage(node, conversationFile, "Node connection using connector definition")); } } } } } }
public void IsMapped_ImplicitComplexSegments() { var segmentPool = new SegmentPool(); var mappings = new ListSegmentMappings(_segmenter, new[] { UnorderedTuple.Create("m", "n"), UnorderedTuple.Create("t", "-"), UnorderedTuple.Create("h#", "-#"), UnorderedTuple.Create("c", "#g"), UnorderedTuple.Create("f", "@") }, true); Shape shape1 = _segmenter.Segment("s͡mat͡h"); Shape shape2 = _segmenter.Segment("k͡no"); Assert.That(mappings.IsMapped(shape1.First.Prev, segmentPool.Get(shape1.First), shape1.First.Next, shape2.First.Prev, segmentPool.Get(shape2.First), shape2.First.Next), Is.True); Assert.That(mappings.IsMapped(shape1.Last.Prev, segmentPool.Get(shape1.Last), shape1.Last.Next, shape2.Last, new Ngram <Segment>(), shape2.Last.Next), Is.True); Assert.That(mappings.IsMapped(shape1.First, segmentPool.Get(shape1.First.Next), shape1.Last, shape2.First, segmentPool.Get(shape2.Last), shape2.Last.Next), Is.False); shape1 = _segmenter.Segment("got͡h"); shape2 = _segmenter.Segment("c͡lo"); Assert.That(mappings.IsMapped(shape1.First.Prev, segmentPool.Get(shape1.First), shape1.First.Next, shape2.First.Prev, segmentPool.Get(shape2.First), shape2.First.Next), Is.True); Assert.That(mappings.IsMapped(shape1.Last.Prev, segmentPool.Get(shape1.Last), shape1.Last.Next, shape2.Last, new Ngram <Segment>(), shape2.Last.Next), Is.True); shape1 = _segmenter.Segment("s͡hog"); shape2 = _segmenter.Segment("oc͡t"); Assert.That(mappings.IsMapped(shape1.First.Prev, segmentPool.Get(shape1.First), shape1.First.Next, shape2.First.Prev, new Ngram <Segment>(), shape2.First), Is.False); Assert.That(mappings.IsMapped(shape1.Last.Prev, segmentPool.Get(shape1.Last), shape1.Last.Next, shape2.Last.Prev, segmentPool.Get(shape2.Last), shape2.Last.Next), Is.False); }
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.AreCognatePredicted) { 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, max = double.MinValue; 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.AreCognatePredicted && wp.GetWord(w1.Variety) == w1 && wp.GetWord(w2.Variety) == w2) { score = wp.CognicityScore; } double distance = 1.0 - score; min = Math.Min(min, distance); max = Math.Max(max, distance); distanceMatrix[UnorderedTuple.Create(w1, w2)] = distance; } } var clusterer = new FlatUpgmaClusterer <Word>((w1, w2) => distanceMatrix[UnorderedTuple.Create(w1, w2)], (max + min) / 2); return(clusterer.GenerateClusters(words).Concat(new Cluster <Word>(noise, true))); }
protected override void MoveItem(int oldIndex, int newIndex) { CognacyDecision item = Items[oldIndex]; UnorderedTuple <Variety, Variety> key = UnorderedTuple.Create(item.Variety1, item.Variety2); _lookupDictionary[key][item.Meaning] = newIndex; base.MoveItem(oldIndex, newIndex); }
private static int AddEdge(Dictionary <UnorderedTuple <object, object>, GlobalCorrespondencesGraphEdge> edges, SoundCorrespondence corr, object key1, GlobalSegmentVertex vertex1, object key2, GlobalSegmentVertex vertex2) { GlobalCorrespondencesGraphEdge edge = edges.GetValue(UnorderedTuple.Create(key1, key2), () => new GlobalCorrespondencesGraphEdge(vertex1, vertex2)); edge.Frequency += corr.Frequency; edge.DomainWordPairs.AddRange(corr.WordPairs); return(edge.Frequency); }
public static void TestUnorderedTuple() { var a = UnorderedTuple.Make(0, 1); var A = UnorderedTuple.Make(1, 0); var b = UnorderedTuple.Make(0, 2); Assert.That(a.Equals(A)); Assert.That(A.Equals(a)); Assert.That(!a.Equals(b)); }
private void RemoveFromLookupDictionary(int index) { CognacyDecision item = Items[index]; UnorderedTuple <Variety, Variety> key = UnorderedTuple.Create(item.Variety1, item.Variety2); Dictionary <Meaning, int> decisions = _lookupDictionary[key]; if (decisions.Remove(item.Meaning) && decisions.Count == 0) { _lookupDictionary.Remove(key); } }
private void StoreConnections(TNode node, bool register) { if (!register) { if (m_connectionDeregisterActions.ContainsKey(node)) { foreach (var deregister in m_connectionDeregisterActions[node]) { deregister(); } } return; } foreach (var connector in node.Data.Connectors) { var connectorTemp = connector; Action <Output, bool> connected = (connection, mustExist) => { ConnectionUpdate(connection, connectorTemp, mustExist); }; connectorTemp.Connected += c => connected(c, true); connectorTemp.Disconnected += connection => { DisconnectionUpdate(connection, connectorTemp, UIInfo(connection).Area.Value); }; foreach (var connection in connectorTemp.Connections) { connected(connection, false); } Action deregister = UIInfo(connectorTemp).Area.Changed.Register(change => { foreach (var connection in connectorTemp.Connections) { var other = UIInfo(connection); //The nature of the bezier splines means they will never reach outside the bounding rectangle which includes their endpoints RectangleF fromBounds = RectangleF.Union(change.From, other.Area.Value); var pair = UnorderedTuple.Make(connectorTemp, connection); SpatiallyOrderedConnections.Remove(Tuple.Create(pair, fromBounds)); RectangleF toBounds = RectangleF.Union(change.To, other.Area.Value); SpatiallyOrderedConnections.Add(Tuple.Create(pair, toBounds), toBounds); } }); if (!m_connectionDeregisterActions.ContainsKey(node)) { m_connectionDeregisterActions[node] = new List <Action>(); } m_connectionDeregisterActions[node].Add(deregister); } }
public string GetRawMessage(UnorderedTuple <InfoType> types) { try { int index = rng.Next(messages[types].Count - 1); return(messages[types][index]); } catch { return(String.Format("No messages found for InfoTypes {0} and {1}", types.item1, types.item2)); } }
private void DisconnectionUpdate(Output connection, Output connectorTemp, RectangleF connectorPosition) { var ui1 = UIInfo(connectorTemp); //The nature of the bezier splines means they will never reach outside the bounding rectangle which includes their endpoints RectangleF bounds = RectangleF.Union(ui1.Area.Value, connectorPosition); var pair = UnorderedTuple.Make(connectorTemp, connection); bool exists = SpatiallyOrderedConnections.FindTouchingRegion(bounds).Contains(Tuple.Create(pair, bounds)); if (exists) { SpatiallyOrderedConnections.Remove(Tuple.Create(pair, bounds)); } }
public bool CanConnect(Id <TConnectorDefinition> a, Id <TConnectorDefinition> b) { var test = UnorderedTuple.Make(a, b); var alwaysAllowed = UnorderedTuple.Make(SpecialConnectors.Input.Id, SpecialConnectors.Output.Id); if (test.Equals(alwaysAllowed)) { return(true); } if (m_allowed.Contains(test)) { return(true); } return(false); }
public bool Remove(WordPair wordPair) { Dictionary <Meaning, CognacyDecision> decisions; UnorderedTuple <Variety, Variety> key = UnorderedTuple.Create(wordPair.VarietyPair.Variety1, wordPair.VarietyPair.Variety2); if (_lookupDictionary.TryGetValue(key, out decisions)) { CognacyDecision decision; if (decisions.TryGetValue(wordPair.Meaning, out decision)) { return(Remove(decision)); } } return(false); }
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); }
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 void KeepScoreForLsdbcClusterer() { foreach (KeyValuePair <UnorderedTuple <string, string>, double> kv in _distanceDict) { UnorderedTuple <string, string> wordPair = kv.Key; double distance = kv.Value; AddToListDict(_distanceGraph, wordPair.Item1, new Tuple <double, string>(distance, wordPair.Item2)); AddToListDict(_distanceGraph, wordPair.Item2, new Tuple <double, string>(distance, wordPair.Item1)); } // TODO: If performance is a serious issue, can replace scoreList with a BinaryHeap<> instead of a List<>. // Leaving things simple for now. foreach (List <Tuple <double, string> > scoreList in _distanceGraph.Values) { scoreList.Sort(); } }
public override bool Equals(object obj) { if (Object.ReferenceEquals(this, obj)) { return(true); } UnorderedTuple <T1, T2> instance = obj as UnorderedTuple <T1, T2>; if (instance == null) { return(false); } return((this.Item1.Equals(instance.Item1) && this.Item2.Equals(instance.Item2)) || (this.Item1.Equals(instance.Item2) && this.Item2.Equals(instance.Item1) )); }
public void IsMapped() { var segmentPool = new SegmentPool(); var mappings = new ListSegmentMappings(_segmenter, new[] { UnorderedTuple.Create("m", "n"), UnorderedTuple.Create("t", "-"), UnorderedTuple.Create("h#", "-#"), UnorderedTuple.Create("c", "#g"), UnorderedTuple.Create("f", "@"), UnorderedTuple.Create("a", "o"), UnorderedTuple.Create("Cw", "-V") }, false); Shape shape1 = _segmenter.Segment("ma͡et"); Shape shape2 = _segmenter.Segment("no"); Assert.That(mappings.IsMapped(shape1.First.Prev, segmentPool.Get(shape1.First), shape1.First.Next, shape2.First.Prev, segmentPool.Get(shape2.First), shape2.First.Next), Is.True); Assert.That(mappings.IsMapped(shape1.Last.Prev, segmentPool.Get(shape1.Last), shape1.Last.Next, shape2.Last, new Ngram <Segment>(), shape2.Last.Next), Is.True); Assert.That(mappings.IsMapped(shape1.First, segmentPool.Get(shape1.First.Next), shape1.Last, shape2.First, segmentPool.Get(shape2.Last), shape2.Last.Next), Is.False); shape1 = _segmenter.Segment("goh"); shape2 = _segmenter.Segment("co"); Assert.That(mappings.IsMapped(shape1.First.Prev, segmentPool.Get(shape1.First), shape1.First.Next, shape2.First.Prev, segmentPool.Get(shape2.First), shape2.First.Next), Is.True); Assert.That(mappings.IsMapped(shape1.Last.Prev, segmentPool.Get(shape1.Last), shape1.Last.Next, shape2.Last, new Ngram <Segment>(), shape2.Last.Next), Is.True); shape1 = _segmenter.Segment("hog"); shape2 = _segmenter.Segment("oc"); Assert.That(mappings.IsMapped(shape1.First.Prev, segmentPool.Get(shape1.First), shape1.First.Next, shape2.First.Prev, new Ngram <Segment>(), shape2.First), Is.False); Assert.That(mappings.IsMapped(shape1.Last.Prev, segmentPool.Get(shape1.Last), shape1.Last.Next, shape2.Last.Prev, segmentPool.Get(shape2.Last), shape2.Last.Next), Is.False); shape1 = _segmenter.Segment("swat"); shape2 = _segmenter.Segment("sat"); Assert.That(mappings.IsMapped(shape1.ElementAt(0), segmentPool.Get(shape1.ElementAt(1)), shape1.ElementAt(2), shape2.ElementAt(0), new Ngram <Segment>(), shape2.ElementAt(1)), Is.True); shape1 = _segmenter.Segment("sawat"); shape2 = _segmenter.Segment("saat"); Assert.That(mappings.IsMapped(shape1.ElementAt(1), segmentPool.Get(shape1.ElementAt(2)), shape1.ElementAt(3), shape2.ElementAt(1), new Ngram <Segment>(), shape2.ElementAt(2)), Is.False); }
public override IEnumerable <ConversationError <T> > Check(IEnumerable <T> nodes, IErrorCheckerUtilities <T> utils) { HashSet <UnorderedTuple2 <T> > results = new HashSet <UnorderedTuple2 <T> >(); foreach (var node in nodes) { foreach (var c in node.Data.Connectors) { foreach (var connection in c.Connections) { if (!c.Rules.CanConnect(c.Definition.Id, connection.Definition.Id)) { results.Add(UnorderedTuple.Make(node, utils.ReverseLookup(connection.Parent))); } } } } return(results.Select(r => new InvalidConnectionError(r))); }
public bool Remove(WordPair wordPair) { Dictionary <Meaning, int> decisions; UnorderedTuple <Variety, Variety> key = UnorderedTuple.Create(wordPair.VarietyPair.Variety1, wordPair.VarietyPair.Variety2); if (_lookupDictionary.TryGetValue(key, out decisions)) { int index; if (decisions.TryGetValue(wordPair.Meaning, out index)) { decisions.Remove(wordPair.Meaning); if (decisions.Count == 0) { _lookupDictionary.Remove(key); } base.RemoveItem(index); return(true); } } return(false); }
private static HashSet <UnorderedTuple2 <Tuple <Id <TConnector>, Id <NodeTemp> > > > ReadLinks(IEnumerable <Id <NodeTemp> > filteredNodes, XElement root) { HashSet <UnorderedTuple2 <Tuple <Id <TConnector>, Id <NodeTemp> > > > result = new HashSet <UnorderedTuple2 <Tuple <Id <TConnector>, Id <NodeTemp> > > >(); foreach (var link in root.Elements("Link")) { Id <NodeTemp> node1 = Id <NodeTemp> .Parse(link.Attribute("node1").Value); Id <TConnector> connector1 = Id <TConnector> .Parse(link.Attribute("connector1").Value); Id <NodeTemp> node2 = Id <NodeTemp> .Parse(link.Attribute("node2").Value); Id <TConnector> connector2 = Id <TConnector> .Parse(link.Attribute("connector2").Value); if (filteredNodes.Any(n => n.Equals(node1) || n.Equals(node2))) { result.Add(UnorderedTuple.Make(Tuple.Create(connector1, node1), Tuple.Create(connector2, node2))); } } return(result); }
private void ConnectionUpdate(Output connection, Output connectorTemp, bool mustExist) { var ui1 = UIInfo(connectorTemp, false); var ui2 = UIInfo(connection, !mustExist); if (ui2 != null) { //The nature of the bezier splines means they will never reach outside the bounding rectangle which includes their endpoints RectangleF bounds = RectangleF.Union(ui1.Area.Value, ui2.Area.Value); var pair = UnorderedTuple.Make(connectorTemp, connection); bool exists = SpatiallyOrderedConnections.FindTouchingRegion(bounds).Contains(Tuple.Create(pair, bounds)); if (!exists) { SpatiallyOrderedConnections.Add(Tuple.Create(pair, bounds), bounds); } else { } } }