PHPackages                             cliffordvickrey/malarkey - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. cliffordvickrey/malarkey

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

cliffordvickrey/malarkey
========================

Generates nonsensical but realistic-sounding text using a Markov chain algorithm

0.4.0(6y ago)036MITPHPPHP &gt;=7.1CI failing

Since Feb 28Pushed 6y ago1 watchersCompare

[ Source](https://github.com/cliffordvickrey/malarkey)[ Packagist](https://packagist.org/packages/cliffordvickrey/malarkey)[ Docs](https://www.cliffordvickrey.com/malarkey/)[ RSS](/packages/cliffordvickrey-malarkey/feed)WikiDiscussions master Synced 5d ago

READMEChangelog (5)Dependencies (2)Versions (6)Used By (0)

cliffordvickrey/malarkey
========================

[](#cliffordvickreymalarkey)

[![Build Status](https://camo.githubusercontent.com/05fec1ed24519439627f9feb034aa71cbbdeae53420fcdecbd0d6a912b58bd00/68747470733a2f2f7472617669732d63692e636f6d2f636c6966666f72647669636b7265792f6d616c61726b65792e7376673f6272616e63683d6d6173746572)](https://travis-ci.com/cliffordvickrey/malarkey/)[![Coverage Status](https://camo.githubusercontent.com/8228efcf28f7d2dff63756703cc48a2d60e6ae74e770ea5b3e91a35e003aafc3/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f636c6966666f72647669636b7265792f6d616c61726b65792f62616467652e737667)](https://coveralls.io/github/cliffordvickrey/malarkey)

**See this library in action at **

This package generates nonsensical but realistic-sounding text (malarkey!) using a simple Markov chain algorithm.

In a Markov chain system, all possible states are determined by previous states. In the context of text, it models the transition from one state ("hello") to a future state ("world!") using a set of fixed probabilities.

A Markov chain generator takes text and, for all sequences of words, models the likelihoods of the next word in the sequence. ("world!" might have a 75% chance of following "Hello," and "Nurse!" might have a 25% chance). It is straightforward to visit the chain and, following these probabilities, emit gibberish that mimics human writing.

For any given word, it is possible to "look behind" any number of words to determine how likely the word is to be the next in the sequence. The more words the text generator looks behind, the less random and more human-seeming will be the output.

Requirements
------------

[](#requirements)

- PHP 7.1 or higher

Installation
------------

[](#installation)

Run the following to install this library:

```
$ composer require cliffordvickrey/malarkey
```

Usage
-----

[](#usage)

```
$text = "I'd buy that for a dollar! But I'd buy this for two dollars!";

$chainGenerator = new CliffordVickrey\Malarkey\Generator\ChainGenerator();
$markovChain = $chainGenerator->generateChain($text, 2);

// the chain stores probability data for every possible state, like so
var_dump($markovChain->getFrequenciesBySequence("I'd", 'buy')); // ['that' => 1, 'this' => 1]

$textGenerator = new CliffordVickrey\Malarkey\Generator\TextGenerator();
$output = $textGenerator->generateText($markovChain, ['maxSentences' => 1]);

var_dump($output); // e.g. I'd buy that for two dollars!
```

Command line text generation utilities are also available. For help, run this in your project folder:

```
$ ./vendor/bin/malarkey
```

### ChainGenerator

[](#chaingenerator)

#### @generateChain

[](#generatechain)

Generates a Markov chain from source text.

Returns: `ChainInterface`

Arguments:

- `text` (`string`): The source text
- `lookBehind` (`int`): The number of words to look behind when determining the next state of the Markov chain. The higher the number, the more coherent will be the randomly-generated text. Defaults to 2

The returned chain object implements `Serializable` and `JsonSerializable` for persistence and portability purposes.

```
$text = "I'd buy that for a dollar! But I'd buy this for two dollars!";

$chainGenerator = new \CliffordVickrey\Malarkey\Generator\ChainGenerator();
/** @var CliffordVickrey\Malarkey\MarkovChain\Chain $markovChain */
$markovChain = $chainGenerator->generateChain($text);

$className = CliffordVickrey\Malarkey\MarkovChain\Chain::class;
$serialized = serialize($markovChain);
$unSerialized = unserialize($serialized, ['allowed_classes' => [$className]]);

var_dump(json_encode($markovChain) === json_encode($unSerialized)); // TRUE
```

#### @getLastGeneratedChunkCount

[](#getlastgeneratedchunkcount)

Returns the paragraph count of the text previously passed to `generateChain`, or NULL if no chain has been generated.

#### @getLastGeneratedWord

[](#getlastgeneratedword)

Returns the word count of the text previously passed to `generateChain`, or NULL if no chain has been generated.

### TextGenerator

[](#textgenerator)

#### @generateText

[](#generatetext)

Visits a Markov chain and returns randomly generated text.

Returns: `string`

Arguments:

- `chain` (`ChainInterface`): The object representation of a Markov Chain
- `options` (`mixed`): Either an instance of `TextGeneratorOptionsInterface`, an associative array of options, or NULL for defaults.

Valid options (none are required):

- `chunkSeparator` (`string`): "Glue" to concatenate chunks emitted by the text generator. Defaults to two newlines
- `maxChunks` (`int`): Maximum number of chunks (paragraphs separated by line breaks) to generate. If no `maxChunks`, `maxSentences`, or `maxWords` provided, the generator will emit one chunk
- `maxSentences` (`int`): Maximum number of sentences to generate. Defaults to NULL
- `maxWords` (`int`): Maximum number of words to generator. Defaults to NULL
- `wordSeparator` (`string`): "Glue" to concatenate words emitted by the text generator. Defaults to " "

###  Health Score

22

—

LowBetter than 22% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity7

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity45

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~2 days

Total

5

Last Release

2262d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/ce68a58565e1d22f89ffdd83db745c3b16e62b41a794af29048cc3cb5aa00e97?d=identicon)[cliffordvickrey](/maintainers/cliffordvickrey)

---

Top Contributors

[![cliffordvickrey](https://avatars.githubusercontent.com/u/25918827?v=4)](https://github.com/cliffordvickrey "cliffordvickrey (25 commits)")

---

Tags

text generatormarkov chain

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Type Coverage Yes

### Embed Badge

![Health badge](/badges/cliffordvickrey-malarkey/health.svg)

```
[![Health](https://phpackages.com/badges/cliffordvickrey-malarkey/health.svg)](https://phpackages.com/packages/cliffordvickrey-malarkey)
```

###  Alternatives

[p-chess/chess

A PHP chess library that is used for chess move generation/validation, piece placement/movement, and check/checkmate/stalemate detection

473.3k2](/packages/p-chess-chess)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
