Advertisement
brcuce132

Untitled

May 14th, 2019
695
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.30 KB | None | 0 0
  1. import java.io.IOException;
  2. import java.util.*;
  3.  
  4. import com.opencsv.bean.CsvBindByName;
  5. import com.opencsv.bean.CsvToBean;
  6. import com.opencsv.bean.CsvToBeanBuilder;
  7. import java.io.*;
  8. import java.nio.file.Files;
  9. import java.nio.file.Paths;
  10. import graph.Graph;
  11.  
  12. /** Parser utility to load the Marvel Comics dataset. */
  13. public class MarvelParser {
  14. public static void main(String[] args) {
  15. MarvelParser newPaser = new MarvelParser();
  16. Graph newGraph =newPaser.creatGraph("/Users/xuanhezhou/Downloads/cse331-19sp-xuanhz/src/main/resources/marvel/data/marvel.tsv");
  17. for(Graph.GraphNode heros:newGraph.getNodesSet()){
  18. System.out.println(heros.getName());
  19. }
  20.  
  21. }
  22.  
  23. /**
  24. * Reads the Marvel Universe dataset. Each line of the input file contains a character name and a
  25. * comic book the character appeared in, separated by a tab character
  26. *
  27. * @spec.requires filename is a valid file path
  28. * @param filename the file that will be read
  29. * @return a map <book, set of hero in the book>
  30. */
  31. // TODO: Pick your return type and document it
  32. public static Map<String,Set<String>> parseData(String filename) {
  33. try {
  34. Reader reader = Files.newBufferedReader(Paths.get(filename));
  35. CsvToBean<UserModel> csvToBean = new CsvToBeanBuilder<UserModel>(reader)
  36. .withType(UserModel.class)
  37. .withIgnoreLeadingWhiteSpace(true)
  38. .withSeparator('\t')
  39. .build();
  40.  
  41. Iterator<UserModel> csvUserIterator = csvToBean.iterator();
  42. Map<String, Set<String>> bookVScharacter = new HashMap<>();
  43. while (csvUserIterator.hasNext()) {
  44. UserModel csvUser = csvUserIterator.next();
  45. if(!bookVScharacter.containsKey(csvUser.getBook())){
  46. bookVScharacter.put(csvUser.getBook(),new HashSet<>());
  47. }
  48. bookVScharacter.get(csvUser.getBook()).add(csvUser.getHero());
  49. }
  50. return bookVScharacter;
  51. }
  52. catch(FileNotFoundException e) {
  53. e.printStackTrace();
  54. System.out.println(filename + ": file not found");
  55. System.exit(1);
  56. return null; // is it okay?
  57. }
  58. catch(IOException e) {
  59. e.printStackTrace();
  60. System.exit(1);
  61. return null;
  62. }
  63. }
  64. public Graph creatGraph(String filePath){
  65. Map<String, Set<String>> result = MarvelParser.parseData(filePath);
  66. Graph newGraph = new Graph();
  67. for(String book : result.keySet()){
  68. for(String hero: result.get(book)){
  69. Graph.GraphNode heroNode = newGraph. new GraphNode(hero);
  70. newGraph.addNode(heroNode);
  71. for(String childHero: result.get(book)){
  72. Graph.GraphNode childHeroNode = newGraph. new GraphNode(childHero);
  73. Graph.Edge newEdge = newGraph. new Edge(heroNode,childHeroNode,book);
  74. newGraph.addEdge(heroNode,childHeroNode,newEdge);
  75. }
  76. }
  77. }
  78. return newGraph;
  79. }
  80.  
  81. public static class UserModel{
  82. @CsvBindByName(column = "hero")
  83. private String hero;
  84.  
  85. @CsvBindByName(column = "book")
  86. private String book;
  87.  
  88. public String getHero(){ return this.hero; }
  89. public void setHero(String newHero){
  90. this.hero = newHero;
  91. }
  92. public String getBook(){
  93. return this.book;
  94. }
  95. public void setBook(String newBook){
  96. this.book = newBook;
  97. }
  98. }
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement