3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

文字列操作でありますまいか

Last updated at Posted at 2020-12-29

調べていたら雑多なメモになってしまった。色々な文字列分割。

  1. キャメルケースとかスネークケースとか
  2. 日本の姓名分割
  3. 日本の住所分割
  4. 中国の etc..

という雑文です。

キャメルケースとスネークケースの文字列変換

使い方はURL参照。https://kagamihoge.hatenablog.com/entry/2017/01/03/225054
速度計測とか、コーディング内容比較してみようかと思ったのだが、CamelCase と snake_case を相互変換する に既に存在したのでそちら参照のこと。よって以下ソースコードのみ参照のこと。

Google Guava

  private static String firstCharOnlyToUpper(String word) {
    return word.isEmpty()
        ? word
        : Ascii.toUpperCase(word.charAt(0)) + Ascii.toLowerCase(word.substring(1));
  }

Commons Lang

    private static String[] splitByCharacterType(final String str, final boolean camelCase) {
        if (str == null) {
            return null;
        }
        if (str.isEmpty()) {
            return ArrayUtils.EMPTY_STRING_ARRAY;
        }
        final char[] c = str.toCharArray();
        final List<String> list = new ArrayList<>();
        int tokenStart = 0;
        int currentType = Character.getType(c[tokenStart]);
        for (int pos = tokenStart + 1; pos < c.length; pos++) {
            final int type = Character.getType(c[pos]);
            if (type == currentType) {
                continue;
            }
            if (camelCase && type == Character.LOWERCASE_LETTER && currentType == Character.UPPERCASE_LETTER) {
                final int newTokenStart = pos - 1;
                if (newTokenStart != tokenStart) {
                    list.add(new String(c, tokenStart, newTokenStart - tokenStart));
                    tokenStart = newTokenStart;
                }
            } else {
                list.add(new String(c, tokenStart, pos - tokenStart));
                tokenStart = pos;
            }
            currentType = type;
        }
        list.add(new String(c, tokenStart, c.length - tokenStart));
        return list.toArray(ArrayUtils.EMPTY_STRING_ARRAY);
    }

ModeShape

    public String underscore( String camelCaseWord,
                              char... delimiterChars ) {
        if (camelCaseWord == null) return null;
        String result = camelCaseWord.trim();
        if (result.length() == 0) return "";
        result = result.replaceAll("([A-Z]+)([A-Z][a-z])", "$1_$2");
        result = result.replaceAll("([a-z\\d])([A-Z])", "$1_$2");
        result = result.replace('-', '_');
        if (delimiterChars != null) {
            for (char delimiterChar : delimiterChars) {
                result = result.replace(delimiterChar, '_');
            }
        }
        return result.toLowerCase();
    }

Java Case Converter

https://github.com/toolpage/java-case-converter
https://en.toolpage.org/cat/case-converter
https://github.com/toolpage/java-case-converter/blob/master/src/org/toolpage/util/text/CaseConverter.java#L121

	public static String convertToSnakeCase(String value) {
		String throwAwayChars = "()[]{}=?!.:,-_+\\\"#~/";
		value = value.replaceAll("[" + Pattern.quote(throwAwayChars) + "]", " ");
		value = CaseConverter.convertToStartCase(value);
		return value.trim().replaceAll("\\s+", "_");
	}

Netbeans Case Converter

NetBeansのプラグイン
https://github.com/eviweb/netbeans-case-converter

日本の姓名分割

と、英語圏の伝統を調べていたところで、NameDividerを触媒にパーフェクト湯婆婆を創造する をふと思い出す。

NameDivider

https://internet.watch.impress.co.jp/docs/yajiuma/1289735.html
https://github.com/rskmoi/namedivider-python
https://github.com/rskmoi/NameDivider

        example:
        -----------------------------------------------------
        >>> namedivider = NameDivider()
        >>> divided_name = namedivider.divide_name("菅義偉")
        >>> print(divided_name)
        "菅 義偉"
        >>> print(divided_name.to_dict())
        {'family': '', 'given': '義偉', 'separator': ' ', 'score': 0.6328842762252201, 'algorithm': 'kanji_feature'}
        -----------------------------------------------------
        """ 

name-divider

Githubを眺めていたところ類似のものが少し出てきたのでこちらも。
https://github.com/iszk/name-divider

中国の姓名分割

以上まで調べて、そういえば中国語にもあるのかどうかと思ったところ見つかったのはせいぜい以下。

将地址簿中中文名字拆分成姓和名按 Last Name 和 First Name 存储。
(アドレスブックの中国語の名前を姓と名に分割し、姓と名として保存します。)

中国の名前判定

mingpipeは、中国の名前の名前マッチャーです。2つの名前を取り、それらが同じエンティティ(人、組織、または場所)を参照できるかどうかを予測します。

例:佛罗伦萨(フィレンツェ) 翡冷翠(フィリピン) true

結論:マルチバイト文字圏の文字列は難しい

以下をオチとして今日の調査は終了。

https://github.com/derek73/python-nameparser
https://github.com/derek73/python-nameparser/issues/83

The parser seems to parse incorrectly for Chinese names in English. (below uses Malaysia's Chinese name) Names without nickname. Current:


>>> name = HumanName('Tham Jun Hoe')
>>> name
<HumanName : [
        title: ''
        first: 'Tham'
        middle: 'Jun'
        last: 'Hoe'
        suffix: ''
        nickname: ''
]>
Expected:

>>> name = HumanName('Tham Jun Hoe')
>>> name
<HumanName : [
        title: ''
        first: 'Jun Hoe'
        middle: ''
        last: 'Tham'
        suffix: ''
        nickname: ''
]>
3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?