0

我想让 Java 生成一个“0”,以防 XPath 表达式计算为“假”。

我有这个Java代码:

//Read the input XML document
private SAXBuilder parser = new SAXBuilder();
    private Document characters;
    private XPath pProbs;
    private List<Attribute> probs;
    private Double[] dprobs;
    private String pathToSourceXml;
    private String content1, content2, content3, content4, content5;

    characters = parser.build(pathToSourceXml);
    pProbs = XPath.newInstance("/n-grams-sorted/n-gram[contains(.,"+content1+") or contains(.,"+content2+") or contains(.,"+content3+") or contains(.,"+content4+") or contains(.,"+content5+")]/@probability");
    probs = (List<Attribute>) pProbs.selectNodes(characters);
    ...

//Return all the values of the @probability attibrutes
public Double[] getProbs(String pathToSourceXml) {
    this.pathToSourceXml = pathToSourceXml;

    List<Double> theDoubles = new ArrayList();
    dprobs = new Double[5];
    for (int i=0; i<probs.size(); i++) {
        theDoubles.add(Double.parseDouble(probs.get(i).getValue()));
        dprobs[i] = theDoubles.get(i);
    }
    return dprobs;
}

这里的问题是这段代码:

pProbs = XPath.newInstance("/n-grams-sorted/n-gram[contains(.,"+content1+") or contains(.,"+content2+") or contains(.,"+content3+") or contains(.,"+content4+") or contains(.,"+content5+")]/@probability");

仅返回 4 个元素,因为 'content1' 返回 'false'。没有内容包含字符串“$ $ $”的 n-gram 节点。但是,其余的内容节点评估为“真”。

如果这些 contains() 表达式之一的计算结果为“假”,我必须在我的 Xaml 代码中绘制一个“0”。屏幕上必须出现“0”,例如:

'# # #' = 0.0015
'? ? ?' = 0.0047
'k i d' = 0.0012
'$ $ $' = 0

我无法获取这个“0”。我不知道怎么说:“如果没有包含 '$ $ $' 作为内容的节点,则返回零。

关于如何做到这一点的任何想法?

谢谢你。

4

2 回答 2

0

这是答案:

public class XPathCharacters {
    private SAXBuilder parser = new SAXBuilder();
    private Document characters;
    private XPath pProbs, pContent1, pContent2, pContent3, pContent4, pContent5;
    private List<Attribute> probs1, probs2, probs3, probs4, probs5;
    private List<List<Attribute>> probs;
    private List<String> noNodes;
    private Double[] dprobs;
    private String pathToSourceXml;
    private String content1, content2, content3, content4, content5;
    private int noNode;

    public XPathCharacters(){

    }

    public XPathCharacters(String path, String content1,String content2,String content3,String content4,String content5){
        setPathToSourceXml(path);
        this.content1 = content1;
        this.content2 = content2;
        this.content3 = content3;
        this.content4 = content4;
        this.content5 = content5;
        noNode = 0;
        initiate();
    }

    public void initiate() {
        try {
        //Evaluate each XPath expression seperately 
            characters = parser.build(pathToSourceXml);
            pContent1 = XPath.newInstance("/n-grams-sorted/n-gram[contains(.,"+content1+")]/@probability");
            pContent2 = XPath.newInstance("/n-grams-sorted/n-gram[contains(.,"+content2+")]/@probability");
            pContent3 = XPath.newInstance("/n-grams-sorted/n-gram[contains(.,"+content3+")]/@probability");
            pContent4 = XPath.newInstance("/n-grams-sorted/n-gram[contains(.,"+content4+")]/@probability");
            pContent5 = XPath.newInstance("/n-grams-sorted/n-gram[contains(.,"+content5+")]/@probability");

            //Convert the result of the above XPath expressions to nodes
            probs1 = (List<Attribute>) pContent1.selectNodes(characters);
            probs2 = (List<Attribute>) pContent2.selectNodes(characters);
            probs3 = (List<Attribute>) pContent3.selectNodes(characters);
            probs4 = (List<Attribute>) pContent4.selectNodes(characters);
            probs5 = (List<Attribute>) pContent5.selectNodes(characters);

        } catch (JDOMException jdome) {
            System.out.println("Error at XPathInformationgain.initiate(): JDOMException: " + jdome.getMessage());
        } catch (IOException ioe) {
            System.out.println("Error at XPathInformationgain.initiate(): IOException: " + ioe.getMessage());
        }
    }

    private void setPathToSourceXml(String path) {
        this.pathToSourceXml = path;
    }

    public Double[] getProbs(String pathToSourceXml) {
        this.pathToSourceXml = pathToSourceXml;

        probs = new ArrayList();
        probs.add(probs1);
        probs.add(probs2);
        probs.add(probs3);
        probs.add(probs4);
        probs.add(probs5);

        List<Double> theDoubles = new ArrayList();
        dprobs = new Double[5];
        noNodes = new ArrayList();
        int j=0;
        for (int i=0; i<5; i++) {
            if (probs.get(i).size() > 0) {
                System.out.println("i: "+i);
                System.out.println("j: "+j);
               //Add the value of all these nodes to an Array so you can work with them
               theDoubles.add(Double.parseDouble(probs.get(i).get(0).getValue()));
                dprobs[j] = theDoubles.get(j);
            }
            else {
                //If one of the values happens to be a zero-length array, add 0.0 to the array of values
                theDoubles.add(0.0);
            }
        }
        return dprobs;
    }

    public List<String> getNoNodes() {
        return noNodes;
    }
}

(此代码不适用于我想在此问题之外完成的工作,但它适用于此问题,因为当 XPath 表达式产生零长度时,我有一种在 java 中返回“0”的方法)。

解决方案实际上是这部分:

        else {
            theDoubles.add(0.0);
        }
于 2011-08-18T15:01:33.187 回答
0

我想让 Java 生成一个“0”,以防 XPath 表达式计算为“假”。

这个 XPath 表达式

number(boolean(yourExpression))

0准确计算为when boolean(yourExpression)is false()

您只需yourExpression用您的表达式替换上面的内容(从问题中根本不清楚),并使用您可用的 XPath 相关类/方法来评估此 XPath 表达式。

于 2011-08-19T03:07:53.163 回答