babble

package module
v0.0.0-...-5792785 Latest Latest
Warning

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

Go to latest
Published: Jul 13, 2022 License: MIT Imports: 7 Imported by: 0

README

Babble 2

package main

import (
	"github.com/anhthong381996/babble"
	"unicode"
)

func main() {
	RunWithMinMax()
	RunWithCustomSeparator()
	RunWithCustomConfig()
	RunWithMinMax()
	RunWithUpcaseConfig()
	RunWithDowncaseConfig()
	Transform()
	CustomWordList()
}

func RunWithMinMax() {
	babbler2 := babble.NewBabbler2(2, 5)

	printBabble2(babbler2)
}

func RunWithCustomSeparator() {
	babbler2 := babble.NewBabbler2WithSeparator(2, 5, "-")

	printBabble2(babbler2)
}

func RunWithCustomConfig() {
	babbler2 := babble.NewBabbler2WithConfig(2, 5,
		babble.DictionaryConfig{
			MinLength: 3,
			MaxLength: 5})

	printBabble2(babbler2)
}

func RunWithUpcaseConfig() {
	babbler2 := babble.NewBabbler2WithConfig(2, 5,
		babble.DictionaryConfig{MinLength: 3, MaxLength: 5,
			Upcase: true})

	printBabble2(babbler2)
}

func RunWithDowncaseConfig() {
	babbler2 := babble.NewBabbler2WithConfig(2, 5,
		babble.DictionaryConfig{MinLength: 3, MaxLength: 5,
			Downcase: true})

	printBabble2(babbler2)
}

func Transform() {
	var alternateCase = func(s string) string {
		rs, upper := []rune(s), false
		for i, r := range rs {
			if unicode.IsLetter(r) {
				if upper = !upper; upper {
					rs[i] = unicode.ToUpper(r)
				}
			}
		}
		return string(rs)
	}

	babbler2 := babble.NewBabbler2WithConfig(2, 5,
		babble.DictionaryConfig{MinLength: 3, MaxLength: 5,
			TransformWord: alternateCase})

	printBabble2(babbler2)
}

func CustomWordList() {
	customList := []string{"luke", "leia", "han", "darth", "r2d2"}

	babbler2 := babble.NewBabbler2WithConfig(2, 5,
		babble.DictionaryConfig{
			MinLength:      3,
			MaxLength:      5,
			CustomWordList: &customList})

	printBabble2(babbler2)
}

func printBabble2(babbler2 babble.Babbler2) {
	println(babbler2.Babble2())
}

/*
Sample output:

sacramenter supracostal cub
cascalote-presume-cetaceous
resaw loxia mop mono lesiy
oarweed beswelter
BOBBY NICE AZOCH FRANK PRIME
noxal agony ped hoju biham
WhInG QuIfF
r2d2 darth han darth luke
*/

Babble

Babble is a small utility that generates random words for you. I found this useful because occasionally you need a random word for testing.

tower of babel

Usage

package your_app

import (
  "github.com/tjarratt/babble"
)

func main() {
  babbler := babble.NewBabbler()
  println(babbler.Babble()) // excessive-yak-shaving (or some other phrase)

  // optionally set your own separator
  babbler.Separator = " "
  println(babbler.Babble()) // "hello from nowhere" (or some other phrase)

  // optionally set the number of words you want
  babbler.Count = 1
  println(babbler.Babble()) // antibiomicrobrial (or some other word)

  return
})

Custom Usage

package your_app

import (
  "github.com/tjarratt/babble"
)

