dictionary

package
v0.0.0-...-6484783 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 19, 2017 License: MIT, MIT Imports: 4 Imported by: 0

README ΒΆ

Dictionary emoji encoding GoDoc

Package dictionary is a small package that allows encoding (or compression) of strings by replacing each unique word with an emoji.

Each compress generates a new dictionary and an encoded version of the text (archive), based on the words found in the text. If the original text doesn't have many repeating words, the "archive" will be longer than the original string.

The dictionary should be sent to the user/client so he can decode the string.

Demo

We have built a full working demo at emoji-compress.com β€Ό

Limitations:
  • you cannot have emojis in the original text
  • only works with a max of 1000 unique words
  • (for now) compress generates a new dictionary for each text
  • you have to use the same dictionary resulted from the Compress into the Decompress
TODO:
  • the ability to use a custom dictionary when Compressing
How

The algorithm is very simple: tries to extract each word from the original text and replace it with an emoji. A dictionary/map is generated along with the "Archive", to remember which word was replaced with each emoji.

The decompress process requires the "Archived" (encoded) version of the text, and the dictionary, in order to reverse the process.

Example

	//snippet of Sonnet 40 Take all my loves, my love, yea, take them all BY WILLIAM SHAKESPEARE
	sonnet := "Take all my loves, my love, yea, take them all:" +
		"\nWhat hast thou then more than thou hadst before?" +
		"\nNo love, my love, that thou mayst true love callβ€”" +
		"\nAll mine was thine before thou hadst this more."

	result, err := CompressString(sonnet)
	if err != nil {
		log.Panic(err)
	}

	fmt.Printf("Archive: %s", result.Archive)
	j, err := json.Marshal(result.Words)
	fmt.Printf("\nDictionary: %s", j)

	// Output: Archive: πŸ˜€ 😬 my 😁, my πŸ˜‚, 🀣, πŸ˜ƒ πŸ˜„ 😬:
	// πŸ˜… πŸ˜† πŸ˜‡ πŸ˜‰ 😊 πŸ™‚ πŸ˜‡ πŸ™ƒ πŸ˜‹?
	// No πŸ˜‚, my πŸ˜‚, 😌 πŸ˜‡ 😍 😘 πŸ˜‚ πŸ˜—β€”
	// πŸ˜™ 😚 😜 😝 πŸ˜‹ πŸ˜‡ πŸ™ƒ πŸ˜› 😊.
	// Dictionary: {"All":"πŸ˜™","Take":"πŸ˜€","What":"πŸ˜…","all":"😬","before":"πŸ˜‹","call":"πŸ˜—","hadst":"πŸ™ƒ","hast":"πŸ˜†","love":"πŸ˜‚","loves":"😁","mayst":"😍","mine":"😚","more":"😊","take":"πŸ˜ƒ","than":"πŸ™‚","that":"😌","them":"πŸ˜„","then":"πŸ˜‰","thine":"😝","this":"πŸ˜›","thou":"πŸ˜‡","true":"😘","was":"😜","yea":"🀣"}

This package has unit tests, GoDoc and Examples.

About

This package is part of a group of emoji-related encoding and compression algorithms built for fun and academic purposes in Go.

Copyright (c) 2017 B.G.Adrian & @Davidescus

Documentation ΒΆ

Overview ΒΆ

Package dictionary is a small package that allows encoding (or compression) of strings by replacing each unique word with an emoji.

Each compress generates a new dictionary and an encoded version of the text (archive), based on the words found in the text. If the original text doesn't have many repeating words, the "archive" will be longer than the original string.

The dictionary should be sent to the user/client so he can decode the string.

Limitations: * you cannot have emojis in the original text * only works with a max of 1000 unique words * (for now) compress generates a new dictionary for each text * you have to use the same dictionary resulted from the Compress into the Decompress

The algorithm is very simple: tries to extract each word from the original text and replace it with an emoji. A dictionary/map is generated along with the "Archive", to remember which word was replaced with each emoji.

The decompress process requires the "Archived" (encoded) version of the text, and the dictionary, in order to reverse the process.

TODO: * the ability to use a custom dictionary when Compressing

Example ΒΆ
//snippet of Sonnet 40 Take all my loves, my love, yea, take them all BY WILLIAM SHAKESPEARE
sonnet := "Take all my loves, my love, yea, take them all:" +
	"\nWhat hast thou then more than thou hadst before?" +
	"\nNo love, my love, that thou mayst true love callβ€”" +
	"\nAll mine was thine before thou hadst this more."

