Skip to content

Commit

Permalink
aplicando lda
Browse files Browse the repository at this point in the history
  • Loading branch information
AdsonEsteves committed Dec 30, 2020
1 parent 0cbabde commit 0f56f3d
Show file tree
Hide file tree
Showing 6 changed files with 336 additions and 94 deletions.
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ dependencies {

implementation 'org.springframework.boot:spring-boot-starter-web'

implementation 'com.github.chen0040:java-lda:1.0.4'

// Use JUnit test framework
testImplementation 'junit:junit:4.13'
}
Expand Down
177 changes: 166 additions & 11 deletions src/main/java/sacip/sti/agents/GrouperAgent.java
Original file line number Diff line number Diff line change
@@ -1,35 +1,190 @@
package sacip.sti.agents;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;

import com.github.chen0040.data.utils.TupleTwo;
import com.github.chen0040.lda.Doc;
import com.github.chen0040.lda.Lda;
import com.github.chen0040.lda.LdaResult;
import org.midas.as.AgentServer;
import org.midas.as.agent.templates.Agent;
import org.midas.as.agent.templates.LifeCycleException;
import org.midas.as.agent.templates.ServiceException;
import org.midas.as.manager.execution.ServiceWrapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import sacip.sti.dataentities.Student;

public class GrouperAgent extends Agent{
public class GrouperAgent extends Agent {

private static Logger LOG = LoggerFactory.getLogger(AgentServer.class);

@Override
public void provide(String service, Map in, List out) throws ServiceException {
// TODO Auto-generated method stub
if(service.equals("getStudentGroups"))
{
System.out.println("Buscando alunos no banco");
System.out.println("Classificando alunos");

Student aluno = (Student) in.get("estudante");

System.out.println("Selecionando cluster de aluno "+aluno.getName()+" requisitado");
System.out.println("retornando grupo");

try {
switch (service) {
case "getStudentGroups":
out.add(findStudentSimilars((Student) in.get("estudante")));
break;

default:
break;
}
} catch (Exception e) {

}
if (service.equals("getStudentGroups")) {
try {

} catch (Exception e) {
LOG.error("ERRO NO AGENT AGRUPADOR", e);
}
}
}

@Override
protected void lifeCycle() throws LifeCycleException, InterruptedException {
// TODO Auto-generated method stub

}

public static void main(String[] args) {
System.setProperty("hadoop.home.dir", "c:\\winutil\\");
GrouperAgent agent = new GrouperAgent();
//agent.findStudentGroup(null);
}

public List<Student> findStudentGroup(Student alunoRequisitado)
{
//List<Student> estudantes = getUsers();
List<Student> grupo = new ArrayList<>();

List<String> docs = Arrays.asList("carros animes youtube História", "comédia animes História Livros", "monstros cultura comédia Tecnologia", "Livros", "mitologia animes", "Livros matemática");

Lda method = new Lda();
method.setTopicCount(3);
method.setMaxVocabularySize(20000);
//method.setStemmerEnabled(true);
//method.setRemoveNumbers(true);
//method.setRemoveXmlTag(true);
//method.addStopWords(Arrays.asList("we", "they"));

LdaResult result = method.fit(docs);



// //System.out.println("Topic Count: "+result.topicCount());
// for(int topicIndex = 0; topicIndex < result.topicCount(); ++topicIndex){
// String topicSummary = result.topicSummary(topicIndex);
// List<TupleTwo<String, Integer>> topKeyWords = result.topKeyWords(topicIndex, 10);
// List<TupleTwo<Doc, Double>> topStrings = result.topDocuments(topicIndex, 5);

// //System.out.println("Topic #" + (topicIndex+1) + ": " + topicSummary);

// for(TupleTwo<String, Integer> entry : topKeyWords){
// String keyword = entry._1();
// int score = entry._2();
// //System.out.println("Keyword: " + keyword + "(" + score + ")");
// }

// for(TupleTwo<Doc, Double> entry : topStrings){
// double score = entry._2();
// int docIndex = entry._1().getDocIndex();
// String docContent = entry._1().getContent();
// //System.out.println("Doc (" + docIndex + ", " + score + ")): " + docContent);
// }
// }

for(Doc doc : result.documents()){
List<TupleTwo<Integer, Double>> topTopics = doc.topTopics(3);
System.out.println("Doc: {"+doc.getDocIndex()+"}"+"TOP TOPIC: {"+topTopics.get(0)._1()+"}");
}

return grupo;
}

private List<Student> findStudentSimilars(Student alunoRequisitado)
{
List<Student> estudantes = getUsers();

if(estudantes.size()<20)
{
return new ArrayList<>();
}

List<Student> sortedStudents = new ArrayList<Student>(){
@Override
public boolean add(Student e) {
super.add(e);
Collections.sort(this, new Comparator<Student>(){
@Override
public int compare(Student o1, Student o2) {
return o2.pontos-o1.pontos;
}
});
return true;
}
};

for (Student student : estudantes)
{
List<String> preferencias = student.getPreferencias();

if(!alunoRequisitado.getName().equals(student.getName()))
{
for (String preferencia : preferencias)
{
if(alunoRequisitado.getPreferencias().contains(preferencia))
{
student.pontos++;
}
// else{
// student.pontos--;
// }
}

if(student.getNivelEducacional().equals(alunoRequisitado.getNivelEducacional()))
{
student.pontos+=5;
}
sortedStudents.add(student);
}
}

List<Student> grupoDe10 = new ArrayList<>();

for (int i = 0; i < 10; i++) {
grupoDe10.add(sortedStudents.get(i));
}

return grupoDe10;
}

private List<Student> getUsers()
{
try
{
ServiceWrapper pegarEstudantes = require("SACIP", "findStudents");
List result = pegarEstudantes.run();
if(result.get(0) instanceof String)
{
return null;
}
return (List<Student>) result.get(0);
}
catch (Exception e)
{
LOG.error("ERRO AO REQUISITAR USUARIOS DO SISTEMA", e);
return null;
}
}


}
6 changes: 3 additions & 3 deletions src/main/java/sacip/sti/agents/PedagogicalAgent.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@ private void montarAlunoExemplo()
List<String> preferencias = new ArrayList<>();
preferencias.add("animes");
preferencias.add("filmes");
this.student = new Student("Adson", "123456", "link", "masculino", 24, "graduação", preferencias);
preferencias.add("jogos");
List<String> trilha = new ArrayList<String>(){{
add("2dcznTNvej");
add("Jvp1ma0QEN");
add("s19ra6yQrd");
add("tsxlrStu9a");
add("UlGW3aVSZI");
}};
this.student.setTrilha(trilha);
this.student = new Student("Adson", "123456", "link", "masculino", 24, "Graduação", preferencias, trilha);
}

private void registrarConteudosDaTrilhaDoAluno()
Expand Down Expand Up @@ -115,7 +115,7 @@ private String suggestContent()
//Pedir recomendação para o recomendador
ServiceWrapper servicoGetContent = require("SACIP", "getRecommendedContent");
servicoGetContent.addParameter("estudante", getAluno());
servicoGetContent.addParameter("grupo", grupo);
servicoGetContent.addParameter("grupo", grupo.get(0));
servicoGetContent.addParameter("trilha", this.trilha);
List resultado = servicoGetContent.run();
if(resultado.isEmpty())
Expand Down
102 changes: 84 additions & 18 deletions src/main/java/sacip/sti/agents/RecommenderAgent.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;
import java.util.Set;

import org.midas.as.AgentServer;
import org.midas.as.agent.templates.Agent;
Expand Down Expand Up @@ -72,19 +72,6 @@ public int compare(Content o1, Content o2) {

try
{
List<String> caracteristicas = new ArrayList<>();

if(!grupo.isEmpty())
{
System.out.println("Verificando semelhanças entre alunos");

caracteristicas.add("exemplo");
}
else
{
caracteristicas.addAll(preferenciasAluno);
}

//Verificar os tópicos e níveis que o aluno utilizou
HashMap<Integer, List<String>> nivelETopico = descobrirNiveisETopicos(trilha);
Integer[] niveisFeitos = nivelETopico.keySet().toArray(new Integer[nivelETopico.size()]);
Expand All @@ -98,6 +85,21 @@ public int compare(Content o1, Content o2) {
return "não há conteúdos";
}
List<Content> conteudos = (List<Content>) resultado.get(0);
List<Content> conteudosDoGrupoNaoFeitos = new ArrayList<>();
if(!grupo.isEmpty())
{
//VERIFICAR TRILHAS E PEGAR CONTEUDOS UTILIZADOS NOS SEMELHANTES
List<Content> conteudosDasTrilhas = filtrarConteudosDasTrilhasDosAlunosDoGrupo(grupo, conteudos);

for (Content content : conteudosDasTrilhas) {
if(!aluno.getTrilha().contains(content.getName()))
{
conteudos.remove(content);
conteudosDoGrupoNaoFeitos.add(content);
}
}
}


//CONTEUDOS DO TÓPICO EM QUE ELE ESTÁ

Expand All @@ -110,7 +112,7 @@ public int compare(Content o1, Content o2) {
conteudosPorNovasTaxonomias = descobrirConteudosDeTaxonomia(conteudos, taxonomiasPorTopicos);

//FILTRAR POR TAGS
conteudosPorNovasTaxonomias = filtrarConteudosPorTags(conteudosPorNovasTaxonomias, caracteristicas);
conteudosPorNovasTaxonomias = filtrarConteudosPorTags(conteudosPorNovasTaxonomias, preferenciasAluno);
}


Expand Down Expand Up @@ -145,11 +147,12 @@ public int compare(Content o1, Content o2) {
{
conteudos = filtrarConteudosPorTopicos(conteudos, topicosFaltantes);
}
conteudos = filtrarConteudosPorTags(conteudos, caracteristicas);
conteudos = filtrarConteudosPorTags(conteudos, preferenciasAluno);

Set<Content> conteudosFiltrados = new HashSet<>();
conteudosFiltrados.addAll(conteudosPorNovasTaxonomias);
conteudosFiltrados.addAll(conteudos);
conteudosFiltrados.addAll(conteudosDoGrupoNaoFeitos);

//PRIORIZAR POR PONTOS
for (Content content : conteudosFiltrados) {
Expand All @@ -161,8 +164,14 @@ public int compare(Content o1, Content o2) {
sortedContent.add(content);
}

List<Content> top10Conteudos = new ArrayList<>();

for (int i = 0; i < 10; i++) {
top10Conteudos.add(sortedContent.get(i));
}

//retornando conteudos
String exercicio = sortedContent.toString();
String exercicio = top10Conteudos.toString();
return exercicio;
}
catch (Exception e)
Expand Down Expand Up @@ -324,4 +333,61 @@ private List<Content> descobrirConteudosDeTaxonomia(List<Content> conteudos, Map

return conteudosRecomendados;
}

private List<Content> filtrarConteudosDasTrilhasDosAlunosDoGrupo(List<Student> grupo, List<Content> conteudos)
{
List<Content> conteudosEmTodos = new ArrayList<>();

Map<String, Integer> conteudosDoGrupo = new HashMap<>();

for (Student aluno : grupo)
{
List<String> trilha = aluno.getTrilha();
for (String conteudo : trilha) {
if(conteudosDoGrupo.containsKey(conteudo))
{
conteudosDoGrupo.put(conteudo, conteudosDoGrupo.get(conteudo)+1);
}
else
{
conteudosDoGrupo.put(conteudo, 1);
}
}
}

Map<String, Integer> conteudosOrdenados = sortByValue(conteudosDoGrupo);

if(conteudosOrdenados.size()>10)
{
conteudosOrdenados = conteudosOrdenados.entrySet().stream()
.limit(10)
.collect(LinkedHashMap::new, (m, e) -> m.put(e.getKey(), e.getValue()), Map::putAll);

}

for (Entry<String, Integer> entry : conteudosOrdenados.entrySet())
{
for (Content content : conteudos) {
if(content.getName().equals(entry.getKey()))
{
content.pontos+=entry.getValue();
conteudosEmTodos.add(content);
}
}
}

return conteudosEmTodos;
}

public <K, V extends Comparable<? super V>> Map<K, V> sortByValue(Map<K, V> map) {
List<Entry<K, V>> list = new ArrayList<>(map.entrySet());
list.sort(Entry.comparingByValue());
Collections.reverse(list);
Map<K, V> result = new LinkedHashMap<>();
for (Entry<K, V> entry : list) {
result.put(entry.getKey(), entry.getValue());
}

return result;
}
}
Loading

0 comments on commit 0f56f3d

Please sign in to comment.