{"id":21322125,"url":"https://github.com/polyrabbit/polyglot","last_synced_at":"2025-10-18T06:04:34.926Z","repository":{"id":16397336,"uuid":"19148170","full_name":"polyrabbit/polyglot","owner":"polyrabbit","description":"Detect Program Language from Source Code Using Naive Bayes Classifier","archived":false,"fork":false,"pushed_at":"2015-05-11T10:09:52.000Z","size":18435,"stargazers_count":6,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-01-22T11:48:03.379Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Lasso","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/polyrabbit.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}},"created_at":"2014-04-25T14:05:49.000Z","updated_at":"2024-10-25T16:14:37.000Z","dependencies_parsed_at":"2022-09-07T11:52:24.541Z","dependency_job_id":null,"html_url":"https://github.com/polyrabbit/polyglot","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/polyrabbit%2Fpolyglot","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/polyrabbit%2Fpolyglot/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/polyrabbit%2Fpolyglot/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/polyrabbit%2Fpolyglot/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/polyrabbit","download_url":"https://codeload.github.com/polyrabbit/polyglot/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243801612,"owners_count":20350106,"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":[],"created_at":"2024-11-21T20:13:28.493Z","updated_at":"2025-10-18T06:04:29.894Z","avatar_url":"https://github.com/polyrabbit.png","language":"Lasso","funding_links":[],"categories":[],"sub_categories":[],"readme":"polyglot\n========\n\n[![Build Status](https://travis-ci.org/polyrabbit/polyglot.svg?branch=master)](https://travis-ci.org/polyrabbit/polyglot)\n\nProgram language savant. It is used to detect program languages just like [github/linguist](https://github.com/github/linguist/), but based on naive Bayes classifier.\n\n### Getting Started\nInstall using pip\n\n```\npip install git+https://github.com/polyrabbit/polyglot\n```\n\nFirst, we need to train polyglot on a multilingual training corpus, each folder in the corpus should contain files of the same language whose name is identified by the folder. e.g.\n```\npolyglot train --corpus=./corpus --ngram=3 --verbose --output=./model.json\n```\n\nA pre-included [model.json](https://github.com/polyrabbit/polyglot/blob/master/model.json) is generated using the above command. Run `polyglot train --help` for usage specifics.\n\nAfter training, we can use the Naive Bayes classifier to classify a given file. e.g.\n```\necho import os | polyglot classify --ngram=3 --top=3 --verbose --model=./model.json -\n```\nWhich outputs top 3 most likely languages in descending order with their scores \n\n```\n[(u'Python', 6.719828065958895), (u'Frege', -11.021531184412824), (u'Objective-C++', -13.244791737113022)]\n```\nRun `polyglot classify --help` for usage specifics.\n\n### Classification Algorithm\n\n1. Lex input string into tokens, and generate n-grams from those tokens (trigram by default)\n\n\n        \"#include\u003cstdio.h\u003e\".lex().ngram(max_n=3)\n        \n        =\u003e[['#'], ['include'], ['\u003c'], ['stdio'], ['.'], ['h'], ['\u003e'], ['#', 'include'], ['include', '\u003c'], ['\u003c', 'stdio'], ['stdio', '.'], ['.', 'h'], ['h', '\u003e'], ['#', 'include', '\u003c'], ['include', '\u003c', 'stdio'], ['\u003c', 'stdio', '.'], ['stdio', '.', 'h'], ['.', 'h', '\u003e']]\n\n\n2. Computing the probability of a language given a token\n\n\n        P(lang | token) \n        \n        = P(token | lang) * P(lang) / P(token)\n\n           n_token_on_lang(token, lang)     n_lang_tokens(lang)     n_token(token)\n        = ------------------------------ * -------------------- /  ---------------\n           n_lang_tokens(lang)              n_tokens()              n_tokens()\n\n           n_token_on_lang(token, lang) \n        = ------------------------------\n           n_token(token)\n\n\n3. Combining individual probabilities\n\n\n   \t\tP(lang | tok_1, tok_2, tok_3...tok_n)\n   \t\t\n          P(tok_1, tok_2, tok_3...tok_n | lang) * P(lang)\n        = --------------------------------------------\n                  P(tok_1, tok_2, tok_3...tok_n)\n\n          (naively assume that tokens are independent from each other)\n\n          P(tok_1|lang) * P(tok_2|lang) * P(tok_3|lang) ... P(tok_n|lang) * P(lang)\n        = ---------------------------------------------------------------------\n             P(tok_1) * P(tok_2) * P(tok_3) ... P(tok_n)\n        \n                P(tok|lang)   P(lang|tok)\n              ( ----------- = ----------- )\n                  P(tok)        P(lang)\n\n          P(lang|tok_1) * P(lang|tok_2) * P(lang|tok_3) ... P(lang|tok_n) * P(lang)\n        = ---------------------------------------------------------------------\n                               P(lang)^N\n\n5. Dealing with rare words\n\n\n\t\tP(unseen_token) = 1.0/(n_all_tokens()+1)\n\n\n### References\n 1. [A Plan For Spam](http://www.paulgraham.com/spam.html)\n 2. [Naive Bayes spam filtering](http://en.wikipedia.org/wiki/Naive_Bayes_spam_filtering)\n 3. [Implementation of naive bayesian spam filter algorithm](http://blog.csdn.net/hexinuaa/article/details/5596862)\n 4. [How To Build a Naive Bayes Classifier](https://www.bionicspirit.com/blog/2012/02/09/howto-build-naive-bayes-classifier.html)\n\n[V2EX讨论](https://www.v2ex.com/t/190152)","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpolyrabbit%2Fpolyglot","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpolyrabbit%2Fpolyglot","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpolyrabbit%2Fpolyglot/lists"}