Git Product home page Git Product logo

pylexer's Introduction

pylexer

A tiny lexer module written in Python

About

By Anthony Nguyen

I was learning about string tokenization for NLP and I wrote this as a sort of application of the concepts I'm learning.

It will stop if it does not find a token at the beginning of the data it is looking at.

Usage

import pylexer, re
lexer = pylexer.Lexer([
	("word", "[a-z]+"),
	("shortdate", "\d{1,2}\/\d{1,2}\/\d{4}")
	], re.I)
for token in lexer.scan("I was born on 01/01/1970", True):
	print token
  • Lexer.__init__ takes two arguments: a list of tokens as tuples ("name", "regex"), and any regex flags
  • Lexer.addTokens takes a list of tokens as tuples ("name", "regex")
  • Lexer.addFlags takes any number of regex flags
  • Lexer.scan is an iterator and takes two arguments: the string to scan, and whether or not to ignore whitespace (optional, disabled by default). It returns objects of the Token class.

The Token class is basically a variable container. It holds the following information:

  • name - The token's name
  • rule - The regex used to match the token
  • data - The token's matched data
  • start - The start index (in the original string) of the token's data
  • end - The end index (in the original string) of the token's data

The Scanner class is the class that's doing the actual scanning work, but it should only ever need to be used from the Lexer class.

License

MIT licensed. See LICENSE.

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.