1:- module(uk_number_range,
    2          [ uk_national/2,          % +Input, -NationalDigits (string, leading 0)
    3            uk_e164/2,              % +Input, -E164 (string, +44...)
    4            uk_number_type/2,       % +Input, -Type
    5            uk_sms_otp_outlook/3,   % +Input, -Outlook, -Reason
    6            uk_drama_number/1       % +Input
    7          ]).

Classify UK telephone numbers by Ofcom number range

Normalises a UK number written in any common way (07700 900123, +44 7700 900123, `0044 (0)7700-900123`) and tells you which Ofcom range it sits in: mobile, personal numbering (070), paging (076), geographic (01/02), UK-wide (03), corporate (055), VoIP / location independent (056), freephone (080), service (084/087) or premium (09).

It also gives a rough outlook for whether a sign-up form that sends an SMS one-time code is likely to accept the number. Most services only send codes to numbers in the mobile ranges and refuse 070, 076, 056 and landline ranges, because those are where virtual (VoIP) numbers usually live. This is a prefix check only: it cannot see porting, and a number in a mobile range can still be a virtual number underneath. Treat the answer as a hint, not a lookup.

Ofcom reserves "drama" ranges that are never given to real subscribers (for example 07700 900000 to 07700 900999 and 020 7946 0000 to 020 7946 0999). Use those in tests and examples; uk_drama_number/1 recognises the common ones.

author
- Ghost SIMs
See also
- https://www.ofcom.org.uk/phones-and-broadband/phone-numbers/numbering
license
- MIT
   35:- use_module(library(lists)).   36:- use_module(library(apply)).
 uk_national(+Input, -National:string) is semidet