func main() {
    c := babble.DictionaryConfig {
				MinLength: 3,
				MaxLength: 5,
			}

  babbler := babble.NewBabblerWithConfig(c)
  babbler.Separator = "-"
  println(babbler.Babble()) // excessive-yak-shaving (or some other phrase)

  // optionally set your own separator
  babbler.Separator = " "
  println(babbler.Babble()) // "hello from nowhere" (or some other phrase)

  // optionally set the number of words you want
  babbler.Count = 1
  println(babbler.Babble()) // antibiomicrobrial (or some other word)

Upcase

package your_app

import (
"github.com/tjarratt/babble"
)

func main() {
   c := babble.DictionaryConfig {
				MinLength: 3,
				MaxLength: 5,
				Upcase: true,
			}

  babbler := babble.NewBabblerWithConfig(c)
  babbler.Separator = "-"
  println(babbler.Babble()) // EXCESSIVE-YAK-SHAVING (or some other phrase)

Downcase

  c = babble.DictionaryConfig {
				MinLength: 3,
				MaxLength: 5,
				Downcase: true,
			}

  babbler = babble.NewBabblerWithConfig(c)
  babbler.Separator = "-"
  println(babbler.Babble()) // excessive-yak-shaving (or some other phrase)

Transform

  var alternateCase = func (s string)string {
                    rs, upper := []rune(s), false
                for i, r := range rs {
                    if unicode.IsLetter(r) {
                        if upper = !upper; upper {
                            rs[i] = unicode.ToUpper(r)
                        }
                    }
                }
                return string(rs)
  }  
  c = babble.DictionaryConfig {
				MinLength: 3,
				MaxLength: 5,
				TransformWord: alternateCase,
			}

  babbler = babble.NewBabblerWithConfig(c)
  babbler.Separator = "-"
  println(babbler.Babble()) // ExCeSsIvE-YaK-SlUrPiNg the transform is applied to each word individually during load

Custom Word List

customList = []string{ "luke", "leia", "han", "darth", "r2d2" } // C-3PO has been expressly excluded 
 c = babble.DictionaryConfig {
				MinLength: 3,
				MaxLength: 5,
				CustomWordList: customList,
			}

  babbler = babble.NewBabblerWithConfig(c)
  babbler.Separator = "-"
  println(babbler.Babble()) // luke-darth

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultDictionaryConfig = DictionaryConfig{
	MinLength: 2,
	MaxLength: 30,
}

Functions

func GenerateEligibleWordList

func GenerateEligibleWordList(a *[]string, min int, max int) []string

Types

type Babbler

type Babbler struct {
	Count      int
	Separator  string
	Words      []string
	Dictionary Dictionary
}

func NewBabbler

func NewBabbler() (b Babbler)

func NewBabblerWithConfig

func NewBabblerWithConfig(config DictionaryConfig) (b Babbler)

func (Babbler) Babble

func (b Babbler) Babble() string

type Babbler2

type Babbler2 struct {
	Min        int
	Max        int
	Separator  string
	Words      []string
	Dictionary Dictionary
}

func NewBabbler2

func NewBabbler2(min, max int) (b Babbler2)

func NewBabbler2WithConfig

func NewBabbler2WithConfig(min, max int, config DictionaryConfig) (b Babbler2)

func NewBabbler2WithSeparator

func NewBabbler2WithSeparator(min, max int, separator string) (b Babbler2)

func (Babbler2) Babble2

func (b Babbler2) Babble2() string

type Dictionary

type Dictionary interface {
	GetRandomWord() string
	GetListLength() int
	GetWordList() []string
}

func NewDictionaryWithConfig

func NewDictionaryWithConfig(c DictionaryConfig) Dictionary

NewDictionaryWithConfig creates a new dictionary with the supplied configuration struct.

   c = babble.DictionaryConfig {
				MinLength: 3,
				MaxLength: 5,
				Downcase: true,

       }
   d = NewDictionaryWithConfig(c)

type DictionaryConfig

type DictionaryConfig struct {
	// Limits the lower bound to the length of words in the list
	MinLength int
	// Limits the upper bound to the length of words in the list
	MaxLength int
	// If this pointer is non-nil, this list is used instead of
	// the default OS dictionary.
	CustomWordList *[]string
	// Changes all letters in each word to lower case
	Downcase bool
	// Changes all letters in each word to upper case
	Upcase bool
	// If present, this function is emitted on all words in the dictionary,
	// if the provided function returns true, the word is not included in the
	// word list.
	ExcludeWord ExcludeWord
	// Emittted on all words in the dictionary
	TransformWord TransformWord
}

type ExcludeWord

type ExcludeWord func(s string) bool

type TransformWord

type TransformWord func(s string) string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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