diff --git a/FakeNews/bin/.gitignore b/FakeNews/bin/.gitignore index 6a85e2c16afb893d0358786b3b27ba03318cef1a..fd56ef212f1e5fc898978819e422c8521756aea3 100644 --- a/FakeNews/bin/.gitignore +++ b/FakeNews/bin/.gitignore @@ -1,2 +1 @@ -/Docs/ /br/ diff --git a/FakeNews/src/br/ufrn/imd/controle/ControladorArquivo.java b/FakeNews/src/br/ufrn/imd/controle/ControladorArquivo.java index 6dc37f6f5899661d81ebb68abd5b9660a7cc840d..806f4e2a8dca071a4a0d5a308e97613a60c6ce08 100644 --- a/FakeNews/src/br/ufrn/imd/controle/ControladorArquivo.java +++ b/FakeNews/src/br/ufrn/imd/controle/ControladorArquivo.java @@ -1,38 +1,82 @@ package br.ufrn.imd.controle; import java.io.FileNotFoundException; -import java.lang.reflect.Array; -import java.math.BigInteger; -import java.security.MessageDigest; -import java.security.NoSuchAlgorithmException; -import java.text.Collator; -import java.util.ArrayList; -import java.util.Collections; +import java.io.IOException; import java.util.HashMap; import br.ufrn.imd.modelo.ArqCsv; import br.ufrn.imd.modelo.ArqWeb; import br.ufrn.imd.modelo.Noticia; +import br.ufrn.imd.modelo.NoticiaCsv; +import br.ufrn.imd.modelo.NoticiaWeb; public class ControladorArquivo { private ArqCsv arqCsv; private ArqWeb arqWeb; - private HashMap<String , Noticia> Map; - - public ControladorArquivo() throws arquivoException { - Map = new HashMap<String, Noticia>(); + private HashMap<String , NoticiaCsv> mapCsv; + private HashMap<String , NoticiaWeb> mapWeb; + + public ControladorArquivo() throws FileNotFoundException { + mapCsv = new HashMap<String, NoticiaCsv>(); + mapWeb = new HashMap<String, NoticiaWeb>(); + arqCsv = new ArqCsv(); arqWeb = new ArqWeb(); - - try { - arqCsv = new ArqCsv(); - } catch (FileNotFoundException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } + } + + public ArqCsv getArqCsv() { + return arqCsv; + } + public void setArqCsv(ArqCsv arqCsv) { + this.arqCsv = arqCsv; } - + public ArqWeb getArqWeb() { + return arqWeb; + } + + public void setArqWeb(ArqWeb arqWeb) { + this.arqWeb = arqWeb; + } + + public HashMap<String, NoticiaCsv> getMapCsv() { + return mapCsv; + } + public void setMapCsv(HashMap<String, NoticiaCsv> mapCsv) { + this.mapCsv = mapCsv; + } + + public HashMap<String, NoticiaWeb> getMapWeb() { + return mapWeb; + } + + public void setMapWeb(HashMap<String, NoticiaWeb> mapWeb) { + this.mapWeb = mapWeb; + } + + public void armazenarCsv() throws IOException { + + + int linha = 1; + NoticiaCsv newCsv = new NoticiaCsv();; + arqCsv.abrir(); + + while(linha <= arqCsv.tamanhaCsv() ) { + + newCsv.setConteudo( arqCsv.ler(linha, 1) ); + System.out.println("Linha "+ linha + " Total "+ arqCsv.tamanhaCsv()+ "Cont" + arqCsv.ler(linha, 1)); + newCsv.Formatar(); + newCsv.setLink(arqCsv.ler(linha, 2)); + newCsv.setTime(arqCsv.ler(linha, 2)); + + mapCsv.put(Noticia.SHA( newCsv.getFormatado() ) , newCsv ) ; + + linha++; + arqCsv.fechar(); + arqCsv.abrir(); + } + + } } diff --git a/FakeNews/src/br/ufrn/imd/controle/ControladorProcesso.java b/FakeNews/src/br/ufrn/imd/controle/ControladorProcesso.java deleted file mode 100644 index f803cc4d53b517084634d1ee8452186959cfb967..0000000000000000000000000000000000000000 --- a/FakeNews/src/br/ufrn/imd/controle/ControladorProcesso.java +++ /dev/null @@ -1,133 +0,0 @@ -package br.ufrn.imd.controle; - -import java.math.BigInteger; -import java.security.MessageDigest; -import java.security.NoSuchAlgorithmException; - -public class ControladorProcesso { - - - public static String removePalavraMenor(String palavra, int tamanho) { - - String[] s = palavra.split(" "); - String out = ""; - int count = 0; - - while(s.length > count) { - - if(s[count].length() > tamanho) { - out += s[count]; - out += " "; - } - - count++; - } - - return out; - } - - public static String palavraMinuscula( String palavra ) { - return palavra.toLowerCase().replaceAll("[^a-z ]", ""); - } - - public static String palavrasOrdenarRepitidas(String frase) { - - String[] s = frase.split(" "); - String aux = ""; - String out = ""; - int count = 0; - int index = 0; - int charAt = 0; - - while(s.length-1 > index) { - - if(s[index].charAt(charAt) > s[count].charAt(charAt)) { - - aux = s[index]; - s[index] = s[count]; - s[count] = aux; - } - - if(s[index].charAt(charAt) == s[count].charAt(charAt)) { - if(s[index].length()-1 > charAt) { - charAt++; - continue; - } - } - - if(count == s.length-1 ) { - out += s[index]; - out += " "; - index++; - count = index; - charAt = 0; - } - charAt = 0; - count++; - } - - - - return palavrasRepetidas(out); - } - - private static String palavrasRepetidas(String palavra) { - - String[] s = palavra.split(" "); - String out = ""; - - int count = 0; - - while(s.length-1 > count) { - if(s[count].equals(s[count+1])) { - - }else{ - out += s[count]; - out += " "; - } - count++; - } - if(s[count-1].equals(s[count])) { - - }else{ - out += s[count]; - out += " "; - } - return out; - } - - //Fonte https://www.geeksforgeeks.org/sha-1-hash-in-java/ - public static String SHA(String palavra) - { - String hashtext; - try { - // getInstance() method is called with algorithm SHA-1 - MessageDigest md = MessageDigest.getInstance("SHA-1"); - - // digest() method is called - // to calculate message digest of the input string - // returned as array of byte - byte[] messageDigest = md.digest(palavra.getBytes()); - - // Convert byte array into signum representation - BigInteger no = new BigInteger(1, messageDigest); - - // Convert message digest into hex value - hashtext = no.toString(16); - - // Add preceding 0s to make it 32 bit - while (hashtext.length() < 32) { - hashtext = "0" + hashtext; - } - - // return the HashText - return hashtext; - } - - // For specifying wrong message digest algorithms - catch (NoSuchAlgorithmException e) { - throw new RuntimeException(e); - } - } - -} diff --git a/FakeNews/src/br/ufrn/imd/modelo/ArqCsv.java b/FakeNews/src/br/ufrn/imd/modelo/ArqCsv.java index 84392fe90833a0dbf80f5658c79343de25fc85d6..81eac4d4b49d7a1f2a847bbf6e2e7cfbb9e1e4b9 100644 --- a/FakeNews/src/br/ufrn/imd/modelo/ArqCsv.java +++ b/FakeNews/src/br/ufrn/imd/modelo/ArqCsv.java @@ -15,8 +15,7 @@ public class ArqCsv implements Arquivo { public ArqCsv() throws FileNotFoundException { - try { - + try { this.leitor = new CSVReader(new FileReader(destino)); } catch (arquivoException e) { // TODO Auto-generated catch block @@ -46,23 +45,36 @@ public class ArqCsv implements Arquivo { } @Override - public String ler(int x) throws IOException { + public String ler(int linha, int coluna ) throws IOException { // TODO Auto-generated method stub int count = 0; - + abrir(); String[] s; - - leitor.readNext(); - + while((s = leitor.readNext()) != null) { - if(count >= x || x < 0) { - return s[1]; + if(count >= linha || linha < 0) { + return s[coluna]; } count++; } - return "Valor fora"; + fechar(); + return " Valor fora "; + + } + + public int tamanhaCsv( ) throws IOException { + // TODO Auto-generated method stub + int count = 0; + abrir(); + leitor.readNext(); + + while((leitor.readNext()) != null) { + count++; + } + fechar(); + return count; } diff --git a/FakeNews/src/br/ufrn/imd/modelo/ArqWeb.java b/FakeNews/src/br/ufrn/imd/modelo/ArqWeb.java index 5be5ccf873cf45168166f40a4b0561bfa7a9c45c..9e9749c6513b7bbd7ac47e0cc17868e303c218b2 100644 --- a/FakeNews/src/br/ufrn/imd/modelo/ArqWeb.java +++ b/FakeNews/src/br/ufrn/imd/modelo/ArqWeb.java @@ -17,7 +17,7 @@ public class ArqWeb implements Arquivo { } @Override - public String ler(int x) { + public String ler(int linha, int coluna) { return url; // TODO Auto-generated method stub } diff --git a/FakeNews/src/br/ufrn/imd/modelo/Arquivo.java b/FakeNews/src/br/ufrn/imd/modelo/Arquivo.java index ff78384ef9506b95db6a999d6c2dd6b5f7315ebb..cde5ac65a5acba9f784a55245136a93e00ee5682 100644 --- a/FakeNews/src/br/ufrn/imd/modelo/Arquivo.java +++ b/FakeNews/src/br/ufrn/imd/modelo/Arquivo.java @@ -8,6 +8,6 @@ public interface Arquivo { public void abrir() throws arquivoException; public void fechar() throws arquivoException; - public String ler(int x) throws IOException; + public String ler(int linha, int coluna) throws IOException; } diff --git a/FakeNews/src/br/ufrn/imd/modelo/Noticia.java b/FakeNews/src/br/ufrn/imd/modelo/Noticia.java index f06e35356d20635a82f137021eec282815aaa546..1d3b387bed76fa7cb8ec780ee36e323fb38b0a4e 100644 --- a/FakeNews/src/br/ufrn/imd/modelo/Noticia.java +++ b/FakeNews/src/br/ufrn/imd/modelo/Noticia.java @@ -1,32 +1,219 @@ package br.ufrn.imd.modelo; -import java.sql.Timestamp; +import java.math.BigInteger; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; -public class Noticia { + +public abstract class Noticia { + private String conteudo = ""; private String formatado = ""; private String link = ""; - private Timestamp time; + private String time; + + public Noticia() { + // TODO Auto-generated constructor stub + } public String getConteudo() { return conteudo; } + public void setConteudo(String conteudo) { this.conteudo = conteudo; } + + public String getFormatado() { + return formatado; + } + + public void setFormatado(String formatado) { + this.formatado = formatado; + } + public String getLink() { return link; } + public void setLink(String link) { this.link = link; } - public Timestamp getTime() { + + public String getTime() { return time; } - public void setTime(Timestamp time) { + + public void setTime(String time) { this.time = time; } + public abstract void Formatar(); + + public static String removePalavraMenor(String palavra, int tamanho) { + + String[] s = palavra.split(" "); + String out = ""; + int count = 0; + + while(s.length > count) { + + if(s[count].length() > tamanho) { + out += s[count]; + out += " "; + } + + count++; + } + return out; + } + + + public static String palavraMinuscula( String palavra ) { + + System.out.println(palavra.toLowerCase().replaceAll("[^a-z]", "")); + String[] s = palavra.split(" "); + String out = ""; + int count = 0; + boolean b = true; + while( b ) { + System.out.println(s[count]); + count++; + } + + while(s.length > count) { + + out += s[count].toLowerCase().replaceAll("[^a-z]", ""); + out += " "; + count++; + } + out.replaceAll(" ", " "); + System.out.println("sainda "+out); + return palavra.toLowerCase().replaceAll("[^a-z]", ""); + } + + public static String palavrasOrdenarRepitidas(String frase) { + + String[] s = frase.split(" "); + String aux = ""; + String out = ""; + int count = 0; + int index = 0; + int charAt = 0; + + while((s.length-1 > index || s.length-1 > count) && s.length > 0) { + + System.out.println(s.length-1 + " > " +index + " || "+count ); + System.out.println(s[index] + " > " + s[count] + " NO "+ charAt ); + if(s[index].equals("")) { + index++; + count = index; + charAt = 0; + } + if(s[count].equals("")) { + charAt = 0; + count++; + } + + if(s[index].length()-1 > charAt || s[count].length()-1 > charAt && + s[index].charAt(charAt) > s[count].charAt(charAt)) { + aux = s[index]; + s[index] = s[count]; + s[count] = aux; + out += s[index]; + out += " "; + index++; + count = index; + charAt = 0; + count++; + continue; + } + if(s[index].charAt(charAt) == s[count].charAt(charAt)) { + if(s[index].length()-1 > charAt || s[count].length()-1 > charAt) { + System.out.println("E igual"); + charAt++; + + }else { + out += s[index]; + out += " "; + index++; + count = index; + charAt = 0; + count++; + } + continue; + } + + out += s[index]; + out += " "; + index++; + count = index; + charAt = 0; + count++; + + + } + return palavrasRepetidas(out); + } + + private static String palavrasRepetidas(String palavra) { + + String[] s = palavra.split(" "); + String out = ""; + + int count = 0; + + while(s.length-1 > count) { + if(s[count].equals(s[count+1])) { + + }else{ + out += s[count]; + out += " "; + } + count++; + } + if(s[count-1].equals(s[count])) { + + }else{ + out += s[count]; + out += " "; + } + return out; + } + + //Fonte https://www.geeksforgeeks.org/sha-1-hash-in-java/ + public static String SHA(String palavra) + { + String hashtext; + try { + // getInstance() method is called with algorithm SHA-1 + MessageDigest md = MessageDigest.getInstance("SHA-1"); + + // digest() method is called + // to calculate message digest of the input string + // returned as array of byte + byte[] messageDigest = md.digest(palavra.getBytes()); + + // Convert byte array into signum representation + BigInteger no = new BigInteger(1, messageDigest); + + // Convert message digest into hex value + hashtext = no.toString(16); + + // Add preceding 0s to make it 32 bit + while (hashtext.length() < 32) { + hashtext = "0" + hashtext; + } + + // return the HashText + return hashtext; + } + + // For specifying wrong message digest algorithms + catch (NoSuchAlgorithmException e) { + throw new RuntimeException(e); + } + } } diff --git a/FakeNews/src/br/ufrn/imd/modelo/NoticiaCsv.java b/FakeNews/src/br/ufrn/imd/modelo/NoticiaCsv.java new file mode 100644 index 0000000000000000000000000000000000000000..ff4b600588e5eb2a6098792c958d17d45ad85caa --- /dev/null +++ b/FakeNews/src/br/ufrn/imd/modelo/NoticiaCsv.java @@ -0,0 +1,23 @@ +package br.ufrn.imd.modelo; + +public class NoticiaCsv extends Noticia { + + + + public NoticiaCsv() { + super(); + // TODO Auto-generated constructor stub + } + + @Override + public void Formatar() { + // TODO Auto-generated method stub + String palavra = super.getConteudo(); + palavra = removePalavraMenor(palavra, 3); + palavra = palavraMinuscula(palavra); +// palavra = palavrasOrdenarRepitidas(palavra); +// super.setFormatado(palavra); + } + + +} diff --git a/FakeNews/src/br/ufrn/imd/modelo/NoticiaWeb.java b/FakeNews/src/br/ufrn/imd/modelo/NoticiaWeb.java new file mode 100644 index 0000000000000000000000000000000000000000..756bd249ab76774ef487f550fa5db97b63a7271a --- /dev/null +++ b/FakeNews/src/br/ufrn/imd/modelo/NoticiaWeb.java @@ -0,0 +1,11 @@ +package br.ufrn.imd.modelo; + +public class NoticiaWeb extends Noticia { + + @Override + public void Formatar() { + // TODO Auto-generated method stub + + } + +} diff --git a/FakeNews/src/br/ufrn/imd/visao/Principal.java b/FakeNews/src/br/ufrn/imd/visao/Principal.java index 70a62d52b564e6d88b2c2b06bba73e43ddcb2c5a..237796de766f5b3d0ec646272e4b411d8f5cc804 100644 --- a/FakeNews/src/br/ufrn/imd/visao/Principal.java +++ b/FakeNews/src/br/ufrn/imd/visao/Principal.java @@ -3,13 +3,30 @@ package br.ufrn.imd.visao; import java.io.IOException; import br.ufrn.imd.controle.ControladorArquivo; import br.ufrn.imd.modelo.ArqCsv; -import br.ufrn.imd.controle.ControladorProcesso; +import br.ufrn.imd.modelo.NoticiaCsv; public class Principal { public static void main(String[] args) throws IOException { + ControladorArquivo ca = new ControladorArquivo(); + ArqCsv ac = new ArqCsv(); + NoticiaCsv nc = new NoticiaCsv(); + +// for(int x = 1; x < 120; x++) +// { + int x = 81; + nc.setConteudo(ac.ler(x, 1)); + System.out.println(x+" "+nc.getConteudo()); + nc.Formatar(); + System.out.println(x+" "+nc.getFormatado()); +// } + + +// System.out.println("FAKE "+ ca.getMapCsv().get( nc.getFormatado()) ); + + }