生成图形时文件数据分析不完整

时间:2019-01-08 12:14:15

标签: java graph

以下代码从文件获取内容,并尝试生成关系图。我有一个带有从文件中读取内容并返回ArrayList的方法的类,其中每个项目代表一行文本。我已经测试了相同的方法,并返回了文本文件中所有行的ArrayList。

input.txt
Dangal / Aamir Khan / Fatima Sana
Sanju / Ranbir Kapoor / Dia Mirza
PK / Aamir Khan / Anushka Sharma
Munna Bhai MBBS / Sanjay Dutt / Arshad Warsi
Zindagi Na Milegi Dobara / Farhan Akhtar / Katrina Kaif

但是,在下面的代码中处理文件内容时,我得到的输出如下:

null
Total number of unique actors/actresses: 2
List of unique actors/actresses: 
Aamir Khan ,  Fatima Sana, 
Total number of unique movies: 2
List of unique movies: 
Dangal , Sanju , 

我相信我在逻辑上犯了一些错误。

public class GenerateGraph {

//List of all unique actors
private List<ActorVertex> allactors = new ArrayList<>();

public List<ActorVertex> getAllactors() {
    return allactors;
}

//List of all unique movies
private List<MovieEdge> allmovies= new ArrayList<>();

public List<MovieEdge> getAllmovies() {
    return allmovies;
}


public void parseFileContent() {

    /* This method takes each line of the input.txt and creates corresponding vertices(actors) and Edges(movies) and links them to each other to construct the graph. */

    ArrayList<String> lines = new FileInput().readFile();

    try {

        for(String line:lines) {
            /* This entire loop iterates for each line in the input.txt */

            MovieEdge me=null;
            ActorVertex av1=null;
            ActorVertex av2=null;

            String[] elements = line.split("/"); //Splitting each line with delimiter '/' to extract movie and actor/actress names.

            /* Note: For each line elements[0]=movie, elements[1]=actor1, elements[2]=actor2 */

            //Creating and adding movie edge

            if (allmovies.size()!=0) { //Assuming that this isn't the 1st iterations. Therefore, we need to ensure duplicate entries are not recorded.
                for (MovieEdge m:allmovies) {
                    if (m.getName()==elements[0]) {
                        return;
                    }else {
                        me = new MovieEdge(elements[0]); //Movie does not exists. So new movie edge object is created.
                        allmovies.add(me); // added to master list for all movies
                    }
                  }
                }else if (allmovies.size()==0){
                    me = new MovieEdge(elements[0]);
                    allmovies.add(me);
                }

            //Creating and adding actor vertices
            if (allactors.size()!=0) {  //Assuming that this isn't the 1st iterations. Therefore, we need to ensure duplicate entries are not recorded.
                for (ActorVertex v:allactors) {
                  if (v.getName()==elements[1]) {
                    av1=v; // If actor already exists then no new actor object will be created. Instead we'll refer to the existing ones.
                  }else {
                    av1 = new ActorVertex(elements[1]); //actor does not exists. So new actor object is created.
                    allactors.add(av1); // added to master list for all actors
                  }

                  if (elements[2]!=null && elements[1]!=elements[2]) { /* Applies to situation where two successive actor names are the same or the second actor does not exists. 
                                                                        * In this case only 1 actor object will be created. */
                    if (v.getName()==elements[2]) { // check whether actor 2 already exists in the master list. If so use the existing one.             
                        av2=v;
                    }else {
                        av2 = new ActorVertex(elements[2]); //Actor2 does not exists. So creating actor object.
                        allactors.add(av2); //added actor2 object to master list all actors.
                    }
                }
              }

            }else if(allactors.size()==0 && elements[1]!=elements[2]){ /*Applies if this is the 1st iteration and master lists for movies and actors are empty.
                                                                        *Also, once again checking for duplicates */    

                //creating new actor vertex objects and adding them to master list - all actors
                av1 = new ActorVertex(elements[1]); 
                av2 = new ActorVertex(elements[2]);
                allactors.add(av1);
                allactors.add(av2);

            }else if (allactors.size()==0 && elements[1]==elements[2]) { //If duplicate entries are found. Will create only 1 actor vertex object.
                av1 = new ActorVertex(elements[1]);
                allactors.add(av1);
            }

            /*** Joining the actor vertices with movie edge to construct the graph for each line ***/

            /* Associating actors/actresses with movie in which they have played a role */
            me.joinActorVertex(av1); //associating 1st actor/actress with its corresponding movie
            if(av2!=null) { // checking if actor2 exists for the movie. If so the associate him/her with the movie. 
                me.joinActorVertex(av2);
            }

            /*Linking corresponding movie edge to actors */
            av1.addMovieEdge(me);
            if(av2!=null) { // Again, checking if actor2 exists for the movie. If so the associate him/her with the movie. 
                av2.addMovieEdge(me);
            }

         }

        }catch(Exception e) {
            System.out.println(e.getMessage());

    }

    }
    }

先谢谢了。感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

我试图通过介绍查找电影或演员的方法来使其更短一些:

public void parseFileContent() {
    List<String> lines = new FileInput().readFile();

    try {
        for (String line : lines) {
            String[] elements = line.split("/"); 
            /* Note: For each line elements[0]=movie, elements[1]=actor1, elements[2]=actor2 */

            // Creating and adding movie edge
            MovieEdge me = findMovieByName(allmovies, elements[0]);
            if (me == null) {
                me = new MovieEdge(elements[0]);
                allmovies.add(me);
            }

            // Creating and adding actor vertices
            ActorVertex av1 = findActorByName(allactors, elements[1]);
            if (av1 == null) {
                av1 = new ActorVertex(elements[1]);
                allactors.add(av1);
            }
            ActorVertex av2 = findActorByName(allactors, elements[2]);
            if (av2 == null) {
                av2 = new ActorVertex(elements[2]);
                allactors.add(av2);
            }

            /*** Joining the actor vertices with movie edge to construct the graph for each line ***/

            /* Associating actors/actresses with movie in which they have played a role */
            me.joinActorVertex(av1); // associating 1st actor/actress with its corresponding movie
            if (av2 != null) { // checking if actor2 exists for the movie. If so the associate him/her with the
                               // movie.
                me.joinActorVertex(av2);
            }

            /* Linking corresponding movie edge to actors */
            av1.addMovieEdge(me);
            if (av2 != null) { // Again, checking if actor2 exists for the movie. If so the associate him/her with
                               // the movie.
                av2.addMovieEdge(me);
            }

        }

    } catch (Exception e) {
        System.out.println(e.getMessage());

    }

}

private ActorVertex findActorByName(List<ActorVertex> avs, String name) {
    for (ActorVertex av : avs) {
        if (av.getName().equals(name)) {
            return av;
        }
    }
    return null;
}

public MovieEdge findMovieByName(List<MovieEdge> mes, String name) {
    for (MovieEdge me : mes) {
        if (me.getName().equals(name)) {
            return me;
        }
    }
    return null;
}