# @datagica/treegram

> Treegram is a hierarchical n-gram extraction library

Latest version **0.3.0** (published 2018-09-27) · GPL-3.0 license · 0 weekly downloads

## Install

```sh
npm install @datagica/treegram
pnpm add @datagica/treegram
yarn add @datagica/treegram
bun add @datagica/treegram
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.3.0 |
| Published | 2018-09-27 |
| First published | 2016-02-06 |
| Weekly downloads | 0 |
| License | GPL-3.0 |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 32.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Julian Bilcke |
| Maintainers | datagica |

## Links

- npm: https://www.npmjs.com/package/@datagica/treegram
- Repository: https://github.com/datagica/treegram
- Homepage: https://github.com/datagica/treegram#readme
- Issues: https://github.com/datagica/treegram/issues
- npm.io page: https://npm.io/package/@datagica/treegram

## Dependencies (2)

- [@datagica/tokenize](https://npm.io/package/@datagica/tokenize.md) ^0.0.2
- [@datagica/fast-index](https://npm.io/package/@datagica/fast-index.md) ^0.1.0

## Recent versions

- 0.3.0 (latest) — 2018-09-27
- 0.2.9 — 2018-09-27
- 0.2.8 — 2018-09-26
- 0.2.7 — 2017-07-18
- 0.2.6 — 2017-05-25
- 0.2.5 — 2017-05-11
- 0.2.4 — 2017-05-05
- 0.2.3 — 2017-05-02
- 0.2.2 — 2017-05-01
- 0.2.1 — 2017-05-01
- 0.2.0 — 2017-05-01
- 0.1.1 — 2017-02-03
- 0.1.0 — 2016-12-18
- 0.0.9 — 2016-09-10
- 0.0.8 — 2016-05-13
- … 8 more at https://npm.io/package/@datagica/treegram/versions

## README

# Treegram

*Treegram is a hierarchical n-gram extraction library*

## Overview

Treegram is a hierarchical n-gram extraction library designed for named entities
identification and substitution.

It was created for use in the Datanote project (which is still in development)
so code should be considered experimental and subject to change.

Yes it is in Javascript, it is not the fastest but that's not really the point:
treegram is more like a prototype. If the project gets real time, HR and money
treegram will eventually be implemented as a native C++ Node module.

Current JS code is not particularly optimized anyway, so there is room for
improvement. Even without optimization, it can scale in parallel: for instance
the intra-sentence algorithm could be a map-reduce operation.

One more thing: it is important to keep in mind that using async functions to
compute chunks in a non-blocking way is good for a multi-user server, but in
term of raw performance and compute time it is slower than an old fashioned
for-loop. It all comes done to compromises and use cases.

In Datanote, processing in done in sub-process so blocking the thread is not
(too much) of a problem.

### Example

```javascript
import Treegram from "@datagica/treegram";

// an array
const database = [{
  label: 'vegetable',
  description: 'edible thing'
}, {
  label: 'vegeta',
  description: 'character'
}];

// these options are here to help you
const options = {
  debug: false,
  fields: 'name',
  spellings: (map, ngram) => {

    // plural to singular
    map.set(ngram.replace(/(es )/gi, 'e '), 0.80)
  }
};

// now you can construct the treegram index
const treegram = new Treegram(database, options);

// get the treegrams
treegram
  .find('Vegeta, eat your vegetables!')
  .then(results => { console.log(results) })

// will print:
[
  {
    "ngram": "Vegeta",
    "value": {
      "label": "vegeta",
      "description": "character"
    },
    "score": 1,
    "position": {
      "sentence": 0, // absolute sentence index (ie. how many sentences before)
      "word": 0,     // absolute word index (ie. how many words before)
      "begin": 0,    // absolute position of the start of the sequence
      "end": 6       // absolute position of the end of the sequence
    },
  },
  {
    "ngram": "vegetables",
    "value": {
      "label": "vegetable",
      "description": "edible thing"
    },
    "score": 0.8,
    "position": {
      "sentence": 0,
      "word": 3,
      "begin": 17,
      "end": 27
    }
  }
]
```


The interesting thing is that you can also use Treegram as a template engine
using the `replaceEntities` function.

I mean this is really a cool feature, look at this example:

```javascript
treegram.replace(
  "Vegeta, eat your vegetables!",
  function (ngram) { return `<a
    href="/resource/${ngram.value.label}"
    class="${ (ngram.score > 0.80) ? 'good' : 'normal' }">${ ngram.ngram }</a>
    <i>(${ ngram.value.description })</i>`
  }
}).then(html => {
  console.log(html);
})

```

the output will be:

```html
<a
  href="/resource/vegeta"
  class="good">Vegeta</a>
  <i>(character)</i>, eat your <a
  href="/resource/vegetable"
  class="normal">vegetables</a>
  <i>(edible thing)</i>!
```

---
_Source: https://npm.io/package/@datagica/treegram · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
