GithubHelp home page GithubHelp logo

anagram.js's Introduction

anagram.js

A NW.js app for resolving anagrams.

What is an anagram? Mary and Army.

A word, phrase, or name formed by rearranging the letters of another, such as spar, formed from rasp. -Oxford Dictionary

Quick start

Install Node.js or io.js.

git clone https://github.com/madwyn/anagram.js.git anagrams
cd anagrams
npm install
npm start

It will start an application window, like a simplified Chrome browser.

The demo provides three functions:

  1. Load a dictionary. You can also add filters. The dictionary should be a text file, words are divided by newlines. You can use this test dictionary.

  2. Process the dictionary. Find out all the anagrams in the dictionary. A summary will be shown after processing.

  3. Search anagrams. Type in any word or pattern to find the corresponding anagrams in the dictionary.

How does it work?

Given a dictionary (word list), find out all the anagrams within.

Hash.js, a hash function

Given a word, randomise the order of the letters, we get a new word. These two words become a set of anagrams.

The hash function is to differ anagrams. It generates "labels" for each word, so that all words in a set of anagrams have the same label, but any two words from different anagram sets should have different lables.

There are several ways to do this. In this implementation, there are two:

  1. Hash.sort(), sort the letters.
  2. Hash.count(), count the letters.

sort will label 'time' as 'eimt'. count will create an integer array for 26 letters, position [0, 25] for [a, z]. Count the letters in the word. For example: 'ace' will generate [1,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0].

In theory, count has an average complexity of O(n) while sort has O(nlog(n)). In reality, sort is natively implemented in browser and runs much faster than count, there is also a dedicated test case for this.

HashMap.js, a hash map

It's a thin wrap around JavaScript object which is already a HashMap.

HashMap has an average O(1) for inserts and lookups, ideal for anagram processing.

Anagram.js, a simple routine to feed the words

It has two hash maps, one for the non-anagrammatic words, the other is for the anagrams. The class provides a feed() function to consume words.

It also gathers information about the biggest anagram set and the loggest anagrams during feeding.

Testing

npm test

Dependencies

Working core

There is no dependency for the core modules: Hash.js, HashMap.js and Anagram.js.

Demo and test

anagram.js's People

Contributors

madwyn avatar

Watchers

James Cloos 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.