Git Product home page Git Product logo

semantic-regex's Introduction

Learning from Uncurated Regular Expressions

CI pre-commit.ci status

Dependencies of all Python code are managed with Pipenv and can be installed with pipenv install. Note that the dataset from the Sherlock project should be available in a copy of the repository in alongside the directory for this project. jq is also required for some JSON processing.

Model training

  1. Download all regular expressions from regex101

./download_patterns.sh

This will create a directory regex101 which has the individual regular expressions and patterns.json which contains only the expressions strings.

  1. Compile a database of all the downloaded regular expressions

pipenv run python compile_db.py < patterns.json > patterns_final.json

patterns_final.json is a subset of the expressions in patterns.json which are supported by Hyperscan. This step will also create hs.db which are the compiled regular expressions that can be used during preprocessing.

  1. Preprocess the data to generate feature vectors

pipenv run python preprocess.py train

This will generate preprocessed_train.txt which contains all the feature vectors extracted using the regular expression extracted using the regular expressions.

  1. Train the model on the extracted features

pipenv run python train.py

The model architecture will be stored in nn_model_sherlock.json with the weights in nn_model_weights_sherlock.h5.

Evaluation

First, the test data must be preprocessed.

pipenv run python preprocess.py test

Then, the model can be evaluated.

pipenv run python test.py

Model explanation

Explains for predictions for an individual class can be generated using SHAP. First, follow the steps for training the model above. The file patterns_final.json will be used to match the patterns back to the original regular expressions.

pipenv run python find_patterns.py > pattern_ids.txt

This file of pattern IDs will then be used to label the SHAP plot with the ID of the regular expression. To generate the SHAP plot in shap.png, run the command below where <class_name> is one of the semantic types defined by Sherlock.

pipenv run python explain.py <class_name>

The IDs displayed in the SHAP plot can be used to reference the regular expressions by ID in the regex101/patterns directory or viewing it directly on regex101 at the URL https://regex101.com/library/<ID>.

semantic-regex's People

Contributors

dependabot[bot] avatar michaelmior avatar

Stargazers

 avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.