cliffordvickrey/malarkey
Composer 安装命令:
composer require cliffordvickrey/malarkey
包简介
Generates nonsensical but realistic-sounding text using a Markov chain algorithm
README 文档
README
See this library in action at https://www.cliffordvickrey.com/malarkey/
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
- PHP 7.1 or higher
Installation
Run the following to install this library:
$ composer require cliffordvickrey/malarkey
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
@generateChain
Generates a Markov chain from source text.
Returns: ChainInterface
Arguments:
text(string): The source textlookBehind(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
Returns the paragraph count of the text previously passed to generateChain, or NULL if no chain has been generated.
@getLastGeneratedWord
Returns the word count of the text previously passed to generateChain, or NULL if no chain has been generated.
TextGenerator
@generateText
Visits a Markov chain and returns randomly generated text.
Returns: string
Arguments:
chain(ChainInterface): The object representation of a Markov Chainoptions(mixed): Either an instance ofTextGeneratorOptionsInterface, 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 newlinesmaxChunks(int): Maximum number of chunks (paragraphs separated by line breaks) to generate. If nomaxChunks,maxSentences, ormaxWordsprovided, the generator will emit one chunkmaxSentences(int): Maximum number of sentences to generate. Defaults to NULLmaxWords(int): Maximum number of words to generator. Defaults to NULLwordSeparator(string): "Glue" to concatenate words emitted by the text generator. Defaults to " "
cliffordvickrey/malarkey 适用场景与选型建议
cliffordvickrey/malarkey 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 36 次下载、GitHub Stars 达 0, 最近一次更新时间为 2020 年 02 月 23 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「markov chain」 「text generator」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 cliffordvickrey/malarkey 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 cliffordvickrey/malarkey 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 cliffordvickrey/malarkey 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Texy converts plain text in easy to read Texy syntax into structurally valid (X)HTML. It supports adding of images, links, nested lists, tables and has full support for CSS. Texy supports hyphenation of long words (which reflects language rules), clickable emails and URL (emails are obfuscated again
Chain Adapter for Secretary
This package provides a generic processor chain to process a given object with processors/task (kind of a pipline, command chain pattern).
A simple markov chain experiment
Register a chain of calls that will be applied on an instance later
Common libraries used by Zimbra Api
统计信息
- 总下载量: 36
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 1
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-02-23