Trainer.java 文件源码

java
阅读 28 收藏 0 点赞 0 评论 0

项目:codebuff 作者:
public static TerminalNode getMatchingLeftSymbol(Corpus corpus,
                                                 InputDocument doc,
                                                 TerminalNode node)
{
    ParserRuleContext parent = (ParserRuleContext)node.getParent();
    int curTokensParentRuleIndex = parent.getRuleIndex();
    Token curToken = node.getSymbol();
    if (corpus.ruleToPairsBag != null) {
        String ruleName = doc.parser.getRuleNames()[curTokensParentRuleIndex];
        RuleAltKey ruleAltKey = new RuleAltKey(ruleName, parent.getAltNumber());
        List<Pair<Integer, Integer>> pairs = corpus.ruleToPairsBag.get(ruleAltKey);
        if ( pairs!=null ) {
            // Find appropriate pair given current token
            // If more than one pair (a,b) with b=current token pick first one
            // or if a common pair like ({,}), then give that one preference.
            // or if b is punctuation, prefer a that is punct
            List<Integer> viableMatchingLeftTokenTypes = viableLeftTokenTypes(parent, curToken, pairs);
            Vocabulary vocab = doc.parser.getVocabulary();
            if ( !viableMatchingLeftTokenTypes.isEmpty() ) {
                int matchingLeftTokenType =
                    CollectTokenPairs.getMatchingLeftTokenType(curToken, viableMatchingLeftTokenTypes, vocab);
                List<TerminalNode> matchingLeftNodes = parent.getTokens(matchingLeftTokenType);
                // get matching left node by getting last node to left of current token
                List<TerminalNode> nodesToLeftOfCurrentToken =
                    filter(matchingLeftNodes, n -> n.getSymbol().getTokenIndex()<curToken.getTokenIndex());
                TerminalNode matchingLeftNode = nodesToLeftOfCurrentToken.get(nodesToLeftOfCurrentToken.size()-1);
                if (matchingLeftNode == null) {
                    System.err.println("can't find matching node for "+node.getSymbol());
                }
                return matchingLeftNode;
            }
        }
    }
    return null;
}
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号