Example #1
0
        public void TestCosts()
        {
            int cost = 1;

            for (int i = 0; i < 3; i++)
            {
                for (int j = 0; j < 3; j++)
                {
                    Assert.AreEqual(cost++, connectionCosts.Get(i, j));
                }
            }
        }
Example #2
0
        private void UpdateNode(ViterbiNode[] viterbiNodes, ViterbiNode node)
        {
            int backwardConnectionId = node.LeftId;
            int wordCost             = node.WordCost;
            int leastPathCost        = DEFAULT_COST;

            foreach (ViterbiNode leftNode in viterbiNodes)
            {
                // If array doesn't contain any more ViterbiNodes, continue to next index
                if (leftNode == null)
                {
                    return;
                }
                else
                {
                    // cost = [total cost from BOS to previous node] + [connection cost between previous node and current node] + [word cost]
                    int pathCost = leftNode.PathCost +
                                   costs.Get(leftNode.RightId, backwardConnectionId) +
                                   wordCost;

                    // Add extra cost for long nodes in "Search mode".
                    if (mode == Mode.SEARCH || mode == Mode.EXTENDED)
                    {
                        pathCost += GetPenaltyCost(node);
                    }

                    // If total cost is lower than before, set current previous node as best left node (previous means left).
                    if (pathCost < leastPathCost)
                    {
                        leastPathCost = pathCost;
                        node.PathCost = leastPathCost;
                        node.LeftNode = leftNode;
                    }
                }
            }
        }
Example #3
0
        private string FormatNodes(JapaneseTokenizer tok, WrappedPositionArray positions, int startPos, Position endPosData, char[] fragment)
        {
            StringBuilder sb = new StringBuilder();

            // Output nodes
            for (int pos = startPos + 1; pos <= endPosData.pos; pos++)
            {
                Position posData = positions.Get(pos);
                for (int idx = 0; idx < posData.count; idx++)
                {
                    sb.Append("  ");
                    sb.Append(GetNodeID(pos, idx));
                    sb.Append(" [label=\"");
                    sb.Append(pos);
                    sb.Append(": ");
                    sb.Append(posData.lastRightID[idx]);
                    sb.Append("\"]\n");
                }
            }

            // Output arcs
            for (int pos = endPosData.pos; pos > startPos; pos--)
            {
                Position posData = positions.Get(pos);
                for (int idx = 0; idx < posData.count; idx++)
                {
                    Position backPosData = positions.Get(posData.backPos[idx]);
                    string   toNodeID    = GetNodeID(pos, idx);
                    string   fromNodeID  = GetNodeID(posData.backPos[idx], posData.backIndex[idx]);

                    sb.Append("  ");
                    sb.Append(fromNodeID);
                    sb.Append(" -> ");
                    sb.Append(toNodeID);

                    string attrs;
                    bestPathMap.TryGetValue(fromNodeID, out string path);
                    if (toNodeID.Equals(path, StringComparison.Ordinal))
                    {
                        // This arc is on best path
                        attrs = " color=\"#40e050\" fontcolor=\"#40a050\" penwidth=3 fontsize=20";
                    }
                    else
                    {
                        attrs = "";
                    }

                    IDictionary dict     = tok.GetDict(posData.backType[idx]);
                    int         wordCost = dict.GetWordCost(posData.backID[idx]);
                    int         bgCost   = costs.Get(backPosData.lastRightID[posData.backIndex[idx]],
                                                     dict.GetLeftId(posData.backID[idx]));

                    string surfaceForm = new string(fragment,
                                                    posData.backPos[idx] - startPos,
                                                    pos - posData.backPos[idx]);

                    sb.Append(" [label=\"");
                    sb.Append(surfaceForm);
                    sb.Append(' ');
                    sb.Append(wordCost);
                    if (bgCost >= 0)
                    {
                        sb.Append('+');
                    }
                    sb.Append(bgCost);
                    sb.Append("\"");
                    sb.Append(attrs);
                    sb.Append("]\n");
                }
            }
            return(sb.ToString());
        }