text_generator

package module
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 14, 2021 License: MIT Imports: 5 Imported by: 0

README

text-generator

Fast text generator on a mask.

Go Report Card Go Reference Release

Written in Golang. I do not use regular expressions and the fastest. I covered tests and simple! Supporting recursive text generation rules.

Installation

go get github.com/liderman/text-generator

Usage

An example of a simple template text generation:

tg := text_generator.New()
template := "Good {morning|day}!"

fmt.Print(tg.Generate(template))
// Displays: Good morning!

fmt.Print(tg.Generate(template))
// Displays: Good day!

An example of a complex generation template text:

tg := text_generator.New()
template := "{Good {morning|evening|day}|Goodnight|Hello}, {friend|brother}! {How are you|What's new with you}?"

fmt.Print(tg.Generate(template))
// Displays: Good morning, friend! How are you?

fmt.Print(tg.Generate(template))
// Displays: Good day, brother! What's new with you?

fmt.Print(tg.Generate(template))
// Displays: Hello, friend! How are you?
...

Requirements

  • Need at least go1.14 or newer.

Features

  • Without external dependencies
  • Fast (does not use regular expressions)
  • Simple
  • Supports caching
  • Covered with tests

Documentation

You can read package documentation here.

Testing

Unit-tests:

go test -v -race ./...

Run linter:

docker run --rm -v $(pwd):/app -w /app golangci/golangci-lint:v1.40 golangci-lint run -v

Benchmarks

go test -test.bench . -benchmem -benchtime=10s

The test result on computer MacBook Pro 2020 (Intel Core i5):

goos: darwin
goarch: amd64
pkg: github.com/liderman/text-generator
BenchmarkGenerateEasyText-8             22446540               531 ns/op             200 B/op          8 allocs/op
BenchmarkGenerateComplexText-8           4721838              2552 ns/op            1351 B/op         24 allocs/op
PASS
ok      github.com/liderman/text-generator      27.227s

CONTRIBUTE

  • write code
  • run go fmt ./...
  • run all linters and tests (see above)
  • create a PR describing the changes

LICENSE

MIT

AUTHOR

Konstantin Osipov k.osipov.msk@gmail.com

Documentation

Overview

Package text_generator Fast text generator on a mask.

Package text_generator Fast text generator on a mask.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CachedTextGenerator added in v1.1.0

type CachedTextGenerator struct {
	// contains filtered or unexported fields
}

func (*CachedTextGenerator) Configure added in v1.1.0

func (t *CachedTextGenerator) Configure(startTag rune, endTag rune, separator rune) TextGeneratorInterface

func (*CachedTextGenerator) Generate added in v1.1.0

func (t *CachedTextGenerator) Generate(text string) string

Generate generates and returns a new text. Use the rules for generating a plurality of texts. Example mask: `Good {morning|day}!`

type TextGenerator

type TextGenerator struct {
	// contains filtered or unexported fields
}

func (*TextGenerator) Configure added in v1.1.0

func (t *TextGenerator) Configure(startTag rune, endTag rune, separator rune) TextGeneratorInterface

Configure method configures the parser

func (*TextGenerator) Generate

func (t *TextGenerator) Generate(text string) string

Generate generates and returns a new text. Use the rules for generating a plurality of texts. Example mask: `Good {morning|day}!`

type TextGeneratorInterface added in v1.1.0

type TextGeneratorInterface interface {
	Generate(text string) string
	Configure(startTag rune, endTag rune, separator rune) TextGeneratorInterface
}

func New

New returns a new instance a text generator.

func NewCached added in v1.1.0

func NewCached(generator TextGeneratorInterface, ttl int) TextGeneratorInterface

NewCached returns a new instance a cached text generator.

Jump to

Keyboard shortcuts

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