Article spinning and spintax/spinning syntax engine written in Go, useful for A/B, testing pieces of text/articles and creating more natural conversations

GoSpin

GoDoc Build Status Go Report Card Release Coverage Status

Article spinning and spintax/spinning syntax engine written in Go, useful for A/B, testing pieces of text/articles and creating more natural conversations. Use as a library or as a CLI.

Installation

Use go get to get the latest version

go get github.com/m1/gospin

Then import it into your projects using the following:

import (
	"github.com/m1/gospin"
)

What is spintax?

Take this example:

{hello|hey} world

When spinning an article (the above sentence) each of the words/phrases contained within the curly brackets are randomly picked and substituted in the sentence. So for example, the above article could be spun to be: hey world and hello world

You can also have nested spintax, take this example:

{hello|hey} world, {hope {you're|you are} {okay|good}|have a nice day}

A few examples of what the above could output:

  • hey world, hope you're okay
  • hello world, hope you are good
  • hello world, have a nice day
  • etc...

You can also have optional phrases, just don't specify a word after or before a pipe to make it optional:

{hello|hey}{ world|}, how are you today?

A few examples of what the above could output:

  • hey, how are you today?
  • hello world, how are you today?
  • etc...

Why use spintax?

Spintax can be used for several things. It used to be used a lot for spinning articles for SEO but is less useful for that these days. It's more used for A/B testing, testing pieces of text for efficiency/click through rate. Also it is used spinning content for users to keep things fresh, i.e home page text or ai/chat bots.

Usage

To use as a library is pretty simple:

spinner := gospin.New(nil)
simple := "The {slow|quick} {fox|deer} {gracefully |}jumps over the {sleeping|lazy} dog"

spin := spinner.Spin(simple) // The slow fox jumps over the sleeping dog
spins := spinner.SpinN(simple, 10)
// spins = [
// "The slow fox gracefully jumps over the lazy dog"
// "The slow deer jumps over the sleeping dog"
// "The quick fox jumps over the lazy dog"
// ...
// ]

You can also configure it to take custom syntax (the package uses Jet format as the default), e.g. if you wanted it to set the start and end characters (default are curly brackets) to square brackets:

spinner := gospin.New(&gospin.Config{
        StartChar:     "[",
        EndChar:       "]",
        DelimiterChar: ";",
})
simple := "The [slow;quick] [fox;deer] [gracefully ;]jumps over the [sleeping;lazy] dog"
spin := spinner.Spin(simple) // The slow fox jumps over the sleeping dog

Escaping

To escape, the default character to use is \\, e.g:

The \{slow|quick\} {fox|deer} {gracefully |}jumps over the {sleeping|lazy} dog

Would output something like:

The {slow|quick} fox jumps over the sleeping dog

You can customize the escape char in the config:

spinner := gospin.New(&gospin.Config{
        EscapeChar:    "@",
})
simple := "The @{slow|quick@} {fox|deer} {gracefully |}jumps over the {sleeping|lazy} dog"
spin := spinner.Spin(simple) // The @{slow|quick@} fox jumps over the sleeping dog

Random seeds

The spin by default generates a random seed each spin, to stop this and use your own global rand seed you can use the UseGlobalRand toggle in the config. This is useful for testing:

spinner := gospin.New(&gospin.Config{
        UseGlobalRand: false,
})

CLI usage

GoSpin can also be used on the cli, just install using: go get github.com/m1/gospin/cmd/gospin

To use:

➜  ~ gospin --help                    
GoSpin is a fast and configurable article spinning and spintax engine written in Go.

Usage:
  gospin [text] [flags]

Flags:
      --delimiter string   Delimiter char (default "|")
      --end string         End char for the spinning engine (default "}")
      --escape string      Escape char (default "\\")
  -h, --help               help for gospin
      --start string       Start char for the spinning engine (default "{")
      --times int          How many articles to generate (default 1)

For example:

➜  ~ gospin "{hello|hey} friend"                     
hey friend

To spin multiple, use the times flag, this is outputted as json for easier parsing:

➜  ~ gospin "The {slow|quick} {fox|deer} {gracefully |}jumps over the {sleeping|lazy} dog" --times=5 | jq
[
  "The slow fox gracefully jumps over the sleeping dog",
  "The slow deer jumps over the lazy dog",
  "The quick deer jumps over the sleeping dog",
  "The slow fox gracefully jumps over the sleeping dog",
  "The quick fox jumps over the lazy dog"
]
Owner
Miles Croxford
Senior Software Engineer
Miles Croxford
Similar Resources

Simple and fast template engine for Go

fasttemplate Simple and fast template engine for Go. Fasttemplate performs only a single task - it substitutes template placeholders with user-defined

Dec 30, 2022

A handy, fast and powerful go template engine.

A handy, fast and powerful go template engine.

Hero Hero is a handy, fast and powerful go template engine, which pre-compiles the html templates to go code. It has been used in production environme

Dec 27, 2022

The world’s most powerful template engine and Go embeddable interpreter.

The world’s most powerful template engine and Go embeddable interpreter.

The world’s most powerful template engine and Go embeddable interpreter

Dec 23, 2022

HTML template engine for Go

Ace - HTML template engine for Go Overview Ace is an HTML template engine for Go. This is inspired by Slim and Jade. This is a refinement of Gold. Exa

Jan 4, 2023

Jet template engine

Jet Template Engine for Go Jet is a template engine developed to be easy to use, powerful, dynamic, yet secure and very fast. simple and familiar synt

Jan 4, 2023

A complete Liquid template engine in Go

A complete Liquid template engine in Go

Liquid Template Parser liquid is a pure Go implementation of Shopify Liquid templates. It was developed for use in the Gojekyll port of the Jekyll sta

Dec 15, 2022

Fast, powerful, yet easy to use template engine for Go. Optimized for speed, zero memory allocations in hot paths. Up to 20x faster than html/template

quicktemplate A fast, powerful, yet easy to use template engine for Go. Inspired by the Mako templates philosophy. Features Extremely fast. Templates

Dec 26, 2022

Razor view engine for go

gorazor gorazor is the Go port of the razor view engine originated from asp.net in 2011. In summary, gorazor is: Extremely Fast. Templates are convert

Dec 25, 2022

gtpl is a template engine for glang

gtpl 使用必读 gtpl is a HTML template engine for golang gtpl 是一个 go 语言模板引擎,它能以极快的速度进行模板语法分析。相比 go 语言官方库 html/template,gtpl 的语法有着简练、灵活、易用的特点。

Nov 28, 2022
Comments
  • Error in spinner.Spin

    Error in spinner.Spin

    When i'm using spinner.Spin("{Apply|Use} this {special |||}{spintax|spin}") sometimes the output is "Use thisspintax" which means the white space after "this" is removed.

  • Invalid result when the same block (e.g. {hello|hi}) is used in level 1 and then in higher levels.

    Invalid result when the same block (e.g. {hello|hi}) is used in level 1 and then in higher levels.

    Hi, i came across the bug that this package spins text like this with invalid result:

    original string:
    The {slow|quick} {brown|blue and {red|yellow}} {fox|deer} {gracefully |}jumps over the {{slow|quick} {fox|deer}}
    
    result:
    The slow blue and yellow fox jumps over the deer}
    

    The problem is that on level!=1 you used block replacement in the original string. So when we are in nested block "{{slow|quick} {fox|deer}}" this replacement is applied to the first occurrence of "{slow|quick}" in the original string.

    I've made a fix and will provide pull request soon.

  • Slow for large templates and number of articles

    Slow for large templates and number of articles

    Hi, this app works quite slow for bigger templates and larger number of articles, I just run 10000 articles from template, it works for 20 mins and doesn't look like going to finish anytime soon.

Useful template functions for Go templates.

Sprig: Template functions for Go templates The Go language comes with a built-in template language, but not very many template functions. Sprig is a l

Jan 4, 2023
A demonstrative template for creating reliable Terraform modules
A demonstrative template for creating reliable Terraform modules

This repository provides a template for creating new Terraform modules. It's intended to demonstrate how one might go about standardizing their modules and subjecting them to integration tests in CI.

Oct 31, 2022
Tool for creating advent of code template

AOC Template Generator ?? I'm tired of creating a folder for each day with the same file structure so why not having a tool to automate that proccess

Dec 9, 2021
A PDF document generator with high level support for text, drawing and images
A PDF document generator with high level support for text, drawing and images

GoFPDF document generator Package gofpdf implements a PDF document generator with high level support for text, drawing and images. Features UTF-8 supp

Dec 28, 2022
Templating system for HTML and other text documents - go implementation

FAQ What is Kasia.go? Kasia.go is a Go implementation of the Kasia templating system. Kasia is primarily designed for HTML, but you can use it for any

Mar 15, 2022
Simple system for writing HTML/XML as Go code. Better-performing replacement for html/template and text/template

Simple system for writing HTML as Go code. Use normal Go conditionals, loops and functions. Benefit from typing and code analysis. Better performance than templating. Tiny and dependency-free.

Dec 5, 2022
"to be defined" - a really simple way to create text templates with placeholders

tbd "to be defined" A really simple way to create text templates with placeholders. This tool is deliberately simple and trivial, no advanced features

Sep 27, 2022
Fusozay Var Var: A CLI tool for quick text template rendering

fvv - Fusozay Var Var A CLI tool for quick text template rendering Fusozay Var Var means "have fun" It is a reference to something I see a lot Fusozay

Dec 11, 2021
mold your templated to HTML/ TEXT/ PDF easily.
mold your templated to HTML/ TEXT/ PDF easily.

mold mold your templated to HTML/ TEXT/ PDF easily. install go get github.com/mayur-tolexo/mold Example 1 //Todo model type Todo struct { Title stri

Jun 7, 2019
Amber is an elegant templating engine for Go Programming Language, inspired from HAML and Jade

amber Notice While Amber is perfectly fine and stable to use, I've been working on a direct Pug.js port for Go. It is somewhat hacky at the moment but

Jan 2, 2023