Exemplo n.º 1
0
 public void Add(RecognizedToken curToken)
 {
     Sentence.Add(curToken);
 }
Exemplo n.º 2
0
            public List <SentenceInterpretation> GetAllPossibleSentenceInterpretations(Token source, Token destination, int pathCount = 5)
            {
                Func <SentenceEdge, double> edgeWeights = (e) =>
                {
                    //Console.WriteLine(e.Source.Value + "[" + e.SourceType + "]" + "->" + e.Target.Value + "[" + e.TargetType + "]" + (1 - e.Probability).ToString("0.00"));
                    return(1 - e.Probability);
                };// IMPROVE!
                var AllPaths = new List <SentenceInterpretation>();


                //var tryGetPath = this.ShortestPathsDijkstra(edgeWeights, source);
                //IEnumerable<SentenceEdge> result;
                //var tmp = tryGetPath(destination, out result);


                foreach (IEnumerable <SentenceEdge> path in AlgorithmExtensions.RankedShortestPathHoffmanPavley(this.ToBidirectionalGraph(), edgeWeights, source, destination, pathCount))
                //var path = result.ToList();
                {
                    RecognizedToken curToken = new RecognizedToken(path.First().SourceType).Add(path.First().Source);

                    var currentInterpretation = new SentenceInterpretation();

                    bool   fullCapture = false;
                    double probability = 1;

                    foreach (var edge in path)
                    {
                        probability *= edge.Probability;

                        switch (edge.SourceType[0])
                        {
                        case BeginTag: { curToken = new RecognizedToken(edge.SourceType).Add(edge.Source); fullCapture = false; break; }

                        case MiddleTag: { curToken.Add(edge.Source); fullCapture = false; break; }

                        case EndTag: { curToken.Add(edge.Source); fullCapture = true; break; }

                        case SingleTag: { curToken = new RecognizedToken(edge.SourceType).Add(edge.Source); fullCapture = true; break; }
                        }

                        if (fullCapture && edge.Source != source)
                        {
                            currentInterpretation.Add(curToken); curToken = null;
                        }
                    }

                    var lastEdge = path.Last();
                    probability *= lastEdge.Probability;

                    switch (lastEdge.TargetType[0])
                    {
                    case BeginTag: { curToken = new RecognizedToken(lastEdge.TargetType).Add(lastEdge.Target); fullCapture = false; break; }

                    case MiddleTag: { curToken.Add(lastEdge.Target); fullCapture = false; break; }

                    case EndTag: { curToken.Add(lastEdge.Target); fullCapture = true; break; }

                    case SingleTag: { curToken = new RecognizedToken(lastEdge.TargetType).Add(lastEdge.Target); fullCapture = true; break; }
                    }
                    if (fullCapture && lastEdge.Target != destination)
                    {
                        currentInterpretation.Add(curToken); curToken = null;
                    }

                    currentInterpretation.Probability = probability;

                    AllPaths.Add(currentInterpretation);
                }

                return(AllPaths);
            }