java - 生成图表时文件数据解析不完整

标签 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());

    }

    }
    }

提前致谢。感谢您的帮助。

最佳答案

我试图通过引入查找电影或 Actor 的方法来使其更短:

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;
}

关于java - 生成图表时文件数据解析不完整,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54091603/

相关文章:

java - 为什么我的对象不会死?

java - 在 Eclipse 中放置配置文件的正确位置

java - 仅当字符串包含每个列表中的单词时才匹配的正则表达式

graph - 在有向加权图中找到平均权重最高的树

algorithm - 在生成树中找到从单个源到所有其他节点的最短路径的最佳算法

ios - 了解 numberForPlot : and numberOfRecordsForPlot: Core Plot

java - Spring mvc表单预填充: list with preferences

c# - 从 C# 控制台应用程序杀死 Java 进程

algorithm - 统计/算法 : How do I compare a weekly graph with its own history to see when in the past it was almost the same?

python - 在 matplotlib 中设置颜色条范围