我正在尝试创建一个图表来读取一个txt文件并将这些单词放在一个图表中,但它不能重复(如果文件中有两个相同的单词)
这是我的代码(USING JUNG API)
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Scanner;
import java.util.Stack;
import edu.uci.ics.jung.graph.DirectedSparseGraph;
import edu.uci.ics.jung.graph.Graph;
import edu.uci.ics.jung.graph.SparseMultigraph;
import edu.uci.ics.jung.graph.util.EdgeType;
public class Main {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
Scanner scan = null;
/*i have a class named 'Palavra' that returns me a string(did that cause // i'll use the class to implement some things at the word) */
Stack<Palavra> palavras = new Stack<Palavra>(); // pilha para delimitar
// tamanho do grafo
// Read txt
try {
scan = new Scanner(new File("C:\\Users\\Auryon.AURYON-PC\\Desktop\\Aula1\\teste.txt"));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
/*int i = 0;*/
Graph<Palavra, Relacao> g = new SparseMultigraph<Palavra, Relacao>();
//我创建的最后一堂课(将是我的单词关系验证)
Relacao tipo_palavra = new Relacao("connect");
#这是问题
while (scan.hasNextLine()) {
String s = scan.next();
Palavra word = new Palavra(s);
if (palavras.contains(word)) {
s = scan.next();
} else {
palavras.push(word);
g.addVertex(word);
}
/*
* if (i > 0) { g.addEdge(tipo_palavra,word,palavras.lastElement());
* //multiple edges }
*/
}
System.out.println(g);
System.out.println(palavras.size());
}
}
MY NEW UPDATE
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Scanner;
import java.util.Stack;
import edu.uci.ics.jung.graph.DirectedSparseGraph;
import edu.uci.ics.jung.graph.Graph;
import edu.uci.ics.jung.graph.SparseMultigraph;
import edu.uci.ics.jung.graph.util.EdgeType;
public class Main {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
Scanner scan = null;
Stack<Palavra> palavras = new Stack<Palavra>(); // Graph Size Stack
// Read txt
try {
scan = new Scanner(new File("C:\\Users\\Auryon.AURYON-PC\\Desktop\\Aula1\\teste.txt"));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
Graph<Palavra, Relacao> g = new SparseMultigraph<Palavra, Relacao>();
Relacao tipo_palavra = new Relacao("connect");
String s = scan.next();
Palavra word = new Palavra(s);
palavras.push(word);
g.addVertex(word);
while (scan.hasNextLine()) {
s = scan.next();
word = new Palavra(s);
//THE REAL PROBLEM
g.addVertex(word);
g.addEdge(tipo_palavra, word, palavras.lastElement());
palavras.push(word);
}
System.out.println(g);
System.out.println(palavras.size());
}
}
答案 0 :(得分:1)
正如@DivDiff所说,你的问题是什么并不完全清楚,但听起来你期望输入文件中的单词数量与你创建的图形中的顶点数量相同(其中顶点是单词)。
简短版本是:只要文件中的单词只出现一次,它就会成立。如果图表中尚未显示word
并且您执行此操作:
boolean result1 = g.addVertex(word);
boolean result2 = g.addVertex(word);
然后result1
将true
(图表已修改),result2
将false
(图未被修改),如{{3}文档中所指定}}
这个约束 - 顶点必须是唯一的 - 允许顶点用作Graph的内部数据结构的键,更重要的是作为getNeighbors()等方法的参数。
更新:下面的评论表明您遇到的实际问题是您尝试使用不同的端点添加相同的边缘。
特别是,您有一个边缘对象(tipo_palavra
)并且您反复将该边添加到具有不同端点的图形中。这不起作用:每个边缘对象必须是唯一的。
如果边缘本身对你没有任何意义,除了作为连接两个顶点的方法,那么你有两个主要选择:
(1)为每对端点生成一个新的边缘对象,例如,使边缘为整数,并且每次都增加它:
g.addEdge(i++, word1, word2);
(2)使用不同的库来表示不需要明确边缘对象的图表,例如addVertex(在v20 RC1中可用),特别是图表类型。
答案 1 :(得分:0)
这是答案,我的图表也完成了工作:
import java.io.FileNotFoundException;
import java.awt.Dimension;
import java.io.File;
import java.io.IOException;
import java.util.Collection;
import java.util.HashMap;
import java.util.Scanner;
import java.util.Stack;
import javax.swing.JFrame;
import edu.uci.ics.jung.algorithms.layout.TreeLayout;
import edu.uci.ics.jung.algorithms.layout.CircleLayout;
import edu.uci.ics.jung.algorithms.layout.Layout;
import edu.uci.ics.jung.algorithms.layout.PolarPoint;
import edu.uci.ics.jung.graph.DirectedSparseGraph;
import edu.uci.ics.jung.graph.Graph;
import edu.uci.ics.jung.graph.SparseMultigraph;
import edu.uci.ics.jung.graph.util.EdgeType;
import edu.uci.ics.jung.visualization.BasicVisualizationServer;
import edu.uci.ics.jung.visualization.decorators.ToStringLabeller;
public class Main {
/**
*
* @param args
* @throws IOException
*
*/
// BEGIN DEEP SEARCH
Stack<Word> pilha;
public void buscaProfundidade(Graph<Word, Relacao> g1, Word n0) {
HashMap<Integer, Word> visitados = new HashMap<Integer, Word>();
pilha = new Stack<Word>();
pilha.add(n0);
while (!pilha.isEmpty()) {
Word n = pilha.pop();
marcarVisitado(n, visitados);
processarNo(n); // print
Collection<Word> adjacencias = g1.getNeighbors(n); // coleção dos
// adjacentes do nó
// atual
for (Word adjacente : adjacencias) {
if (!foiVisitado(adjacente, visitados)) {
pilha.push(adjacente);
}
}
}
// RENDERIZA GRAFO
Layout<Word, Relacao> layout = new CircleLayout<Word, Relacao>(g1);
layout.setSize(new Dimension(800, 600)); // sets the initial size of the
// space
// The BasicVisualizationServer<V,E> is parameterized by the edge types
BasicVisualizationServer<Word, Relacao> vv = new BasicVisualizationServer<Word, Relacao>(layout);
vv.setPreferredSize(new Dimension(800, 600)); // Sets the viewing area
// size
vv.getRenderContext().setVertexLabelTransformer(new ToStringLabeller());
vv.getRenderContext().setEdgeLabelTransformer(new ToStringLabeller());
vv.getRenderContext().setLabelOffset(20);
JFrame frame = new JFrame("WORD INDICATOR");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(vv);
frame.pack();
frame.setVisible(true);
}
private void processarNo(Word n) {
System.out.println(n);
}
boolean foiVisitado(Word adjacente, HashMap<Integer, Word> visitados) {
return visitados.containsKey(adjacente.getId());
}
private void marcarVisitado(Word n, HashMap<Integer, Word> visitados) {
visitados.put(n.getId(), n);
}
// DEEP SEARCH END
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
Scanner scan = null;
Stack<Word> words = new Stack<Word>();
// CRIA GRAFO
// Read txt
try {
scan = new Scanner(
new File("C:\\Users\\Auryon.AURYON-PC\\Documents\\GitHub\\java-works\\Graphs_JUNG\\teste.txt"));
// scan = new Scanner(new
// File("C:\\Users\\Auryon.AURYON-PC\\Desktop\\flatWiki.txt"));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
Graph<Word, Relacao> g = new SparseMultigraph<Word, Relacao>();
int i = 1;
Relacao word_type = new Relacao("connect");
String s = scan.next();
Word word = new Word(i, s, s, s, s);
words.push(word);
g.addVertex(word);
while (scan.hasNextLine()) {
i++;
s = scan.next(); // TODO:implementar classes separadas
/*
* s2 s3 s4
*/
word = new Word(i, s, s, s, s);
g.addVertex(word);
try {
word_type = new Relacao("");
g.addEdge(word_type, words.lastElement(), word, EdgeType.DIRECTED);
words.push(word);
} catch (Exception e) {
}
;
System.out.println(g);
System.out.println(words.size());
}
Main busca = new Main();
Collection<Word> vertices = g.getVertices();
Word p = vertices.iterator().next(); // parametro busca
busca.buscaProfundidade(g, p);
// TERMINA GRAFO
}
}