예제 #1
0
        // inspiré de https://en.wikipedia.org/wiki/Trie#Algorithms
        public void Insert(string str, string value)
        {
            CodePointIndexedString cpi = new CodePointIndexedString(str);
            IVertex node      = graph.SearchNode("")[0];
            int     i         = 0;
            int     stopIndex = cpi.Length - 1;

            while (i <= stopIndex)
            {
                IEdge link = GetLabeledEdge(node, cpi.AtIndex(i));
                if (link != null)
                {
                    node = link.GetLinkedObjects().Where(o => o.ObjectId != node.ObjectId).First() as IVertex;
                    if (i == stopIndex)
                    {
                        Annotation nodeValue = new Annotation()
                        {
                            Key = "id", Value = value
                        };
                        graph.AddAnnotation(node, nodeValue);
                    }
                    i++;
                }
                else
                {
                    break;
                }
            }

            while (i <= stopIndex)
            {
                string  currentChar = cpi.AtIndex(i);
                IVertex newNode     = new TextVertex(cpi.Substring(0, i + 1));
                graph.AddVertex(newNode);
                Annotation edgeLabel = new Annotation()
                {
                    Key = "label", Value = currentChar
                };
                IEdge edge = graph.CreateEdge(trieLink, node, newNode);
                graph.AddAnnotation(edge, edgeLabel);
                node = newNode;

                if (i == stopIndex)
                {
                    Annotation nodeValue = new Annotation()
                    {
                        Key = "id", Value = value
                    };
                    graph.AddAnnotation(node, nodeValue);
                }
                i++;
            }
        }
예제 #2
0
        public List <TrieMatchResult> MatchPrefix(string search)
        {
            CodePointIndexedString cpi = new CodePointIndexedString(search);
            IVertex node      = graph.SearchNode("")[0];
            int     i         = 0;
            int     stopIndex = cpi.Length - 1;

            while (i <= stopIndex)
            {
                IEdge link = GetLabeledEdge(node, cpi.AtIndex(i));
                if (link != null)
                {
                    node = link.GetLinkedObjects().Where(o => o.ObjectId != node.ObjectId).First() as IVertex;
                    if (i == stopIndex)
                    {
                        List <TrieMatchResult> result = new List <TrieMatchResult>();
                        foreach (Annotation a in graph.GetAnnotations(node))
                        {
                            result.Add(new TrieMatchResult(node.ToString(), a.Value));
                        }
                        result.AddRange(_matchPrefix(node).ToList());
                        return(result);
                    }
                    i++;
                }
                else
                {
                    return(new List <TrieMatchResult>());
                }
            }
            return(_matchPrefix(node).ToList());

            IEnumerable <TrieMatchResult> _matchPrefix(IVertex node_p)
            {
                foreach (IVertex tempNode in node_p.Successors(trieLink))
                {
                    IEnumerable <Annotation> annotations = graph.GetAnnotations(tempNode);
                    foreach (Annotation a in annotations)
                    {
                        yield return(new TrieMatchResult(tempNode.ToString(), a.Value));
                    }
                    foreach (var v in _matchPrefix(tempNode))
                    {
                        yield return(v);
                    }
                }
            }
        }