Reorganized code to be more object-oriented.
This commit is contained in:
@@ -18,15 +18,24 @@ import java.util.Random;
|
||||
|
||||
public final class MarkovChain {
|
||||
private static final char DEFAULT_SENTENCE_END = '§';
|
||||
private static final Random rng = new SecureRandom();
|
||||
|
||||
private final long id;
|
||||
private final Crabstero crabstero;
|
||||
private final Random rng;
|
||||
|
||||
public MarkovChain(final long id) {
|
||||
public MarkovChain(final long id, final Crabstero crabstero) {
|
||||
this.id = id;
|
||||
this.crabstero = crabstero;
|
||||
this.rng = new SecureRandom();
|
||||
}
|
||||
|
||||
private static final boolean isCompleteSentence(final String sentence) {
|
||||
public MarkovChain(final long id, final Crabstero crabstero, final Random random) {
|
||||
this.id = id;
|
||||
this.crabstero = crabstero;
|
||||
this.rng = random;
|
||||
}
|
||||
|
||||
private static boolean isCompleteSentence(final String sentence) {
|
||||
if (sentence.isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
@@ -42,8 +51,8 @@ public final class MarkovChain {
|
||||
|
||||
final String[] sentences = paragraph.trim().replaceAll(" +", " ").replaceAll("\n", " ").split("(?<=[.!?]) ");
|
||||
|
||||
for (int i = 0; i < sentences.length; i++) {
|
||||
this.ingestSentence(sentences[i]);
|
||||
for (String sentence : sentences) {
|
||||
this.ingestSentence(sentence);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -54,7 +63,7 @@ public final class MarkovChain {
|
||||
|
||||
String[] words = sentence.trim().replaceAll(" +", " ").split(" ");
|
||||
|
||||
try (final Jedis jedis = Crabstero.getJedis()) {
|
||||
try (final Jedis jedis = this.crabstero.getJedisPool().getResource()) {
|
||||
final Pipeline pipeline = jedis.pipelined();
|
||||
for (int i = 0; i < words.length - 1; i++) {
|
||||
if (i == 0) {
|
||||
@@ -70,7 +79,7 @@ public final class MarkovChain {
|
||||
public String generate(final int softCharacterLimit, final int hardCharacterLimit) {
|
||||
final StringBuilder newSentence = new StringBuilder();
|
||||
|
||||
try (final Jedis jedis = Crabstero.getJedis()) {
|
||||
try (final Jedis jedis = this.crabstero.getJedisPool().getResource()) {
|
||||
if (!jedis.exists(this.id + ":start")) {
|
||||
this.ingestSentence("Hello world!");
|
||||
}
|
||||
@@ -104,7 +113,7 @@ public final class MarkovChain {
|
||||
}
|
||||
|
||||
word = wordChoices.get(index);
|
||||
newSentence.append(" " + word);
|
||||
newSentence.append(" ").append(word);
|
||||
|
||||
final int sentenceLength = newSentence.length();
|
||||
if (sentenceLength >= hardCharacterLimit) {
|
||||
|
||||
@@ -25,22 +25,22 @@ import java.util.List;
|
||||
import java.util.Random;
|
||||
|
||||
public class MarkovChainMessages {
|
||||
private static final AllowedMentions allowedMentions;
|
||||
private static final Random rng = new SecureRandom();
|
||||
private final Crabstero crabstero;
|
||||
|
||||
private final AllowedMentions allowedMentions;
|
||||
private final Random rng = new SecureRandom();
|
||||
|
||||
public MarkovChainMessages(final Crabstero crabstero) {
|
||||
this.crabstero = crabstero;
|
||||
|
||||
static {
|
||||
final AllowedMentionsBuilder builder = new AllowedMentionsBuilder();
|
||||
builder.setMentionEveryoneAndHere(false);
|
||||
builder.setMentionRoles(false);
|
||||
builder.setMentionUsers(false);
|
||||
allowedMentions = builder.build();
|
||||
this.allowedMentions = builder.build();
|
||||
}
|
||||
|
||||
private MarkovChainMessages() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
public static void replyToMessage(final Message message) {
|
||||
public void replyToMessage(final Message message) {
|
||||
final TextChannel channel = message.getChannel();
|
||||
if (!channel.canYouWrite()) {
|
||||
return;
|
||||
@@ -55,20 +55,20 @@ public class MarkovChainMessages {
|
||||
|
||||
|
||||
final MessageBuilder response = new MessageBuilder();
|
||||
final MarkovChain markovChain = new MarkovChain(channelID);
|
||||
final MarkovChain markovChain = new MarkovChain(channelID, this.crabstero);
|
||||
|
||||
response.replyTo(message);
|
||||
response.setContent(markovChain.generate(750, 1000));
|
||||
|
||||
if (rng.nextDouble() >= 0.95 && channel.canYouEmbedLinks()) {
|
||||
if (this.rng.nextDouble() >= 0.95 && channel.canYouEmbedLinks()) {
|
||||
final EmbedBuilder embed = new EmbedBuilder();
|
||||
embed.setTitle(markovChain.generate(200, 300));
|
||||
embed.setDescription(markovChain.generate(300, 500));
|
||||
try (final Jedis jedis = Crabstero.getJedis()) {
|
||||
try (final Jedis jedis = this.crabstero.getJedisPool().getResource()) {
|
||||
final List<String> embedImageURLs = jedis.lrange(channelID + ":images", 0, -1);
|
||||
|
||||
if (embedImageURLs.size() > 0) {
|
||||
embed.setImage(embedImageURLs.get(rng.nextInt(embedImageURLs.size())));
|
||||
embed.setImage(embedImageURLs.get(this.rng.nextInt(embedImageURLs.size())));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -80,14 +80,14 @@ public class MarkovChainMessages {
|
||||
response.send(channel).exceptionally(ExceptionLogger.get());
|
||||
}
|
||||
|
||||
public static void ingestMessage(final Message message) {
|
||||
public void ingestMessage(final Message message) {
|
||||
final MessageAuthor author = message.getAuthor();
|
||||
if (author.isBotUser() || author.isWebhook() || message.getMentionedUsers().contains(message.getApi().getYourself())) {
|
||||
return;
|
||||
}
|
||||
|
||||
final long channelID = message.getChannel().getId();
|
||||
final MarkovChain markovChain = new MarkovChain(channelID);
|
||||
final MarkovChain markovChain = new MarkovChain(channelID, this.crabstero);
|
||||
|
||||
markovChain.ingest(message.getContent());
|
||||
|
||||
@@ -96,19 +96,14 @@ public class MarkovChainMessages {
|
||||
}
|
||||
}
|
||||
|
||||
public static void ingestEmbed(final long channelID, final Embed embed) {
|
||||
final MarkovChain markovChain = new MarkovChain(channelID);
|
||||
public void ingestEmbed(final long channelID, final Embed embed) {
|
||||
final MarkovChain markovChain = new MarkovChain(channelID, this.crabstero);
|
||||
|
||||
embed.getTitle().ifPresent((title) -> {
|
||||
markovChain.ingest(title);
|
||||
});
|
||||
|
||||
embed.getDescription().ifPresent((description) -> {
|
||||
markovChain.ingest(description);
|
||||
});
|
||||
embed.getTitle().ifPresent(markovChain::ingest);
|
||||
embed.getDescription().ifPresent(markovChain::ingest);
|
||||
|
||||
embed.getImage().ifPresent((image) -> {
|
||||
try (final Jedis jedis = Crabstero.getJedis()) {
|
||||
try (final Jedis jedis = this.crabstero.getJedisPool().getResource()) {
|
||||
jedis.lpush(channelID + ":images", image.getUrl().toString());
|
||||
}
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user