A Case Study on Grammatical-based Representation for Regular Expression Evolution

Similar documents
CSCI 2132 Software Development. Lecture 7: Wildcards and Regular Expressions

MODELLING DOCUMENT CATEGORIES BY EVOLUTIONARY LEARNING OF TEXT CENTROIDS

Genetic programming. Lecture Genetic Programming. LISP as a GP language. LISP structure. S-expressions

Formal Languages and Compilers Lecture IV: Regular Languages and Finite. Finite Automata

Lecture 4: Syntax Specification

Hybridization EVOLUTIONARY COMPUTING. Reasons for Hybridization - 1. Naming. Reasons for Hybridization - 3. Reasons for Hybridization - 2

CS 301. Lecture 05 Applications of Regular Languages. Stephen Checkoway. January 31, 2018

MIT Specifying Languages with Regular Expressions and Context-Free Grammars. Martin Rinard Massachusetts Institute of Technology

MIT Specifying Languages with Regular Expressions and Context-Free Grammars

Genetic Programming. and its use for learning Concepts in Description Logics

Suppose you have a problem You don t know how to solve it What can you do? Can you use a computer to somehow find a solution for you?

Evolutionary Computation. Chao Lan

Regular Expressions. with a brief intro to FSM Systems Skills in C and Unix

G52LAC Languages and Computation Lecture 6

Lexical Analysis. COMP 524, Spring 2014 Bryan Ward

Learning Restarting Automata by Genetic Algorithms

Neha 1, Abhishek Sharma 2 1 M.Tech, 2 Assistant Professor. Department of Cse, Shri Balwant College of Engineering &Technology, Dcrust University

1. Introduction. 2. Motivation and Problem Definition. Volume 8 Issue 2, February Susmita Mohapatra

Computer Science Department Carlos III University of Madrid Leganés (Spain) David Griol Barres

ECS 120 Lesson 7 Regular Expressions, Pt. 1

CSE 105 THEORY OF COMPUTATION

Domain-Independent Genotype to Phenotype Mapping through XML Rules

CHAPTER 5 ENERGY MANAGEMENT USING FUZZY GENETIC APPROACH IN WSN

Decision, Computation and Language

David Griol Barres Computer Science Department Carlos III University of Madrid Leganés (Spain)

A Steady-State Genetic Algorithm for Traveling Salesman Problem with Pickup and Delivery

The Genetic Algorithm for finding the maxima of single-variable functions

Genetic Algorithms and Genetic Programming. Lecture 9: (23/10/09)

Genetic Algorithms. Kang Zheng Karl Schober

Using a genetic algorithm for editing k-nearest neighbor classifiers

Meta- Heuristic based Optimization Algorithms: A Comparative Study of Genetic Algorithm and Particle Swarm Optimization

Automata Construct with Genetic Algorithm

BI-OBJECTIVE EVOLUTIONARY ALGORITHM FOR FLEXIBLE JOB-SHOP SCHEDULING PROBLEM. Minimizing Make Span and the Total Workload of Machines

Evolving SQL Queries for Data Mining

Experimental Comparison of Different Techniques to Generate Adaptive Sequences

CS 432 Fall Mike Lam, Professor. Finite Automata Conversions and Lexing

Evolutionary design for the behaviour of cellular automaton-based complex systems

Genetic Algorithms for Vision and Pattern Recognition

Evolutionary Decision Trees and Software Metrics for Module Defects Identification

Evolutionary Linkage Creation between Information Sources in P2P Networks

Binary Representations of Integers and the Performance of Selectorecombinative Genetic Algorithms

FAdo: Interactive Tools for Learning Formal Computational Models

Outline. Motivation. Introduction of GAs. Genetic Algorithm 9/7/2017. Motivation Genetic algorithms An illustrative example Hypothesis space search

Santa Fe Trail Problem Solution Using Grammatical Evolution

Question Bank. 10CS63:Compiler Design

Lecture 6: Genetic Algorithm. An Introduction to Meta-Heuristics, Produced by Qiangfu Zhao (Since 2012), All rights reserved

CSE450. Translation of Programming Languages. Lecture 20: Automata and Regular Expressions

A GENETIC ALGORITHM FOR CLUSTERING ON VERY LARGE DATA SETS

Multiobjective Job-Shop Scheduling With Genetic Algorithms Using a New Representation and Standard Uniform Crossover

Polyhedron Evolver Evolution of 3D Shapes with Evolvica

Using Meta-Modelling and Graph Grammars to create Modelling Environments

Figure 2.1: Role of Lexical Analyzer

