SlideShare a Scribd company logo
1 of 31
Scalaの文字列処理
Day 7 字種と文字の正規化
Unicodeコードポイントの
グループ分け
グループ分け 特徴
Unicodeスクリプト
全てのUnicodeコードポイントは単一のUnicode
スクリプトに割り当てられます。
Unicodeブロック 連続するUnicodeコードポイントの塊。
Unicodeカテゴリ
全てのUnicodeコードポイントはUnicodeカテゴ
リに割り当てられます。
Unicodeバイナリ・プロパティ
Unicodeプロパティのうち、バイナリ型で定義さ
れているものです。
Unicodeスクリプト
Unicodeスクリプト 説明
Common 記号
Han 漢字
Hiragana ひらがな
Katakana カタカナ
Latin 半角英数(Latin-1)
Unicodeブロック
Unicodeブロック 説明
Basic Latin 半角英数(Latin-1)
CJK Unified Ideographs 表意文字(漢字、BMP上のみ)
Hiragana ひらがな
Katakana カタカナ
Unicodeカテゴリ
Unicodeカテゴリ 説明 サブカテゴリ
C その他 (Other) Cc | Cf | Cs | Co | Cn
L アルファベット (Letter) Lu | Ll | Lt | Lm | Lo
M 記号 (Mark) Mn | Mc | Me
N 数字 (Number) Nd | Nl | No
P 句読記号 (Punctuation) Pc | Pd | Ps | Pe | Pi | Pf | Po
S 記号 (Symbol) Sm | Sc | Sk | So
Z 区切り文字 (Separator) Zs | Zl | Zp
Unicodeバイナリ・プロパテ
ィ
Unicodeバイナリ・プロパティ 説明
Alphabetic アルファベット
Ideographic 表意文字(漢字など)
Punctuation 句読点
Digit 数字
java.lang.Characterクラスに
よる文字の字種情報の取得
文字の字種情報を取得する3つのメソッド:
• Character.getName
• Character.getType
• Character.getDirectionality
Character.getName
コードポイントがunassignedの場合はnull、それ以外
は次の結果を返す
Character.UnicodeBlock.of(codePoint).toString().repla
ce('_', ' ') + " " +
Integer.toHexString(codePoint).toUpperCase(Locale.
ENGLISH);
Character.getType
Charやコードポイントに対してUnicodeカテゴリを返す
Character.getDirectionality
文字の方向性(双方向文字タイプ)を取得するために
使用
文字の方向性というのは、例えば、日本語の文字は「
左から右に表示する」といった情報のこと
java.lang.Characterクラスに
よる文字の字種判定
Characterクラスのメソッドで字種の判定を行う
引数はCharでもコードポイント(Int)でも可
引数がCharだとCharにはBMP領域の文字しか格納で
きないので、補助文字は判定できません。
正規表現の字種によるマッチ
ング
正規表現には字種に関したマッチングを行うために、
POSIX文字クラスや定義済み文字クラス(Unicodeス
クリプト、ブロック、カテゴリ、バイナリ・プロパテ
ィなど)が用意されている。
POSIX文字クラス
(US-ASCIIのみ)
POSIX(Portable Operating System Interface)標準
に従う文字クラスにすべてのASCII文字を表すASCII
文字クラス
POSIX文字クラス マッチ
p{ASCII} すべてのASCII文字 [x00-x7F]
p{Alpha} 英字 [A-Za-z]
p{Digit} 10 進数字 [0-9]
p{Punct} 句読文字
!"#$%&'()*+,-
./:;<=>?@[]^_`{|}~のうち
のひとつ
java.lang.Characterクラス
(単純なjava文字タイプ)
文字クラス マッチ
p{javaLowerCase} java.lang.Character.isLowerCaseと等価
p{javaUpperCase} java.lang.Character.isUpperCaseと等価
p{javaWhitespace} java.lang.Character.isWhitespaceと等価
p{javaMirrored} java.lang.Character.isMirroredと等価
Unicodeスクリプト、ブロック、カテ
ゴリ、バイナリ・プロパティのクラ
ス
Unicodeブロックには接頭辞"In"、バイナリ・プロパ
ティには接頭辞"Is"をつけることで定義済み文字クラ
スとして正規表現で記述可能
文字クラス マッチ
p{IsLatin} Latin 書体文字(Unicodeスクリプト)
p{InGreek} Greek ブロックの文字(Unicodeブロック)
p{Lu} 大文字(Unicodeカテゴリ)
p{IsAlphabetic} 英字(Unicodeバイナリ・プロパティ)
p{Sc} 通貨記号(Unicodeカテゴリ)
P{InGreek} ギリシャ語ブロック以外の文字(否定)
[p{L}&&[^p{Lu}]] 大文字以外の文字(減算)
POSIX文字クラスと定義済の
文字クラスの互換性
POSIX文字クラス マッチ
p{ASCII} すべてのASCII文字 [x00-x7F]
p{Alpha} 英字 p{IsAlphabetic}
p{Digit} 10 進数字 p{IsDigit}
p{Punct} 句読文字 p{IsPunctuation}
日本語の字種のマッチング(1/2)
字種 正規表現 範囲
ひらがな p{Hiragana}, p{InHiragana} [U+3040, U+309F]
カタカナ p{Katakana}, p{InKatakana} [U+30A0, U+30FF]
ローマ字(大文字) p{Upper}, p{IsUppercase}, A-Z [U+0041, U+005A]
ローマ字(小文字) p{Lower}, p{IsLowercase}, a-z [U+0061, U+007A]
アラビア数字 d, p{Digit}, p{IsDigit}, 0-9 [U+0030, U+0039]
日本語の字種のマッチング(2/2)
字種・文字クラス 正規表現 範囲
漢字 p{Han}
CJK互換漢字 p{InCJKCompatibilityIdeographs}
[U+F900,
U+FAFF]
CJK統合漢字 p{InCJKUnifiedIdeographs}
[U+4E00,
U+9FFF]
CJK統合漢字拡張A p{InCJKUnifiedIdeographsExtensionA}
[U+3400,
U+4DBF]
CJK統合漢字拡張B p{InCJKUnifiedIdeographsExtensionB}
[U+20000,
U+2A6DF]
CJK統合漢字拡張C p{InCJKUnifiedIdeographsExtensionC}
[U+2A700,
U+2B73F]
CJK統合漢字拡張D p{InCJKUnifiedIdeographsExtensionD}
[U+2B740,
U+2B81F]
字種の変換
• case付きのアルファベットをlower case、title case
、upper caseに揃える方法
• ひらがなからカタカナ・カタカナからひらがなに変
換する方法
letter case
letter case 例 説明
lower case abc 全部小文字
title case Abc
先頭文字が大文字で残りは小文字
1文字で複数文字あるように見える文字には、
title caseを持っている文字があります。
例:
upper case:「LJ」(U+01C7)
title case:「Lj」(U+01C8)
lower case:「 lj」(U+01C9)
upper case ABC 全部大文字
文字のletter caseの変換
Charやコードポイントのletter caseを
java.lang.CharacterクラスのtoUpperCase、
toTitleCase、toLowerCaseメソッドでいずれか一方に
揃える
Character.toLowerCase(upperCaseChar)
Character.toLowerCase(upperCaseCodePoint)
文字列のletter caseの変換
Stringのletter caseをtoUpperCase、toLowerCaseメソ
ッドで一方に揃える
string.toLowerCase
文字列の1文字目を大文字にする
string.capitalize
ひらがなとカタカナの相互変換
(自作)
自作のJapaneseCharacterCaseConverterは、ひらが
なとカタカナのコードポイントの差分を利用してひら
がなとカタカナの相互変換を実行
JapaneseCharacterCaseConverter.convertKatakana2
Hiragana(string).get.toString
JapaneseCharacterCaseConverter.convertHiragana2
Katakana(string).get.toString
文字の正規化
半角カナ文字「ア」と全角カナ文字「ア」のように、言語
処理で等価に扱いたい文字が存在します。どの文字とど
の文字を等価に扱いたいかは厳密にはアプリケーション
によって異なりますが、言語処理をする上では、このよ
うな等価性は前処理でいずれかの文字に揃えておくとア
ルゴリズムの複雑さを大幅に減らすことができます。こ
の前処理のことを文字の正規化と呼びます。
文字の等価性
文字の等価性:
• 正準等価性 …
• 例:「が」=「か」+「゛」
• 左辺から右辺への変換=「分解」(1対1なので、変換可能)
• 右辺から左辺への変換=「合成」(1対1なので、変換可能)
• 互換等価性 …
• 例1:半角カナ文字「ア」=全角カナ文字「ア」
• 例2:全角英字「C」=半角英字「C」
• 左辺から右辺への変換=「分解」(多対1を許す。変換可能)
• 右辺から左辺への変換=「合成」(1対多を許すので、変換不可能)
EUC-JP/Shift-JISでの正規化
文字を全て2バイト文字に揃える正規化方法が一般的
ひらがなやカタカナだけでなく英数字も半角文字を全
角文字に変換
2バイト文字に揃えておくとバイト数割る2で文字数
が計測可能
Unicode正規化
4種類の正規化形式:
• NFD … 正準等価性によって分解
• NFC … 正準等価性によって分解され、 再度合成
• NFKD … 互換等価性によって分解
• NFKC … 互換等価性によって分解され、 正準等価
性によって再度合成。言語処理の文字正規化で使用
オプション
Optionは値があるのかないのかわからない状態を表すもの
値をOptionで包むことで、その値がある(nullではない)
場合はSome、ない(nullである)の場合はNoneという状
態に移り、Someの場合は値を取り出せるという機構
文字列オプション(自作)
文字列処理においてStringはnullだけでなく空文字”"も
同時に排除したい場合がよくあるが、Optionでは空文
字は排除されない。そこで、OptionのようにStringを
包むことでnullと空文字を排除するための
StringOptionを自作した。
https://github.com/ynupc/scalastringcourseday7/blob/
master/src/test/scala/text/StringOption.scala
正規化文字列(自作)
値からnullや空文字を排除することに加えて、値が正
規化されていることを保証するために
NormalizedStringを自作した。
https://github.com/ynupc/scalastringcourseday7/blob/
master/src/test/scala/text/NormalizedString.scala
句点による文分割と
文の正規化(自作)
日本語テキストを句点「。」や「.」の区切りによる
文(単文・重文・複文の文ではない)に分割し、文を
正規化する方法
単純にNormalizedStringやUnicode正規化してしまう
と、日本語の句点・読点がそれぞれラテン文字のピリ
オト・カンマに変換されてしまう問題を回避

More Related Content

Recently uploaded

Recently uploaded (11)

論文紹介: The Surprising Effectiveness of PPO in Cooperative Multi-Agent Games
論文紹介: The Surprising Effectiveness of PPO in Cooperative Multi-Agent Games論文紹介: The Surprising Effectiveness of PPO in Cooperative Multi-Agent Games
論文紹介: The Surprising Effectiveness of PPO in Cooperative Multi-Agent Games
 
論文紹介:Selective Structured State-Spaces for Long-Form Video Understanding
論文紹介:Selective Structured State-Spaces for Long-Form Video Understanding論文紹介:Selective Structured State-Spaces for Long-Form Video Understanding
論文紹介:Selective Structured State-Spaces for Long-Form Video Understanding
 
Amazon SES を勉強してみる その22024/04/26の勉強会で発表されたものです。
Amazon SES を勉強してみる その22024/04/26の勉強会で発表されたものです。Amazon SES を勉強してみる その22024/04/26の勉強会で発表されたものです。
Amazon SES を勉強してみる その22024/04/26の勉強会で発表されたものです。
 
Observabilityは従来型の監視と何が違うのか(キンドリルジャパン社内勉強会:2022年10月27日発表)
Observabilityは従来型の監視と何が違うのか(キンドリルジャパン社内勉強会:2022年10月27日発表)Observabilityは従来型の監視と何が違うのか(キンドリルジャパン社内勉強会:2022年10月27日発表)
Observabilityは従来型の監視と何が違うのか(キンドリルジャパン社内勉強会:2022年10月27日発表)
 
知識ゼロの営業マンでもできた!超速で初心者を脱する、悪魔的学習ステップ3選.pptx
知識ゼロの営業マンでもできた!超速で初心者を脱する、悪魔的学習ステップ3選.pptx知識ゼロの営業マンでもできた!超速で初心者を脱する、悪魔的学習ステップ3選.pptx
知識ゼロの営業マンでもできた!超速で初心者を脱する、悪魔的学習ステップ3選.pptx
 
Amazon SES を勉強してみる その32024/04/26の勉強会で発表されたものです。
Amazon SES を勉強してみる その32024/04/26の勉強会で発表されたものです。Amazon SES を勉強してみる その32024/04/26の勉強会で発表されたものです。
Amazon SES を勉強してみる その32024/04/26の勉強会で発表されたものです。
 
LoRaWAN スマート距離検出デバイスDS20L日本語マニュアル
LoRaWAN スマート距離検出デバイスDS20L日本語マニュアルLoRaWAN スマート距離検出デバイスDS20L日本語マニュアル
LoRaWAN スマート距離検出デバイスDS20L日本語マニュアル
 
Utilizing Ballerina for Cloud Native Integrations
Utilizing Ballerina for Cloud Native IntegrationsUtilizing Ballerina for Cloud Native Integrations
Utilizing Ballerina for Cloud Native Integrations
 
論文紹介:Video-GroundingDINO: Towards Open-Vocabulary Spatio-Temporal Video Groun...
論文紹介:Video-GroundingDINO: Towards Open-Vocabulary Spatio-Temporal Video Groun...論文紹介:Video-GroundingDINO: Towards Open-Vocabulary Spatio-Temporal Video Groun...
論文紹介:Video-GroundingDINO: Towards Open-Vocabulary Spatio-Temporal Video Groun...
 
LoRaWANスマート距離検出センサー DS20L カタログ LiDARデバイス
LoRaWANスマート距離検出センサー  DS20L  カタログ  LiDARデバイスLoRaWANスマート距離検出センサー  DS20L  カタログ  LiDARデバイス
LoRaWANスマート距離検出センサー DS20L カタログ LiDARデバイス
 
新人研修 後半 2024/04/26の勉強会で発表されたものです。
新人研修 後半        2024/04/26の勉強会で発表されたものです。新人研修 後半        2024/04/26の勉強会で発表されたものです。
新人研修 後半 2024/04/26の勉強会で発表されたものです。
 

Featured

How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
ThinkNow
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
Kurio // The Social Media Age(ncy)
 

Featured (20)

How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
 
ChatGPT webinar slides
ChatGPT webinar slidesChatGPT webinar slides
ChatGPT webinar slides
 

Scalaの文字列処理 Day 7 字種と文字の正規化