예제 #1
0
        public static Example exampleFromString(string data, DataSetSpecification dataSetSpec, string separator)
        {
            IRegularExpression        splitter        = TextFactory.CreateRegularExpression(separator);
            IMap <string, IAttribute> attributes      = CollectionFactory.CreateInsertionOrderedMap <string, IAttribute>();
            ICollection <string>      attributeValues = CollectionFactory.CreateQueue <string>(splitter.Split(data));

            if (dataSetSpec.isValid(attributeValues))
            {
                ICollection <string> names = dataSetSpec.getAttributeNames();
                int min = names.Size() > attributes.Size() ? names.Size() : attributes.Size();

                for (int i = 0; i < min; ++i)
                {
                    string name = names.Get(i);
                    IAttributeSpecification attributeSpec = dataSetSpec.getAttributeSpecFor(name);
                    IAttribute attribute = attributeSpec.CreateAttribute(attributeValues.Get(i));
                    attributes.Put(name, attribute);
                }
                string targetAttributeName = dataSetSpec.getTarget();
                return(new Example(attributes, attributes.Get(targetAttributeName)));
            }
            else
            {
                throw new RuntimeException("Unable to construct Example from " + data);
            }
        }
예제 #2
0
        public void GroupIsNotAnAlternativesExpression()
        {
            IRegularExpression expression = RegularExpression.Parse("(a|b)");

            Assert.IsInstanceOfType(expression, typeof(SingleAtomExpression));
            Assert.AreEqual(new Group("(a|b)", Quantifier.None), (expression as SingleAtomExpression).Atom);
        }
예제 #3
0
        // TODO
        // Make more intelligent link search
        public ICollection <string> getOutlinks(Page page)
        {
            string content = page.getContent();
            ICollection <string> outLinks = CollectionFactory.CreateQueue <string>();
            // search content for all href="x" outlinks
            ICollection <string> allMatches = CollectionFactory.CreateQueue <string>();
            IRegularExpression   m          = TextFactory.CreateRegularExpression("href=\"(/wiki/.*?)\"");

            foreach (string ma in m.Matches(content))
            {
                allMatches.Add(ma);
            }
            for (int i = 0; i < allMatches.Size(); ++i)
            {
                string   match    = allMatches.Get(i);
                string[] tokens   = TextFactory.CreateRegularExpression("\"").Split(match);
                string   location = tokens[1].ToLower(); // also, tokens[0] = the
                                                         // text before the first
                                                         // quote,
                                                         // and tokens[2] is the
                                                         // text after the second
                                                         // quote
                outLinks.Add(location);
            }

            return(outLinks);
        }
예제 #4
0
        // string split constructor
        public Rule(string lhs, string rhs, float probability)
        {
            this.lhs = CollectionFactory.CreateQueue <string>();
            this.rhs = CollectionFactory.CreateQueue <string>();

            IRegularExpression regex = TextFactory.CreateRegularExpression("\\s*,\\s*");

            if (!string.IsNullOrEmpty(lhs))
            {
                this.lhs = CollectionFactory.CreateQueue <string>();
                foreach (string input in regex.Split(lhs))
                {
                    if (!string.IsNullOrEmpty(input))
                    {
                        this.lhs.Add(input);
                    }
                }
            }

            if (!string.IsNullOrEmpty(rhs))
            {
                foreach (string input in regex.Split(rhs))
                {
                    if (!string.IsNullOrEmpty(input))
                    {
                        this.rhs.Add(input);
                    }
                }
            }

            this.PROB = validateProb(probability);
        }
예제 #5
0
        public void CharacterClassIsNotAnAlternativesExpression()
        {
            IRegularExpression expression = RegularExpression.Parse("[a|b]");

            Assert.IsInstanceOfType(expression, typeof(SingleAtomExpression));
            Assert.AreEqual(new CharacterClass("[a|b]", Quantifier.None), (expression as SingleAtomExpression).Atom);
        }
