{"id":14966054,"url":"https://github.com/bbkr/text-markov","last_synced_at":"2025-10-25T13:31:11.768Z","repository":{"id":138947670,"uuid":"13227135","full_name":"bbkr/Text-Markov","owner":"bbkr","description":"Markov-chain based text generator for Raku language. Generate superficially real-looking text.","archived":false,"fork":false,"pushed_at":"2023-01-14T17:21:30.000Z","size":145,"stargazers_count":13,"open_issues_count":0,"forks_count":5,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-01-31T07:21:42.929Z","etag":null,"topics":["markov-chain","raku"],"latest_commit_sha":null,"homepage":"","language":"Raku","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"artistic-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/bbkr.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2013-09-30T20:55:56.000Z","updated_at":"2023-01-31T16:32:24.000Z","dependencies_parsed_at":null,"dependency_job_id":"bc987580-f439-4b77-8aa1-16c5243afa35","html_url":"https://github.com/bbkr/Text-Markov","commit_stats":{"total_commits":46,"total_committers":5,"mean_commits":9.2,"dds":0.08695652173913049,"last_synced_commit":"409b184166f10402a8925ebfae110835d82653ed"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bbkr%2FText-Markov","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bbkr%2FText-Markov/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bbkr%2FText-Markov/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bbkr%2FText-Markov/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/bbkr","download_url":"https://codeload.github.com/bbkr/Text-Markov/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":238147585,"owners_count":19424287,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["markov-chain","raku"],"created_at":"2024-09-24T13:35:45.750Z","updated_at":"2025-10-25T13:31:06.486Z","avatar_url":"https://github.com/bbkr.png","language":"Raku","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Markov-chain based text generator for [Raku](https://www.raku.org) language\n\n[![.github/workflows/test.yml](https://github.com/bbkr/Text-Markov/actions/workflows/test.yml/badge.svg)](https://github.com/bbkr/Text-Markov/actions/workflows/test.yml)\n\n## SYNOPSIS\n\n```raku\n    use Text::Markov;\n    \n    my $mc = Text::Markov.new;\n    \n    $mc.feed( qw{Easy things should be easy and hard things should be possible.} );\n    $mc.feed( qw{People who live in glass houses should not throw stones.} );\n    $mc.feed( qw{Live and let live.} );\n    \n    say $mc.read( );\n    # People who live in glass houses should be easy and let live.\n```\n\n## METHODS\n\nMarkov chain is a mathematical system.\n\nTo understand terminology used below read [OPERATING PRINCIPLE](#operating-principle) paragraph first.\n\n### new( order =\u003e 2 )\n\nOrder (optional, default ```1```) controls how many past states determine possibe future states.\n\n### feed( 'I', 'like', 'pancakes.' )\n\nAdd transitions of states.\n\nState can be represented by any object that can be identified by **nonempty** String.\n\n### feeder( $sequence )\n\nStream version of `feed( )`, allows to load transition of states from Sequence.\nUseful for feeding large inputs on the fly, like for example whole book word by word.\n\n### read( ) / read( 128 )\n\nGenerate chain of states up to requested length (optional, default ```1024```).\n\n### reader( ) / reader( 'I', 'like' )\n\nStream version of `read( )`. Returns lazy Sequence that will provide states.\nUseful for generating large (potentially infinite) outputs on the fly.\n\nAccepts initial states, provided list must be no longer than chain `order`.\n\n## OPERATING PRINCIPLE\n\nLet's put abstract hat on and imagine that ___each word represents state___.\n\nTherefore sentence made of words can be represented as ___transitions between states___.\n\n\nFor example sentence ```I like what I see``` is expressed by the following graph:\n\n\n```\n                            4     +------+\n                     +------------| what |\u003c----+\n                     |            +------+     |\n                     |                         |\n                     v                         | 3\n+-------+    1     +---+    2     +------+     |\n| START |---------\u003e| I |---------\u003e| like |-----+\n+-------+          +---+          +------+\n                     |\n                     |\n                     |      5     +-----+    6     +-----+\n                     +-----------\u003e| see |---------\u003e| END |\n                                  +-----+          +-----+\n```\n\nIt may be surprising but transition number is not important for [feed](#feed-foo-bar-baz-) and can be discarded.\n\nInstead of that transitions counters are stored (in this example each transition occured only once):\n\n\n```\n                           1x     +------+\n                     +------------| what |\u003c----+\n                     |            +------+     |\n                     |                         |\n                     v                         | 1x\n+-------+    1x    +---+    1x    +------+     |\n| START |---------\u003e| I |---------\u003e| like |-----+\n+-------+          +---+          +------+\n                     |\n                     |\n                     |     1x     +-----+    1x    +-----+\n                     +-----------\u003e| see |---------\u003e| END |\n                                  +-----+          +-----+\n```\n\nNext sentence```Now I see you like cookies``` when passed to [feed](#feed-foo-bar-baz-)\nwill simply add new transitions or increase counters of already existing ones in the same graph:\n\n\n```\n                           1x     +------+\n                     +------------| what |\u003c----+\n                     |            +------+     |\n                     |                         |\n                     v                         | 1x\n+-------+    1x    +---+    1x    +------+     |\n| START |---------\u003e| I |---------\u003e| like |-----+\n+-------+          +---+          +------+\n    |               ^ |            ^    |\n    |               | |         1x |    |\n 1x |            1x | |            |    | 1x\n    |   +-----+     | |        +-----+  |        +---------+\n    +--\u003e| Now |-----+ |        | you |  +-------\u003e| cookies |\n        +-----+       |        +-----+           +---------+\n                      |            ^                  |\n                      |            | 1x               | 1x\n                      |            |                  v\n                      |    2x     +-----+    1x    +-----+\n                      +----------\u003e| see |---------\u003e| END |\n                                  +-----+          +-----+\n\n```\n\n[Markov chain](http://en.wikipedia.org/wiki/Markov_chain) is generated\nby making transitions from the current state to one of the next possible future states\nwith respecting probability assigned to each transition.\nThe higher the counter the more probable transition is.\n\n\nLet's generate:\n\n* From ```START``` transition can be made to ```I``` [50% chance] or ```Now``` [50% chance] - ```I``` is rolled.\n* From ```I``` transition can be made to ```like``` [33.(3)% chance] or ```see``` [66.(6)% chance] - ```like``` is rolled.\n* From ```like``` transition can be made to ```what``` [50% chance] or ```cookies``` [50% chance] - ```cookies``` is rolled.\n* From ```cookies``` transition can be made only to ```END``` [100% chance].\n\nNew sentence ```I like cookies``` is generated!\n\n\nNote that it is not subpart of any sentence that was used by [feed](#feed-foo-bar-baz-) to create graph,\nyet it has correct grammar and makes sense.\n\n### Improving output quality\n\nDefault setup will produce a lot of nonsense. From sentences...\n\n* ```I was tired.```\n* ```It was snowing.```\n* ```Today I was going to do something useful.```\n\n...new sentence ```I was snowing.``` may be generated.\n\n\nIt happens because single ```was``` word does not give enough context to make rational transitions only.\nParam ```order =\u003e 2``` in constructor restricts possible transitions to those which appears after two past states.\nSo from ```I was``` only two transitions are possible and more reasonable ```Today I was tired.``` sentence may be generated.\n\nThis is called [Markov chain of order m](http://en.wikipedia.org/wiki/Markov_chain#Variations).\n\n\nThe higher the order the more sensible output but more feed is also required. You have to experiment :)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbbkr%2Ftext-markov","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbbkr%2Ftext-markov","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbbkr%2Ftext-markov/lists"}