aboutsummaryrefslogtreecommitdiffstats
path: root/app/src/main/java/net/lacolaco/smileessence/util/Morse.java
blob: 7aa5f697111046bd0e36169cc342e73a4e4a3615 (plain)
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*
 * 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 HashMap<String, String> jaMc;
    private static HashMap<String, String> mcJa;

    // -------------------------- STATIC METHODS --------------------------

    /**
     * 与えられた文字列がモールス文を含むかどうかを返す
     *
     * @param mc 判定する文字列
     * @return モールスを含むならtrue, そうでなければfalse
     */
    public static boolean isMorse(String mc) {
        Pattern pattern = Pattern.compile("[-・]+");
        Matcher matcher = pattern.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();
    }

    /**
     * ひらがな・カタカナ・数字をモールス文に変換する
     *
     * @param str 変換したい文字列
     * @return 変換部分が置換された文字列
     */
    public static String jaToMorse(String str) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < str.length(); i++) {
            String tok = String.valueOf(str.charAt(i));
            if (jaMc.containsKey(tok)) {
                sb.append(jaMc.get(tok));
                sb.append(" ");
            } else {
                sb.append(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 {
        jaMc = new HashMap<>();
        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]);
            jaMc.put(pr[1], pr[0]);
        }

        String[][] ja1 = {{"い", "・-"}, {"ィ", "・-"}, {"ぃ", "・-"}, {"ろ", "・-・-"}, {"は", "-・・・"}, {"に", "-・-・"}, {"ほ", "-・・"}, {"へ", "・"}, {"と", "・・-・・"}, {"ち", "・・-・"}, {"り", "--・"}, {"ぬ", "・・・・"}, {"る", "-・--・"}, {"を", "・---"}, {"わ", "-・-"}, {"ヮ", "-・-"}, {"ゎ", "-・-"}, {"か", "・-・・"}, {"ヵ", "・-・・"}, {"よ", "--"}, {"ョ", "--"}, {"ょ", "--"}, {"た", "-・"}, {"れ", "---"}, {"そ", "---・"}, {"つ", "・--・"}, {"ッ", "・--・"}, {"っ", "・--・"}, {"ね", "--・-"}, {"な", "・-・"}, {"ら", "・・・"}, {"む", "-"}, {"う", "・・-"}, {"ゥ", "・・-"}, {"ぅ", "・・-"}, {"ゐ", "・-・・-"}, {"の", "・・--"}, {"お", "・-・・・"}, {"ォ", "・-・・・"}, {"ぉ", "・-・・・"}, {"く", "・・・-"}, {"や", "・--"}, {"ャ", "・--"}, {"ゃ", "・--"}, {"ま", "-・・-"}, {"け", "-・--"}, {"ヶ", "-・--"}, {"ふ", "--・・"}, {"こ", "----"}, {"え", "-・---"}, {"ェ", "-・---"}, {"ぇ", "-・---"}, {"て", "・-・--"}, {"あ", "--・--"}, {"ァ", "--・--"}, {"ぁ", "--・--"}, {"さ", "-・-・-"}, {"き", "-・-・・"}, {"ゆ", "-・・--"}, {"ュ", "-・・--"}, {"ゅ", "-・・--"}, {"め", "-・・・-"}, {"み", "・・-・-"}, {"し", "--・-・"}, {"ゑ", "・--・・"}, {"ひ", "--・・-"}, {"も", "-・・-・"}, {"せ", "・---・"}, {"す", "---・-"}, {"ん", "・-・-・"}, {"ガ", "・-・・ ・・"}, {"が", "・-・・ ・・"}, {"ギ", "-・-・・ ・・"}, {"ぎ", "-・-・・ ・・"}, {"グ", "・・・- ・・"}, {"ぐ", "・・・- ・・"}, {"ゲ", "-・-- ・・"}, {"げ", "-・-- ・・"}, {"ゴ", "---- ・・"}, {"ご", "---- ・・"}, {"ザ", "-・-・- ・・"}, {"ざ", "-・-・- ・・"}, {"ジ", "--・-・ ・・"}, {"じ", "--・-・ ・・"}, {"ズ", "---・- ・・"}, {"ず", "---・- ・・"}, {"ゼ", "・---・ ・・"}, {"ぜ", "・---・ ・・"}, {"ゾ", "---・ ・・"}, {"ぞ", "---・ ・・"}, {"ダ", "-・ ・・"}, {"だ", "-・ ・・"}, {"ヂ", "・・-・ ・・"}, {"ぢ", "・・-・ ・・"}, {"ヅ", "・--・ ・・"}, {"づ", "・--・ ・・"}, {"デ", "・-・-- ・・"}, {"で", "・-・-- ・・"}, {"ド", "・・-・・ ・・"}, {"ど", "・・-・・ ・・"}, {"バ", "-・・・ ・・"}, {"ば", "-・・・ ・・"}, {"ビ", "--・・- ・・"}, {"び", "--・・- ・・"}, {"ブ", "--・・ ・・"}, {"ぶ", "--・・ ・・"}, {"ベ", "・ ・・"}, {"べ", "・ ・・"}, {"ボ", "-・・ ・・"}, {"ぼ", "-・・ ・・"}, {"パ", "-・・・ ・・--・"}, {"ぱ", "-・・・ ・・--・"}, {"ピ", "--・・- ・・--・"}, {"ぴ", "--・・- ・・--・"}, {"プ", "--・・ ・・--・"}, {"ぷ", "--・・ ・・--・"}, {"ペ", "・ ・・--・"}, {"ぺ", "・ ・・--・"}, {"ポ", "-・・ ・・--・"}, {"ぽ", "-・・ ・・--・"}, {"ヴ", "・・- ・・"}};
        for (String[] to : ja1)
            jaMc.put(to[0], to[1]);
    }

}