Geometric Crossover for Sets, Multisets and Partitions

Compiler Construction

Compiler Design. 2. Regular Expressions & Finite State Automata (FSA) Kanat Bolazar January 21, 2010

Constructing an Optimisation Phase Using Grammatical Evolution. Brad Alexander and Michael Gratton

A New Selection Operator - CSM in Genetic Algorithms for Solving the TSP

Genetic Programming: A study on Computer Language

entire search space constituting coefficient sets. The brute force approach performs three passes through the search space, with each run the se

Different approaches for development tools for natural computers: grammar driven vs. model driven approaches

Composability Test of BOM based models using Petri Nets

Genetic Image Network for Image Classification

Introduction to Evolutionary Computation

JHPCSN: Volume 4, Number 1, 2012, pp. 1-7

JGA User Guide. WinNT/2000 Deployment v

CSE 105 THEORY OF COMPUTATION

Sparse Matrices Reordering using Evolutionary Algorithms: A Seeded Approach

Genetic Programming. Charles Chilaka. Department of Computational Science Memorial University of Newfoundland

Compiler Construction I

1 Finite Representations of Languages

Regular Expressions. Agenda for Today. Grammar for a Tiny Language. Programming Language Specifications

Crew Scheduling Problem: A Column Generation Approach Improved by a Genetic Algorithm. Santos and Mateus (2007)

CSE 413 Programming Languages & Implementation. Hal Perkins Autumn 2012 Grammars, Scanners & Regular Expressions

Using Genetic Algorithms to optimize ACS-TSP

Improvement of Web Search Results using Genetic Algorithm on Word Sense Disambiguation

Wildcards and Regular Expressions

Regular Languages. MACM 300 Formal Languages and Automata. Formal Languages: Recap. Regular Languages

Adaptive Information Filtering: evolutionary computation and n-gram representation 1

Applied Cloning Techniques for a Genetic Algorithm Used in Evolvable Hardware Design

CONCEPT FORMATION AND DECISION TREE INDUCTION USING THE GENETIC PROGRAMMING PARADIGM

Automating Test Driven Development with Grammatical Evolution

Lecture 18 Regular Expressions

An Analysis of Approaches to XML Schema Inference

A Framework for adaptive focused web crawling and information retrieval using genetic algorithms

Lexical Analysis/Scanning

Theoretical Part. Chapter one:- - What are the Phases of compiler? Answer:

Implementation of Lexical Analysis. Lecture 4

CLOSED LOOP SYSTEM IDENTIFICATION USING GENETIC ALGORITHM

Artificial Intelligence Application (Genetic Algorithm)

Lexical Analysis. Prof. James L. Frankel Harvard University

Chapter 9: Genetic Algorithms

Genetic Programming of Autonomous Agents. Functional Description and Complete System Block Diagram. Scott O'Dell

Introduction to Genetic Algorithms. Based on Chapter 10 of Marsland Chapter 9 of Mitchell

Chapter Seven: Regular Expressions. Formal Language, chapter 7, slide 1

A Parallel Evolutionary Algorithm for Discovery of Decision Rules

ON THE MECHANIZATION OF KLEENE ALGEBRA IN FORMAL LANGUAGE ANALYZER. Dmitry Cheremisinov

Compiler Construction

Genetic Process Mining: A Basic Approach and its Challenges

METAHEURISTICS Genetic Algorithm

Languages and Compilers

Transcription:

A Case Study on Grammatical-based Representation for Regular Expression Evolution Antonio González 1, David F. Barrero 2, David Camacho 1, María D. R-Moreno 2 Abstract Regular expressions, or simply regex, have been widely used as a powerful pattern matching and text extractor tool through decades. Although they provide a powerful and flexible notation to define and retrieve patterns from text, the syntax and the grammatical rules of these regex notations are not easy to use, and even to understand. Any regex can be represented as a Deterministic or Non-Deterministic Finite Automata; so it is possible to design a representation to automatically build a regex, and a optimization algorithm able to find the best regex in terms of complexity. This paper introduces both, a graph-based representation for regex, and a particular heuristic-based evolutionary computing algorithm based on grammatical features from this language in a particular data extraction problem. Keywords: Regular Expressions, Grammatical-based representation, Evolutionary algorithms. 1 Introduction Any Regular Expression, or regex [Friedl, 2002], can be described as a particular kind of notation for describing patterns of text. When a particular string is in the set described by a regex, it is said that the regex matches the string. The powerful pattern matching facilities provided by regex in different programming languages such as Perl, PHP, JavaScript, PCRE, Python, Ruby, or Java have not been conveniently exploited by the programmers or the computer scientists due the difficulty 1 Departamento de Informática. Universidad Autónoma de Madrid. C/Francisco Tomás y Valiente, n 11, 28049 Madrid, Spain. {antonio.gonzalez,david.camacho}@uam.es 2 Departamento de Automática. Universidad de Alcalá. Ctra Madrid-Barcelona, Km. 33,6. 28871 Alcalá de Henares (Madrid), Spain. {mdolores,david}@aut.uah.es 1

