/
phone_matcher.rb
141 lines (127 loc) · 6.89 KB
/
phone_matcher.rb
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
137
138
139
140
# coding: utf-8
# 電話番号判定用モジュール
module PrivacyMaskTools::PhoneMatcher
# 携帯番号 ソフト判定用正規表現
MOBILE_NUMBER_REGEXP = /([((]?([00][7-97-9][00])[-ー()()・ ]*([0-90-9]{4})[-ー()()・ ]*([0-90-9]{4})(?![0-90-9]))/
# 携帯番号 ハード判定用正規表現
JARGON_MOBILE_NUMBER_REGEXP = /([((]?([00oO〇十]?[7-97-9⑦⑧⑨七八九][00oO〇十])[-ー()()・ ]*([0-90-9oO①-⑨一二三四五六七八九十〇壱弐参]{4})[-ー()()・ ]*([0-90-9oO①-⑨一二三四五六七八九十〇壱弐参]{4})(?![0-90-9]))/
# 固定電話(IP電話込) ソフト判定用正規表現
PHONE_NUMBER_REGEXP = /([((]?(([00][334466])[-ー()()・ ]*([0-90-9]{4})|([00][1-91-9]{2})[-ー()()・ ]*([0-90-9]{3})|([00][1-91-9]{2}[0-90-9])[-ー()()・ ]*([0-90-9]{2})|([00][1-91-9]{2}[0-90-9]{2})[-ー()()・ ]*([0-90-9])|([00][55][00])[-ー()()・ ]*([0-90-9]{4}))[-ー()()・ ]*([0-90-9]{4})(?![0-90-9]))/
# 固定電話(IP電話込) ハード判定用正規表現
JARGON_PHONE_NUMBER_REGEXP = /([((]?(([00oO〇十][33三参③44四④66六⑥])[-ー()()・ ]*([0-90-9oO①-⑨一二三四五六七八九十〇壱弐参]{4})|([00oO〇十][1-91-9一二三四五六七八九四①-⑨壱弐参]{2})[-ー()()・ ]*([0-90-9oO①-⑨一二三四五六七八九十〇壱弐参]{3})|([00oO〇十][1-91-9一二三四五六七八九十①-⑨壱弐参]{2}[0-90-9oO①-⑨一二三四五六七八九十〇壱弐参])[-ー()()・ ]*([0-90-9oO①-⑨一二三四五六七八九十〇壱弐参]{2})|([00oO〇十][1-91-9一-九四①-⑨壱弐参]{2}[0-90-9oO①-⑨一二三四五六七八九十〇壱弐参]{2})[-ー()()・ ]*([0-90-9oO①-⑨一二三四五六七八九十〇壱弐参])|([00oO〇十][55五⑤][00oO〇十])[-ー()()・ ]*([0-90-9oO①-⑨一二三四五六七八九十〇壱弐参]{4}))[-ー()()・ ]*([0-90-9oO①-⑨一二三四五六七八九十〇壱弐参]{4})(?![0-90-9]))/
# 携帯番号が含まれているかチェックします
# jargonをfalseにした場合は判定条件はゆるめです
# そのため、漢数字や丸数字はパスしてしまいます
# ただし、セパレータ部分は強めの制限となっており、スペースも許容しています
# そのため"000 9999 9999"もマッチします
# @param [String] チェック対象の文字列
# @param [Boolean] 隠語判定強化フラグ(漢数字・丸付き数値にもマッチ)
# @return [Boolean]
# 含まれている場合 true
# 含まれていない場合 false
def has_mobile_number?(text, jargon=false)
reg = jargon ? JARGON_MOBILE_NUMBER_REGEXP : MOBILE_NUMBER_REGEXP
!text.match(reg).nil?
end
# 携帯番号が含まれているかチェックします
# 隠語らしきもの(漢数字・丸付き数字・〇など)も判定します
# そのため正常な文字列も置換する確率が高くなります
# 現時点ではテストが不十分です
# @param [String] チェック対象の文字列
# @return [Boolean]
# 含まれている場合 true
# 含まれていない場合 false
def has_jargon_mobile_number?(text)
has_mobile_number?(text,true)
end
# 固定電話番号が含まれているかチェックします
#
# 基本的な仕様はhas_mobile_number?と同一です
# @param [String] チェック対象の文字列
# @param [Boolean]
# @return [Boolean]
# 含まれている場合 true
# 含まれていない場合 false
def has_phone_number?(text, jargon=false)
reg = jargon ? JARGON_PHONE_NUMBER_REGEXP : PHONE_NUMBER_REGEXP
!text.match(reg).nil?
end
# 固定電話番号が含まれているかチェックします
#
# 基本的な仕様はhas_jargon_mobile_number?と同一です
# @param [String] チェック対象の文字列
# @return [Boolean]
# 含まれている場合 true
# 含まれていない場合 false
def has_jargon_phone_number?(text)
has_phone_number?(text, true)
end
# 携帯番号らしき箇所をマスキングします
# wordに2文字以上を与えるとマッチした部分をその2文字で置き換えます
# wordが1文字だとマッチした部分それぞれの数値をその文字で置き換えます
# @param [String] マスキング対象の文字列
# @param [String] マッチした部分の置換文字
# @param [Boolean] 隠語判定を強化フラグ
# @return [String] マスキング後の文字列
# @example
# mobile_number_masking("携帯番号 : 080-7900-5678"
# # => "携帯番号 : xxx-xxxx-xxxx"
# mobile_number_masking("携帯番号 : 080-7900-5678", "[Tel No]")
# # => "携帯番号 : [Tel No]"
def mobile_number_masking(text, word="*", jargon=false)
reg = jargon ? JARGON_MOBILE_NUMBER_REGEXP : MOBILE_NUMBER_REGEXP
text.scan(reg).each do |f|
if word.size >= 2
text = text.sub(f[0], word)
else
replace_word = f[0].dup
f.each_with_index do |g, index|
next if index == 0
replace_word.sub!(g, word*g.size)
end
text = text.sub(f[0], replace_word)
end
end
text
end
# 固定電話らしき箇所をマスキングします
#
# 基本的な仕様はmobile_number_maskingと同一です
# @see mobile_number_masking
# @param [String] マスキング対象の文字列
# @param [String] マッチした部分の置換文字
# @param [Boolean] 隠語判定を強化フラグ
# @return [String] マスキング後の文字列
def phone_nomber_masking(text, word="*", jargon=false)
reg = jargon ? JARGON_PHONE_NUMBER_REGEXP : PHONE_NUMBER_REGEXP
text.scan(reg).each do |f|
if word.size >= 2
text = text.sub(f[0], word)
else
replace_word = f[0].dup
f.each_with_index do |g, index|
next if index <= 1 or g.nil?
replace_word.sub!(g, word*g.size)
end
text = text.sub(f[0], replace_word)
end
end
text
end
# text中に含まれる固定電話番号を抽出します
# @param [String] 探索対象文字列
# @param [Boolean] Jargonモードを利用するか
# @return [Array]
def pick_phone_number(text, jargon=false)
reg = jargon ? JARGON_PHONE_NUMBER_REGEXP : PHONE_NUMBER_REGEXP
[].tap { |o| text.scan(reg).each { |f| o << f[0] } }
end
# text中に含まれる携帯番号を抽出します
# @param [String] 探索対象文字列
# @param [Boolean] Jargonモードを利用するか
# @return [Array]
def pick_mobile_number(text, jargon=false)
reg = jargon ? JARGON_MOBILE_NUMBER_REGEXP : MOBILE_NUMBER_REGEXP
[].tap { |o| text.scan(reg).each { |f| o << f[0] } }
end
end