Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

Huffman encode/decode text

NotificationsYou must be signed in to change notification settings

kelreel/huffman-javascript

Repository files navigation


Huffman coding JS (TypeScript)

Huffman code is a particular type of optimal prefix code that is commonly used for lossless data compression. This is the implementation of the algorithm on TypeScript.

Installation

Clone this repository and install modules:

git clone https://github.com/kanitelk/huffman-javascript.gitcd huffman-javascriptnpm installnpm run dev(or build)

Usage

The algorithm implementation is in the file /src/index.ts

Let's encode and decode plain text!

import{getCodesFromText,encode,decode}from'./huffman';/** ENCODING */lettext:string='abracadabra';letencodedText:string='';letcodes:Map<string,string>=getCodesFromText(text);// Symbols codesletencodedArray:Array<any>=encode(text,codes);// Get array of encoded symbolsencodedText=encodedArray.join('');// Encoded array to string. Equals 0101100.../** DECODING */text=decode(encodedArray,codes);// Equals 'abracadabra'

APIs

Encode text

encode(text:string,codes:Map<string,string>):Array<string>

Decode text

decode(text:Array<string>,codes:Map<string,string>):string

Get symbols codes from text

getCodesFromText(text:string):Map<string,string>

Get symbols frequency

getFrequency(text:string):Array<any>

Get Huffman Tree from frequency array

getTree(arr:Array<any>)

Get relative frequency array

getRelativeFrequency(arr:Array<any>):Array<any>

Get text entropy

getEntropyOfText(text:string):number

[8]ページ先頭

©2009-2025 Movatter.jp