gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 10:24:16 25 4
gpt4 key购买 nike

下面的代码从文件中获取内容并尝试生成关系图。我有一个类,它的方法可以从文件中读取内容并返回一个 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com