-3

テキストボックスを検証して、入力された値が英語であるかどうかを確認したいのですが、これが私の検証クラスです

using System;
using System.ComponentModel;
using System.Text.RegularExpressions;
using Vytru.Base;

namespace Vytru.Platform.Bridge.Configuration.Manager
{
     class MapDevice : IDataErrorInfo
     {
        public string UserName { get; set; }
        public string AgentName { get; set; }
        public string SipURI { get; set; }
        public string Password { get; set; }
        public string FQDN { get; set; }
        public string Domain { get; set; }
        public SipServerTransportType _TransportType { get; set; }
        public PeerType _PeerType { get; set; }
        public string PeerURI { get; set; }


        #region Implementation of IDataErrorInfo

        public string this[string columnName]
        {
            get
            {
                string result = null;
                if (columnName.Equals("UserName"))
                {
                    // check for null or empty values 
                    if (String.IsNullOrEmpty(UserName))
                    {
                         result = "User Name cannot be null or empty";
                    }
                    else if (UserName.Length > 50)
                    {
                        result = "More than 50 characters";
                    }
                    else if (UserName.Length < 3)
                    {
                        result = "less than 3 characters";
                    }
                    else if (!Regex.IsMatch(UserName, @"\w+"))
                    {
                         result = "Entered User Name format is not valid ...only A-Z 0-9";
                    }

                }

                else if (columnName.Equals("AgentName"))
                {
                    // check for null or empty values 
                    if (String.IsNullOrEmpty(AgentName))
                    {
                         result = "Agent Name cannot be null or empty";
                    }
                    else if (AgentName.Length < 3)
                    {
                         result = "less than 3 characters";
                    }

                    else if (AgentName.Length > 50)
                    {
                         result = "More than 50 characters";
                    }

                    else if (!Regex.IsMatch(AgentName, @"\w+"))
                    {
                         result = "Entered AgentName format is not valid ... only A-Z 0-9";
                    }

                }

                else if (columnName.Equals("SipURI"))
                {
                    // check for null or empty values 
                    if (String.IsNullOrEmpty(SipURI))
                    {
                         result = "Sip URI cannot be null or empty";
                    }

                    else if (SipURI.Length < 3)
                    {
                         result = "less than 3 characters";
                    }

                    else if (SipURI.Length > 50)
                    {
                         result = "More than 50 characters";
                    }

                    else if (!Regex.IsMatch(SipURI, @"(s|S)+(i|I)+(p|P)+(:)+\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"))
                    {
                         result = "Entered SipURI format is not valid ... ";
                    }


                }

                if (columnName.Equals("Password"))
                {
                     // check for null or empty values 
                     if (String.IsNullOrEmpty(Password))
                     {
                          result = "Password cannot be null or empty";
                     }
                     else if (Password.Length > 50)
                     {
                          result = "More than 50 characters";
                     }
                     else if (Password.Length < 3)
                     {
                          result = "Less than 3 characters";
                     }

                     else if (!Regex.IsMatch(Password, @"\w+"))
                     {
                          result = "Entered Password is not valid ... only A-Z 0-9 ";
                     }


                }

                else if (columnName.Equals("FQDN"))
                {
                     // check for null or empty values 
                     if (String.IsNullOrEmpty(FQDN))
                     {
                          result = "FQDN cannot be null or empty";
                     }
                     else if (FQDN.Length > 50)
                     {
                          result = "More than 50 characters";
                     }
                     else if (FQDN.Length < 3)
                     {
                          result = "Less than 3 characters";
                     }
                     else if (!Regex.IsMatch(FQDN, @"\w+"))
                     {
                          result = "Entered FQDN format is not valid ... only A-Z 0-9";
                     }

                }

                else if (columnName.Equals("Domain"))
                {
                     // check for null or empty values 
                     if (String.IsNullOrEmpty(Domain))
                     {
                          result = "Domain cannot be null or empty";
                     }

                     else if (Domain.Length > 50)
                     {
                          result = "More than 50 characters";
                     }
                     else if (Domain.Length < 2)
                     {
                          result = "Less than 3 characters";
                     }

                     else if (!Regex.IsMatch(Domain, @"\w+"))
                     {
                          result = "Entered Domain format is not valid ...  ";
                     }


                }

                else if (columnName.Equals("PeerURI"))
                {
                     // check for null or empty values 
                     if (String.IsNullOrEmpty(PeerURI))
                     {
                          result = "PeerURI cannot be null or empty";
                     }

                     else if (PeerURI.Length > 50)
                     {
                          result = "More than 50 characters";
                     }
                     else if (PeerURI.Length < 3)
                     {
                          result = "Less than 3 characters";
                     }

                     else if (!Regex.IsMatch(PeerURI, @"\w+"))
                     {
                          result = "Entered PeerURI format is not valid ...";
                     }

                }

                return result;

            }
        }

        public string Error
        {
            get { throw new NotImplementedException(); }
        }

        #endregion



     }
}
4

4 に答える 4

2

何かが英語であることを検証するのは現実的ではないと思います。英語とは何かを定義する必要がありますが、それはほとんど不可能な作業です。

ヒューリスティックに、英語の辞書を使ってスペルチェッカーで値を実行したり、Google 言語 API などを使用して「言語の自動検出」呼び出しを実行したりすることができますが、これは確かに 100% 信頼できるわけではありません。フィールド (どのフィールドを検証するかは明確ではありませんが、すべて短い入力があるように見えます)。

于 2012-07-12T14:17:16.120 に答える
0

一致指数を使用する必要があります。十分なテキストを評価すると、かなり正確です。おそらく、特定の特徴的な組み合わせ (「th」など) を探す正規表現と組み合わせたり、正規表現を使用して記事 (a、an & the) を探したりします。

次のステップは、たとえばドイツ語など、ほぼ同じスコアを持つ別の言語がある場合に、たとえば Ringel-S を探して、より詳しく調べることです。

最後の、おそらく望ましくないステップは、ある種の辞書に対して単語をチェックすることです。たとえば、 dictionary.reference.comを使用して、探している単語が見つからないという応答ではなく結果を返すかどうかを確認できます。そのようなサイトに POST と GET を修正できる場合は、別のチェックでビルドします。

繰り返しますが、テキストが必要です。いくつかの単語だけではありません...いくつかの言語を定義する表がどこかにあります。ご自分で見つけられない場合は、必要に応じてお送りします。

于 2012-07-12T14:35:11.197 に答える
0

これを実現するには、2 つの方法が考えられます。

1 つ目: できるだけ多くの単語を含む XML (またはその他の形式) の英語辞書をダウンロードします。次に、各単語をハッシュしてインデックス ファイルに作成します。次に、入力内の各単語をハッシュし、最後にインデックス ファイルと比較します。

2 番目: 英語の CFG を見つけることができます。次に、入力が実際に英語であるかどうかを確認するために、CFG に従って入力を解析できる語彙および構文アナライザーを取得します。

私の意見では、これを達成するのは非常に簡単ではありません。しかし、他の誰かがより簡単な方法を考えることができる場合は、共有してください.

よろしくお願いします。

于 2012-07-12T14:21:37.733 に答える