Skip to content

Commit db4a341

Browse files
committed
URL省略改善
1 parent b6d2ed6 commit db4a341

File tree

3 files changed

+146
-2
lines changed

3 files changed

+146
-2
lines changed

core/src/main/java/dev/felnull/itts/core/dict/AbbreviationDictionary.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@ public class AbbreviationDictionary implements Dictionary {
2424
* 正規表現関係
2525
*/
2626
private final RegexUtil regexUtil = new RegexUtil()
27-
.addOption(1, "ユーアルエルショウリャク", s -> {
27+
/*.addOption(1, "ユーアルエルショウリャク", s -> {
2828
Pattern pattern = Pattern.compile("https?://[\\w!?/+\\-_~=;.,*&@#$%()'\\[\\]]+");
2929
Matcher matcher = pattern.matcher(s);
3030
return matcher.find();
31-
})
31+
})*/
3232
.addOption(1, "ドメインショウリャク", s -> {
3333
Pattern pattern = Pattern.compile("^([a-zA-Z0-9][a-zA-Z0-9-]*[a-zA-Z0-9]*\\.)+[a-zA-Z]{2,}$");
3434
Matcher matcher = pattern.matcher(s);
@@ -56,9 +56,14 @@ public class AbbreviationDictionary implements Dictionary {
5656
return matcher.matches();
5757
});
5858

59+
/**
60+
* URLリプレーサー
61+
*/
62+
private final URLReplacer urlReplacer = new URLReplacer("ユーアルエルショウリャク");
5963

6064
@Override
6165
public @NotNull String apply(@NotNull String text, long guildId) {
66+
text = urlReplacer.replace(text);
6267
text = CODE_BLOCK_REGEX.matcher(text).replaceAll("コードブロックショウリャク");
6368
return regexUtil.replaceText(text);
6469
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package dev.felnull.itts.core.dict;
2+
3+
import java.util.regex.Pattern;
4+
5+
/**
6+
* URLを置き換える処理
7+
*/
8+
public class URLReplacer {
9+
10+
/**
11+
* URL抽出用正規表現
12+
*/
13+
private static final Pattern URL_REGEX = Pattern.compile("((?:ht|f)tps?://|(?<![\\p{L}0-9_.])www\\.)[-A-Za-z0-9+$&@#/%?=~_|!:,.;]*[^\\s ]*");
14+
15+
/**
16+
* 置き換えるテキスト
17+
*/
18+
private final String replacedText;
19+
20+
/**
21+
* コンストラクタ
22+
*
23+
* @param replacedText 置き換えるテキスト
24+
*/
25+
public URLReplacer(String replacedText) {
26+
this.replacedText = replacedText;
27+
}
28+
29+
/**
30+
* テキスト置き換え
31+
*
32+
* @param text テキスト
33+
* @return 置き換えられたテキスト
34+
*/
35+
public String replace(String text) {
36+
37+
if (text.contains("http") | text.contains("ftp")) {
38+
return URL_REGEX.matcher(text).replaceAll(replacedText);
39+
}
40+
41+
return text;
42+
}
43+
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package dev.felnull.itts.core.dict;
2+
3+
import org.junit.jupiter.api.Assertions;
4+
import org.junit.jupiter.params.ParameterizedTest;
5+
import org.junit.jupiter.params.provider.Arguments;
6+
import org.junit.jupiter.params.provider.MethodSource;
7+
8+
import java.util.Arrays;
9+
import java.util.stream.Stream;
10+
11+
public class URLReplacerTest {
12+
private static final String REPLACED_TEXT = "[URL_SYORYAKU]";
13+
14+
private static final TestEntry[] ENTRIES = new TestEntry[]{
15+
new TestEntry("https://www.google.com/", "[URL_SYORYAKU]"),
16+
new TestEntry("AAA https://www.google.com/ AAA", "AAA [URL_SYORYAKU] AAA"),
17+
new TestEntry("AAAhttps://www.google.com/aaa", "AAA[URL_SYORYAKU]"),
18+
new TestEntry("AAAhttps://www.google.com/ AAA", "AAA[URL_SYORYAKU] AAA"),
19+
new TestEntry("課長壊れる https://www.google.com/ 課長壊れる", "課長壊れる [URL_SYORYAKU] 課長壊れる"),
20+
new TestEntry("課長壊れるhttps://www.google.com/課長壊れる", "課長壊れる[URL_SYORYAKU]"),
21+
new TestEntry("課長壊れるhttps://www.google.com/ 課長壊れる", "課長壊れる[URL_SYORYAKU] 課長壊れる"),
22+
new TestEntry("課長壊れるhttps://www.google.com/ 課長壊れる", "課長壊れる[URL_SYORYAKU] 課長壊れる"),
23+
new TestEntry("課長壊れる https://www.google.com/ 課長壊れる", "課長壊れる [URL_SYORYAKU] 課長壊れる"),
24+
new TestEntry("""
25+
AAA
26+
https://www.google.com/
27+
AAA
28+
""", """
29+
AAA
30+
[URL_SYORYAKU]
31+
AAA
32+
"""),
33+
new TestEntry("""
34+
AAA
35+
AAA https://www.google.com/ AAA
36+
AAA
37+
""", """
38+
AAA
39+
AAA [URL_SYORYAKU] AAA
40+
AAA
41+
"""),
42+
new TestEntry("""
43+
AAA
44+
AAAhttps://www.google.com/aaa
45+
AAA
46+
""", """
47+
AAA
48+
AAA[URL_SYORYAKU]
49+
AAA
50+
"""),
51+
new TestEntry("""
52+
AAA
53+
AAAhttps://www.google.com AAA
54+
AAA
55+
""", """
56+
AAA
57+
AAA[URL_SYORYAKU] AAA
58+
AAA
59+
"""),
60+
new TestEntry("""
61+
https://www.google.com
62+
AAAhttps://www.google.com AAAhttps://www.google.com
63+
AAA
64+
""", """
65+
[URL_SYORYAKU]
66+
AAA[URL_SYORYAKU] AAA[URL_SYORYAKU]
67+
AAA
68+
"""),
69+
new TestEntry("https://www.google.com/ https://www.google.com/", "[URL_SYORYAKU] [URL_SYORYAKU]"),
70+
new TestEntry("AAA http://ikisugi.ad.jp/katyou/broken/aiki/#:~:text=野獣先輩 AAA", "AAA [URL_SYORYAKU] AAA"),
71+
new TestEntry("AAAhttp://ikisugi.ad.jp/katyou/broken/aiki/#:~:text=野獣先輩aaa", "AAA[URL_SYORYAKU]"),
72+
new TestEntry("AAAhttp://ikisugi.ad.jp/katyou/broken/aiki/#:~:text=野獣先輩 AAA", "AAA[URL_SYORYAKU] AAA"),
73+
new TestEntry("AAA https://www.google.com/114514 AAA", "AAA [URL_SYORYAKU] AAA"),
74+
new TestEntry("ftp://ikisugi.tokyo/", "[URL_SYORYAKU]"),
75+
new TestEntry("AAA ftp://ikisugi.tokyo/ AAA", "AAA [URL_SYORYAKU] AAA"),
76+
new TestEntry("https://www.google.com", "[URL_SYORYAKU]"),
77+
new TestEntry("AAA https://www.google.com AAA", "AAA [URL_SYORYAKU] AAA"),
78+
new TestEntry("http://www.google.com/", "[URL_SYORYAKU]"),
79+
new TestEntry("AAA http://www.google.com/ AAA", "AAA [URL_SYORYAKU] AAA"),
80+
};
81+
82+
@ParameterizedTest
83+
@MethodSource("entries")
84+
void testReplace(String before, String after) {
85+
URLReplacer replacer = new URLReplacer(REPLACED_TEXT);
86+
String ret = replacer.replace(before);
87+
Assertions.assertEquals(after, ret);
88+
}
89+
90+
private static Stream<Arguments> entries() {
91+
return Arrays.stream(ENTRIES).map(it -> Arguments.arguments(it.before, it.after));
92+
}
93+
94+
private record TestEntry(String before, String after) {
95+
}
96+
}

0 commit comments

Comments
 (0)