diff --git a/WikiRating/src/main/java/computations/BadgeGenerator.java b/WikiRating/src/main/java/computations/BadgeGenerator.java index 2aacac8..10c0ae6 100644 --- a/WikiRating/src/main/java/computations/BadgeGenerator.java +++ b/WikiRating/src/main/java/computations/BadgeGenerator.java @@ -1,169 +1,169 @@ package main.java.computations; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import com.tinkerpop.blueprints.Vertex; import com.tinkerpop.blueprints.impls.orient.OrientGraph; import main.java.utilities.Connections; /** * This class will calculate the Badges that will be assigned * to the Pages based on the analysis of Page Rating distribution */ public class BadgeGenerator { //These varibales will store the computed cutoffs for the various badges static double platinumBadgeRatingCutoff; static double goldBadgeRatingCutoff; static double silverBadgeRatingCutoff; static double bronzeBadgeRatingCutoff; static double stoneBadgeRatingCutoff; /** * This enum has the percentile ranges for various badges */ public enum Badges { PLATINUM_BADGE_START_PERCENTILE(80), GOLD_BADGE_START_PERCENTILE(60), SILVER_BADGE_START_PERCENTILE(40), BRONZE_BADGE_START_PERCENTILE(20), STONE_BADGE_START_PERCENTILE(0); private int value; Badges(int value) { this.value = value; } public int getValue() { return value; } } /** * This class will store our PageObjects to insert into Arraylist for * percentile calculations */ public class PageRatingData{ int pid; double pageRating; String pageName; public PageRatingData(int pid,double pageRating,String pageName) { this.pid=pid; this.pageRating=pageRating; this.pageName=pageName; } } /** - * This is the custome comparator to sort the pageList in the ascending + * This is the custom comparator to sort the pageList in the ascending * order of PageRatings */ class PageRatingComparator implements Comparator{ @Override public int compare(PageRatingData pageRating1, PageRatingData pageRating2) { if(pageRating1.pageRating>pageRating2.pageRating) return 1; else return -1; } } /** * This method will assign badges based on the percentile */ public void generateBadges(){ ArrayList pageList=new ArrayList(); OrientGraph graph = Connections.getInstance().getDbGraph(); String badgeName=""; int currentPageID=0; double currentPageRating=0; String currentPageName=""; for(Vertex pageNode:graph.getVertices("@class","Page")){ currentPageID=pageNode.getProperty("pid"); currentPageRating=pageNode.getProperty("PageRating"); currentPageName=pageNode.getProperty("title"); pageList.add(new PageRatingData(currentPageID, currentPageRating,currentPageName)); } Collections.sort(pageList,new PageRatingComparator()); calculateBadgeCutoff(pageList); for(PageRatingData currentPage:pageList){ badgeName=getBadgeName(currentPage.pageRating); System.out.println(currentPage.pageName + " ------with ratings= "+currentPage.pageRating+" earned "+badgeName); } } /** * This method will calculate the cutoff for the various badges * @param pageList The Arraylist containg Page Objects */ public static void calculateBadgeCutoff(ArrayList pageList){ int noOfPages=pageList.size(); int platinumPageIndex; int goldPageIndex; int silverPageIndex; int bronzePageIndex; int stonePageIndex; //Storing index where the cutoff of badges start to get the respective cutoffs platinumPageIndex=(int)(noOfPages*(Badges.PLATINUM_BADGE_START_PERCENTILE.value/100.00)); goldPageIndex=(int)(noOfPages*(Badges.GOLD_BADGE_START_PERCENTILE.value/100.00)); silverPageIndex=(int)(noOfPages*(Badges.SILVER_BADGE_START_PERCENTILE.value/100.00)); bronzePageIndex=(int)(noOfPages*(Badges.BRONZE_BADGE_START_PERCENTILE.value/100.00)); stonePageIndex=(int)(noOfPages*(Badges.STONE_BADGE_START_PERCENTILE.value/100.00)); //Storing cutoffs platinumBadgeRatingCutoff=pageList.get(platinumPageIndex).pageRating; goldBadgeRatingCutoff=pageList.get(goldPageIndex).pageRating; silverBadgeRatingCutoff=pageList.get(silverPageIndex).pageRating; bronzeBadgeRatingCutoff=pageList.get(bronzePageIndex).pageRating; stoneBadgeRatingCutoff=pageList.get(stonePageIndex).pageRating; System.out.println("Index "+platinumPageIndex+"marks platinum cutoff -------"+platinumBadgeRatingCutoff); System.out.println("Index "+goldPageIndex+"marks gold cutoff------"+goldBadgeRatingCutoff); System.out.println("Index "+silverPageIndex+"marks silver cutoff------"+silverBadgeRatingCutoff); System.out.println("Index "+bronzePageIndex+"marks bronze cutoff------"+bronzeBadgeRatingCutoff); System.out.println("Index "+stonePageIndex+"marks stone cutoff------"+stoneBadgeRatingCutoff); } /** * This method will pick the badge according to the passed pageRating * @param pageRating PageRating of the page under consideration * @return The name of the Badge earned */ public static String getBadgeName(double pageRating){ if(pageRating>=platinumBadgeRatingCutoff) return "PLATINUM"; else if(pageRating>=goldBadgeRatingCutoff) return "GOLD"; else if(pageRating>=silverBadgeRatingCutoff) return "SILVER"; else if(pageRating>=bronzeBadgeRatingCutoff) return "BRONZE"; else return "STONE"; } } diff --git a/WikiRating/src/main/java/computations/UserCredibility.java b/WikiRating/src/main/java/computations/UserCredibility.java index 4ea37d4..7f06cb0 100644 --- a/WikiRating/src/main/java/computations/UserCredibility.java +++ b/WikiRating/src/main/java/computations/UserCredibility.java @@ -1,114 +1,114 @@ package main.java.computations; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import com.tinkerpop.blueprints.Direction; import com.tinkerpop.blueprints.Edge; import com.tinkerpop.blueprints.Vertex; import com.tinkerpop.blueprints.impls.orient.OrientGraph; import main.java.utilities.Connections; import main.java.utilities.PropertiesAccess; /** * This class will deal with the calculations of User Credibility */ public class UserCredibility { final static double USER_CONTRI_IMPORTANCE_PARAMETER=Double.parseDouble(PropertiesAccess.getParameterProperties("USER_CONTRI_IMPORTANCE_PARAMETER")); final static double USER_VOTE_IMPORTANCE_PARAMETER=Double.parseDouble(PropertiesAccess.getParameterProperties("USER_VOTE_IMPORTANCE_PARAMETER")); /** *This method will compute the credibility for all the Users */ public static void getUserCredibility(){ OrientGraph graph = Connections.getInstance().getDbGraph(); double alpha=0,relativeUserContribution=0,voteDeviation=0,credibility=0; HashMap pageEditMap=Contribution.getPageEdits(); //To iterate over all the Users for getting their respective Credibility try{ for(Vertex userNode:graph.getVertices("@class", "User")){ relativeUserContribution=getRelativeUserContribution(userNode,graph,pageEditMap); voteDeviation=getVoteDeviation(userNode,graph); alpha=(USER_CONTRI_IMPORTANCE_PARAMETER*relativeUserContribution+USER_VOTE_IMPORTANCE_PARAMETER*voteDeviation)/(USER_CONTRI_IMPORTANCE_PARAMETER+USER_VOTE_IMPORTANCE_PARAMETER); credibility=alpha; userNode.setProperty("credibility",credibility); System.out.println(userNode.getProperty("username")+" has "+credibility); graph.commit(); } }catch(Exception e){e.printStackTrace();} //graph.commit(); graph.shutdown(); } /** * This method calculates the parameter 'a'(relativeUserContribution) for credibility calculation * @param userNode The Vertex of the User class whose credibility is being calculated * @param graph OrientGraph object * @param pageEditMap HashMap containing all the edits and their corresponding pid * @return The value of parameter 'a' */ public static double getRelativeUserContribution(Vertex userNode,OrientGraph graph,HashMap pageEditMap){ HashMap userPageContributions=new HashMap(); int contpid=0,countContribution=0; double userEdits=0,totalEdits=1,finalPageVote=0; double userPageContributionsTemp=0,userPageContributionsTotal=0; int contributionSize=0; for(Edge contributeEdge:userNode.getEdges(Direction.OUT,"@class","Contribute")){ contpid=(int)graph.getVertices("title",contributeEdge.getVertex(Direction.IN).getProperty("Page").toString()).iterator().next().getProperty("pid"); contributionSize=contributeEdge.getProperty("contributionSize"); if(userPageContributions.containsKey(contpid)){ contributionSize+=(int)userPageContributions.get(contpid); userPageContributions.put(contpid,(Integer)contributionSize); } else { userPageContributions.put(contpid,(Integer)contributionSize); } } Iterator it = userPageContributions.entrySet().iterator(); while (it.hasNext()) { Map.Entry pair = (Map.Entry) it.next(); contpid=(int)pair.getKey(); userEdits=(int)userPageContributions.get(contpid); totalEdits=(int)pageEditMap.get(contpid); finalPageVote=graph.getVertices("pid",contpid).iterator().next().getProperty("currentPageVote"); if(totalEdits==0)totalEdits=1; userPageContributionsTemp=(finalPageVote*userEdits/totalEdits); userPageContributionsTotal+=userPageContributionsTemp; countContribution++; } if(countContribution==0)countContribution=1; return userPageContributionsTotal/countContribution; } /** * This method calculates the parameter 'b'(voteDeviation) for credibility calculation * @param userNode The Vertex of the User class whose credibility is being calculated * @param graph OrientGraph object * @return The value of parameter 'b' */ public static double getVoteDeviation(Vertex userNode,OrientGraph graph){ - double bTemp=0,bTotal=0,userVote,versionVote; + double voteDeviationTemp=0,voteDeviationTotal=0,userVote,versionVote; int countReview=0; try{ for(Edge reviewEdge:userNode.getEdges(Direction.OUT,"@class","Review")){ userVote=reviewEdge.getProperty("vote"); versionVote=reviewEdge.getVertex(Direction.IN).getProperty("previousVote"); - bTemp=1-Math.abs(userVote-versionVote); - bTotal+=bTemp; + voteDeviationTemp=1-Math.abs(userVote-versionVote); + voteDeviationTotal+=voteDeviationTemp; countReview++; } }catch(Exception e){e.printStackTrace();} if(countReview==0)countReview=1; - return bTotal/countReview; + return voteDeviationTotal/countReview; } } diff --git a/WikiRating/src/main/java/controllers/Firstrun.java b/WikiRating/src/main/java/controllers/Firstrun.java index 1218108..7f6980e 100644 --- a/WikiRating/src/main/java/controllers/Firstrun.java +++ b/WikiRating/src/main/java/controllers/Firstrun.java @@ -1,86 +1,90 @@ package main.java.controllers; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.core.Response; import main.java.computations.BadgeGenerator; import main.java.computations.NormalisedVotes; import main.java.computations.PageRating; import main.java.computations.Pagerank; import main.java.computations.RandomVoteGenerator; import main.java.computations.Reliability; import main.java.computations.UserCredibility; -import main.java.models.*; -import main.java.utilities.AllowedNamespaces; +import main.java.models.InitialiseDB; +import main.java.models.LinkPages; +import main.java.models.LinkUserContributions; +import main.java.models.Page; +import main.java.models.Revision; +import main.java.models.User; /** * This class will be used to initialize the engine. * */ @Path("firstRun") public class Firstrun { @GET @Produces("application/json") // Warning: If you add revisions before the Users, only those users who have // not contributed to Wiki will be added. // However this behaviour can be inverted too /** * This method will call different other methods that will initialize the engine * @return Response object showing time taken to run the computation */ public Response pCompute() { long startTime = System.currentTimeMillis(); InitialiseDB.createClass(); System.out.println("==================Classes creation over====================="); Page.insertPages(); System.out.println("==================Page insertion over====================="); LinkPages.linkAll("@class","Page"); System.out.println("==================Page linking over====================="); User.insertAllUsers(); System.out.println("==================All Users inserted====================="); Revision.getAllRevisions("@class","Page"); System.out.println("==================Page Revisions over====================="); Pagerank.pageRankCompute(); System.out.println("==================Page rank over====================="); LinkUserContributions.linkAll(); System.out.println("==================All Users Linked====================="); RandomVoteGenerator.generateVotes(); System.out.println("==================All Versions voted====================="); NormalisedVotes.calculatePageVotes(); System.out.println("==================All Page Votes computed====================="); UserCredibility.getUserCredibility(); System.out.println("==================User Credibility computed====================="); Reliability.calculateReliability(); System.out.println("==================Vote Reliability computed====================="); PageRating.computePageRatings(); System.out.println("==================Page Ratings computed====================="); new BadgeGenerator().generateBadges(); System.out.println("==================Badges given====================="); long estimatedTime = System.currentTimeMillis() - startTime; estimatedTime = estimatedTime / 60000; return Response.status(200).entity("Successful and took" + estimatedTime + "Minutes").build(); } } diff --git a/WikiRating/src/main/java/controllers/Secondrun.java b/WikiRating/src/main/java/controllers/Secondrun.java index ccee30e..e12a192 100644 --- a/WikiRating/src/main/java/controllers/Secondrun.java +++ b/WikiRating/src/main/java/controllers/Secondrun.java @@ -1,63 +1,78 @@ package main.java.controllers; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.core.Response; import main.java.computations.BadgeGenerator; import main.java.computations.NormalisedVotes; +import main.java.computations.PageRating; +import main.java.computations.Pagerank; import main.java.computations.Reliability; +import main.java.computations.UserCredibility; import main.java.models.AddNewPages; import main.java.models.User; /** * This class will be used for detecting and storing any new changes like * Page addition, Page modification and User Addition to the platform. */ @Path("secondRun") public class Secondrun { @GET @Produces("application/json") /** * This method will call suitable methods to insert all the new users and * update the database for any new changes * @return Response object showing time taken to run the computation */ public Response pCompute() { long startTime = System.currentTimeMillis(); /*Now we will check for new pages and add revisions to them too. *Make links to the user contributions too *Calculate the user votes and then calculate the recursive votes too. *We will calculate the backlinks too *Drop backlinks and then create the new ones again *Calculate the votes *Calculate the user reliability */ User.insertAllUsers(); System.out.println("==================Checked for new User's insertion====================="); AddNewPages.checkForPages(); System.out.println("==================Checked for any new pages,revisions and linked the user contributions and made backlinks====================="); NormalisedVotes.calculatePageVotes(); System.out.println("==================Calculated new page votes====================="); Reliability.calculateReliability(); System.out.println("==================Calculated new reliabilities====================="); + Pagerank.pageRankCompute(); + System.out.println("==================Page rank over====================="); + + UserCredibility.getUserCredibility(); + System.out.println("==================User Credibility computed====================="); + + PageRating.computePageRatings(); + System.out.println("==================Page Ratings computed====================="); + + new BadgeGenerator().generateBadges(); + System.out.println("==================Badges given====================="); + long estimatedTime = System.currentTimeMillis() - startTime; estimatedTime = estimatedTime / 60000; return Response.status(200).entity("Successful and took" + estimatedTime + "Minutes").build(); } }