diff --git a/WikiRating/src/main/java/computations/NormalisedVotes.java b/WikiRating/src/main/java/computations/NormalisedVotes.java index 32f6444..7cb59d6 100644 --- a/WikiRating/src/main/java/computations/NormalisedVotes.java +++ b/WikiRating/src/main/java/computations/NormalisedVotes.java @@ -1,165 +1,168 @@ package main.java.computations; import com.tinkerpop.blueprints.Direction; import com.tinkerpop.blueprints.Edge; import com.tinkerpop.blueprints.Vertex; import com.tinkerpop.blueprints.impls.orient.OrientGraph; import com.tinkerpop.blueprints.impls.orient.OrientVertex; import main.java.utilities.Connections; import main.java.utilities.Loggings; import main.java.utilities.PropertiesAccess; /** * This class will calculate the Normalised Votes * of all the revisions and hence the page by using the given * recursive formula that takes keeps scaling the votes on the * previous versions with the new ones * */ public class NormalisedVotes { static Class className=NormalisedVotes.class; //To check for cases where latest version is voted on without any change static boolean latestVoteCheck=true; final static double PHI_POWER_PARAMETER=Double.parseDouble(PropertiesAccess.getParameterProperties("PHI_POWER_PARAMETER")); /** *This method will calculate the Normalised Votes of all the pages in on the platform *along with their respective revisions. * @return void */ public static void calculatePageVotes(){ OrientGraph graph = Connections.getInstance().getDbGraph(); double currentPageVote=0; Vertex revisionNode=null; for (Vertex pageNode : graph.getVertices("@class","Page")) { latestVoteCheck=true; try{ revisionNode = pageNode.getEdges(Direction.OUT, "@class", "PreviousVersionOfPage").iterator().next().getVertex(Direction.IN); currentPageVote=recursiveVotes(graph,(int)revisionNode.getProperty("revid")); pageNode.setProperty("currentPageVote",currentPageVote); graph.commit(); }catch(Exception e){Loggings.getLogs(className).error(e);} } getTotalVotes(graph); graph.shutdown(); } /** * This method will calculate and store the Normalised votes for all the revisions of a particular page * and then return the final Normalised vote for the page itself * @param graph OrientGraph object * @param revid Revision Id of the latest version connected to the Page * @return final vote of the latest version is computed and returned */ public static double recursiveVotes(OrientGraph graph,int revid){ double lastVote=0,phi=0,normalVote=0,currVote=0; Vertex revisionNode=graph.getVertices("revid", revid).iterator().next(); - - if(latestVoteCheck==false&&(double)revisionNode.getProperty("previousVote")!=-1){ + + //Since we can't directly check for equality with floating numebers safetly therefore working with inequalities + if(latestVoteCheck==false&&(double)revisionNode.getProperty("previousVote")>-1){ Loggings.getLogs(className).info(revisionNode.getProperty("revid")+" of "+revisionNode.getProperty("Page")+" has--- "+revisionNode.getProperty("previousVote")); return (double)revisionNode.getProperty("previousVote"); } latestVoteCheck=false; if((int)revisionNode.getProperty("parentid")==0){ lastVote=simpleVote(graph,revid); revisionNode.setProperty("previousVote",lastVote); graph.commit(); Loggings.getLogs(className).info(revisionNode.getProperty("revid")+" of "+revisionNode.getProperty("Page")+" has--- "+lastVote); return lastVote; } else{ phi=getPhi(graph,revid); currVote=simpleVote(graph,revid); normalVote=((simpleVote(graph,revid)+phi*recursiveVotes(graph,(int)revisionNode.getProperty("parentid")))/(phi+1)); revisionNode.setProperty("previousVote",normalVote); graph.commit(); Loggings.getLogs(className).info(revisionNode.getProperty("revid")+" of "+revisionNode.getProperty("Page")+" has--- "+normalVote); return normalVote; } } /**This method will calculate the weighted average of votes of the current Revision Node * * @param graph OrientGraph object * @param revid Revision Id for the revision node under the calculation * @return The calculated Simple weighted average. */ public static double simpleVote(OrientGraph graph,int revid){ double denominator=0,numerator=0,simpleVote=0; Vertex userNode=null; Vertex revisionNode=graph.getVertices("revid",revid).iterator().next(); for(Edge reviewEdge:revisionNode.getEdges(Direction.IN,"@class","Review")){ //userNode=reviewEdge.getVertex(Direction.OUT); numerator+=(double)reviewEdge.getProperty("voteCredibility")*(double)reviewEdge.getProperty("vote"); denominator+=(double)reviewEdge.getProperty("vote"); } - if(denominator==0)denominator=1; + //denominator=1; + if(denominator>0) simpleVote=numerator/denominator; return simpleVote; } /** * This will calculate the parameter phi to scale the votes of the previous versions * @param graph OrientGraph object * @param revid Revision Id for the revision node under the calculation * @return The parameter phi */ public static double getPhi(OrientGraph graph,int revid){ double phi=0; double sizePrev=0,newEdits=0,currSize=0; Vertex revisionNode=graph.getVertices("revid",revid).iterator().next(); Vertex parentNode =graph.getVertices("revid",(int)revisionNode.getProperty("parentid")).iterator().next(); sizePrev=(int)parentNode.getProperty("size"); currSize=(int)revisionNode.getProperty("size"); newEdits=Math.abs(sizePrev-currSize); - if(sizePrev==0)sizePrev=1; + //sizePrev=1; + if(sizePrev>0) phi=Math.pow(Math.E,-1*(Math.pow(newEdits/sizePrev, PHI_POWER_PARAMETER))); return phi; } /** * This method will compute the no of Votes given to a particular page * for all the pages * @param graph OrientGraph */ public static void getTotalVotes(OrientGraph graph){ long totalVotes=0; OrientVertex revisionNode=null; for (Vertex pageNode : graph.getVertices("@class","Page")) { totalVotes=0; revisionNode=(OrientVertex)pageNode.getEdges(Direction.OUT, "@class", "PreviousVersionOfPage").iterator().next().getVertex(Direction.IN); while((int)revisionNode.getProperty("parentid")!=0){ totalVotes+=revisionNode.countEdges(Direction.IN, "@class","Review"); revisionNode=(OrientVertex) graph.getVertices("revid", (int)revisionNode.getProperty("parentid")).iterator().next(); Loggings.getLogs(className).info(revisionNode.getProperty("revid")); } totalVotes+=revisionNode.countEdges(Direction.IN, "@class","Review"); Loggings.getLogs(className).info(pageNode.getProperty("title")+" "+totalVotes); //Adding the totalVotes into the DB for faster retrieval pageNode.setProperty("totalVotes", totalVotes); graph.commit(); } } } diff --git a/WikiRating/src/main/java/controllers/WikiUtil.java b/WikiRating/src/main/java/controllers/WikiUtil.java index 2ea1768..4f85a26 100644 --- a/WikiRating/src/main/java/controllers/WikiUtil.java +++ b/WikiRating/src/main/java/controllers/WikiUtil.java @@ -1,204 +1,208 @@ package main.java.controllers; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.UnsupportedEncodingException; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import org.wikidata.wdtk.wikibaseapi.ApiConnection; import com.tinkerpop.blueprints.Vertex; import com.tinkerpop.blueprints.impls.orient.OrientEdge; import com.tinkerpop.blueprints.impls.orient.OrientGraph; import main.java.utilities.Connections; import main.java.utilities.Loggings; /**This class contains various utilities methods for the other classes * */ public class WikiUtil { static Class className=WikiUtil.class; /** * This method converts an InputStream object to String * @param in InputStream object to be converted * @return Converted String */ public static String streamToString(InputStream in) { String result = ""; BufferedReader reader = null; try { reader = new BufferedReader(new InputStreamReader(in, "UTF-8")); } catch (UnsupportedEncodingException e1) { e1.printStackTrace(); } StringBuilder builder = new StringBuilder(); String line; try { + + if(reader!=null){ + while ((line = reader.readLine()) != null) { builder.append(line); } + } result = builder.toString(); in.close(); } catch (IOException e) { Loggings.getLogs(className).error(e); } return result; } /** * This method constructs the MAP of parameters to attach with the MediaWiki Query to fetch all the pages * residing in all the namespaces * @param ns The namespace whose pages are requested * @return Map having parameters */ public static Map getPageParam(String ns) { Map queryParameterMap = new HashMap(); queryParameterMap.put("action", "query"); queryParameterMap.put("list", "allpages"); queryParameterMap.put("apfrom", "a"); queryParameterMap.put("aplimit", "max"); queryParameterMap.put("apnamespace", ns); queryParameterMap.put("format", "json"); return queryParameterMap; } /** * This method constructs the MAP of parameters to attach with the MediaWiki Query to fetch all the revisions * of the given page * @param pid The PageID of the page for which revisions are requested * @return Map having parameters */ public static Map getRevisionParam(String pid) { Map queryParameterMap = new HashMap(); queryParameterMap.put("action", "query"); queryParameterMap.put("prop", "revisions"); queryParameterMap.put("pageids", pid); queryParameterMap.put("rvprop", "userid|ids|timestamp|user|flags|size"); queryParameterMap.put("rvlimit", "max"); queryParameterMap.put("rvdir", "newer"); queryParameterMap.put("format", "json"); return queryParameterMap; } /** * This method constructs the MAP of parameters to attach with the MediaWiki Query to get * all the backlinks for the specified page * @param pid The PageID of the page for which backlinks are requested * @return Map having parameters */ public static Map getLinkParam(String pid) { Map queryParameterMap = new HashMap(); queryParameterMap.put("action", "query"); queryParameterMap.put("list", "backlinks"); queryParameterMap.put("blpageid", pid); queryParameterMap.put("blfilterredir", "all"); queryParameterMap.put("bllimit", "max"); queryParameterMap.put("format", "json"); return queryParameterMap; } /** * This method constructs the MAP of parameters to attach with the MediaWiki Query to get * all the users * @param username username to continue from in case the results are more than 500 * @return Map having parameters */ public static Map getUserParam(String username) { Map queryParameterMap = new HashMap(); queryParameterMap.put("action", "query"); queryParameterMap.put("list", "allusers"); queryParameterMap.put("aulimit", "max"); queryParameterMap.put("aufrom", username); queryParameterMap.put("rawcontinue", ""); queryParameterMap.put("format", "json"); return queryParameterMap; } /** * This method constructs the MAP of parameters to attach with the MediaWiki Query to get * all the contributions by the specified User * @param username Username for whom the contributions have to be fetched * @return Map having parameters */ public static Map getUserContriParam(String username) { Map queryParameterMap = new HashMap(); queryParameterMap.put("action", "query"); queryParameterMap.put("list", "usercontribs"); queryParameterMap.put("uclimit", "max"); queryParameterMap.put("ucdir", "newer"); queryParameterMap.put("ucuser", username); queryParameterMap.put("ucshow", "!minor"); queryParameterMap.put("ucprop", "sizediff|title|ids|flags"); queryParameterMap.put("format", "json"); return queryParameterMap; } /** * This method sends a POST request to MediaWiki API and then gets back an InputStream * @param con The ApiConnection object * @param queryParameterMap The Map having all the query parameters * @return InputStream object having the requested data */ public static InputStream reqSend(ApiConnection con, Map queryParameterMap) { InputStream in = null; try { in = con.sendRequest("POST", queryParameterMap); } catch (IOException e) { Loggings.getLogs(className).error(e); } return in; } /** * This method will check for the duplicate entities in the database * @param key The name of the class for which redundancy needs to be checked * @param value The value to be checked * @param graph OrientGraph object * @return true or false depending on whether entity is absent or present respectively */ public static boolean rCheck(String key, int value, OrientGraph graph) { Iterable checkNode = graph.getVertices(key, value); Iterator it = checkNode.iterator(); if (it.hasNext()) { return false; } else return true; } /** * This method prints all the pages * @return A formatted string containing all the Page names */ public static String printVertex() { String result = ""; OrientGraph graph = Connections.getInstance().getDbGraph(); for (Vertex pageNode : graph.getVertices("@class", "Page")) { result = result + " \n" + pageNode.getProperty("title"); } return result; } }