Code Monkey home page Code Monkey logo

subseq-matcher's Introduction

subseq-matcher

Build status of the master branch on Unix Build status of the master branch on Windows

A simple filter that reads lines from STDIN, ranks them using subsequence-matching with a specified query and outputs the sorted list to STDOUT. Designed to serve as the engine powering any keyboard based selection interface. For example, quickly selecting a file in an editor, or quickly executing a command from your shell history, with only a few keystrokes.

Look at the integrations directory to see how to integrate subseq-matcher with various programs.

Installation

As simple as:

make && sudo make install

This will install /usr/bin/subseq-matcher. You can also run it without installation directly from build/subseq-matcher.

Understanding the matching algorithm

The most basic fact about the matching algorithm is that it is a subsequence match. This means that ct will match both act and cot. It will match any string that contains the letters c followed somewhere by t.

What makes it useful is the concept of anchors. These are special locations, which increase the score of the ranking algorithm if a character matches at them. For example, when filtering paths, it is useful to use the / character as an anchor. So the query abc will match both the paths:

  • /A/Better/Catch
  • /some/AlaBaster/torC

But, the first match will score higher. This matches the intuitive mental model we use to find files inside a directory hierarchy. The mental route is something like: folder a to sub folder b to file c. The query is naturally encoded using the anchored-subsequence algorithm as simply abc. This results in fast, intuitive, minimum mental overhead navigation of directory hierarchies.

subseq-matcher has three configurable levels of anchors. The defaults are setup for general matching, prioritizing characters that occur after path and extension separators (/ and .) as well as underscores, camelCase characters and spaces. These can be easily changed via command line options allowing the algorithm to be tuned for specific scenarios, as desired.

Run subseq-matcher -h for a list of command line options.

Performance

subseq-matcher is written in C and uses a non-recursive, optimized algorithm. On my machine it can filter 10,000 strings with a query of four characters in 0.054s (avg), making it imperceptible to human senses.

Acknowledgements

Sub-sequence matching is obviously not new. It has a long history, the first time I came across it personally, was in the Command-T and YouCompleteMe plugins for vim. However, as I quickly found myself wanting to use it in more and more contexts, it made sense to separate out the core algorithm into a standalone utility that makes it easy to integrate into different work flows.

subseq-matcher's People

Contributors

kovidgoyal avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  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.