2010年9月16日木曜日

Pythonで unicodedata.normalize を使って全角半角を正規化する方法

(この記事は 2008年03月14日に別のブログに投稿した記事を移動・編集したものです。)

インターネット上の日本語を扱っていると、全角半角が統一されていない事による表記揺れをなんとかしたい場面に出くわします。半角で書かれた iPod も、全角で書かれたiPodも同じものとして扱いたいときや、あるいは単に見た目がきれいになるように、英数字は半角に、カタカナは全角にそろえたい、という場合です。

Python では setomitsさんによる zenhan.py を使うと、文字種ごとに全角半角を選択して変換することができ、除外文字も設定できるので便利です。ただ、そこまで柔軟でなくても良く、単に表記揺れがなくなれば良い、という場合も多いかと思います。その場合はPythonの標準ライブラリに含まれる unicodedata モジュールの normalize 関数を使うと便利です。

>>> import unicodedata
>>>
>>> s = 'フガホゲ-%*@ABC−%*@123'.decode('euc-jp')
>>> n = unicodedata.normalize('NFKC', s)
>>>
>>> print s.encode('euc-jp')
フガホゲ-%*@ABC−%*@123
>>>
>>> print n.encode('euc-jp')
フガホゲ-%*@ABC−%*@123

Unicode で定義される各文字は、等価な別表現がある場合(たとえば半角のAと全角のA)に、どの表現を標準とするべきか、という情報が付与されているそうです。normalize関数はこの情報を使って、表記を統一する処理を行います。normalize関数の第一引数に 'NFKC' を渡すことで、ガ(カ+濁点)のような複合文字が期待通りに分解・再合成されます。(詳しく知りたい方は Unicode正規化 あたりをどうぞ)

実際の変換結果を見てみると、英字、数字は半角に、カタカナは全角に正規化されることになっているようです。記号については、基本的に半角のようですが、全角マイナスと半角マイナスのように、別の文字として扱われているものもあるようです。マイナスが半角に正規化されるべきかどうかは場合によって異なると思われますが、ほとんどの用途にはこれで十分なのではないでしょうか。

6 件のコメント:

  1. casino online | Xn - Xn - Osm - Osm - Osm
    online casino online | Xn - Osm ラッキーニッキー - Osm 우리카지노 - Osm. 1xbet korean

    返信削除
  2. Harrah's Resort Southern California Casino Tickets - KTNV
    Buy 부천 출장샵 Harrah's 전라북도 출장마사지 Resort Southern California Casino 강릉 출장마사지 Tickets. Find Harrah's Resort Southern California Casino 아산 출장샵 venue concert and event schedules, venue information, directions, Jan 7, 2022John 남원 출장샵 FogertyJan 14, 2022Shinedown

    返信削除
  3. In common, there are extra Android apps than iOS apps, but there 바카라사이트 are restrictions that result on} each operating techniques. Since we want to have one of the best experience potential while we’re gaming, functionality might be the largest consideration when it comes to of|in relation to} choosing between a mobile browser and on line casino app. With this in mind, they have already got one foot within the door in the race to launch their online sportsbook in Maryland, with it now simply seemingly like a matter of time before this turns into a truth. However FanDuel ended up donating $500,000 as a substitute of $250,000, displaying simply how dedicated they are to help sports betting launch within the state. It wouldn’t make sense for DraftKings to pledge all that money to helping Maryland launching sports betting without getting in on the act themselves, thus DK ought to be one of the first ones to launch in MD quickly. BetRivers’ retail sportsbook can currently be discovered at Bingo World in Baltimore, with those in MD able to to} place their retail bets with BetRivers for a number of|numerous|a variety of} months now following their launch.

    返信削除
  4. The on-line casinos that did not stay a lot as} our expectations had been eradicated. We seemed for factors like their costs, high quality, video games assortment, bonuses, deposits, and so on. It has greater than 3.5 million players worldwide, and Bitstarz is claimed to be the King of slots. This is as a result of|as a end result of} it offers one of the best slot 카지노사이트 video games on the web and has over 2000 slot video games.

    返信削除
  5. Each player has an independent sport with the vendor, so it's potential for the vendor to lose minimal of} one|to 1} player however still beat the opposite players in the identical round. Always Split two 8's - Statistically the worst hand in Blackjack is a value of sixteen because of|as a end result of} something above a five will bust you. Splitting two 8's gives you a significantly better probability of successful with at least of|no much less than} one hand quite than standing with a sixteen or risking a Bust by taking 더킹카지노 another Hit.

    返信削除
  6. Some of our hottest articles are The Best US Online Slots and The Best Online Blackjack within the USA guides. You have to verify your stake from $1 to as high as $2,000. This sport is for the actual fans and those who|and people who|and these that} love high stakes. European Roulette Pro from Borgata on-line — just straightforward, uncomplicated roulette. How We Selected the Best Online Roulette Casino There are plenty of really 퍼스트카지노 good on-line ... You should consider whether or not you'll be able to|you probably can} afford the high danger of shedding your cash.

    返信削除