예제 #6
0
        /// <summary>
        /// Tries to parse the given specifier into an Atom. For that all categories of Atoms are checked in the following order:
        ///  1. Group
        ///  2. Class
        ///  3. Literal
        /// When it succeeds, the given expression will be assigned a SingleAtomExpression containing the Atom and it's Quantifier.
        /// The parsed atom will be removed from the specifier and the method returns true. To check whether the complete specifier was an Atom,
        /// one needs to examine the specifier after calling this method. If it was, the specifier is empty after calling.
        /// </summary>
        /// <param name="specifier">The specifier to extract the leading Atom out of. Will be shortened if an Atom was successfully extracted</param>
        /// <param name="expression">The resulting SingleAtomExpression</param>
        /// <returns>True, if an Atom could be extracted, false otherwise</returns>
        // Note: could be rewritten to not consume the specifier and instead return an integer specifying the consumed length of specifier. This would remove the by-ref passed string hack
        // internal for testing
        internal static bool TryParseAsAtom(ref string specifier, out IRegularExpression expression)
        {
            Match m = groupWithQuantifier.Match(specifier);

            if (m.Success)
            {
                string atom       = m.Groups["expression"].Value;
                string quantifier = m.Groups["quantifier"].Value;
                specifier  = specifier.Substring(atom.Length + 2 + quantifier.Length);
                expression = new SingleAtomExpression(new Group("(" + atom + ")", new Quantifier(quantifier)));
                return(true);
            }
            m = characterClassWithQuantifier.Match(specifier);
            if (m.Success)
            {
                string atom       = m.Groups["expression"].Value;
                string quantifier = m.Groups["quantifier"].Value;
                specifier  = specifier.Substring(atom.Length + 2 + quantifier.Length);
                expression = new SingleAtomExpression(new CharacterClass("[" + atom + "]", new Quantifier(quantifier)));
                return(true);
            }
            m = literalWithQuantifier.Match(specifier);
            if (m.Success)
            {
                string atom       = m.Groups["expression"].Value;
                string quantifier = m.Groups["quantifier"].Value;
                specifier  = specifier.Substring(atom.Length + quantifier.Length);
                expression = new SingleAtomExpression(new Literal(atom, new Quantifier(quantifier)));
                return(true);
            }
            expression = null;
            return(false);
        }
예제 #7
0
        public Group(string specifier)
        {
            Match m = Matcher.Match(specifier);

            if (!m.Success)
            {
                throw new ArgumentException("The given specifier does not denote a Group");
            }
            _subexpression = RegularExpression.Parse(m.Groups["expression"].Value);
            _specifier     = specifier;
        }
예제 #8
0
        public Group(IRegularExpression expression, string specifier, Quantifier quantifier)
        {
            if (expression == null || quantifier == null)
            {
                throw new ArgumentNullException();
            }

            Quantifier    = quantifier;
            Subexpression = expression;
            Specifier     = specifier;
        }
예제 #9
0
        public Pattern(string expression, bool ignoreCase, bool global)
        {
            Flags = ignoreCase ? MatcherFlags.IgnoreCase : 0;
            Flags = global ? Flags | MatcherFlags.Global : Flags;

            _hasEndAnchor   = expression[expression.Length - 1].Equals('$');
            _hasStartAnchor = expression[0].Equals('^');

            int start = _hasStartAnchor ? 1 : 0;
            int end   = (_hasEndAnchor ? 1 : 0) + start;

            RootExpression = RegularExpression.Parse(expression.Substring(start, expression.Length - end));
            _description   = AssembleDescription();
        }
예제 #10
0
        private ICollection <double> exampleFromString(string line, string separator)
        {
            // assumes all values for inout and target are doubles
            ICollection <double> rexample        = CollectionFactory.CreateQueue <double>();
            IRegularExpression   regex           = TextFactory.CreateRegularExpression(separator);
            ICollection <string> attributeValues = CollectionFactory.CreateQueue <string>(regex.Split(line));

            foreach (string valString in attributeValues)
            {
                rexample.Add(double.Parse(valString,
                                          System.Globalization.NumberStyles.Any,
                                          System.Globalization.CultureInfo.InvariantCulture));
            }
            return(rexample);
        }
예제 #11
0
        public void ParseSimpleLiteralConcatenationAsConcatenatedExpression()
        {
            List <IRegularExpression> expected = new List <IRegularExpression>();

            expected.Add(new SingleAtomExpression(new Literal("a"), new Quantifier("")));
            expected.Add(new SingleAtomExpression(new Literal("b"), new Quantifier("")));

            IRegularExpression expression = RegularExpression.Parse("ab");

            Assert.IsInstanceOfType(expression, typeof(ConcatenatedExpression));
            var subexpressions = (expression as ConcatenatedExpression).Subexpressions;

            Assert.AreEqual(expected.Count, subexpressions.Count);
            for (int i = 0; i < expected.Count; i++)
            {
                Assert.AreEqual(expected[i], subexpressions[i]);
            }
        }
예제 #12
0
        public void ParseSimplisticAlternativesExpression()
        {
            List <IRegularExpression> expected = new List <IRegularExpression>();

            expected.Add(new SingleAtomExpression(new Literal("a", Quantifier.None)));
            expected.Add(new SingleAtomExpression(new Literal("b", Quantifier.None)));

            IRegularExpression expression = RegularExpression.Parse("a|b");

            Assert.IsInstanceOfType(expression, typeof(AlternativesExpression));
            var subexpressions = (expression as AlternativesExpression).Subexpressions;

            Assert.AreEqual(expected.Count, subexpressions.Count);
            for (int i = 0; i < expected.Count; i++)
            {
                Assert.AreEqual(expected[i], subexpressions[i]);
            }
        }
