use of net.robinfriedli.stringlist.StringList in project aiode by robinfriedli.
the class MessageService method separateMessage.
private List<String> separateMessage(String message) {
List<String> outputParts = Lists.newArrayList();
StringList paragraphs = StringList.separateString(message, "\n");
for (int i = 0; i < paragraphs.size(); i++) {
String paragraph = paragraphs.get(i);
if (paragraph.length() + System.lineSeparator().length() < limit) {
// check that paragraph is not an empty line
if (!paragraph.isBlank()) {
if (i < paragraphs.size() - 1)
paragraph = paragraph + System.lineSeparator();
fillPart(outputParts, paragraph);
}
} else {
// if the paragraph is too long separate into sentences
StringList sentences = StringList.separateString(paragraph, "\\. ");
for (String sentence : sentences) {
if (sentence.length() < limit) {
fillPart(outputParts, sentence);
} else {
// if the sentence is too long split into words
StringList words = StringList.separateString(sentence, " ");
for (String word : words) {
if (word.length() < limit) {
fillPart(outputParts, word);
} else {
StringList chars = StringList.splitChars(word);
for (String charString : chars) {
fillPart(outputParts, charString);
}
}
}
}
}
}
}
return outputParts;
}
use of net.robinfriedli.stringlist.StringList in project aiode by robinfriedli.
the class UpdateCommand method getInputStreamString.
private String getInputStreamString(InputStream inputStream) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
StringList lineList = StringList.create();
String s;
while ((s = bufferedReader.readLine()) != null) {
lineList.add(s);
}
return lineList.toSeparatedString(System.lineSeparator());
}
use of net.robinfriedli.stringlist.StringList in project aiode by robinfriedli.
the class AbstractCommand method splitInlineArgument.
@Deprecated
protected Pair<String, String> splitInlineArgument(String part, String argument) {
StringList words = StringList.createWithRegex(part, " ");
List<Integer> positions = words.findPositionsOf("$" + argument, true);
if (positions.isEmpty()) {
throw new InvalidCommandException("Expected inline argument: " + argument);
}
int position = positions.get(0);
if (position == 0 || position == words.size() - 1) {
throw new InvalidCommandException("No input before or after inline argument: " + argument);
}
String left = words.subList(0, position).toSeparatedString(" ");
String right = words.subList(position + 1, words.size()).toSeparatedString(" ");
if (left.isBlank() || right.isBlank()) {
throw new InvalidCommandException("No input before or after inline argument: " + argument);
}
return Pair.of(left.trim(), right.trim());
}
use of net.robinfriedli.stringlist.StringList in project aiode by robinfriedli.
the class RemoveCommand method doRun.
@Override
public void doRun() {
Session session = getContext().getSession();
String playlistName = getArgumentValue("from");
Playlist playlist = SearchEngine.searchLocalList(session, playlistName);
if (playlist == null) {
throw new NoResultsFoundException(String.format("No local list found for '%s'", playlistName));
} else if (playlist.isEmpty()) {
throw new InvalidCommandException("Playlist is empty");
}
if (argumentSet("index")) {
StringList indices = StringList.createWithRegex(getCommandInput(), "-");
if (indices.size() == 1 || indices.size() == 2) {
indices.applyForEach(String::trim);
if (indices.size() == 1) {
int index = parse(indices.get(0));
checkIndex(index, playlist);
invoke(() -> session.delete(playlist.getItemsSorted().get(index - 1)));
} else {
int start = parse(indices.get(0));
int end = parse(indices.get(1));
checkIndex(start, playlist);
checkIndex(end, playlist);
if (end <= start) {
throw new InvalidCommandException("End index needs to be greater than start.");
}
invoke(() -> playlist.getItemsSorted().subList(start - 1, end).forEach(session::delete));
}
} else {
throw new InvalidCommandException("Expected one or two indices but found " + indices.size());
}
} else {
List<PlaylistItem> playlistItems = SearchEngine.searchPlaylistItems(playlist, getCommandInput());
if (playlistItems.size() == 1) {
invoke(() -> session.delete(playlistItems.get(0)));
} else if (playlistItems.isEmpty()) {
throw new NoResultsFoundException(String.format("No tracks found for '%s' on list '%s'", getCommandInput(), playlistName));
} else {
askQuestion(playlistItems, PlaylistItem::display, item -> valueOf(item.getIndex() + 1));
}
}
}
use of net.robinfriedli.stringlist.StringList in project aiode by robinfriedli.
the class YouTubeService method getBestMatch.
private Video getBestMatch(List<Video> videos, SpotifyTrack spotifyTrack, StringList artists) {
Video video;
int size = videos.size();
if (size == 1) {
video = videos.get(0);
} else {
Map<Integer, Video> videosByScore = new HashMap<>();
Map<Video, Integer> editDistanceMap = new HashMap<>();
long[] viewCounts = new long[size];
for (int i = 0; i < size; i++) {
Video v = videos.get(i);
viewCounts[i] = getViewCount(v);
editDistanceMap.put(v, getBestEditDistance(spotifyTrack, v));
}
int index = 0;
for (Video v : videos) {
int artistMatchScore = 0;
if (artists.stream().anyMatch(a -> {
String artist = a.toLowerCase();
String artistNoSpace = artist.replaceAll(" ", "");
VideoSnippet snippet = v.getSnippet();
if (snippet == null) {
return false;
}
String channelTitle = snippet.getChannelTitle();
if (channelTitle == null) {
return false;
}
String channel = channelTitle.toLowerCase();
String channelNoSpace = channel.replace(" ", "");
return channel.contains(artist) || artist.contains(channel) || channelNoSpace.contains(artistNoSpace) || artistNoSpace.contains(channelNoSpace);
})) {
artistMatchScore = ARTIST_MATCH_SCORE_MULTIPLIER * size;
}
long viewCount = getViewCount(v);
int editDistance = editDistanceMap.get(v);
long viewRank = Arrays.stream(viewCounts).filter(c -> viewCount < c).count();
long editDistanceRank = editDistanceMap.values().stream().filter(d -> d < editDistance).count();
int viewScore = VIEW_SCORE_MULTIPLIER * (int) (size - viewRank);
int editDistanceScore = EDIT_DISTANCE_SCORE_MULTIPLIER * (int) (size - editDistanceRank);
int indexScore = INDEX_SCORE_MULTIPLIER * (size - index);
int totalScore = artistMatchScore + viewScore + editDistanceScore + indexScore;
videosByScore.putIfAbsent(totalScore, v);
++index;
}
@SuppressWarnings("OptionalGetWithoutIsPresent") int bestScore = videosByScore.keySet().stream().mapToInt(k -> k).max().getAsInt();
video = videosByScore.get(bestScore);
}
return video;
}
Aggregations