# `Amplified.InvertedIndex`
[🔗](https://github.com/amplifiedai/amplified_inverted_index/blob/v0.1.0/lib/amplified/inverted_index.ex#L1)

Converts between plain text and inverted index format.

An inverted index maps each unique word to the list of positions where it
appears in the original text:

    %{"Despite" => [0], "growing" => [1], "interest" => [2], "in" => [3, 57]}

This format is used by [OpenAlex](https://openalex.org) to represent
scholarly abstracts. This module converts between that representation and
plain text strings.

## Examples

    iex> index = %{"Hello" => [0], "world" => [1]}
    iex> Amplified.InvertedIndex.to_text(index)
    "Hello world"

    iex> Amplified.InvertedIndex.from_text("Hello world")
    %{"Hello" => [0], "world" => [1]}

# `from_text`

```elixir
@spec from_text(String.t() | nil) :: map() | nil
```

Converts plain text to an inverted index map.

Returns `nil` if the input is `nil`.

## Examples

    iex> Amplified.InvertedIndex.from_text("Hello world")
    %{"Hello" => [0], "world" => [1]}

    iex> Amplified.InvertedIndex.from_text("the cat chased the dog")
    %{"cat" => [1], "chased" => [2], "dog" => [4], "the" => [0, 3]}

    iex> Amplified.InvertedIndex.from_text(nil)
    nil

# `to_text`

```elixir
@spec to_text(map() | nil) :: String.t() | nil
```

Converts an inverted index map to plain text.

Returns `nil` if the input is `nil`.

## Examples

    iex> Amplified.InvertedIndex.to_text(%{"Hello" => [0], "world" => [1]})
    "Hello world"

    iex> Amplified.InvertedIndex.to_text(%{"the" => [0, 3], "cat" => [1], "chased" => [2], "dog" => [4]})
    "the cat chased the dog"

    iex> Amplified.InvertedIndex.to_text(nil)
    nil

---

*Consult [api-reference.md](api-reference.md) for complete listing*
