1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
|
/*
* The MIT License (MIT)
*
* Copyright (c) 2012-2014 lacolaco.net
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package net.lacolaco.smileessence.util;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Morse {
// ------------------------------ FIELDS ------------------------------
private static final HashMap<String, String> mcJa;
private static final Pattern morsePattern = Pattern.compile("[-・]+");
// -------------------------- STATIC METHODS --------------------------
/**
* 与えられた文字列がモールス文を含むかどうかを返す
*
* @param mc 判定する文字列
* @return モールスを含むならtrue, そうでなければfalse
*/
public static boolean isMorse(String mc) {
Matcher matcher = morsePattern.matcher(mc);
ArrayList<String> list = new ArrayList<>();
while (matcher.find()) {
list.add(matcher.group());
}
if (list.size() <= 2) {
return false;
} else {
for (String s : list) {
if (!"・・・".equals(s) && !"・・".equals(s) && !"・".equals(s)) {
return true;
}
}
return false;
}
}
/**
* 和文モールスをカタカナ・数字に復元する *
*
* @param str 復元したい文字列
* @return 復元部分が置換された文字列
*/
public static String morseToJa(String str) {
String[] strArr = toRightMorse(str).split(" ");
StringBuilder sb = new StringBuilder();
for (String tok : strArr) {
sb.append(mcJa.containsKey(tok) ? mcJa.get(tok) : tok);
}
return sb.toString();
}
private static String toRightMorse(String str) {
str = str.replace("‐", "-").replace(" ", " ").trim();
Pattern pattern = Pattern.compile("[^・- ][・-]");
StringBuilder sb = new StringBuilder(str);
Matcher matcher = pattern.matcher(str);
while (matcher.find()) {
int i = matcher.start();
sb.insert(i + 1, " ");
matcher.reset(sb);
}
pattern = Pattern.compile("[・-][^・- ]");
matcher = pattern.matcher(sb);
while (matcher.find()) {
int i = matcher.start();
sb.insert(i + 1, " ");
matcher.reset(sb);
}
return sb.toString();
}
static {
mcJa = new HashMap<>();
String[][] ja2 = {{"・-", "イ"}, {"・-・-", "ロ"}, {"-・・・", "ハ"}, {"-・-・", "ニ"}, {"-・・", "ホ"}, {"・", "ヘ"}, {"・・-・・", "ト"}, {"・・-・", "チ"}, {"--・", "リ"}, {"・・・・", "ヌ"}, {"-・--・", "ル"}, {"・---", "ヲ"}, {"-・-", "ワ"}, {"・-・・", "カ"}, {"--", "ヨ"}, {"-・", "タ"}, {"---", "レ"}, {"---・", "ソ"}, {"・--・", "ツ"}, {"--・-", "ネ"}, {"・-・", "ナ"}, {"・・・", "ラ"}, {"-", "ム"}, {"・・-", "ウ"}, {"・-・・-", "ヰ"}, {"・・--", "ノ"}, {"・-・・・", "オ"}, {"・・・-", "ク"}, {"・--", "ヤ"}, {"-・・-", "マ"}, {"-・--", "ケ"}, {"--・・", "フ"}, {"----", "コ"}, {"-・---", "エ"}, {"・-・--", "テ"}, {"--・--", "ア"}, {"-・-・-", "サ"}, {"-・-・・", "キ"}, {"-・・--", "ユ"}, {"-・・・-", "メ"}, {"・・-・-", "ミ"}, {"--・-・", "シ"}, {"・--・・", "ヱ"}, {"--・・-", "ヒ"}, {"-・・-・", "モ"}, {"・---・", "セ"}, {"---・-", "ス"}, {"・-・-・", "ン"}, {"・・", "゛"}, {"・・--・", "゜"}, {"・--・-", "ー"}, {"・-・-・-", "、"}, {"-・--・-", "("}, {"・-・・-・", ")"}, {"・----", "1"}, {"・・---", "2"}, {"・・・--", "3"}, {"・・・・-", "4"}, {"・・・・・", "5"}, {"-・・・・", "6"}, {"--・・・", "7"}, {"---・・", "8"}, {"----・", "9"}, {"-----", "0"}, {"", ""}};
for (String[] pr : ja2) {
mcJa.put(pr[0], pr[1]);
}
}
}
|