【问题标题】:Stanford Dependency Parser - how to get spans?斯坦福依赖解析器 - 如何获得跨度?
【发布时间】:2013-04-16 00:36:40
【问题描述】:

我正在使用 Java 中的斯坦福库进行依赖项解析。 有没有办法在我的原始依赖字符串中取回索引? 我尝试调用 getSpans() 方法,但它为每个标记返回 null:

LexicalizedParser lp = LexicalizedParser.loadModel(
        "edu/stanford/nlp/models/lexparser/englishPCFG.ser.gz",
        "-maxLength", "80", "-retainTmpSubcategories");
TreebankLanguagePack tlp = new PennTreebankLanguagePack();
GrammaticalStructureFactory gsf = tlp.grammaticalStructureFactory();
Tree parse = lp.apply(text);
GrammaticalStructure gs = gsf.newGrammaticalStructure(parse);
Collection<TypedDependency> tdl = gs.typedDependenciesCollapsedTree();
for(TypedDependency td:tdl)
{
      td.gov().getSpan()  // it's null!
      td.dep().getSpan()  // it's null!
}

有什么想法吗?

【问题讨论】:

    标签: java parsing nlp stanford-nlp


    【解决方案1】:

    我终于编写了自己的辅助函数来获取原始字符串的跨度:

    public HashMap<Integer, TokenSpan> getTokenSpans(String text, Tree parse)
    {
        List<String> tokens = new ArrayList<String>();
        traverse(tokens, parse, parse.getChildrenAsList());
        return extractTokenSpans(text, tokens);
    }
    
    private void traverse(List<String> tokens, Tree parse, List<Tree> children)
    {
        if(children == null)
            return;
        for(Tree child:children)
        {
            if(child.isLeaf())
            {
                tokens.add(child.value());
            }
            traverse(tokens, parse, child.getChildrenAsList());         
        }
    }
    
    private HashMap<Integer, TokenSpan> extractTokenSpans(String text, List<String> tokens)
    {
        HashMap<Integer, TokenSpan> result = new HashMap<Integer, TokenSpan>();
        int spanStart, spanEnd;
    
        int actCharIndex = 0;
        int actTokenIndex = 0;
        char actChar;
        while(actCharIndex < text.length())
        {
            actChar = text.charAt(actCharIndex);
            if(actChar == ' ')
            {
                actCharIndex++;
            }
            else
            {
                spanStart = actCharIndex;
                String actToken = tokens.get(actTokenIndex);
                int tokenCharIndex = 0;
                while(tokenCharIndex < actToken.length() && text.charAt(actCharIndex) == actToken.charAt(tokenCharIndex))
                {
                    tokenCharIndex++;
                    actCharIndex++;
                }
    
                if(tokenCharIndex != actToken.length())
                {
                    //TODO: throw exception
                }
                actTokenIndex++;
                spanEnd = actCharIndex;
                result.put(actTokenIndex, new TokenSpan(spanStart, spanEnd));
            }
        }
        return result;
    }
    

    那我打个电话

     getTokenSpans(originalString, parse)
    

    所以我得到了一个映射,它可以将每个令牌映射到其对应的令牌跨度。 这不是一个优雅的解决方案,但至少它有效。

    【讨论】:

      【解决方案2】:

      即使您已经回答了自己的问题,而且这是一个老话题:我今天偶然发现了同样的问题,但使用的是 (Stanford) LexicalizedParser,而不是 Dependency Parser。还没有针对依赖项对其进行测试,但是以下解决了我在 lexParser 场景中的问题:

      List<Word> wl = tree.yieldWords();
      int begin = wl.get(0).beginPosition();
      int end = wl.get(wl.size()-1).endPosition();
      Span sp = new Span(begin, end);
      

      Span 然后保存(子)树的索引。 (如果你一直走到终端,我想这应该适用于令牌级别)。

      希望这可以帮助遇到同样问题的其他人!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-01-01
        相关资源
        最近更新 更多