Skip to content

Commit

Permalink
Merge pull request #6 from SOBotics/bugfix/4-use-less-quota
Browse files Browse the repository at this point in the history
Bugfix/4 use less quota
  • Loading branch information
FelixSFD authored Jan 18, 2017
2 parents 2827bf0 + 73cf292 commit b60478f
Show file tree
Hide file tree
Showing 7 changed files with 219 additions and 8 deletions.
7 changes: 7 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
language: java
jdk:
- oraclejdk8
deploy:
provider: releases
api_key: "FLKt5pVsysQnhXEk3umg"
file: "Guttenberg.jar"
skip_cleanup: true
on:
tags: true
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ public void mention(Room room, PingMessageEvent event, boolean isReply){
List<SpecialCommand> commands = new ArrayList<>(Arrays.asList(
new Alive(message),
new Check(message),
new Say(message)
new Say(message),
new Pfiatdi(message)
));

commands.add(new Commands(message,commands));
Expand Down
44 changes: 39 additions & 5 deletions src/main/java/org/sobotics/guttenberg/clients/Guttenberg.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.io.FileInputStream;
import java.io.IOException;
import java.time.Instant;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
Expand All @@ -11,6 +12,7 @@

import org.sobotics.guttenberg.finders.NewAnswersFinder;
import org.sobotics.guttenberg.finders.PlagFinder;
import org.sobotics.guttenberg.finders.RelatedAnswersFinder;
import org.sobotics.guttenberg.printers.SoBoticsPostPrinter;
import org.sobotics.guttenberg.roomdata.BotRoom;
import org.sobotics.guttenberg.utils.FilePathUtils;
Expand Down Expand Up @@ -74,25 +76,55 @@ public void start() {
}

private void execute() {
System.out.println("Executing...");
System.out.println("Executing at - "+Instant.now());
//NewAnswersFinder answersFinder = new NewAnswersFinder();

//Fetch recent answers / The targets

JsonArray recentAnswers = NewAnswersFinder.findRecentAnswers();

//Fetch their question_ids
List<Integer> ids = new ArrayList<Integer>();
for (JsonElement answer : recentAnswers) {
Integer id = answer.getAsJsonObject().get("question_id").getAsInt();
if (!ids.contains(id))
ids.add(id);
}


//Initialize the PlagFinders
List<PlagFinder> plagFinders = new ArrayList<PlagFinder>();

for (JsonElement answer : recentAnswers) {
PlagFinder plagFinder = new PlagFinder(answer.getAsJsonObject());
plagFinders.add(plagFinder);
}

//fetch all /questions/ids/answers sort them later

RelatedAnswersFinder related = new RelatedAnswersFinder(ids);
List<JsonObject> relatedAnswersUnsorted = related.fetchRelatedAnswers();

//Let PlagFinders collect data and print the post
System.out.println("Add the answers to the PlagFinders...");
//add relatedAnswers to the PlagFinders
for (PlagFinder finder : plagFinders) {
Integer targetId = finder.getTargetAnswerId();
//System.out.println("TargetID: "+targetId);

for (JsonObject relatedItem : relatedAnswersUnsorted) {
//System.out.println(relatedItem);
if (relatedItem.has("answer_id") && relatedItem.get("answer_id").getAsInt() != targetId) {
finder.relatedAnswers.add(relatedItem);
//System.out.println("Added answer: "+relatedItem);
}
}

}

System.out.println("Find the duplicates...");
//Let PlagFinders find the best match
for (PlagFinder finder : plagFinders) {
finder.collectData();
JsonObject otherAnswer = finder.getMostSimilarAnswer();
if (finder.getJaroScore() > 0.8) {
if (finder.getJaroScore() > 0.77) {
for (Room room : this.chatRooms) {
if (room.getRoomId() == 111347) {
SoBoticsPostPrinter printer = new SoBoticsPostPrinter();
Expand All @@ -106,5 +138,7 @@ private void execute() {
System.out.println("Score "+finder.getJaroScore()+" too low");
}
}

System.out.println("Finished at - "+Instant.now());
}
}
51 changes: 51 additions & 0 deletions src/main/java/org/sobotics/guttenberg/commands/Pfiatdi.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package org.sobotics.guttenberg.commands;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import org.sobotics.guttenberg.utils.CommandUtils;

import fr.tunaki.stackoverflow.chat.Message;
import fr.tunaki.stackoverflow.chat.Room;

/**
* Created by bhargav.h on 30-Sep-16.
*/
public class Pfiatdi implements SpecialCommand {

private Message message;

public Pfiatdi(Message message) {
this.message = message;
}

@Override
public boolean validate() {
return CommandUtils.checkForCommand(message.getPlainContent(),"cya") ||
CommandUtils.checkForCommand(message.getPlainContent(),"o/") ||
CommandUtils.checkForCommand(message.getPlainContent(),"bye");
}

@Override
public void execute(Room room) {
List<String> array = new ArrayList<String>();
array.add("[Pfiat di!](http://german.stackexchange.com/q/254) o/");
array.add("[Pfiats eich!](http://german.stackexchange.com/q/254)");
array.add("[Pfüad di Gott](http://german.stackexchange.com/q/254) o/");

int rnd = new Random().nextInt(array.size());

room.send(array.get(rnd));
}

@Override
public String description() {
return "Psscht. Hier derfsch it naluaga!";
}

@Override
public String name() {
return "secret";
}
}
12 changes: 11 additions & 1 deletion src/main/java/org/sobotics/guttenberg/finders/PlagFinder.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class PlagFinder {
/**
* A list of answers that are somehow related to targetAnswer.
* */
private List<JsonObject> relatedAnswers;
public List<JsonObject> relatedAnswers;

private double jaroScore = 0;

Expand All @@ -37,6 +37,12 @@ public class PlagFinder {
* */
public PlagFinder(JsonObject jsonObject) {
this.targetAnswer = jsonObject;
this.relatedAnswers = new ArrayList<JsonObject>();
}

public PlagFinder(JsonObject target, List<JsonObject> related) {
this.targetAnswer = target;
this.relatedAnswers = related;
}

public void collectData() {
Expand Down Expand Up @@ -132,6 +138,10 @@ public JsonObject getTargetAnswer() {
return this.targetAnswer;
}

public Integer getTargetAnswerId() {
return this.targetAnswer.get("answer_id").getAsInt();
}

public double getJaroScore() {
return this.jaroScore;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package org.sobotics.guttenberg.finders;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import org.sobotics.guttenberg.utils.ApiUtils;
import org.sobotics.guttenberg.utils.FilePathUtils;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;

/**
* Collects all related answers in less API calls
* */
public class RelatedAnswersFinder {
/**
* The question_ids of the targeted answers
* */
List<Integer> targedIds;

public RelatedAnswersFinder(List<Integer> ids) {
this.targedIds = ids;
}


public List<JsonObject> fetchRelatedAnswers() {
//The question_ids of all the new answers
String idString = "";
int n = 0;
for (Integer id : this.targedIds) {
idString += n++ == 0 ? id : ";"+id;
}

System.out.println(idString);

Properties prop = new Properties();

try{
prop.load(new FileInputStream(FilePathUtils.loginPropertiesFile));
}
catch (IOException e){
e.printStackTrace();
}

System.out.println("Fetch the linked/related questions...");

try {
JsonObject relatedQuestions = ApiUtils.getRelatedQuestionsByIds(idString, "stackoverflow", prop.getProperty("apikey", ""));
JsonObject linkedQuestions = ApiUtils.getLinkedQuestionsByIds(idString, "stackoverflow", prop.getProperty("apikey", ""));

String relatedIds = "";

for (JsonElement question : relatedQuestions.get("items").getAsJsonArray()) {
int id = question.getAsJsonObject().get("question_id").getAsInt();
//System.out.println("Add: "+id);
relatedIds += id+";";
}

for (JsonElement question : linkedQuestions.get("items").getAsJsonArray()) {
int id = question.getAsJsonObject().get("question_id").getAsInt();
//System.out.println("Add: "+id);
relatedIds += id+";";
}

if (relatedIds.length() > 0) {
relatedIds = relatedIds.substring(0, relatedIds.length()-1);

List<JsonObject> relatedFinal = new ArrayList<JsonObject>();

JsonObject relatedAnswers = ApiUtils.getAnswersToQuestionsByIdString(relatedIds, "stackoverflow", prop.getProperty("apikey", ""));
//System.out.println(relatedAnswers);
for (JsonElement answer : relatedAnswers.get("items").getAsJsonArray()) {
JsonObject answerObject = answer.getAsJsonObject();
relatedFinal.add(answerObject);
}

System.out.println("Collected "+relatedFinal.size()+" answers");
return relatedFinal;
} else {
System.out.println("No ids found!");
}


} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}



return null;
}


}
12 changes: 11 additions & 1 deletion src/main/java/org/sobotics/guttenberg/utils/ApiUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,19 @@ public static JsonObject getLinkedQuestionsById(Integer questionId, String site,
return JsonUtils.get(questionIdUrl,"site",site,"key",apiKey);
}

public static JsonObject getRelatedQuestionsByIds(String questionIds, String site, String apiKey) throws IOException{
String questionIdUrl = "https://api.stackexchange.com/2.2/questions/"+questionIds+"/related";
return JsonUtils.get(questionIdUrl,"site",site,"key",apiKey);
}

public static JsonObject getLinkedQuestionsByIds(String questionIds, String site, String apiKey) throws IOException{
String questionIdUrl = "https://api.stackexchange.com/2.2/questions/"+questionIds+"/related";
return JsonUtils.get(questionIdUrl,"site",site,"key",apiKey);
}

public static JsonObject getAnswersToQuestionsByIdString(String questionIds, String site, String apiKey) throws IOException{
String questionIdUrl = "https://api.stackexchange.com/2.2/questions/"+questionIds+"/answers";
return JsonUtils.get(questionIdUrl,"site",site,"key",apiKey,"filter","!bGqd96Ed_k-mDe");
return JsonUtils.get(questionIdUrl,"site",site,"key",apiKey,"filter","!bGqd96Ed_k-mDe","pagesize","100");
}

public static JsonObject getQuestionDetailsByIds(List<Integer> questionIdList, String site, String apiKey) throws IOException {
Expand Down

0 comments on commit b60478f

Please sign in to comment.