Пример #1
0
        public LinkTypesValidatorTests()
        {
            _keywordServiceMock = new Mock <IKeywordService>();
            _validator          = new KeywordValidator(_keywordServiceMock.Object);

            _metadata = new MetadataBuilder().GenerateSampleKeyword().Build();
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="MobyToMemoryReader"/> class.
 /// </summary>
 /// <param name="validateKeywords">if set to <c>true</c> [validate keywords].</param>
 public MobyToMemoryReader(bool validateKeywords)
 {
     if (validateKeywords)
     {
         this.validator = new KeywordValidator();
     }
     else
     {
         this.validator = null;
     }
 }
        protected void Page_Init(object sender, EventArgs e)
        {
            // SET UP THE MINIMUM LENGTH VALIDATOR
            int minLength = AbleContext.Current.Store.Settings.MinimumSearchLength;

            KeywordValidator.MinimumLength = minLength;
            KeywordValidator.ErrorMessage  = String.Format(KeywordValidator.ErrorMessage, minLength);

            _manufacturerId = AlwaysConvert.ToInt(Request.QueryString["m"]);
            _categoryId     = AlwaysConvert.ToInt(Request.QueryString["c"]);

            // IF CONTROL IS ON CATEGORY PAGE
            if (_categoryId == 0)
            {
                _categoryId = AbleCommerce.Code.PageHelper.GetCategoryId();
            }

            // CHECK IF PAGE IS CATEGORY PAGE
            _isCategoryPage = AbleCommerce.Code.PageHelper.GetCategoryId() > 0;

            _keyword = Server.UrlDecode(Request.QueryString["k"]);
            if (!string.IsNullOrEmpty(_keyword))
            {
                _keyword = StringHelper.StripHtml(_keyword);
            }

            string eventTarget = Request.Form["__EVENTTARGET"];

            if (!string.IsNullOrEmpty(eventTarget))
            {
                if (eventTarget.StartsWith(ShowAllManufacturers.UniqueID))
                {
                    ShowAllManufacturers.Visible = false;
                }

                if (eventTarget.StartsWith(KeywordButton.UniqueID))
                {
                    string kw = StringHelper.StripHtml(Request.Form[KeywordField.UniqueID]).Trim();
                    if (!string.IsNullOrEmpty(kw) && kw != _keyword && KeywordValidator.EvaluateIsValid(kw))
                    {
                        SearchHistory record = new SearchHistory(kw, AbleContext.Current.User);
                        record.Save();
                        Response.Redirect(ModifyQueryStringParameters("k", Server.UrlEncode(kw)));
                    }
                }
            }
            BootstrapCollaspe.Visible = PageHelper.IsResponsiveTheme(this.Page);
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="WordAndLinksINMemory"/> class.
        /// </summary>
        /// <param name="keywordvalue">The keyword value.</param>
        /// <param name="ignorevalidation">if set to <c>true</c> [ignorevalidation]. May be useful if validation performed outside.</param>
        /// <exception cref="System.ArgumentException">New keyword didn't pass validation. Make sure it is valid word or sequence of words without non-word symbols</exception>
        public WordAndLinksINMemory(string keywordvalue, bool ignorevalidation = false)
        {
            if (keywordvalue.Equals(string.Empty))
            {
                throw new ArgumentException("Keyword Value should not be null");
            }

            this.keyword = keywordvalue;
            this.ignorevalidation = ignorevalidation;
            if (!this.ignorevalidation)
            {
                this.validator = new KeywordValidator();
                if (!this.validator.Validate(keywordvalue))
                {
                    throw new ArgumentException("New keywords didn't pass validation. Make sure it is valid word or sequence of words without non-word symbols");
                }
            }

            this.synonyms = new Dictionary<string, StringKeywordWithRaiting>();
        }
 public void KeywordValidator_passRightAndWringStrings_validateResults()
 {
     var sut = new KeywordValidator();
     Assert.True(sut.Validate("word"), "Simple word should pass");
     Assert.True(sut.Validate("Word"), "Words with different cases should pass");
     Assert.True(sut.Validate("PhD"), "Words such as abbreviations should pass");
     Assert.True(sut.Validate("One two three"), "Composed words acceptable");
     Assert.True(sut.Validate("français"), "All UTF8 word symbols should pass");
     Assert.True(sut.Validate("français русский übersetzen"), "All UTF8 word symbols should pass");
     Assert.True(sut.Validate("dad's"), "' should pass");
     Assert.True(sut.Validate("ABC."), "dot is acceptable");
     Assert.True(sut.Validate("data-structure"), "dash is ok");
     Assert.True(sut.Validate("1"), "Numbers are ok You can find something like 3-d say in MOBY examples");
     Assert.True(sut.Validate("test1"), "Numbers are ok You can find something like 3-d say in MOBY examples");
     Assert.True(sut.Validate("$100-a-plate dinner"), "Numbers are ok You can find something like $100-a-plate dinner say in MOBY examples");
     Assert.False(sut.Validate("five%"), "% is not a word symbol");
     Assert.False(sut.Validate("#1"), "# is not a word symbol");
     Assert.False(sut.Validate("synonym #4"), "# is not a word symbol");
     Assert.False(sut.Validate("contains\n"), "terminal symbols should rise error");
     Assert.False(sut.Validate("\n"), "terminal symbols should rise error");
     Assert.False(sut.Validate("False!"), "Sentence marks are not ok");
     Assert.False(sut.Validate("True?"), "Sentence marks are not ok");
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="MobyToMemoryReader"/> class.
 /// </summary>
 public MobyToMemoryReader()
 {
     this.validator = new KeywordValidator();
 }
        /// <summary>
        /// Reads file with synonyms from Ripicts format (word, syn1;raiing1,syn2;raiting2 etc.)
        /// </summary>
        /// <param name="filename">The filename.</param>
        /// <param name="datastructure">The in memory storage structure.</param>
        /// <exception cref="System.ArgumentNullException">data structure must me initialized and could not be null</exception>
        /// <exception cref="System.IO.FileNotFoundException">Passed filename with keywords not found.</exception>
        /// <exception cref="System.IO.IOException">Failed to read file.</exception>
        public void ReadFile(string filename, ref IinMemoryStorageStructure datastructure)
        {
            if (datastructure == null)
            {
                throw new ArgumentNullException("datastructure must me initialized and could not be null");
            }

            if (!File.Exists(filename))
            {
                throw new FileNotFoundException("Passed filename with keywords not found.", filename);
            }

            int stringCounter = 0;
            using (StreamReader reader = new System.IO.StreamReader(filename))
            {
                KeywordValidator validator = new KeywordValidator();
                while (!reader.EndOfStream)
                {
                    stringCounter++;
                    var input = reader.ReadLine();

                    // empty string ignore them
                    if (!input.Trim().Equals(string.Empty))
                    {
                        string keyword;
                        var synRelevanceCollection = new List<Tuple<string, int>>();

                        if (input.IndexOf(',') > 0)
                        {
                            keyword = input.Substring(0, input.IndexOf(","));
                            var collection = input.Substring(input.IndexOf(",") + 1).Trim().Split(',').Select(s => s.Trim()).ToArray();

                            for (int i = 0; i < collection.Length; i++)
                            {
                                var element = collection[i];
                                var pair = element.Trim().Split(';');
                                if (pair.Length > 0 && validator.Validate(pair[0]))
                                {
                                    var synvalue = pair[0].Trim();
                                    int relValue = 0;
                                    if (pair.Length > 1)
                                    {
                                        int.TryParse(pair[1], out relValue);
                                    }

                                    if (!synvalue.Equals(string.Empty))
                                    {
                                        synRelevanceCollection.Add(new Tuple<string, int>(synvalue, relValue));
                                    }
                                }
                                else
                                {
                                    throw new FormatException(string.Format("Format on the line {0} is not right. Missing ';' symbol or value '{1}' illegal.", stringCounter, element));
                                }
                            }
                        }
                        else
                        {
                            // means that this is just a word without synonyms;
                            keyword = input.Trim();
                        }

                        try
                        {
                            datastructure.AddSynonyms(keyword, synRelevanceCollection.ToArray());
                        }
                        catch (Exception ex)
                        {
                            throw new Exception(string.Format("Format on the line {0}.", stringCounter), ex);
                        }
                    }
                }
            }
        }