Skip to content

Commit 2827bf0

Browse files
authored
Merge pull request #3 from SOBotics/feature/check-command
+ added "check" command
2 parents b86c9bd + d4902ec commit 2827bf0

File tree

5 files changed

+101
-10
lines changed

5 files changed

+101
-10
lines changed

src/main/java/org/sobitics/guttenberg/commandlists/SoBoticsCommandsList.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,15 @@ public void mention(Room room, PingMessageEvent event, boolean isReply){
2828
Message message = event.getMessage();
2929
List<SpecialCommand> commands = new ArrayList<>(Arrays.asList(
3030
new Alive(message),
31+
new Check(message),
3132
new Say(message)
3233
));
3334

3435
commands.add(new Commands(message,commands));
3536

36-
System.out.println("Looking for the command to execute");
37-
3837
for(SpecialCommand command: commands){
3938
if(command.validate()){
4039
command.execute(room);
41-
} else {
42-
System.out.println("It's not " +command.description());
4340
}
4441
}
4542

src/main/java/org/sobotics/guttenberg/clients/Guttenberg.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,18 +91,15 @@ private void execute() {
9191
//Let PlagFinders collect data and print the post
9292
for (PlagFinder finder : plagFinders) {
9393
finder.collectData();
94-
System.out.println("Collected");
9594
JsonObject otherAnswer = finder.getMostSimilarAnswer();
9695
if (finder.getJaroScore() > 0.8) {
97-
System.out.println("Prepare message");
9896
for (Room room : this.chatRooms) {
99-
System.out.println("Room: "+room);
10097
if (room.getRoomId() == 111347) {
10198
SoBoticsPostPrinter printer = new SoBoticsPostPrinter();
10299
room.send(printer.print(finder));
103100
System.out.println("Posted: "+printer.print(finder));
104101
} else {
105-
System.out.println("Score too low");
102+
System.out.println("Not SOBotics");
106103
}
107104
}
108105
} else {
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
package org.sobotics.guttenberg.commands;
2+
3+
import java.io.FileInputStream;
4+
import java.io.IOException;
5+
import java.util.Properties;
6+
7+
import org.sobotics.guttenberg.finders.PlagFinder;
8+
import org.sobotics.guttenberg.utils.ApiUtils;
9+
import org.sobotics.guttenberg.utils.CommandUtils;
10+
import org.sobotics.guttenberg.utils.FilePathUtils;
11+
12+
import com.google.gson.JsonObject;
13+
14+
import fr.tunaki.stackoverflow.chat.Message;
15+
import fr.tunaki.stackoverflow.chat.Room;
16+
17+
public class Check implements SpecialCommand {
18+
19+
private Message message;
20+
21+
public Check(Message message) {
22+
this.message = message;
23+
}
24+
25+
@Override
26+
public boolean validate() {
27+
return CommandUtils.checkForCommand(message.getPlainContent(),"check");
28+
}
29+
30+
@Override
31+
public void execute(Room room) {
32+
String word = CommandUtils.extractData(message.getPlainContent()).trim();
33+
Integer returnValue = 0;
34+
35+
if(word.contains(" ")){
36+
String parts[] = word.split(" ");
37+
/*if(parts[0].toLowerCase().equals("value")){
38+
returnValue = 1;
39+
word = parts[1];
40+
}
41+
else if (parts[0].toLowerCase().equals("explain")){
42+
returnValue = 2;
43+
word = parts[1];
44+
}*/
45+
}
46+
if(word.contains("/"))
47+
{
48+
word = CommandUtils.getAnswerId(word);
49+
}
50+
51+
Integer answerId = new Integer(word);
52+
53+
Properties prop = new Properties();
54+
55+
try{
56+
prop.load(new FileInputStream(FilePathUtils.loginPropertiesFile));
57+
}
58+
catch (IOException e){
59+
e.printStackTrace();
60+
}
61+
62+
63+
try {
64+
JsonObject answer = ApiUtils.getAnswerDetailsById(answerId, "stackoverflow", prop.getProperty("apikey", ""));
65+
JsonObject target = answer.get("items").getAsJsonArray().get(0).getAsJsonObject();
66+
PlagFinder finder = new PlagFinder(target);
67+
finder.collectData();
68+
finder.getMostSimilarAnswer();
69+
double score = Math.round(finder.getJaroScore()*100.0)/100.0;
70+
String link = finder.getJaroAnswer().get("link").getAsString();
71+
72+
if (score > 0) {
73+
String reply = "The closest match with a score of **"+score+"** is [this post]("+link+").";
74+
room.replyTo(message.getId(), reply);
75+
} else {
76+
room.replyTo(message.getId(), "There are no similar posts.");
77+
}
78+
79+
} catch (IOException e) {
80+
// TODO Auto-generated catch block
81+
e.printStackTrace();
82+
}
83+
84+
85+
}
86+
87+
@Override
88+
public String description() {
89+
return "Checks a post for plagiarism: check <answer url>";
90+
}
91+
92+
@Override
93+
public String name() {
94+
return "check";
95+
}
96+
97+
}

src/main/java/org/sobotics/guttenberg/printers/SoBoticsPostPrinter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public String print(PlagFinder finder) {
1818
String link = finder.getJaroAnswer().get("link").getAsString();
1919
String targetLink = "https://stackoverflow.com/a/"+finder.getTargetAnswer().get("answer_id").getAsString();
2020

21-
String post = "[Possible plagiarism]("+targetLink+") with a score of **"+ score +"**. [Original post]("+link+")";
21+
String post = "[ [Guttenberg](https://git.io/vMrPa) ] [Possible plagiarism]("+targetLink+") with a score of **"+ score +"**. [Original post]("+link+")";
2222

2323
return post;
2424
}

src/main/java/org/sobotics/guttenberg/utils/CommandUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public static boolean checkForCommand(String message, String command){
2121
}
2222
catch (IOException e){
2323
e.printStackTrace();
24-
username = "nat";
24+
username = "gut";
2525
}
2626

2727
return message.split(" ")[0].toLowerCase().startsWith("@"+username) && message.split(" ")[1].toLowerCase().equals(command);

0 commit comments

Comments
 (0)