National is the 10 or 11 digit national form starting with 0. Input may be an atom, string or code list. Spaces, dots, dashes, brackets and a "(0)" trunk marker after +44 are ignored.
   44uk_national(Input, National) :-
   45    text_to_string(Input, S0),
   46    string_codes(S0, C0),
   47    strip_trunk_marker(C0, C1),
   48    include(keep_code, C1, C2),
   49    string_codes(S2, C2),
   50    to_national(S2, National),
   51    string_length(National, L),
   52    between(10, 11, L),
   53    \+ sub_string(National, 0, 2, _, "00").
   54
   55strip_trunk_marker(C0, C) :-
   56    (   append(Pre, [0'(, 0'0, 0')|Post], C0)
   57    ->  append(Pre, Post, C1), strip_trunk_marker(C1, C)
   58    ;   C = C0
   59    ).
   60
   61keep_code(C) :- code_type(C, digit).
   62keep_code(0'+).
   63
   64to_national(S, N) :- string_concat("+44", Rest, S), !, digits_only(Rest), string_concat("0", Rest, N).
   65to_national(S, N) :- string_concat("0044", Rest, S), !, digits_only(Rest), string_concat("0", Rest, N).
   66to_national(S, N) :- string_concat("44", Rest, S), string_length(Rest, 10), !, string_concat("0", Rest, N).
   67to_national(S, S) :- string_concat("0", _, S), digits_only(S).
   68
   69digits_only(S) :- string_codes(S, Cs), Cs \== [], maplist([C]>>code_type(C, digit), Cs).
 uk_e164(+Input, -E164:string) is semidet
   73uk_e164(Input, E164) :-
   74    uk_national(Input, N),
   75    sub_string(N, 1, _, 0, Rest),
   76    string_concat("+44", Rest, E164).
 uk_number_type(+Input, -Type:atom) is semidet
Type is one of mobile, personal_numbering, paging, geographic, uk_wide, corporate, voip_location_independent, freephone, service, premium_rate or unknown.
   84uk_number_type(Input, Type) :-
   85    uk_national(Input, N),
   86    string_length(N, Len),
   87    range_type(N, Len, Type), !.
   88
   89range_type(N, 11, mobile)          :- sub_string(N, 0, 5, _, "07624"), !.
   90range_type(N, 11, paging)          :- sub_string(N, 0, 3, _, "076"), !.
   91range_type(N, 11, personal_numbering) :- sub_string(N, 0, 3, _, "070"), !.
   92range_type(N, 11, mobile)          :- sub_string(N, 0, 2, _, "07"), sub_string(N, 2, 1, _, D),
   93                                      memberchk(D, ["1","2","3","4","5","7","8","9"]), !.
   94range_type(N, _, geographic)       :- sub_string(N, 0, 2, _, P), memberchk(P, ["01","02"]), !.
   95range_type(N, 11, uk_wide)         :- sub_string(N, 0, 2, _, "03"), !.
   96range_type(N, 11, corporate)       :- sub_string(N, 0, 3, _, "055"), !.
   97range_type(N, 11, voip_location_independent) :- sub_string(N, 0, 3, _, "056"), !.
   98range_type(N, _, freephone)        :- sub_string(N, 0, 3, _, "080"), !.
   99range_type(N, 11, service)         :- sub_string(N, 0, 3, _, P), memberchk(P, ["084","087"]), !.
  100range_type(N, 11, premium_rate)    :- sub_string(N, 0, 2, _, "09"), !.
  101range_type(_, _, unknown).
 uk_sms_otp_outlook(+Input, -Outlook:atom, -Reason:string) is semidet
Outlook is likely, unlikely or no_sms. A prefix-based hint only.
  107uk_sms_otp_outlook(Input, Outlook, Reason) :-
  108    uk_number_type(Input, Type),
  109    outlook(Type, Outlook, Reason).
  110
  111outlook(mobile, likely,
  112        "UK mobile range (07). Most SMS verification forms accept these, unless the carrier behind the number is flagged as virtual.").
  113outlook(personal_numbering, unlikely,
  114        "070 personal numbering forwards calls and is often blocked as a virtual number.").
  115outlook(paging, unlikely,
  116        "076 is the radiopaging range and is not treated as a mobile.").
  117outlook(voip_location_independent, unlikely,
  118        "056 is Ofcom's location-independent (VoIP) range, the classic reject for SMS codes.").
  119outlook(corporate, unlikely,
  120        "055 corporate numbers route to company systems, not a handset.").
  121outlook(geographic, no_sms,
  122        "01/02 geographic landline. Most landlines cannot receive SMS, so forms reject them or fall back to a voice call.").
  123outlook(uk_wide, no_sms, "03 UK-wide rate number, usually a landline or call centre.").
  124outlook(freephone, no_sms, "080 freephone numbers do not receive SMS codes.").
  125outlook(service, no_sms, "084/087 service numbers do not receive SMS codes.").
  126outlook(premium_rate, no_sms, "09 premium rate numbers are refused by sign-up forms.").
  127outlook(unknown, unlikely, "Not in a range this library recognises.").
 uk_drama_number(+Input) is semidet
True for numbers in Ofcom's reserved drama ranges (never allocated).
  133uk_drama_number(Input) :-
  134    uk_national(Input, N),
  135    drama(Prefix, Low, High),
  136    string_concat(Prefix, Tail, N),
  137    string_length(Tail, TL),
  138    atom_length(Low, TL),
  139    number_string(T, Tail),
  140    atom_number(Low, L), atom_number(High, H),
  141    between(L, H, T), !.
  142
  143drama("07700",  '900000', '900999').
  144drama("01632",  '960000', '960999').
  145drama("0207946", '0000', '0999').
  146drama("0113496", '0000', '0999').
  147drama("0114496", '0000', '0999').
  148drama("0115496", '0000', '0999').
  149drama("0116496", '0000', '0999').
  150drama("0117496", '0000', '0999').
  151drama("0118496", '0000', '0999').
  152drama("0121496", '0000', '0999').
  153drama("0131496", '0000', '0999').
  154drama("0141496", '0000', '0999').
  155drama("0151496", '0000', '0999').
  156drama("0161496", '0000', '0999').
  157drama("0292018", '0000', '0999').
  158drama("0289649", '6000', '6999').
  159drama("0808157", '0000', '0999').
  160drama("0909879", '0000', '0999').
  161drama("0306999", '0000', '0999')