Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.IOException;
- import java.util.*;
- import com.opencsv.bean.CsvBindByName;
- import com.opencsv.bean.CsvToBean;
- import com.opencsv.bean.CsvToBeanBuilder;
- import java.io.*;
- import java.nio.file.Files;
- import java.nio.file.Paths;
- import graph.Graph;
- /** Parser utility to load the Marvel Comics dataset. */
- public class MarvelParser {
- public static void main(String[] args) {
- MarvelParser newPaser = new MarvelParser();
- Graph newGraph =newPaser.creatGraph("/Users/xuanhezhou/Downloads/cse331-19sp-xuanhz/src/main/resources/marvel/data/marvel.tsv");
- for(Graph.GraphNode heros:newGraph.getNodesSet()){
- System.out.println(heros.getName());
- }
- }
- /**
- * Reads the Marvel Universe dataset. Each line of the input file contains a character name and a
- * comic book the character appeared in, separated by a tab character
- *
- * @spec.requires filename is a valid file path
- * @param filename the file that will be read
- * @return a map <book, set of hero in the book>
- */
- // TODO: Pick your return type and document it
- public static Map<String,Set<String>> parseData(String filename) {
- try {
- Reader reader = Files.newBufferedReader(Paths.get(filename));
- CsvToBean<UserModel> csvToBean = new CsvToBeanBuilder<UserModel>(reader)
- .withType(UserModel.class)
- .withIgnoreLeadingWhiteSpace(true)
- .withSeparator('\t')
- .build();
- Iterator<UserModel> csvUserIterator = csvToBean.iterator();
- Map<String, Set<String>> bookVScharacter = new HashMap<>();
- while (csvUserIterator.hasNext()) {
- UserModel csvUser = csvUserIterator.next();
- if(!bookVScharacter.containsKey(csvUser.getBook())){
- bookVScharacter.put(csvUser.getBook(),new HashSet<>());
- }
- bookVScharacter.get(csvUser.getBook()).add(csvUser.getHero());
- }
- return bookVScharacter;
- }
- catch(FileNotFoundException e) {
- e.printStackTrace();
- System.out.println(filename + ": file not found");
- System.exit(1);
- return null; // is it okay?
- }
- catch(IOException e) {
- e.printStackTrace();
- System.exit(1);
- return null;
- }
- }
- public Graph creatGraph(String filePath){
- Map<String, Set<String>> result = MarvelParser.parseData(filePath);
- Graph newGraph = new Graph();
- for(String book : result.keySet()){
- for(String hero: result.get(book)){
- Graph.GraphNode heroNode = newGraph. new GraphNode(hero);
- newGraph.addNode(heroNode);
- for(String childHero: result.get(book)){
- Graph.GraphNode childHeroNode = newGraph. new GraphNode(childHero);
- Graph.Edge newEdge = newGraph. new Edge(heroNode,childHeroNode,book);
- newGraph.addEdge(heroNode,childHeroNode,newEdge);
- }
- }
- }
- return newGraph;
- }
- public static class UserModel{
- @CsvBindByName(column = "hero")
- private String hero;
- @CsvBindByName(column = "book")
- private String book;
- public String getHero(){ return this.hero; }
- public void setHero(String newHero){
- this.hero = newHero;
- }
- public String getBook(){
- return this.book;
- }
- public void setBook(String newBook){
- this.book = newBook;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement