{"id":21466565,"url":"https://github.com/permafrost-dev/text-classifier","last_synced_at":"2025-03-17T05:48:04.664Z","repository":{"id":57037869,"uuid":"268153413","full_name":"permafrost-dev/text-classifier","owner":"permafrost-dev","description":"Basic text classification using algorithms such as Naive-Bayes","archived":false,"fork":false,"pushed_at":"2021-05-27T14:23:41.000Z","size":36,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-01-23T15:32:04.298Z","etag":null,"topics":["naive-bayes","text-classification","text-classifier"],"latest_commit_sha":null,"homepage":null,"language":"PHP","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/permafrost-dev.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-05-30T20:20:14.000Z","updated_at":"2023-01-11T02:54:56.000Z","dependencies_parsed_at":"2022-08-23T21:00:22.412Z","dependency_job_id":null,"html_url":"https://github.com/permafrost-dev/text-classifier","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/permafrost-dev%2Ftext-classifier","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/permafrost-dev%2Ftext-classifier/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/permafrost-dev%2Ftext-classifier/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/permafrost-dev%2Ftext-classifier/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/permafrost-dev","download_url":"https://codeload.github.com/permafrost-dev/text-classifier/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243982136,"owners_count":20378606,"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":["naive-bayes","text-classification","text-classifier"],"created_at":"2024-11-23T08:14:37.360Z","updated_at":"2025-03-17T05:48:04.646Z","avatar_url":"https://github.com/permafrost-dev.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# text-classifier\nPerforms basic text classification using algorithms such as Naive-Bayes.\n\n---\n##### Installation:   \nYou may install text-classifier using composer: \n\n\u003e `composer require permafrost-dev/text-classifier`\n\n\nNote: The higher-quality and more complete training data used to train the model, the more accurate the classifications will be.\n\n***\n#### Example - Email Address Classification\n\nA common use-case for classifying text is to determine whether or not an email is spam or not spam.  While that's beyond\nthe scope of this example, we can try to determine if a given email address is spam or not spam based on its features.\n*Note: all email addresses used for training/examples were randomly generated.  If your email address somehow ended up\nwithin the sample data, please contact packages@permafrost.dev and it will be promptly removed.*\n\n\n```php\n\u003c?php\n\nuse Permafrost\\TextClassifier\\TextClassifier;\nuse Permafrost\\TextClassifier\\Classifiers\\NaiveBayes;\nuse Permafrost\\TextClassifier\\Pipelines\\TextProcessingPipeline;\nuse Permafrost\\TextClassifier\\Tokenizers\\EmailAddressTokenizer;\nuse Permafrost\\TextClassifier\\Processors\\EmailAddressNormalizer;\n\n$processors = new TextProcessingPipeline([\n    new EmailAddressNormalizer(),\n]);\n\n$tc = new TextClassifier($processors, [new EmailAddressTokenizer()], new NaiveBayes());\n$tc = $tc-\u003etrainFromFile(__DIR__ . '/email-train.txt');\n\n$emails = [\n    'blah44657457@whatever.rut',\n    'john@gmail.com',\n];\n\nforeach ($emails as $email) {\n    echo \"classification for '$email': \" . $tc-\u003eclassify($email) . PHP_EOL;\n}\n```\n\nResulting output:\n\n- `classification for 'blah44657457@whatever.rut': spam`\n- `classification for 'john@gmail.com': valid`\n\nThis method can easily be applied to other areas for spam checking, such as classifiying user-provided domain names.\n\n\n***\n\n#### Example - Sentiment Analysis\n\nSee `examples/sentiment.php` for a working demo.\n\n```php\n\u003c?php\n\nuse Skyeng\\Lemmatizer;\nuse Permafrost\\TextClassifier\\TextClassifier;\nuse Permafrost\\TextClassifier\\Classifiers\\NaiveBayes;\nuse Permafrost\\TextClassifier\\Processors\\TextLemmatizer;\nuse Permafrost\\TextClassifier\\Tokenizers\\BasicTokenizer;\nuse Permafrost\\TextClassifier\\Tokenizers\\NGramTokenizer;\nuse Permafrost\\TextClassifier\\Processors\\StopwordRemover;\nuse Permafrost\\TextClassifier\\Processors\\BasicTextNormalizer;\nuse Permafrost\\TextClassifier\\Pipelines\\TextProcessingPipeline;\n\n//Use different processors for training and classifying.  Since we're using keyword tokens,\n//add all lemmas for each token during training to increase the size of the training data.\n$trainingProcessors = [new TextLemmatizer(new Lemmatizer()), new BasicTextNormalizer()];\n\n//When classifying, let's remove stopwords in addition to basic text normalization, because\n//we'll be processing phrases.\n$classifyProcessors = [new StopwordRemover(), new BasicTextNormalizer()];\n\n//Let's use a basic tokenizer (word-based tokens), and an NGram tokenizer, which creates \n//trigrams (N=3). This should give us a good mix of keywords and partial keywords to look\n//for when classifying text.\n$tokenizers = [new BasicTokenizer(), new NGramTokenizer(3)];\n\n$textClassifier = new TextClassifier(\n    new TextProcessingPipeline($trainingProcessors, $classifyProcessors),\n    $tokenizers,\n    new NaiveBayes() //use Naive-Bayes as the classifier\n);\n\n$textClassifier-\u003etrainFromFile(__DIR__ . '/sentiment-train.txt');\n\n$phrases = [\n    'this is fantastic',\n    'this is terrible',\n];\n\nforeach ($phrases as $phrase) {\n    echo $phrase . ' - ' . $textClassifier-\u003eclassify($phrase) . PHP_EOL;\n}\n```\n\nResulting output:\n\n- `this is fantastic - positive`\n- `this is terrible! - negative`\n\n\n***\n\nWith more robust pre-processing and tokenizing, these methods can be applied to other data, such as determining whether or not\nan email message is likely a spam message, whether a given article is of interest to a user based on basic preferences, and so on.\n\nThis does only go so far, however - machine learning is recommended when highly-accurate results are needed.\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpermafrost-dev%2Ftext-classifier","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpermafrost-dev%2Ftext-classifier","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpermafrost-dev%2Ftext-classifier/lists"}