2 A.González et. al. to write and understand the syntax, as well as the semantic meaning of those regular expressions. Any regex can be represented as a Deterministic or Non-Deterministic Finite Automata, a complete discussion of this problem can be found at [Thompson, 1968], [Kleene, 1956], [Chang and Paige, 1992]. From an algorithmic and programming perspective the problem about how to represent and look for the optimal regex is an interesting, but usually a hard problem [Gold, 1978]. From the set of optimization and Machine Learning methods that can be used to represent and automatically search for regular expressions, the Evolutionary Computation (EC) [Eiben and Smith, 2008] provides a collection of algorithms inspired in the biological evolution whose characteristics make them promising to address this problem. 2 Graph-based representation for Regex Different approaches can be defined to represent a regular expression as an evolutionary based individual, from GAs [Barrero et al., 2009] to GP [Dunay et al., 1994]. The final representation of this individual is a critical aspect in any EC algorithm. This work presents an initial approach based on graphs that uses the basic and syntactic considerations of the regex notation. 2.1 A brief introduction to Regular Expressions Regular expressions [Cox and Russ, 2007] are a tool used for providing a compact representation of string patterns. They have been widely used by programmers and systems administrators since most of common languages such as Perl, Java, C or PHP support regex and many UNIX administration tools such as grep or sed use them. Regex are commonly used, for instance, to extract strings or validate user inputs. Regular expressions contain some special symbols called wildcards Following the basic wildcards from IEEE POSIX Basic Regular Expressions (BRE) standard, and POSIX Extended Regular Expressions (ERE) notation, 4 wildcards 1 are considered in this work which are Plus, Star, Question mark and Pipe. These wildcards are explained bellow and a graph-based representation are provided in Figure 1: Plus +. Repeats the previous item once or more. Start *. Repeats the previous item zero or more times. Question mark?: Makes the preceding item optional. Pipe : Causes the regex engine to match either the part on the left side, or the part on the right side. Can be strung together into a series of options. 1 A complete syntax reference can be found at http://www.regular-expressions.info/reference.html

A Case Study on Grammatical-based Representation for regex 3 a+ a* a b a? a a a a b Fig. 1 Graph-based representation for main wildcards. 2.2 A particular example on web data extraction The extraction of simple web data, as URL addresses, is a very common and useful task achieved by a huge type of programs that crawling and searching the World Wide Web for useful information. Using the representation shown in previous section, any regex could be represented as a Finite Automata. Next regex could be used to retrieve a generic URL link (it only considers most current usual protocols as http, https or ftp) from a web page: (https http ftp)://[a-za-z]+(\.[a-za-z]+)*\.(com net org) Figure 2 shows in detail the graphical representation of this regex. Note that the aim of the graph is to accept the string that matches the regex, not to accept the string that represents the regex. The graph must accept, for example, http://my.url.com but not the regex itself. Also note the initial node is represented as a node with a double border and without any label. https \. a-za-z net http : / / a-za-z \. com ftp org Fig. 2 Graph-based representation for a regex. 2.3 Nodes representation A major problem in a graph representation is the combinatorial explosion that might happen if each character is represented by a node. In order to avoid it, the representation must reduce the size of the graphs using nodes to represent composed symbols instead of simple ones. In this way the search space is drastically reduced, however a new problem arises: how to automatically construct the composed alphabet. Our approach uses the Zipf Law [Zipf, 1935] to construct the alphabet. Zipf observed that given a corpus, a small subset of the words concentrates a high frequency of occurrences, while most words appears few times in the corpus. This fact is used

