Reformatted code.
This commit is contained in:
@@ -17,107 +17,107 @@ import java.util.List;
|
||||
import java.util.Random;
|
||||
|
||||
public final class MarkovChain {
|
||||
private static final char DEFAULT_SENTENCE_END = '§';
|
||||
private static final Random rng = new SecureRandom();
|
||||
private static final char DEFAULT_SENTENCE_END = '§';
|
||||
private static final Random rng = new SecureRandom();
|
||||
|
||||
private final long id;
|
||||
private final long id;
|
||||
|
||||
public MarkovChain(final long id){
|
||||
this.id = id;
|
||||
}
|
||||
public MarkovChain(final long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
private static final boolean isCompleteSentence(final String sentence){
|
||||
if (sentence.isEmpty()){
|
||||
return false;
|
||||
}
|
||||
private static final boolean isCompleteSentence(final String sentence) {
|
||||
if (sentence.isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
final char lastChar = sentence.charAt(sentence.length() - 1);
|
||||
return (lastChar == DEFAULT_SENTENCE_END || lastChar == '.' || lastChar == '!' || lastChar == '?');
|
||||
}
|
||||
final char lastChar = sentence.charAt(sentence.length() - 1);
|
||||
return (lastChar == DEFAULT_SENTENCE_END || lastChar == '.' || lastChar == '!' || lastChar == '?');
|
||||
}
|
||||
|
||||
public void ingest(String paragraph){
|
||||
if (!isCompleteSentence(paragraph)){
|
||||
paragraph += DEFAULT_SENTENCE_END;
|
||||
}
|
||||
public void ingest(String paragraph) {
|
||||
if (!isCompleteSentence(paragraph)) {
|
||||
paragraph += DEFAULT_SENTENCE_END;
|
||||
}
|
||||
|
||||
final String[] sentences = paragraph.trim().replaceAll(" +", " ").replaceAll("\n", " ").split("(?<=[.!?]) ");
|
||||
final String[] sentences = paragraph.trim().replaceAll(" +", " ").replaceAll("\n", " ").split("(?<=[.!?]) ");
|
||||
|
||||
for (int i = 0; i < sentences.length; i++){
|
||||
this.ingestSentence(sentences[i]);
|
||||
}
|
||||
}
|
||||
for (int i = 0; i < sentences.length; i++) {
|
||||
this.ingestSentence(sentences[i]);
|
||||
}
|
||||
}
|
||||
|
||||
private void ingestSentence(String sentence){
|
||||
if (!isCompleteSentence(sentence)){
|
||||
sentence += DEFAULT_SENTENCE_END;
|
||||
}
|
||||
private void ingestSentence(String sentence) {
|
||||
if (!isCompleteSentence(sentence)) {
|
||||
sentence += DEFAULT_SENTENCE_END;
|
||||
}
|
||||
|
||||
String[] words = sentence.trim().replaceAll(" +", " ").split(" ");
|
||||
String[] words = sentence.trim().replaceAll(" +", " ").split(" ");
|
||||
|
||||
try (final Jedis jedis = Crabstero.getJedis()){
|
||||
final Pipeline pipeline = jedis.pipelined();
|
||||
for (int i = 0; i < words.length - 1; i++){
|
||||
if (i == 0){
|
||||
pipeline.lpush(this.id + ":start", words[i]);
|
||||
pipeline.lpush(this.id + "::" + words[i], words[i + 1]);
|
||||
} else {
|
||||
pipeline.lpush(this.id + "::" + words[i], words[i + 1]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
try (final Jedis jedis = Crabstero.getJedis()) {
|
||||
final Pipeline pipeline = jedis.pipelined();
|
||||
for (int i = 0; i < words.length - 1; i++) {
|
||||
if (i == 0) {
|
||||
pipeline.lpush(this.id + ":start", words[i]);
|
||||
pipeline.lpush(this.id + "::" + words[i], words[i + 1]);
|
||||
} else {
|
||||
pipeline.lpush(this.id + "::" + words[i], words[i + 1]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public String generate(final int softCharacterLimit, final int hardCharacterLimit){
|
||||
final StringBuilder newSentence = new StringBuilder();
|
||||
public String generate(final int softCharacterLimit, final int hardCharacterLimit) {
|
||||
final StringBuilder newSentence = new StringBuilder();
|
||||
|
||||
try (final Jedis jedis = Crabstero.getJedis()){
|
||||
if (!jedis.exists(this.id + ":start")){
|
||||
this.ingestSentence("Hello world!");
|
||||
}
|
||||
try (final Jedis jedis = Crabstero.getJedis()) {
|
||||
if (!jedis.exists(this.id + ":start")) {
|
||||
this.ingestSentence("Hello world!");
|
||||
}
|
||||
|
||||
String word = "";
|
||||
String word = "";
|
||||
|
||||
final List<String> startingWords = jedis.lrange(this.id + ":start", 0, -1);
|
||||
int index = rng.nextInt(startingWords.size());
|
||||
word = startingWords.get(index);
|
||||
newSentence.append(word);
|
||||
final List<String> startingWords = jedis.lrange(this.id + ":start", 0, -1);
|
||||
int index = rng.nextInt(startingWords.size());
|
||||
word = startingWords.get(index);
|
||||
newSentence.append(word);
|
||||
|
||||
while (!isCompleteSentence(word)){
|
||||
final List<String> wordChoices = jedis.lrange(this.id + "::" + word, 0, -1);
|
||||
while (!isCompleteSentence(word)) {
|
||||
final List<String> wordChoices = jedis.lrange(this.id + "::" + word, 0, -1);
|
||||
|
||||
index = -1;
|
||||
if (newSentence.length() >= softCharacterLimit){
|
||||
for (int i = 0; i < wordChoices.size(); i++){
|
||||
final String candidate = wordChoices.get(i);
|
||||
index = -1;
|
||||
if (newSentence.length() >= softCharacterLimit) {
|
||||
for (int i = 0; i < wordChoices.size(); i++) {
|
||||
final String candidate = wordChoices.get(i);
|
||||
|
||||
if (isCompleteSentence(candidate)){
|
||||
index = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (isCompleteSentence(candidate)) {
|
||||
index = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (index == -1){
|
||||
index = rng.nextInt(wordChoices.size());
|
||||
}
|
||||
} else {
|
||||
index = rng.nextInt(wordChoices.size());
|
||||
}
|
||||
if (index == -1) {
|
||||
index = rng.nextInt(wordChoices.size());
|
||||
}
|
||||
} else {
|
||||
index = rng.nextInt(wordChoices.size());
|
||||
}
|
||||
|
||||
word = wordChoices.get(index);
|
||||
newSentence.append(" " + word);
|
||||
word = wordChoices.get(index);
|
||||
newSentence.append(" " + word);
|
||||
|
||||
final int sentenceLength = newSentence.length();
|
||||
if (sentenceLength >= hardCharacterLimit){
|
||||
newSentence.delete(hardCharacterLimit, sentenceLength);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
final int sentenceLength = newSentence.length();
|
||||
if (sentenceLength >= hardCharacterLimit) {
|
||||
newSentence.delete(hardCharacterLimit, sentenceLength);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (newSentence.charAt(newSentence.length() - 1) == DEFAULT_SENTENCE_END){
|
||||
return newSentence.deleteCharAt(newSentence.length() - 1).toString();
|
||||
} else {
|
||||
return newSentence.toString();
|
||||
}
|
||||
}
|
||||
if (newSentence.charAt(newSentence.length() - 1) == DEFAULT_SENTENCE_END) {
|
||||
return newSentence.deleteCharAt(newSentence.length() - 1).toString();
|
||||
} else {
|
||||
return newSentence.toString();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -25,86 +25,86 @@ import java.util.List;
|
||||
import java.util.Random;
|
||||
|
||||
public class MarkovChainMessages {
|
||||
private static final AllowedMentions allowedMentions;
|
||||
private static final Random rng = new SecureRandom();
|
||||
private static final AllowedMentions allowedMentions;
|
||||
private static final Random rng = new SecureRandom();
|
||||
|
||||
static {
|
||||
final AllowedMentionsBuilder builder = new AllowedMentionsBuilder();
|
||||
builder.setMentionEveryoneAndHere(false);
|
||||
builder.setMentionRoles(false);
|
||||
builder.setMentionUsers(false);
|
||||
allowedMentions = builder.build();
|
||||
}
|
||||
static {
|
||||
final AllowedMentionsBuilder builder = new AllowedMentionsBuilder();
|
||||
builder.setMentionEveryoneAndHere(false);
|
||||
builder.setMentionRoles(false);
|
||||
builder.setMentionUsers(false);
|
||||
allowedMentions = builder.build();
|
||||
}
|
||||
|
||||
private MarkovChainMessages(){
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
private MarkovChainMessages() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
public static void replyToMessage(final Message message){
|
||||
final TextChannel channel = message.getChannel();
|
||||
if (!channel.canYouWrite()){
|
||||
return;
|
||||
}
|
||||
public static void replyToMessage(final Message message) {
|
||||
final TextChannel channel = message.getChannel();
|
||||
if (!channel.canYouWrite()) {
|
||||
return;
|
||||
}
|
||||
|
||||
final long channelID = channel.getId();
|
||||
final long channelID = channel.getId();
|
||||
|
||||
final MessageBuilder response = new MessageBuilder();
|
||||
final MarkovChain markovChain = new MarkovChain(channelID);
|
||||
final MessageBuilder response = new MessageBuilder();
|
||||
final MarkovChain markovChain = new MarkovChain(channelID);
|
||||
|
||||
response.replyTo(message);
|
||||
response.setContent(markovChain.generate(750, 1000));
|
||||
response.replyTo(message);
|
||||
response.setContent(markovChain.generate(750, 1000));
|
||||
|
||||
if (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()){
|
||||
final List<String> embedImageURLs = jedis.lrange(channelID + ":images", 0, -1);
|
||||
if (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()) {
|
||||
final List<String> embedImageURLs = jedis.lrange(channelID + ":images", 0, -1);
|
||||
|
||||
if (embedImageURLs.size() > 0){
|
||||
embed.setImage(embedImageURLs.get(rng.nextInt(embedImageURLs.size())));
|
||||
}
|
||||
}
|
||||
if (embedImageURLs.size() > 0) {
|
||||
embed.setImage(embedImageURLs.get(rng.nextInt(embedImageURLs.size())));
|
||||
}
|
||||
}
|
||||
|
||||
embed.setFooter("Crabstero is a logal.dev project", "https://logal.dev/images/logo.png");
|
||||
response.setEmbed(embed);
|
||||
}
|
||||
embed.setFooter("Crabstero is a logal.dev project", "https://logal.dev/images/logo.png");
|
||||
response.setEmbed(embed);
|
||||
}
|
||||
|
||||
response.setAllowedMentions(allowedMentions);
|
||||
response.send(channel).exceptionally(ExceptionLogger.get());
|
||||
}
|
||||
response.setAllowedMentions(allowedMentions);
|
||||
response.send(channel).exceptionally(ExceptionLogger.get());
|
||||
}
|
||||
|
||||
public static void ingestMessage(final Message message){
|
||||
final MessageAuthor author = message.getAuthor();
|
||||
if (author.isBotUser() || author.isWebhook() || message.getMentionedUsers().contains(message.getApi().getYourself())){
|
||||
return;
|
||||
}
|
||||
public static 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 long channelID = message.getChannel().getId();
|
||||
final MarkovChain markovChain = new MarkovChain(channelID);
|
||||
|
||||
markovChain.ingest(message.getContent());
|
||||
markovChain.ingest(message.getContent());
|
||||
|
||||
for (final Embed embed : message.getEmbeds()){
|
||||
ingestEmbed(channelID, embed);
|
||||
}
|
||||
}
|
||||
for (final Embed embed : message.getEmbeds()) {
|
||||
ingestEmbed(channelID, embed);
|
||||
}
|
||||
}
|
||||
|
||||
public static void ingestEmbed(final long channelID, final Embed embed){
|
||||
final MarkovChain markovChain = new MarkovChain(channelID);
|
||||
public static void ingestEmbed(final long channelID, final Embed embed) {
|
||||
final MarkovChain markovChain = new MarkovChain(channelID);
|
||||
|
||||
embed.getTitle().ifPresent((title) -> {
|
||||
markovChain.ingest(title);
|
||||
});
|
||||
embed.getTitle().ifPresent((title) -> {
|
||||
markovChain.ingest(title);
|
||||
});
|
||||
|
||||
embed.getDescription().ifPresent((description) -> {
|
||||
markovChain.ingest(description);
|
||||
});
|
||||
embed.getDescription().ifPresent((description) -> {
|
||||
markovChain.ingest(description);
|
||||
});
|
||||
|
||||
embed.getImage().ifPresent((image) -> {
|
||||
try (final Jedis jedis = Crabstero.getJedis()){
|
||||
jedis.lpush(channelID + ":images", image.getUrl().toString());
|
||||
}
|
||||
});
|
||||
}
|
||||
embed.getImage().ifPresent((image) -> {
|
||||
try (final Jedis jedis = Crabstero.getJedis()) {
|
||||
jedis.lpush(channelID + ":images", image.getUrl().toString());
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user