Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dev #6

Merged
merged 2 commits into from
Feb 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions code/ArgumentIR/Resources/views/proposal-info.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
<div style="color:#4682B4; font-size:16pt; margin-top: 5px; margin-bottom: 10px;">
<strong>$TITLE$</strong>
</div>
<span>$DATE$ &#8226; Comments: $NUM_COMMENTS$ &#8226; Supports: $NUM_SUPPORTS$</span><br />
<span>Proposal code: <b>$CODE$</b></span>
<span>Proposal <b>$CODE$</b> of $DATE$</span><br />
<span>Arguments: <span style="font-size: 11px;">$NUM_ARGUMENTS$</span> &#8226; Comments: <span style="font-size: 11px;">$NUM_COMMENTS$</span> &#8226; Supports: <span style="font-size: 11px;">$NUM_SUPPORTS$</span></span>
<p>$SUMMARY$</p><br />
<span>Districts</span>: $DISTRICTS$<br />
<span>Categories</span>: $CATEGORIES$<br />
<span>Topics</span>: $TOPICS$<br />
<span>Link</span>: $URL$<br />
<span>- Districts</span>: $DISTRICTS$<br />
<span>- Categories</span>: $CATEGORIES$<br />
<span>- Topics</span>: $TOPICS$<br />
<p>Link: $URL$</p>
<p><strong>Comments:</strong></p>
<div>$COMMENTS$</div>
</div>
48 changes: 43 additions & 5 deletions code/ArgumentIR/src/es/uam/irg/decidemadrid/db/MongoDbManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.model.Filters;
import com.mongodb.client.model.UpdateOptions;
import es.uam.irg.nlp.am.arguments.Argument;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
Expand All @@ -40,16 +42,16 @@
public class MongoDbManager {

// Public constants
public static final String DB_COLLECTION = "annotations";
public static final String DB_NAME = "decide_madrid_2019_09";
public static final int DB_PORT = 27017;
public static final String DB_SERVER = "localhost";
public static final String DB_COLLECTION = "annotations";
private static final String NO_TOPIC = "-";

// Private connector object
private String collName;
private MongoDatabase db;
private MongoClient mongoClient;
private String collName;

/**
* Manager constructor.
Expand All @@ -72,15 +74,15 @@ public MongoDbManager(String client, int port, String database, String collectio
}

/**
*
* @param setup
*
* @param setup
*/
public MongoDbManager(Map<String, Object> setup) {
String client = setup.get("db_server").toString();
int port = Integer.parseInt(setup.get("db_port").toString());
String database = setup.get("db_name").toString();
String collection = setup.get("db_collection").toString();

this.mongoClient = new MongoClient(client, port);
this.db = mongoClient.getDatabase(database);
this.collName = collection;
Expand Down Expand Up @@ -124,6 +126,42 @@ public List<Document> getDocumentsByFilter(String topic, Integer[] customProposa
return docs;
}

/**
*
* @param maxTreeLevel
* @return
*/
public Map<Integer, List<Argument>> selectProposalArguments(int maxTreeLevel) {
Map<Integer, List<Argument>> arguments = new HashMap<>();
int proposalId;
Argument argument;

try {

// Query documents
MongoCollection<Document> collection = db.getCollection(collName);
FindIterable<Document> cursor = collection.find();

for (Iterator<Document> it = cursor.iterator(); it.hasNext();) {
Document doc = it.next();
argument = new Argument(doc);
proposalId = argument.getProposalId();

if (argument.getTreeLevel() <= maxTreeLevel) {
if (!arguments.containsKey(proposalId)) {
arguments.put(proposalId, new ArrayList<>());
}
arguments.get(proposalId).add(argument);
}
}

} catch (Exception ex) {
System.err.println("MongoDB error: " + ex.getMessage());
}

return arguments;
}

/**
*
* @param doc
Expand Down
5 changes: 3 additions & 2 deletions code/ArgumentIR/src/es/uam/irg/ir/ArgumentIR.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package es.uam.irg.ir;

import es.uam.irg.ir.gui.ArgumentIRForm;
import es.uam.irg.utils.FunctionUtils;

/**
* Program main class.
Expand Down Expand Up @@ -46,12 +47,12 @@ private static void showWinform() {

/* Create and display the form */
java.awt.EventQueue.invokeLater(() -> {
System.out.println(">> ARG-IR BEGINS");
FunctionUtils.printWithDatestamp(">> ARG-IR BEGINS");

ArgumentIRForm form = new ArgumentIRForm();
form.setVisible(true);

System.out.println(">> ARG-IR ENDS");
FunctionUtils.printWithDatestamp(">> ARG-IR ENDS");
});
}

Expand Down
34 changes: 16 additions & 18 deletions code/ArgumentIR/src/es/uam/irg/ir/InfoRetriever.java
Original file line number Diff line number Diff line change
Expand Up @@ -100,37 +100,35 @@ public void createIndex(Map<Integer, DMProposal> proposals, Map<Integer, DMPropo
}

/**
*
* Searches (within the index) for records that fulfill a certain
* information need (query).
*
* @param querystr
* @param hitsPerPage
* @param reRankBy
* @return
*/
public List<Integer> queryData(String querystr, int hitsPerPage, String reRankBy) {
public List<Integer> queryData(String querystr, int hitsPerPage) {
List<Integer> docList = new ArrayList<>();

try {
// The "title" arg specifies the default field to use when no field is explicitly specified in the query
Query q = new QueryParser("title", analyzer).parse(querystr);

// Search
IndexReader reader = DirectoryReader.open(index);
IndexSearcher searcher = new IndexSearcher(reader);
TopDocs docs = searcher.search(q, hitsPerPage);
ScoreDoc[] hits = docs.scoreDocs;

// Store results
for (int i = 0; i < hits.length; ++i) {
int docId = hits[i].doc;
Document doc = searcher.doc(docId);
int proposalId = Integer.parseInt(doc.get("id"));
docList.add(proposalId);
// Search within the index
try ( IndexReader reader = DirectoryReader.open(index)) {
IndexSearcher searcher = new IndexSearcher(reader);
TopDocs docs = searcher.search(q, hitsPerPage);
ScoreDoc[] hits = docs.scoreDocs;

// Store results
for (int i = 0; i < hits.length; ++i) {
int docId = hits[i].doc;
Document doc = searcher.doc(docId);
int proposalId = Integer.parseInt(doc.get("id"));
docList.add(proposalId);
}
}

// Reader can only be closed when there is no need to access the documents any more.
reader.close();

} catch (ParseException | IOException ex) {
Logger.getLogger(InfoRetriever.class.getName()).log(Level.SEVERE, null, ex);
}
Expand Down
71 changes: 63 additions & 8 deletions code/ArgumentIR/src/es/uam/irg/ir/gui/ArgumentIRModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,15 @@
package es.uam.irg.ir.gui;

import es.uam.irg.decidemadrid.db.DMDBManager;
import es.uam.irg.decidemadrid.db.MongoDbManager;
import es.uam.irg.decidemadrid.entities.DMComment;
import es.uam.irg.decidemadrid.entities.DMCommentTree;
import es.uam.irg.decidemadrid.entities.DMProposal;
import es.uam.irg.decidemadrid.entities.DMProposalSummary;
import es.uam.irg.ir.InfoRetriever;
import es.uam.irg.nlp.am.arguments.Argument;
import es.uam.irg.utils.FunctionUtils;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand All @@ -36,6 +39,7 @@
public class ArgumentIRModel {

// Class constants
private static final int MAX_TREE_LEVEL = 2;
private static final boolean VERBOSE = true;

// Class objects
Expand All @@ -44,6 +48,7 @@ public class ArgumentIRModel {
private final Map<String, Object> msqlSetup;

// Class data variables
private Map<Integer, List<Argument>> proposalArguments;
private Map<Integer, List<DMCommentTree>> proposalCommentTrees;
private Map<Integer, DMComment> proposalComments;
private Map<Integer, DMProposalSummary> proposalSummaries;
Expand Down Expand Up @@ -84,11 +89,15 @@ public String getQueryResult(String query, int nTop, String reRankBy) {
} else {
// Query data
long start = System.nanoTime();
List<Integer> docList = this.retriever.queryData(query, nTop, reRankBy);
List<Integer> ids = this.retriever.queryData(query, nTop);
long finish = System.nanoTime();
double timeElapsed = (finish - start) / 1000000;
int nReports = docList.size();
System.out.println(">> Found " + nReports + " hits in " + timeElapsed + " ms");
int nReports = ids.size();
FunctionUtils.printWithDatestamp(">> Found " + nReports + " hits in " + timeElapsed + " ms");

// Rerank data
List<Integer> docList = sortResults(ids, reRankBy);
FunctionUtils.printWithDatestamp(">> Data reranked by: " + reRankBy);

// Create user report
if (docList.size() > 0) {
Expand All @@ -97,13 +106,16 @@ public String getQueryResult(String query, int nTop, String reRankBy) {
DMProposal proposal;
DMProposalSummary summary;
List<DMCommentTree> commentTrees;
List<Argument> arguments;

// Format data
for (int docId : docList) {
proposal = proposals.get(docId);
summary = proposalSummaries.get(docId);
commentTrees = proposalCommentTrees.get(docId);
report = this.formatter.getProposalInfoReport(proposal, summary, commentTrees, proposalComments);
arguments = proposalArguments.get(docId);

report = this.formatter.getProposalInfoReport(proposal, summary, commentTrees, proposalComments, arguments);
body.append(report);
}

Expand All @@ -118,7 +130,7 @@ public String getQueryResult(String query, int nTop, String reRankBy) {
*
*/
private void createIndex() {
System.out.println(">> Creating Lucene index...");
FunctionUtils.printWithDatestamp(">> Creating Lucene index");
this.retriever = new InfoRetriever();
this.retriever.createIndex(proposals, proposalSummaries);
}
Expand All @@ -130,16 +142,26 @@ private void loadData() {
proposals = new HashMap<>();
proposalComments = new HashMap<>();

// Connecting to databse
// Connecting to databases and fetching data
try {
System.out.println(">> Loading data...");
FunctionUtils.printWithDatestamp(">> Creating connections");

DMDBManager dbManager = null;
if (msqlSetup != null && msqlSetup.size() == 4) {
dbManager = new DMDBManager(msqlSetup);
} else {
dbManager = new DMDBManager();
}

MongoDbManager mngManager = null;
if (mdbSetup != null && mdbSetup.size() == 4) {
mngManager = new MongoDbManager(mdbSetup);
} else {
mngManager = new MongoDbManager();
}

FunctionUtils.printWithDatestamp(">> Loading data");

// Get proposals
proposals = dbManager.selectProposals();

Expand All @@ -152,16 +174,49 @@ private void loadData() {
// Get comments trees
proposalCommentTrees = dbManager.selectCommentTrees();

// Get arguments data
proposalArguments = mngManager.selectProposalArguments(MAX_TREE_LEVEL);

} catch (Exception ex) {
Logger.getLogger(InfoRetriever.class.getName()).log(Level.SEVERE, null, ex);
}

// Show results
if (VERBOSE) {
System.out.println(" - Number of proposals: " + proposals.size());
System.out.println(" - Number of proposal summaries: " + proposals.size());
System.out.println(" - Number of proposal summaries: " + proposalSummaries.size());
System.out.println(" - Number of comments: " + proposalComments.size());
System.out.println(" - Number of comment trees: " + proposalCommentTrees.size());
System.out.println(" - Number of arguments: " + proposalArguments.size());
}
}

/**
*
* @param docs
* @param reRankBy
* @return
*/
private List<Integer> sortResults(List<Integer> ids, String reRankBy) {
List<Integer> docList = new ArrayList<>();

if (reRankBy.equals("Nothing")) {
docList.addAll(ids);

} else if (reRankBy.equals("Arguments")) {
Map<Integer, Integer> argsByProp = new HashMap<>();
int nArgs;

for (int id : ids) {
nArgs = (proposalArguments.containsKey(id) ? proposalArguments.get(id).size() : 0);
argsByProp.put(id, nArgs);
}

argsByProp = FunctionUtils.sortMapByValue(argsByProp);
docList.addAll(argsByProp.keySet());
}

return docList;
}

}
9 changes: 6 additions & 3 deletions code/ArgumentIR/src/es/uam/irg/ir/gui/ReportFormatter.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import es.uam.irg.decidemadrid.entities.DMProposal;
import es.uam.irg.decidemadrid.entities.DMProposalSummary;
import es.uam.irg.io.IOManager;
import es.uam.irg.nlp.am.arguments.Argument;
import java.text.DecimalFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
Expand Down Expand Up @@ -93,17 +94,19 @@ public String getNoValidQueryReport() {
* @param summary
* @param commentTrees
* @param comments
* @param arguments
* @return
*/
public String getProposalInfoReport(DMProposal proposal, DMProposalSummary summary, List<DMCommentTree> commentTrees, Map<Integer, DMComment> comments) {
public String getProposalInfoReport(DMProposal proposal, DMProposalSummary summary, List<DMCommentTree> commentTrees, Map<Integer, DMComment> comments, List<Argument> arguments) {
String report = reports.get("PROPOSAL_INFO");
StringBuilder body = new StringBuilder();

report = report.replace("$TITLE$", proposal.getTitle());
report = report.replace("$TITLE$", proposal.getTitle().toUpperCase());
report = report.replace("$CODE$", proposal.getCode());
report = report.replace("$DATE$", proposal.getDate());
report = report.replace("$NUM_ARGUMENTS$", "" + (arguments != null ? arguments.size() : 0));
report = report.replace("$NUM_COMMENTS$", "" + proposal.getNumComments());
report = report.replace("$NUM_SUPPORTS$", "" + proposal.getNumSupports());
report = report.replace("$CODE$", proposal.getCode());
report = report.replace("$CATEGORIES$", summary.getCategories());
report = report.replace("$DISTRICTS$", summary.getDistricts());
report = report.replace("$TOPICS$", summary.getTopics());
Expand Down
Loading