# node-easynmt

> multi-language translate using UKPLab/EasyNMT

Latest version **1.2.0** (published 2024-06-03) · BSD-2-Clause license · 0 weekly downloads

## Install

```sh
npm install node-easynmt
pnpm add node-easynmt
yarn add node-easynmt
bun add node-easynmt
```

Provides the command `easynmt-server`.

## Health

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

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.2.0 |
| Published | 2024-06-03 |
| First published | 2024-04-09 |
| Weekly downloads | 0 |
| License | BSD-2-Clause |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 77.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Beeno Tung |
| Maintainers | beenotung |
| Keywords | translation, neural machine translation, EasyNMT, Opus-MT, mBART50, M2M_100, docker, node, browser, isomorphic, typescript, wrapper, client, server, api, http |

## Links

- npm: https://www.npmjs.com/package/node-easynmt
- Repository: https://github.com/beenotung/node-EasyNMT
- Homepage: https://github.com/beenotung/node-EasyNMT#readme
- Issues: https://github.com/beenotung/node-EasyNMT/issues
- npm.io page: https://npm.io/package/node-easynmt

## Dependencies (2)

- [debug](https://npm.io/package/debug.md) ^4.3.4
- [cast.ts](https://npm.io/package/cast.ts.md) ^1.12.2

## Alternatives

- [launchdarkly-js-client-sdk](https://npm.io/package/launchdarkly-js-client-sdk.md) — 2.5M weekly downloads
- [@elastic/elasticsearch](https://npm.io/package/@elastic/elasticsearch.md) — 2.1M weekly downloads
- [@c8y/client](https://npm.io/package/@c8y/client.md) — 15.3K weekly downloads
- [@signaldb/maverickjs](https://npm.io/package/@signaldb/maverickjs.md) — 1.7K weekly downloads
- [@bbc/http-transport-cache](https://npm.io/package/@bbc/http-transport-cache.md) — 1.2K weekly downloads

## Recent versions

- 1.2.0 (latest) — 2024-06-03
- 1.1.1 — 2024-04-23
- 1.1.0 — 2024-04-23
- 1.0.2 — 2024-04-23
- 1.0.1 — 2024-04-21
- 1.0.0 — 2024-04-09
- 0.0.0 — 2024-04-09

## README

# node-EasyNMT

multi-language translate using [UKPLab/EasyNMT](https://github.com/UKPLab/EasyNMT)

[![npm Package Version](https://img.shields.io/npm/v/node-easynmt)](https://www.npmjs.com/package/node-easynmt)

Remark: For the server side, you need to have docker installed and available in the `PATH`. Also, your current user should have the privilege to run docker cli.

## Usage (browser)

```html
<script src="https://cdn.jsdelivr.net/npm/node-easynmt@1/bundle.js"></script>
<script>
  console.log(easyNMT)
  /*
  {
    translate,
    patchedTranslate,
    clearCache,
    preloadModel,
  }
  */
</script>
```

## Usage (nodejs)

### Installation

```bash
npm i node-easynmt
```

run server:

```typescript
import { autoStartServer } from 'node-easynmt/server'

// start a docker container if not already running
autoStartServer({ port: 24080 })
  .then(() => console.log('ready.'))
  .catch(err => console.error(err))
```

call from client:

```typescript
import { translate } from 'node-easynmt/client'

let zh = '世界你好'
let en = await translate({
  text: zh,
  source_lang: 'zh',
  target_lang: 'en',
})
console.log(`sample: ${zh} -> ${en}`)
```

### Typescript Signature

Core Function: `translate()`

```typescript
/**
 * @description set HTTP request to the translate service running in the docker container
 * */
export function translate(options: TranslateOptions): Promise<string>

export type TranslateOptions = {
  /** @default 'localhost' */
  host?: string
  /** @default 24080 */
  port?: number
  /** @example 'Hello World!' */
  text: string
  /** @example 'zh' */
  target_lang: string
  /** @description auto detect if not specified */
  source_lang?: string
  /** @default false */
  debug?: boolean
}
```

Patched core function: `patchedTranslate()`

```typescript
/**
 * @description apply combination of fixes to workaround common errors
 */
export async function patchedTranslate(
  options: PatchedTranslateOptions,
): Promise<string>

export type PatchedTranslateOptions = TranslateOptions & {
  /**
   * @description to avoid ajax timeout when doing lots on translate concurrently
   * @default true
   * */
  async_queue?: boolean
  /**
   * @description to keep in-memory cache
   * @default true
   * */
  cached?: boolean
  /**
   * @description to avoid repeating (wrong) result.
   * e.g. without wrapping: Transparent -> 透明透明
   * @default true
   */
  wrap_text?: boolean
  /**
   * @description trim the output if the input is already trimmed
   * @default true
   */
  smart_trim?: boolean
}
```

Helper Functions:

```typescript
/**
 * @description optionally step to preload the translate model before the actual usage.
 */
export async function preloadModel(options: {
  target_lang: string
  source_lang: string
  /**
   * @description to log in console or not
   * @default false
   *
   */
  debug?: boolean
}): Promise<void>

/**
 * @description release the memory used by patchedTranslate()
 */
export function clearCache(): void
```

## Usage (cli)

run server:

```bash
docker run -p 24080:80 easynmt/api:2.0-cpu
```

send request:

```bash
curl "http://localhost:24080/translate?target_lang=en&text=Hallo%20Welt"
```

json response:

```json
{
  "target_lang": "en",
  "source_lang": null,
  "detected_langs": ["de"],
  "translated": ["Hello world"],
  "translation_time": 77.64211463928223
}
```

## License

This project is licensed with [BSD-2-Clause](./LICENSE)

This is free, libre, and open-source software. It comes down to four essential freedoms [[ref]](https://seirdy.one/2021/01/27/whatsapp-and-the-domestication-of-users.html#fnref:2):

- The freedom to run the program as you wish, for any purpose
- The freedom to study how the program works, and change it so it does your computing as you wish
- The freedom to redistribute copies so you can help others
- The freedom to distribute copies of your modified versions to others

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