Skip to content

Instantly share code, notes, and snippets.

@tesu
Created August 22, 2017 05:57
Show Gist options
  • Save tesu/b937d92da01cb50c25dbd85a43a20868 to your computer and use it in GitHub Desktop.
Save tesu/b937d92da01cb50c25dbd85a43a20868 to your computer and use it in GitHub Desktop.
From a254a9a40c75b172ae1abb8e75f531208df392c9 Mon Sep 17 00:00:00 2001
From: Jason Lam <3edgy6u@gmail.com>
Date: Sun, 13 Sep 2015 12:22:21 -0400
Subject: [PATCH] added furigana conversion
---
.../java/com/geecko/QuickLyric/lyrics/JLyric.java | 25 +++++++++++++++++++++-
1 file changed, 24 insertions(+), 1 deletion(-)
diff --git a/QuickLyric/src/main/java/com/geecko/QuickLyric/lyrics/JLyric.java b/QuickLyric/src/main/java/com/geecko/QuickLyric/lyrics/JLyric.java
index 83251c4b..072e0aed 100644
--- a/QuickLyric/src/main/java/com/geecko/QuickLyric/lyrics/JLyric.java
+++ b/QuickLyric/src/main/java/com/geecko/QuickLyric/lyrics/JLyric.java
@@ -25,9 +25,11 @@
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
+import org.jsoup.nodes.TextNode;
import java.io.IOException;
import java.net.URLEncoder;
+import java.util.List;
import java.util.ArrayList;
@Reflection
@@ -107,6 +109,27 @@ public static Lyrics fromURL(String url, String artist, String song) {
Document lyricsPage = Jsoup.connect(url).get();
if (!lyricsPage.location().contains(domain))
throw new IOException("Redirected to wrong domain " + lyricsPage.location());
+
+ if (lyricsPage.select("p#lyricBody").html() != null) {
+ List<TextNode> lines = lyricsPage.select("p#lyricBody").get(0).textNodes();
+ for (TextNode line : lines) {
+ Document furiganaApi = Jsoup.connect(String.format("http://jlp.yahooapis.jp/FuriganaService/V1/furigana?appid=%1$s&grade=1&sentence=%2$s",
+ "API_KEY_GOES_HERE",
+ URLEncoder.encode(line.text(), "UTF-8"))).get();
+ Elements words = furiganaApi.select("WordList > Word");
+ line.text("");
+ String d = " ";
+ for (Element word : words) {
+ if (word.select("Word > Furigana").size() > 0) {
+ line.text(line.text() + d + word.select("Word > Furigana").text());
+ } else {
+ line.text(line.text() + d + word.select("Word > Surface").text());
+ }
+ d = " ";
+ }
+ }
+ }
+
text = lyricsPage.select("p#lyricBody").html();
if (artist == null)
artist = lyricsPage.select("div.body")
@@ -130,4 +153,4 @@ public static Lyrics fromURL(String url, String artist, String song) {
}
-}
\ No newline at end of file
+}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment