public static bool IsPhone(this string value,
                                   PhoneKind phoneKind, bool allowExtension = false)
        {
            if (string.IsNullOrWhiteSpace(value))
            {
                return(false);
            }

            const string EXTENSION_SUFFIX =
                @"(\s+?[xX]\d{1,5})?";

            const string DOMESTIC_PREFIX =
                @"[2-9]\d{2}\-[2-9]\d{2}\-\d{4}";

            const string INTERNATIONAL_PREFIX =
                @"(\(?\+?[0-9]*\)?)?[0-9_\- \(\)]*";

            if (value.Trim().Length == 0)
            {
                return(false);
            }

            var pattern = new StringBuilder();

            pattern.Append('^');

            switch (phoneKind)
            {
            case PhoneKind.Domestic:
                pattern.Append(DOMESTIC_PREFIX);
                break;

            case PhoneKind.International:
                pattern.Append(INTERNATIONAL_PREFIX);
                break;

            case PhoneKind.EitherFormat:
                pattern.Append("((");
                pattern.Append(DOMESTIC_PREFIX);
                pattern.Append(")|(");
                pattern.Append(INTERNATIONAL_PREFIX);
                pattern.Append("))");
                break;

            case PhoneKind.NoFormat:
                return(IsTrimmed(value, false));
            }

            if (allowExtension)
            {
                pattern.Append(EXTENSION_SUFFIX);
            }

            pattern.Append('$');

            return(IsMatch(value, pattern.ToString()));
        }
Example #2
0
 internal void Correct()
 {
     if (Kind == PhoneKind.Undefined)
     {
         if (this.FindSlot(ATTR_ADDNUMBER, null, true) != null)
         {
             Kind = PhoneKind.Work;
         }
         else if (CountryCode == null || CountryCode == "7")
         {
             string num = Number;
             if (num.Length == 10 && num[0] == '9')
             {
                 Kind = PhoneKind.Mobile;
             }
         }
     }
 }
Example #3
0
 public override void Process(Pullenti.Ner.Core.AnalysisKit kit)
 {
     PhoneAnalizerData ad = kit.GetAnalyzerData(this) as PhoneAnalizerData;
     for (Pullenti.Ner.Token t = kit.FirstToken; t != null; t = t.Next) 
     {
         List<Pullenti.Ner.Phone.Internal.PhoneItemToken> pli = Pullenti.Ner.Phone.Internal.PhoneItemToken.TryAttachAll(t, 15);
         if (pli == null || pli.Count == 0) 
             continue;
         PhoneReferent prevPhone = null;
         int kkk = 0;
         for (Pullenti.Ner.Token tt = t.Previous; tt != null; tt = tt.Previous) 
         {
             if (tt.GetReferent() is PhoneReferent) 
             {
                 prevPhone = tt.GetReferent() as PhoneReferent;
                 break;
             }
             else if (tt is Pullenti.Ner.ReferentToken) 
             {
             }
             else if (tt.IsChar(')')) 
             {
                 Pullenti.Ner.Token ttt = tt.Previous;
                 int cou = 0;
                 for (; ttt != null; ttt = ttt.Previous) 
                 {
                     if (ttt.IsChar('(')) 
                         break;
                     else if ((++cou) > 100) 
                         break;
                 }
                 if (ttt == null || !ttt.IsChar('(')) 
                     break;
                 tt = ttt;
             }
             else if (!tt.IsCharOf(",;/\\") && !tt.IsAnd) 
             {
                 if ((++kkk) > 5) 
                     break;
                 if (tt.IsNewlineBefore || tt.IsNewlineAfter) 
                     break;
             }
         }
         int j = 0;
         bool isPhoneBefore = false;
         bool isPref = false;
         PhoneKind ki = PhoneKind.Undefined;
         while (j < pli.Count) 
         {
             if (pli[j].ItemType == Pullenti.Ner.Phone.Internal.PhoneItemToken.PhoneItemType.Prefix) 
             {
                 if (ki == PhoneKind.Undefined) 
                     ki = pli[j].Kind;
                 isPref = true;
                 isPhoneBefore = true;
                 j++;
                 if ((j < pli.Count) && pli[j].ItemType == Pullenti.Ner.Phone.Internal.PhoneItemToken.PhoneItemType.Delim) 
                     j++;
             }
             else if (((j + 1) < pli.Count) && pli[j + 1].ItemType == Pullenti.Ner.Phone.Internal.PhoneItemToken.PhoneItemType.Prefix && j == 0) 
             {
                 if (ki == PhoneKind.Undefined) 
                     ki = pli[0].Kind;
                 isPref = true;
                 pli.RemoveAt(0);
             }
             else 
                 break;
         }
         if (prevPhone != null) 
             isPhoneBefore = true;
         if (pli.Count == 1 && pli[0].ItemType == Pullenti.Ner.Phone.Internal.PhoneItemToken.PhoneItemType.Number) 
         {
             Pullenti.Ner.Token tt = t.Previous;
             if ((tt is Pullenti.Ner.TextToken) && !tt.Chars.IsLetter) 
                 tt = tt.Previous;
             if (tt is Pullenti.Ner.TextToken) 
             {
                 if (Pullenti.Ner.Uri.UriAnalyzer.m_Schemes.TryParse(tt, Pullenti.Ner.Core.TerminParseAttr.No) != null) 
                     continue;
             }
         }
         List<Pullenti.Ner.ReferentToken> rts = this.TryAttach(pli, j, isPhoneBefore, prevPhone);
         if (rts == null) 
         {
             for (j = 1; j < pli.Count; j++) 
             {
                 if (pli[j].ItemType == Pullenti.Ner.Phone.Internal.PhoneItemToken.PhoneItemType.Prefix) 
                 {
                     pli.RemoveRange(0, j);
                     rts = this.TryAttach(pli, 1, true, prevPhone);
                     break;
                 }
             }
         }
         if (rts == null) 
             t = pli[pli.Count - 1].EndToken;
         else 
         {
             if ((ki == PhoneKind.Undefined && prevPhone != null && !isPref) && prevPhone.Kind != PhoneKind.Mobile && kkk == 0) 
                 ki = prevPhone.Kind;
             foreach (Pullenti.Ner.ReferentToken rt in rts) 
             {
                 PhoneReferent ph = rt.Referent as PhoneReferent;
                 if (ki != PhoneKind.Undefined) 
                     ph.Kind = ki;
                 else 
                 {
                     if (rt == rts[0] && (rt.WhitespacesBeforeCount < 3)) 
                     {
                         Pullenti.Ner.Token tt1 = rt.BeginToken.Previous;
                         if (tt1 != null && tt1.IsTableControlChar) 
                             tt1 = tt1.Previous;
                         if ((tt1 is Pullenti.Ner.TextToken) && ((tt1.IsNewlineBefore || ((tt1.Previous != null && tt1.Previous.IsTableControlChar))))) 
                         {
                             string term = (tt1 as Pullenti.Ner.TextToken).Term;
                             if (term == "T" || term == "Т") 
                                 rt.BeginToken = tt1;
                             else if (term == "Ф" || term == "F") 
                             {
                                 ph.Kind = (ki = PhoneKind.Fax);
                                 rt.BeginToken = tt1;
                             }
                             else if (term == "M" || term == "М") 
                             {
                                 ph.Kind = (ki = PhoneKind.Mobile);
                                 rt.BeginToken = tt1;
                             }
                         }
                     }
                     ph.Correct();
                 }
                 rt.Referent = ad.RegisterReferent(rt.Referent);
                 kit.EmbedToken(rt);
                 t = rt;
             }
         }
     }
 }
Example #4
0
 public Phone(string number, PhoneKind kind)
 {
     Number = number;
     Kind   = kind;
 }
        public static bool IsPhone(this string value,
            PhoneKind phoneKind, bool allowExtension = false)
        {
            if (string.IsNullOrWhiteSpace(value))
                return false;

            const string EXTENSION_SUFFIX =
                @"(\s+?[xX]\d{1,5})?";

            const string DOMESTIC_PREFIX =
                @"[2-9]\d{2}\-[2-9]\d{2}\-\d{4}";

            const string INTERNATIONAL_PREFIX =
                @"(\(?\+?[0-9]*\)?)?[0-9_\- \(\)]*";

            if (value.Trim().Length == 0)
                return false;

            var pattern = new StringBuilder();

            pattern.Append('^');

            switch (phoneKind)
            {
                case PhoneKind.Domestic:
                    pattern.Append(DOMESTIC_PREFIX);
                    break;
                case PhoneKind.International:
                    pattern.Append(INTERNATIONAL_PREFIX);
                    break;
                case PhoneKind.EitherFormat:
                    pattern.Append("((");
                    pattern.Append(DOMESTIC_PREFIX);
                    pattern.Append(")|(");
                    pattern.Append(INTERNATIONAL_PREFIX);
                    pattern.Append("))");
                    break;
                case PhoneKind.NoFormat:
                    return (IsTrimmed(value, false));
            }

            if (allowExtension)
                pattern.Append(EXTENSION_SUFFIX);

            pattern.Append('$');

            return IsMatch(value, pattern.ToString());
        }