Usage

Programmatic

Amargan takes any iterable that yields strings, thus making it memory efficient:

Example: From the contents of a file

1
2
3
4
5
6
7
8
>>> from amargan import Amargan
...
... with open(filename) as fp:
...     anagrams = Amargan(fp.readlines())
... anagrams['abc']
set(['abc', 'acb', 'cba'])
... anagrams.for_word('abc')
set(['abc', 'acb', 'cba'])

Example: From an open file

1
2
3
4
5
6
7
8
>>> from amargan import Amargan
...
... with open(filename) as fp:
...     anagrams = Amargan(fp)
... anagrams['abc']
set(['abc', 'acb', 'cba'])
... anagrams.for_word('abc')
set(['abc', 'acb', 'cba'])

Example: From a StringIO

1
2
3
4
5
6
7
8
>>> from amargan import Amargan
...
... sio = six.StringIO(buf='cba\nabc\nacb\n')
... anagrams = Amargan(sio)
... anagrams['abc']
set(['abc', 'acb', 'cba'])
... anagrams.for_word('abc')
set(['abc', 'acb', 'cba'])

Example: From a list

1
2
3
4
5
6
7
>>> from amargan import Amargan
...
... anagrams = Amargan(['cba', 'abc', 'acb'])
... anagrams['abc']
set(['abc', 'acb', 'cba'])
... anagrams.for_word('abc')
set(['abc', 'acb', 'cba'])

There are configurable Iterators to allow you to read from a file using a non-default configuration.

For example, to iterate over a multi-line file containing words separated by a comma:

1
2
3
4
5
6
7
>>> from amargan import Amargan, Iterator, IteratorType
...
... with open(filename) as fp:
...     iterator = Iterator(IteratorType.multi_per_line, sep=',')
...     anagrams = Amargan(iterator(fp))
... anagrams['abc']
set(['abc', 'acb', 'cba'])

To iterate over a multi-line file containing lines of one or more words separated by a whitespace (the default iterator configuration):

1
2
3
4
5
6
>>> from amargan import Amargan
...
... with open(filename) as iterator:
...     anagrams = Amargan(iterator)
... anagrams['abc']
set(['abc', 'acb', 'cba'])

Add and remove words from the dictionary:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
>>> from amargan import Amargan
...
... anagrams = Amargan()
... anagrams['acb']
frozenset()
... anagrams += 'abc acb cab'
... anagrams['acb']
set(['abc', 'acb', 'cba'])
... anagrams -= 'acb'
... anagrams['acb']
set(['abc', 'cba'])
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
>>> from amargan import Amargan
...
... anagrams = Amargan()
... anagrams['acb']
frozenset()
... x = anagrams + 'abc acb cab'
... x
Amargan(True - 1)
... x['acb']
set(['abc', 'acb', 'cba'])
... x = anagrams - 'acb'
... x['acb']
set(['abc', 'cba'])

Command-line

To use amargan from the command-line for single words (anagrams are ordered and contain the original word by default):

1
2
$ find_anagrams -i words.txt hello
elloh hello lehol

and for multiple words:

1
2
3
$ find_anagrams --ip=words.txt hello world
elloh hello lehol
lordw rlwdo world

and with options:

1
2
3
$ find_anagrams --exclude --output-iterator=one_per_line --case-sensitive --ip=words.txt Hello
elloH
leHol