result, err := CompressString(sonnet)
if err != nil {
	log.Panic(err)
}

fmt.Printf("Archive: %s", result.Archive)
j, err := json.Marshal(result.Words)
fmt.Printf("\nDictionary: %s", j)
Output:

Archive: πŸ˜€ 😬 my 😁, my πŸ˜‚, 🀣, πŸ˜ƒ πŸ˜„ 😬:
πŸ˜… πŸ˜† πŸ˜‡ πŸ˜‰ 😊 πŸ™‚ πŸ˜‡ πŸ™ƒ πŸ˜‹?
No πŸ˜‚, my πŸ˜‚, 😌 πŸ˜‡ 😍 😘 πŸ˜‚ πŸ˜—β€”
πŸ˜™ 😚 😜 😝 πŸ˜‹ πŸ˜‡ πŸ™ƒ πŸ˜› 😊.
Dictionary: {"All":"πŸ˜™","Take":"πŸ˜€","What":"πŸ˜…","all":"😬","before":"πŸ˜‹","call":"πŸ˜—","hadst":"πŸ™ƒ","hast":"πŸ˜†","love":"πŸ˜‚","loves":"😁","mayst":"😍","mine":"😚","more":"😊","take":"πŸ˜ƒ","than":"πŸ™‚","that":"😌","them":"πŸ˜„","then":"πŸ˜‰","thine":"😝","this":"πŸ˜›","thou":"πŸ˜‡","true":"😘","was":"😜","yea":"🀣"}

Index ΒΆ

Examples ΒΆ

Constants ΒΆ

This section is empty.

Variables ΒΆ

This section is empty.

Functions ΒΆ

func Decompress ΒΆ

func Decompress(r *Result) (string, error)

Decompress reverse an encoded/compressed emoji text to the original form, using the provided dictionary (resulted by the Compress function).

func DecompressString ΒΆ

func DecompressString(dict map[string]string, archive string) (string, error)

DecompressString is an overload for Decompress.

Example ΒΆ
archive := "πŸ˜€ 😬 my 😁, my πŸ˜‚, 🀣, πŸ˜ƒ πŸ˜„ 😬:" +
	"\nπŸ˜… πŸ˜† πŸ˜‡ πŸ˜‰ 😊 πŸ™‚ πŸ˜‡ πŸ™ƒ πŸ˜‹?" +
	"\nNo πŸ˜‚, my πŸ˜‚, 😌 πŸ˜‡ 😍 😘 πŸ˜‚ πŸ˜—β€”" +
	"\nπŸ˜™ 😚 😜 😝 πŸ˜‹ πŸ˜‡ πŸ™ƒ πŸ˜› 😊."

dict := map[string]string{
	"All": "πŸ˜™", "Take": "πŸ˜€", "What": "πŸ˜…", "all": "😬",
	"before": "πŸ˜‹", "call": "πŸ˜—", "hadst": "πŸ™ƒ", "hast": "πŸ˜†",
	"love": "πŸ˜‚", "loves": "😁", "mayst": "😍", "mine": "😚",
	"more": "😊", "take": "πŸ˜ƒ", "than": "πŸ™‚", "that": "😌",
	"them": "πŸ˜„", "then": "πŸ˜‰", "thine": "😝", "this": "πŸ˜›",
	"thou": "πŸ˜‡", "true": "😘", "was": "😜", "yea": "🀣",
}

original, err := DecompressString(dict, archive)

if err != nil {
	log.Panic(err)
}

fmt.Printf("Poetry: %s", original)
Output:

Poetry: Take all my loves, my love, yea, take them all:
What hast thou then more than thou hadst before?
No love, my love, that thou mayst true love callβ€”
All mine was thine before thou hadst this more.

Types ΒΆ

type Result ΒΆ

type Result struct {
	Words   map[string]string `json:"dict"`
	Source  string            `json:"source"`
	Archive string            `json:"archive"`
	Ratio   float32           `json:"ratio"`
}

Result contains the original phrase, dictionary and other data for Compress and Decompress

func Compress ΒΆ

func Compress(original []byte) (r *Result, err error)

Compress a string by replacing it's words with emojis. The result will contain a map of words/emojis that can be used to revert the process.

func CompressString ΒΆ

func CompressString(s string) (*Result, error)

CompressString is an overload for Compress.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL