1
1

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.

PowerShellでマニアックなデータ加工をする("十人十色" => "人十色")

Last updated at Posted at 2021-02-07

コード

文字をソートして重複を消す。"十人十色" => "人十色"。

function uniqsortString($str){
  $arr = [regex]::Matches($str,".{1}").value | sort -Unique
  return $arr -join ""
}

漢字を対象にすると

国立国語研究所(2004)『分類語彙表増補改訂版データベース』(ver.1.0)から
漢字熟語を抜き出す。また、構成する漢字の列を追加する。
さらに、[,,,]のような区切りを追加してファイル保存する。

# 文字列を重複無しソートにする  [十人十色] => [人十色]
function uniqsortString($str){
  $arr = [regex]::Matches($str,".{1}").value | sort -Unique
  return $arr -join ""
}

$csv |
# 漢字のみで構成されたものを取り出す
  where{$_.col14 -match "^([〇\u3400-\u9FFF\uF900-\uFAFF]|[\uD840-\uD87F][\uDC00-\uDFFF])+$"} |
#  2文字以上で重複なし
  where{$_.col14.length -gt 1} | Sort-Object -Property col14 -Unique |
# idと分類と熟語を抜き出す。構成漢字列を付加する。
  %{echo ("["+ $_.col1 +","+ $_.col5 +","+ $_.col6 +","+ $_.col7 +","+ $_.col14 +","+ (uniqsortString $_.col14) +"]," )} >> jukugo_uniqsort.txt

解説

  • sort -Unique 配列等をソートして重複を消す。sortはSort-Objectのエイリアス
  • [regex]::Matches($str,".{1}").value 文字列を1文字ずつの配列にする
  • $arr -join "" join == 文字列配列をまとめて1つの文字列にする。(""を区切り文字にする)
  • $str -match "^([〇\u3400-\u9FFF\uF900-\uFAFF]|[\uD840-\uD87F][\uDC00-\uDFFF])+$" 文字列が漢字のみであればtrue
  • Sort-Object -Property col00 オブジェクトをcol00という要素でソート
  • echo (文字列) オブジェクトを並べると改行されてしまうので、文字列にしてまとめてからechoした

補足

col14に単語が入っていて、それが漢字のみの熟語の行のみとってきてます。
要らない列は省いてます。
追加した列は重複無しでソートされた熟語です。構成する漢字が似通ったものを探すために便利なのでくっつけました。
メモ書き記事です。

1
1
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
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?