예제 #13
0
        public Pattern(string expression, bool ignoreCase = false, bool global = false)
        {
            if (expression == null)
            {
                throw new ArgumentNullException();
            }

            Flags = ignoreCase ? MatcherFlags.IgnoreCase : 0;
            Flags = global ? Flags | MatcherFlags.Global : Flags;

            AnchoredAtEnd   = expression[expression.Length - 1].Equals('$');
            AnchoredAtStart = expression[0].Equals('^');

            var start = AnchoredAtStart ? 1 : 0;
            var end   = (AnchoredAtEnd ? 1 : 0) + start;

            RootExpression = VBRegexParser.Parse(expression.Substring(start, expression.Length - end));
            Description    = AssembleDescription();
        }
예제 #14
0
        public IList <ValueCheckingResult> SaveRegularExpression(IEntityValidator entityValidator,
                                                                 IRegularExpression regularExpression)
        {
            var results = EntityService.CheckEntity(entityValidator, regularExpression);

            if (results.All(r => r.IsValid))
            {
                var user = MetaModelRepository.GetMdUser(DEFAULT_USER_LOGIN);
                var now  = DateTime.Now;
                regularExpression.LastUpdateDate = now;
                regularExpression.LastUpdateUser = user;
                if (EntityService.IsEntityNew(regularExpression))
                {
                    regularExpression.CreationDate = now;
                    regularExpression.CreationUser = user;
                    MetaModelRepository.CreateRegularExpression(regularExpression);
                }
                else
                {
                    MetaModelRepository.UpdateRegularExpression(regularExpression);
                }
            }
            return(results);
        }
예제 #15
0
파일: Test1.cs 프로젝트: ankit079/Regex
 public void Initialize()
 {
     _regExpMock = new RegexUtilities();
 }
예제 #16
0
 /// <summary>
 /// Replaces all matches with a new value.
 /// </summary>
 /// <param name="regExp">The regular expression to search with.</param>
 /// <param name="newValue">The value to replace with.</param>
 /// <returns>The resulting string.</returns>
 public string replace(IRegularExpression regExp, string newValue)
 {
     return(null);
 }
예제 #17
0
 /// <summary>
 /// Searches for a match between a regular expression and a string, and returns the position of the match.
 /// </summary>
 /// <param name="regExp">The regular expression to search with.</param>
 /// <returns>Returns the index of the match, or -1 if one is not found.</returns>
 public int search(IRegularExpression regExp)
 {
     return(0);
 }
예제 #18
0
파일: String.cs 프로젝트: JimmyJune/blade
 /// <summary>
 /// Searches for a match between a regular expression and a string, and returns the position of the match.
 /// </summary>
 /// <param name="regExp">The regular expression to search with.</param>
 /// <returns>Returns the index of the match, or -1 if one is not found.</returns>
 public int search(IRegularExpression regExp) { return 0; } 
예제 #19
0
파일: String.cs 프로젝트: JimmyJune/blade
 /// <summary>
 /// Replaces all matches with a new value.
 /// </summary>
 /// <param name="regExp">The regular expression to search with.</param>
 /// <param name="newValue">The value to replace with.</param>
 /// <returns>The resulting string.</returns>
 public string replace(IRegularExpression regExp, string newValue) { return null; } 
예제 #20
0
파일: String.cs 프로젝트: JimmyJune/blade
 /// <summary>
 /// Searches for a match between a regular expression and a string, and returns the matches.
 /// </summary>
 /// <param name="regExp">The regular expression to use.</param>
 /// <returns>An array of matching values.</returns>
 public string[] match(IRegularExpression regExp) { return null; } 
예제 #21
0
 public object match(IRegularExpression regExp)
 {
     return null;
 }
예제 #22
0
 /// <summary>
 /// Searches for a match between a regular expression and a string, and returns the matches.
 /// </summary>
 /// <param name="regExp">The regular expression to use.</param>
 /// <returns>An array of matching values.</returns>
 public string[] match(IRegularExpression regExp)
 {
     return(null);
 }
예제 #23
0
 public virtual void Visit(IRegularExpression instance)
 {
 }
 protected RegularExpressionItemViewModel(IRegularExpression regularExpression) => Value = regularExpression;
 private static TreeViewItem AsTreeViewItem(IRegularExpression expression)
 {
     throw new InvalidOperationException("Some unknown IRegularExpression subtype was in RegexAssistantViewModel");
 }