如何获取布尔 XPath 表达式的结果(例如“false”)?

发布于 2024-11-30 03:18:50 字数 1758 浏览 2 评论 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 元语法节点。但是,其余内容节点的计算结果为“true”。

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

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

I can't fetch this '0'。我不知道怎么说:“如果没有包含 '$ $ $' 作为内容的节点,则返回零。

关于如何执行此操作的任何想法?

谢谢。

I would like to have Java generate a '0' in case an XPath expression evaluates to 'false'.

I have this Java code:

//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;
}

The problem here is that this code:

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

only returns 4 elements because 'content1' returns 'false'. There are no n-gram nodes whose content contains the string '$ $ $'. However, the rest of the content nodes evaluates to 'true'.

If one of these contains() expressions evaluates to 'false', I must draw a '0' in my Xaml code. A '0' must appear on the screen, such as:

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

I can't fetch this '0'. I don't know how to say: "return a zero in case that there are no nodes that contain '$ $ $' as content.

Any ideas on how to do this?

Thank you.

= 0

I can't fetch this '0'. I don't know how to say: "return a zero in case that there are no nodes that contain '$ $ $' as content.

Any ideas on how to do this?

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

扶醉桌前 2024-12-07 03:18:50

这是答案:(

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);
        }

Here is the answer:

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;
    }
}

(This code is not working for what I want to accomplish outside of this question, but it works for this question because I have a way of returning a '0' in java, when the XPath expression yields zero-length).

The solution is this part really:

        else {
            theDoubles.add(0.0);
        }
爱冒险 2024-12-07 03:18:50

我想让 Java 在 XPath 表达式的情况下生成“0”
计算结果为“假”。

此 XPath 表达式

number(boolean(yourExpression))

boolean(yourExpression)false() 时,计算结果恰好为 0

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

I would like to have Java generate a '0' in case an XPath expression
evaluates to 'false'.

This XPath expression:

number(boolean(yourExpression))

evaluates to 0 exactly when boolean(yourExpression) is false().

You need simply to substitute yourExpression above with your expression (which isn't at all clear from the question) and to evaluate this XPath expression with the XPath related classes/methods available to you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文