4 A.González et. al. to design a heuristic able to identify important strings, and use it to build the alphabet. For instance, given a set of URL examples, it is possible to identify http and ftp as common strings, and thus use them as a symbol. The graph that is evolved is composed by three types of nodes. Each node is able to accept a composed symbol such as http. Nodes of type N 1 contain the symbols that generated Zipf valid strings, while nodes of type N 2 are the strings extracted using Zipf Law. Symbols N 3, N 4 and N 5 are the especial characters from regex with an increasing semantics, so the wildcard * represents a cyclic graph (see previous section), whereas symbol ( is used to maintain the syntax correctness of the regex. The information given from these kind of nodes will be used by an evolutionary algorithm to guide both the mutation and crossover operations. Table 1 Categorization of Nodes Node Elements N 1 {:, /,\.} N 2 {http, https, ftp, com, es, org, net, 8080} N 3 {\d, \w, a-z, A-Z, } N 4 {(, ), {, }, [, ]} N 5 {*, +,?} 2.4 Population representation and initial population generation Each individual in the population represents a graph whose phenotype is a regex. The graph is represented by a variable length list of interconnected nodes. In order to generate always a syntactically correct regex the set of N 5 nodes has been extended with three more elements, which are +, * and?. These nodes do not belong to the vocabulary about regex nor the vocabulary about URL, but they are needed to restrict the effect of the elements +, * and?, respectively. For example, the regex abc(de)* will be represented as abc*(de)*, and the program will know that only the string ed is affected by the operator *. Figure 3 shows the genotype of an individual that represents the regex (http ftp):(\w)*. Fig. 3 Regular expression for URL address represented as a list of N j nodes. Once an individual can be represented, it has been defined some heuristics to guide the generation of the initial population. These heuristics allow to create wellformed (syntactically and grammatically) individuals. In order to do that, a maximum length of the genotype has been defined. Note, that this length is a constraint for the generation of the initial population, for that reason its possible to obtain, in

A Case Study on Grammatical-based Representation for regex 5 any generation, any individual whose genotype exceeds this maximum length. A manually generated regex able to accept all the patterns of this study has 55 nodes. The maximum length of the genotype of the initial population will be 70 nodes. This value allows initial population to exceed the length of the valid regex in, approximately, a 25%. 3 Mutation and crossover operations for regex evolution From our previous nodes classification, two different types of mutation have been defined: Random. This is equivalent to the uniform mutation operator found in GA or point mutation in GP. Once a particular node i that belongs to class j (n i, j ) in the graph is selected, it is changed by any randomly token available in our sets of nodes. Therefore, any node in the graph can be substituted by any other. Guided. This mutation takes randomly the node n i, j N j from the graph, and it only can be changed by other token (randomly selected) which belongs to the class N j. This guided mutation tries to use the statistical knowledge acquired from the analysis of natural language, so the token http could be mutated into ftp, and the grammatical knowledge from the regex notation, so the wildcard * could be mutated into +. Note, that in the case of the node belongs to class N 4 and N 5, the same mutation has to be performed twice due to nodes belonging to these classes appear in pairs. That means, if a node * is mutated into +, then the corresponding node *, which represent the end of the operator, must change into +. The guided mutation allows one to reuse grammatical and syntactical knowledge from the language used to generate regex, it allows to easily generate correct individuals. However, this produces a second important bias in the searching process of individuals. The crossover operation using a graph and grammatical-based representation (see section 2) needs to consider the inner structure of this graph to select part of the graphs that can be later used in the offspring generation. To compare how the representation works, some crossover operations have been defined: One-point. This is the equivalent crossover of the traditional one-point crossover found in GA. A point is randomly selected from both parents splitting its genotype in two parts. The new individual generated will inherit two parts, one from each parent. Two-point. Two points inside the graph (both randomly) are selected, then both sections of the graph are interchanged. It is the equivalent of two-points GA crossover. Grammatical-based selection. In order to explain this operation, a new concept typed block is defined. Let be B x, where x {N 4,N 5 }, a block of variable

6 A.González et. al. length limited by nodes of class x, B x is named as block of type x. For example: +(http ftp)+ is a block limited by nodes belonging to N 5 class, thus is a B 5 block. Grammatical-based selection is heuristically guided and it works as follows: 1. One point in the graph is selected. 2. If the selected node is contained into a B 4 block, then the entire block is selected. 3. If the selected node is not included into a B 4 block, the algorithm will try to determine the B 5 block that contains the selected node. If that block exists, then it is selected. 4. Finally, if the selected node does not belong to a B 5 block nor a B 4 block, then only the selected node will be exchanged. 4 Experimental Results Genetic Algorithms (GA) need an objective function which allows to evaluate the population and to differentiate its individual. This function is called fitness function. In order to evaluate each regex generated by the program, a file with different URLs is used. With this two notions and the context of this case study, it is easy to identify that a possible fitness function could be the number of URLs that the generated regex is able to match. Nevertheless, this fitness function is very restrictive and this makes the evolution of the individual difficult. The fitness function used in this case study is based on the proportion of character that the regex is able to match from the whole test file. Given an individuals i, the fitness of i is defined by equation 1. F(i)= N j=1 Where N is the number of URLs in the test file, m i j is the number of characters that the individual i matches from the URL number j and l j is the total length of the URL number j. There are some parameters which must be specified to execute the GA, these parameters are the number of parents in the population, the number of offspring population and the mutation rate. These values have been set with the following values: the parent population is 30, the offspring population is 45 and the mutation rate is 0,05. With these values, the program has been executed taking into account all possible combinations of Mutation and Crossover operations, that means trying to launch the program using random crossover and random mutation, guided crossover and random mutation, random crossover and guided mutation and, guided crossover and guided mutation. Figure 4(a) shows the results of launching the program with random crossover and random mutation. As it can be seen, there is no evolution in the population due to data dispersion. These bad results are expected because with random mutation N m i j l j (1)

A Case Study on Grammatical-based Representation for regex 7 and random crossover, there is no guarantee of generating well-formed individual. Therefore, the probability of generating any individual with a good fitness value is very low. Another execution uses guided crossover and random mutation in order to obtain better results than the previous one. The results of this execution are shown in Figure 4(b). As it can be seen, this experiment provides better results than the previous one, but they are not useful since it only recognizes the 30% of the examples. The experiment with random crossover and guided mutation does not work due to guided mutation needs the creation of well-formed individual (syntactically and grammatically), and this restriction is not always true by the random crossover. (a) Random crossover and random mutation (b) Guided crossover and random mutation Fig. 4 Mean fitness of graph evolution. Finally, with guided mutation and guided crossover the best result are obtained. All the generated individuals are well-formed (syntactically and grammatically) due to the heuristic applied in the operators (see Section 3). Figure 5 shows the result of this experiment. The evolution of the population is represented in the increase of the fitness of the population for each generation. The chart shows how the mean fitness increases and it is closer to the optimal value. This value is 1 which represents that it is matched a 100% of the examples in the test file. An example of individual generated by the program is the following:[\w ft p \d/?/a Z] 5 Conclusions This work shows a representation and evolutionary algorithm case study based on both, graphs and grammatical features from the syntax and grammar standards used to define regex. It have been defined two different kind of specific evolutionary operators, mutation and crossover guided by these grammatical considerations. Although the experimental results provide an initial evaluation of the genetic algorithm, more experiments must be carried out in the future and other parameters, such as precision, need to be considered in order to measure the efficiency of the algorithm.

8 A.González et. al. Fig. 5 Results of execution with guided crossover and guided mutation. Acknowledgements This work has been partially supported by the Spanish Ministry of Science and Innovation under the projects Castilla-La Mancha project PEII09-0266-6640, COM- PUBIODIVE (TIN2007-65989), and by HADA (TIN2007-64718). References [Barrero et al., 2009] Barrero, D. F., Camacho, D., and R-Moreno, M. D. (2009). Data Mining and Multiagent Integration, chapter Automatic Web Data Extraction Based on Genetic Algorithms and Regular Expressions. Springer. [Chang and Paige, 1992] Chang, C.-H. and Paige, R. (1992). From regular expressions to dfa s using compressed nfa s. pages 90 110. [Cox and Russ, 2007] Cox and Russ (2007). Regular expression matching can be simple and fast. [Dunay et al., 1994] Dunay, B. D., Petry, F., and Buckles, B. P. (1994). Regular language induction with genetic programming. In Proceedings of the 1994 IEEE World Congress on Computational Intelligence, pages 396 400, Orlando, Florida, USA. IEEE Press. [Eiben and Smith, 2008] Eiben, A. E. and Smith, J. E. (2008). Introduction to Evolutionary Computing (Natural Computing Series). Springer. [Friedl, 2002] Friedl, J. E. F. (2002). Mastering Regular Expressions. O Reilly & Associates, Inc., Sebastopol, CA, USA. [Gold, 1978] Gold, E. M. (1978). Complexity of automaton identification from given data. Inform. Control, 37:302 320. [Kleene, 1956] Kleene, S. C. (1956). Representation of events in nerve nets and finite automata. In Shannon, C. E. and McCarthy, J., editors, Automata studies, volume 34, pages 3 40. [Thompson, 1968] Thompson, K. (1968). Regular expression search algorithm. Comm. Assoc. Comp. Mach., 11(6):419 422. [Zipf, 1935] Zipf, G. (1935). The psycho-biology of language. Houghton Mifflin, Boston, MA.