I have a plan to traverse a dom tree in postorder fashion, then when its traversing through the siblings of each depth , for each sibling group, i want to get the number of elements in the text content for it. to make it clear, lets have a look at an example:
<?xml version="1.0" encoding="UTF-8"?>
<title text="title1">
<comment1 id="comment1">
<data1> this is an example</data1>
<data2> this example tries to do a demo over a dom tree</data2>
</comment1>
<comment2 id="comment2">
<data3> while it' beeing traversing in postorder fashion </data3>
<data4> hope it works! </data4>
<data5> :) </data5>
</comment2>
</title>
for example, i want to find out the number of charcater for data 1 and data2 together and for data3-5 togetehr too. here is the code i wrote so far to traverse the tree and calculating the TFIDF value, but as i mentioned, i want to find the TF for each group of siblings seperately, any clue? thanks in advance
lass tree{
private static int total=0;
private static double tf=0;
private static double result=0;
private static double TFIDFresult = 0;
static double TFIDF(int wordcount,String segment,String keyword)
{
if(segment==null)
return TFIDFresult;
StringTokenizer tokenizer =new StringTokenizer(segment) ;
while(tokenizer.hasMoreTokens()){
total++;
if( tokenizer.nextToken().equals(keyword))
wordcount++;
tf= (double) wordcount / total;
double inverseTF = Math.log10((float) wordcount / 4);
TFIDFresult = (((double) wordcount / total * inverseTF ));
}
return TFIDFresult;
}
public static void check(Node node){
if (node == null || node.getNodeName() == null)
return;
result= TFIDF(total, node.getNodeValue(), "this");
check(node.getFirstChild());
System.out.println(node.getNodeValue() != null && node.getNodeValue().trim().length() == 0 ? "" : node);
check(node.getNextSibling());
}
public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException {
File file = new File("d:\\a.xml");
DocumentBuilderFactory dbf =
DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document document = db.parse(file);
document.getDocumentElement().normalize();
Node b=document.getFirstChild();
check(b);
System.out.println(result);
}
}
ps: manually i asumed that number of doc in the calculation is 4 just for some reason .