{"id":24393612,"url":"https://github.com/mrivasperez/markov-smallchat","last_synced_at":"2026-05-01T02:32:40.258Z","repository":{"id":271445192,"uuid":"913479833","full_name":"mrivasperez/markov-smallchat","owner":"mrivasperez","description":"A  simple chatbot using a Markov Chain language model","archived":false,"fork":false,"pushed_at":"2025-01-07T19:11:15.000Z","size":72,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-01-19T18:45:11.946Z","etag":null,"topics":["chatbot","language-model","markov-chain","markov-model","nlp","python","text-generation"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mrivasperez.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":"2025-01-07T19:06:35.000Z","updated_at":"2025-01-07T19:12:23.000Z","dependencies_parsed_at":null,"dependency_job_id":"753cde9e-bea9-431e-a97d-e65e6e8b4c3d","html_url":"https://github.com/mrivasperez/markov-smallchat","commit_stats":null,"previous_names":["mrivasperez/markov-smallchat"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mrivasperez%2Fmarkov-smallchat","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mrivasperez%2Fmarkov-smallchat/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mrivasperez%2Fmarkov-smallchat/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mrivasperez%2Fmarkov-smallchat/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mrivasperez","download_url":"https://codeload.github.com/mrivasperez/markov-smallchat/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243324268,"owners_count":20273099,"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":["chatbot","language-model","markov-chain","markov-model","nlp","python","text-generation"],"created_at":"2025-01-19T18:40:49.124Z","updated_at":"2026-05-01T02:32:35.214Z","avatar_url":"https://github.com/mrivasperez.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# markov-smallchat\n\nThis project implements a simple chatbot using a [Markov Chain language model](./LEARN.md). The chatbot is trained on a dataset of question-answer pairs and generates responses based on the learned probabilities of word sequences.\n\n## Project Description\n\nThe goal of this project was to build a basic chatbot that can engage in short conversations. Due to the limitations of the Markov Chain model, the chatbot has a limited understanding of context and may generate nonsensical or irrelevant responses at times. This project serves as an educational example of how Markov Chains can be used for language modeling and text generation.\n\nThe current implementation uses the following:\n\n- **Markov Chain Order:** 4-grams (considers the previous three words to predict the next word)\n- **Data Preprocessing:**\n  - Questions and answers from each line in the dataset are concatenated into a single string.\n  - Text is converted to lowercase.\n  - Sentences are tokenized into words.\n- **Training Data:** The chatbot is trained on a dataset of question-answer pairs, where each line in the dataset file (`data.txt`) is formatted as:\n  ```\n  question\u003ctab\u003eanswer\n  ```\n\n## Limitations\n\n- **Limited Context:** The Markov Chain model has a limited memory, making it difficult to maintain context over multiple turns in a conversation.\n- **Incoherent Responses:** The chatbot may produce nonsensical or irrelevant responses due to the statistical nature of the model and its lack of true understanding of language.\n- **Data Dependency:** The chatbot's performance is heavily dependent on the size and quality of the training dataset.\n\n## Getting Started\n\n### Prerequisites\n\n- Python 3.x\n- The dataset file (`data.txt`) should be in the same directory as the Python script.\n\n### Installation\n\n1. Clone the repository:\n   ```bash\n   git clone https://github.com/mrivasperez/markov-smallchat.git\n   ```\n2. Navigate to the project directory:\n   ```bash\n   cd markov-smallchat\n   ```\n\n### Running the Chatbot\n\n1. Run the Python script:\n   ```bash\n   python smallchat.py\n   ```\n2. The chatbot will start in the console, and you can interact with it by typing your messages and pressing Enter.\n3. To end the conversation, type `quit`, `exit`, or `bye`.\n\n## Code Explanation\n\n### `load_data(filepath)`\n\nLoads the training data from the specified file. The file is expected to be encoded in UTF-8.\n\n### `create_markov_chain(tokenized_sentences, n=4)`\n\nCreates the Markov Chain model from the tokenized sentences.\n\n- `tokenized_sentences`: A list of tokenized sentences (each sentence is a list of words).\n- `n`: The order of the Markov Chain (default is 4).\n\nThe function calculates the probabilities of word sequences based on the training data and returns a dictionary representing the Markov Chain.\n\n### `generate_response(markov_chain, user_input, n=4)`\n\nGenerates a response to the user input using the Markov Chain model.\n\n- `markov_chain`: The Markov Chain dictionary.\n- `user_input`: The user's input as a string.\n- `n`: The order of the Markov Chain (default is 4).\n\nThe function returns a generated response as a string.\n\n### Conversation Loop\n\nThe main loop of the program:\n\n1. Prompts the user for input.\n2. Checks for exit conditions (`quit`, `exit`, `bye`).\n3. Generates a response using `generate_response`.\n4. Prints the chatbot's response.\n\n## Future Improvements\n\n- **Larger Dataset:** Train the model on a significantly larger and more diverse dataset of conversations.\n- **Data Cleaning:** Implement more robust data cleaning and preprocessing techniques.\n- **Experiment with n:** Try different values of `n` (e.g., 5-grams) to see if it improves the responses (be aware of diminishing returns).\n\n## Contributing\n\nContributions to this project are welcome! If you have any suggestions or improvements, please feel free to open an issue or submit a pull request.\n\n## License\n\nThis project is licensed under the MIT License - see the [LICENSE](./LICENSE) file for details.\n\n## Credits\n\nThe dataset used in this project is derived from the \"[Dataset for chatbot](https://www.kaggle.com/datasets/grafstor/simple-dialogs-for-chatbot/data)\" by Georgy Silkin, available on Kaggle. It is a collection of question-answer pairs suitable for training simple chatbots.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmrivasperez%2Fmarkov-smallchat","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmrivasperez%2Fmarkov-smallchat","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmrivasperez%2Fmarkov-smallchat/lists"}