{"id":19284176,"url":"https://github.com/swang/markovchain","last_synced_at":"2025-08-24T06:35:01.573Z","repository":{"id":11859051,"uuid":"14417936","full_name":"swang/markovchain","owner":"swang","description":"generates a markov chain of words based on input files","archived":false,"fork":false,"pushed_at":"2017-09-01T08:30:38.000Z","size":42,"stargazers_count":50,"open_issues_count":3,"forks_count":7,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-01T18:22:02.708Z","etag":null,"topics":["javascript","markov-chain"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/swang.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2013-11-15T07:35:42.000Z","updated_at":"2024-06-18T23:17:06.000Z","dependencies_parsed_at":"2022-09-14T21:50:19.262Z","dependency_job_id":null,"html_url":"https://github.com/swang/markovchain","commit_stats":null,"previous_names":[],"tags_count":9,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/swang%2Fmarkovchain","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/swang%2Fmarkovchain/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/swang%2Fmarkovchain/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/swang%2Fmarkovchain/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/swang","download_url":"https://codeload.github.com/swang/markovchain/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250167574,"owners_count":21386004,"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":["javascript","markov-chain"],"created_at":"2024-11-09T21:36:54.802Z","updated_at":"2025-04-22T03:31:45.372Z","avatar_url":"https://github.com/swang.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"markovchain\n=========================================\nmarkovchain generates a markov chain based on text passed into it.\n\n[![build status](https://secure.travis-ci.org/swang/markovchain.png)](http://travis-ci.org/swang/markovchain)\n\n## Requirements\n- [node v0.6+](http://nodejs.org/)\n\n## Install\n- npm install markovchain\n\n## Example\n\n```js\nvar MarkovChain = require('markovchain')\n  , fs = require('fs')\n  , quotes = new MarkovChain(fs.readFileSync('./quotes.txt', 'utf8'))\n\nconsole.log(quotes.start('The').end(5).process())\n```\nThis will read a file, \"quotes.txt\", generate a word chain, then attempt to\ngenerate sentences starting with the word\n\n\"The\" that are 5 words long, and then output to console.\n\n## Methods\n### constructor([string: content][, function: normalizeFn])\n`content`: the content that you want passed into the markov chain\n`normalizeFn`: the function to apply to every word (generally to clean it up,\ne.g. removing commas and other non-letter words)\n\n### start([string: str|function: func])\nThe `start` method can take in either a `string` str, in which case it will look\nto use that word to start the sentence.\n\nIf you instead pass a `function` func with one parameter, `wordList`, you will\nbe given the entire list of word chains in which you can decide what words to\nuse to start a sentence. For example, you can generate sentences based on the\nnumber of times a word occurs, or if the word starts with a capital letter.\n\nExample:\n```js\n\nvar useUpperCase = function(wordList) {\n  var tmpList = Object.keys(wordList).filter(function(word) {\n    return word[0] \u003e= 'A' \u0026\u0026 word[0] \u003c= 'Z'\n  })\n  return tmpList[~~(Math.random()*tmpList.length)]\n}\n\nconsole.log(quotes.start(useUpperCase).end().process())\n```\n\n### end([string: str|function: func|integer: int])\nThe `end` method can take a `String`, `Integer`, or `Function`\n\n- If you pass a String, `str`, the markov chain will generate words until the\nword matches `str` or the generator can no longer find words to chain.\n- If you pass an Integer, `int`, the markov chain will generate words until the\nsentence length (as measured by word count) matches `int` or the generator can no longer find words to\nchain.\n- If you pass a Function, `func`, the markov chain will generate words until\nfunction `func` returns true. `func` will be passed one parameter, `sentence`\nthat returns the generated sentence so far\n\nExample:\n```js\n// same as passing value, 5 to end function\nvar stopAfterFiveWords = function(sentence) {\n  return sentence.split(\" \").length \u003e= 5\n}\n\nconsole.log(quotes.start(useUpperCase).end(stopAfterFiveWords).process())\n```\n\n- If you pass nothing in `end`, the markov chain will generate words until it\ncan no longer find words to chain.\n\n### parse(string: content[, parseBy:regex/string])\nThe `parse` function adds more content to the markov chain. This allows you to\ncontent later on, rather than needing all the next when you instantiate the\nmarkov chain.\n\nIf you pass a second parameter, `parseBy`, it specifies how the content will be\nparsed into sentences (which are then further parsed down into words). By\ndefault `parseBy` will parse into newlines, periods, and question marks.\n\nExample:\n```js\nvar m = new MarkovChain('some text here')\n\nm.parse('add additional text')\n\nconsole.log(m.parse('more and more text').end(5).process())\n```\n\n## Author\n- [Shuan Wang](https://github.com/swang) [(twitter)](https://twitter.com/swang) (author)\n\n## CHANGELOG\n1.0.0\n- Deprecate older version. Still accessible with this library the same exact way\n  it was called before: `var MarkovChain = require('markovchain').MarkovChain`\n- This library now will be focused on the markov chain processing portion\n  rather than file processing. Thus all file processing related functions were\n  removed.\n\n### NOTE ####\n0.0.6 IS PROBABLY THE LAST VERSION WITH THIS API.\n------\n0.0.6\n- Fix when passing a string to `end()`\n\n0.0.5\n- Added default startFn/endFn functions\n- use() now actually handles array of strings\n- Use async.parallel instead of async.series\n\n0.0.4\n- Fix undefined sentence if start was passed a function\n\n0.0.3\n- Passing a Function into `end()` has changed a little bit, before the markov\nchain would continue until the Function passed returned false, now the Function\nbeing passed into `end()` should only return true when you want the markov chain\ngenerator to stop generating the sentence.\n- `start` can now accept a Function instead of just a String\n- The logic to split sentences has changed from just a newline, to both a\nnewline and a period.\n- Also previous versions changed all the words to strip them of any\nnon-letters/numbers and also lowercased them. This version now doesn't modify\nthe string other than to delete a period at the end of a word.\n\n0.0.2\n- Small change to how words are stripped\n\n0.0.1\n- Initial Release\n\n## LICENSE\n- MIT\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fswang%2Fmarkovchain","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fswang%2Fmarkovchain","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fswang%2Fmarkovchain/lists"}