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 ΒΆ
Decompress reverse an encoded/compressed emoji text to the original form, using the provided dictionary (resulted by the Compress function).
func DecompressString ΒΆ
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 ΒΆ
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 ΒΆ
CompressString is an overload for Compress.