Order Now  +1 678 648 4277 

Implementing an Algorithm for Page Ranking in Java assignment help

The goal of this task was to implement a well-known Page Rank algorithm in Java. Our Java assignment helpers must read all the necessary web-page data from a given sample file and process with Page Rank algorithm to determine the order of these pages by relevance. Necessary algorithm parameters such as convergence delta, iterations number, etc, are provided. The algorithm must also output intermediate information during the algorithm run.

Web-page Ranking in Java

PageRank.java import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; import java.util.Comparator; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Scanner; import java.util.Set; public class PageRank { private static final String TEST_FILE_NAME = "CS.txt"; private static final int ITERATIONS = 10; private static final int SEARCH_RESULT_LIMIT = 7; private static final double D = 0.15; private Map pageByUrl = new HashMap<>(); public PageRank(String fileName) throws RuntimeException { try { BufferedReader br = new BufferedReader(new FileReader(fileName)); Page currPage = null; Map> links = new HashMap<>(); String line = ""; while ((line = br.readLine()) != null) { if ("PAGE".equals(line)) { currPage = new Page(); currPage.url = br.readLine(); currPage.content = br.readLine().toLowerCase(); pageByUrl.put(currPage.url, currPage); links.put(currPage.url, new HashSet<>()); } else if (currPage != null) { links.get(currPage.url).add(line); } } br.close(); for (Map.Entry> pair : links.entrySet()) { Page source = pageByUrl.get(pair.getKey()); for (String s : pair.getValue()) { Page ref = pageByUrl.get(s); if (ref != null) { ref.addReferencedBy(source); source.addRegerence(ref); } } } } catch (IOException ioe) { throw new RuntimeException(ioe); } } public void search(String[] terms) { List pages = hit(terms); int N = pages.size(); System.out.println("There were " + N + " hits"); for (Page page : pages) { page.rank = 1.0/N; } for (int i = 0; i tempRank = new HashMap<>(); for (Page page : pages) { double newRank = (1-D)/N; for (Page refBy : page.referencedBy) { newRank += D*refBy.rank/refBy.references.size(); } tempRank.put(page, newRank); } for (Page page : pages) { page.rank = tempRank.get(page); } } pages.sort(new Comparator() { @Override public int compare(Page arg0, Page arg1) { if (arg0.rank - arg1.rank < 0) return 1; if (arg0.rank - arg1.rank > 0) return -1; return 0; } }); System.out.println(String.format("%-10s%-20s", "Rank","URL")); for (int i = 0; i hit (String[] terms) { List result = new ArrayList<>(); for (Page page : pageByUrl.values()) { if (page.isHit(terms)) { result.add(page); } } return result; } class Page { String url; String content; Set referencedBy = new HashSet<>(); Set references = new HashSet<>(); double rank; public boolean isHit(String[] terms) { for (String term : terms) { if (!content.contains(term.toLowerCase())) return false; } return true; } public void addReferencedBy(Page page) { referencedBy.add(page); } public void addRegerence(Page page) { references.add(page); } } public static void main(String[] args) { PageRank pageRank = new PageRank(TEST_FILE_NAME); Scanner scanner = new Scanner(System.in); System.out.println("Enter your search terms: "); String line = scanner.nextLine(); String[] terms = line.split(" "); scanner.close(); pageRank.search(terms); } }