internal GraphLayerBase GetLayerFromIds(IEnumerable <string> ids) { var midTable = new HashSet <string>(ids); var layer = new ExplicitLayer(); iterateLines((entityId, edge, targetEntities) => { if (!midTable.Contains(entityId)) { return; } var entityNode = layer.CreateReference(intern(entityId)); edge = intern(edge); foreach (var targetEntityId in targetEntities) { if (!midTable.Contains(targetEntityId)) { continue; } var targetNode = layer.CreateReference(intern(targetEntityId)); layer.AddEdge(entityNode, edge, targetNode); } }); return(layer); }
internal static void RunToyGeneralization() { var data = new ExplicitLayer(); var A = data.CreateReference("A"); var B = data.CreateReference("B"); var C = data.CreateReference("C"); var D = data.CreateReference("D"); var H1 = data.CreateReference("H1"); var H2 = data.CreateReference("H2"); var H3 = data.CreateReference("H3"); var fH1 = data.CreateReference("fH1"); var fH2 = data.CreateReference("fH2"); var fH3 = data.CreateReference("fH3"); var H = data.CreateReference("H"); var X = data.CreateReference("X"); var edge1 = "e1"; var edgeZ1 = "z1"; var edgeZ2 = "z2"; var edgeIs = "is"; data.AddEdge(A, edge1, H1); data.AddEdge(B, edge1, H2); data.AddEdge(C, edge1, H3); data.AddEdge(A, edge1, fH1); data.AddEdge(B, edge1, fH2); data.AddEdge(C, edge1, fH3); data.AddEdge(H1, edgeIs, H); data.AddEdge(H2, edgeIs, H); data.AddEdge(H3, edgeIs, H); data.AddEdge(D, edgeZ1, H1); data.AddEdge(D, edgeZ2, H2); var graph = new ComposedGraph(data); var group = new KnowledgeDialog.Knowledge.Group(graph); group.AddNode(A); group.AddNode(B); var linker = new SingleWordLinker(); linker.Add(A, B, C, D, H1, H2, H3); var generalizer = new PatternGeneralizer(graph, linker.LinkUtterance); generalizer.AddExample("Where A lives?", H1); generalizer.AddExample("Where B lives?", H2); var answer = generalizer.GetAnswer("Where does C live?"); }
public Loader(string path) { _reader = new StreamReader(path, Encoding.UTF8); DataLayer = new ExplicitLayer(); Console.WriteLine("Counting lines: " + path); var lineCount = 0; var edges = new HashSet <string>(); var myInterning = new Dictionary <string, string>(); while (!_reader.EndOfStream) { var line = _reader.ReadLine(); ++lineCount; } _reader.BaseStream.Seek(0, SeekOrigin.Begin); Console.WriteLine("Loading graph(" + lineCount + "): " + path); var lastPercent = 0; var lineIndex = 0; while (!_reader.EndOfStream) { var line = _reader.ReadLine(); var parts = split(line); var source = intern(parts[0]); var edge = intern(parts[1]); var target = intern(parts[2]); var sourceNode = GraphLayerBase.CreateReference(source); var targetNode = GraphLayerBase.CreateReference(target); _nodes.Add(sourceNode); _nodes.Add(targetNode); DataLayer.AddEdge(sourceNode, edge, targetNode); UtteranceParser.RegisterEntity(target); ++lineIndex; var currentPercent = 100 * lineIndex / lineCount; if (currentPercent != lastPercent) { GC.Collect(); Console.WriteLine(currentPercent); lastPercent = currentPercent; } } }
internal GraphLayerBase GetLayer() { var layer = new ExplicitLayer(); iterateLines((entity, edge, targetEntities) => { var entityNode = layer.CreateReference(intern(entity)); edge = intern(edge); foreach (var targetEntity in targetEntities) { var targetNode = layer.CreateReference(intern(targetEntity)); layer.AddEdge(entityNode, edge, targetNode); } }); return(layer); }
private static void RepairPresidentPosition(NodeReference node, NodeReference positionRoot, ExplicitLayer layer, ComposedGraph graph) { if (!IsTemporaryNode(positionRoot)) { //only temporary nodes will be repaired return; } var endNode = graph.OutcommingTargets(positionRoot, EndEdge).FirstOrDefault(); if (endNode != null) { //nothing to repair return; } layer.AddEdge(positionRoot, EndEdge, graph.GetNode(NullNode)); }
private static void RemovePosition(NodeReference node, NodeReference positionRoot, ExplicitLayer layer) { //remove node-->PositionEdge-->positionRoot //it does not matter if positionRoot is temporary or not layer.RemoveEdge(node, HeldPositionEdge, positionRoot); }