{"id":16568684,"url":"https://github.com/drupol/phpngrams","last_synced_at":"2025-10-06T13:33:54.093Z","repository":{"id":56972713,"uuid":"120319004","full_name":"drupol/phpngrams","owner":"drupol","description":"Get N-Grams !","archived":false,"fork":false,"pushed_at":"2019-07-24T06:42:55.000Z","size":174,"stargazers_count":8,"open_issues_count":0,"forks_count":1,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-02-01T20:44:59.382Z","etag":null,"topics":["n-grams","ngrams"],"latest_commit_sha":null,"homepage":"https://not-a-number.io/phpngrams","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/drupol.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":"2018-02-05T14:55:35.000Z","updated_at":"2023-07-07T04:22:33.000Z","dependencies_parsed_at":"2022-08-21T07:10:28.751Z","dependency_job_id":null,"html_url":"https://github.com/drupol/phpngrams","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/drupol%2Fphpngrams","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/drupol%2Fphpngrams/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/drupol%2Fphpngrams/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/drupol%2Fphpngrams/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/drupol","download_url":"https://codeload.github.com/drupol/phpngrams/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":238751172,"owners_count":19524536,"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":["n-grams","ngrams"],"created_at":"2024-10-11T21:11:22.260Z","updated_at":"2025-10-06T13:33:49.044Z","avatar_url":"https://github.com/drupol.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![Latest Stable Version](https://poser.pugx.org/drupol/phpngrams/v/stable)](https://packagist.org/packages/drupol/phpngrams)\n [![Total Downloads](https://poser.pugx.org/drupol/phpngrams/downloads)](https://packagist.org/packages/drupol/phpngrams)\n [![Build Status](https://travis-ci.org/drupol/phpngrams.svg?branch=master)](https://travis-ci.org/drupol/phpngrams)\n [![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/drupol/phpngrams/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/drupol/phpngrams/?branch=master)\n [![Code Coverage](https://scrutinizer-ci.com/g/drupol/phpngrams/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/drupol/phpngrams/?branch=master)\n [![Mutation testing badge](https://badge.stryker-mutator.io/github.com/drupol/phpngrams/master)](https://stryker-mutator.github.io)\n [![License](https://poser.pugx.org/drupol/phpngrams/license)](https://packagist.org/packages/drupol/phpngrams)\n\n## PHPNgrams\n\nPHP N-Grams library\n\n## Introduction\n\nIn the fields of computational linguistics, machine-learning and probability, an n-gram is a contiguous sequence of n items from a given sample of text or speech. The items can be phonemes, syllables, letters, words or base pairs according to the application. The n-grams typically are collected from a text or speech corpus. When the items are words, n-grams may also be called shingles.\n\nAn n-gram of size 1 is referred to as a \"unigram\"; size 2 is a \"bigram\" (or, less commonly, a \"digram\"); size 3 is a \"trigram\". Larger sizes are sometimes referred to by the value of n in modern language, e.g., \"four-gram\", \"five-gram\", and so on. (More on [Wikipedia](https://en.wikipedia.org/wiki/N-gram))\n\n## Requirements\n\n* PHP \u003e= 7.0\n\n## Installation\n\nInclude this library in your project by doing:\n\n`composer require drupol/phpngrams`\n\nThe library provides two classes:\n\n* NGrams\n* NGramsCyclic\n\nand one trait:\n\n* NGramsTrait\n\n## Usage\n\n```php\n\u003c?php\n\ndeclare(strict_types = 1);\n\nnamespace drupol\\phpngrams\\tests;\n\nuse drupol\\phpngrams\\NGrams;\nuse drupol\\phpngrams\\NGramsCyclic;\n\ninclude 'vendor/autoload.php';\n\n$string = 'hello world';\n\n// Better use preg_split() than str_split() in case of UTF8 strings.\n$chars = preg_split('/(?!^)(?=.)/u', $string);\n\n$ngrams = (new NGrams())-\u003engrams($chars, 3);\n\nprint_r(iterator_to_array($ngrams));\n/*\n[\n    0 =\u003e\n        [\n            0 =\u003e 'h',\n            1 =\u003e 'e',\n            2 =\u003e 'l',\n        ],\n    1 =\u003e\n        [\n            0 =\u003e 'e',\n            1 =\u003e 'l',\n            2 =\u003e 'l',\n        ],\n    2 =\u003e\n        [\n            0 =\u003e 'l',\n            1 =\u003e 'l',\n            2 =\u003e 'o',\n        ],\n    3 =\u003e\n        [\n            0 =\u003e 'l',\n            1 =\u003e 'o',\n            2 =\u003e ' ',\n        ],\n    4 =\u003e\n        [\n            0 =\u003e 'o',\n            1 =\u003e ' ',\n            2 =\u003e 'w',\n        ],\n    5 =\u003e\n        [\n            0 =\u003e ' ',\n            1 =\u003e 'w',\n            2 =\u003e 'o',\n        ],\n    6 =\u003e\n        [\n            0 =\u003e 'w',\n            1 =\u003e 'o',\n            2 =\u003e 'r',\n        ],\n    7 =\u003e\n        [\n            0 =\u003e 'o',\n            1 =\u003e 'r',\n            2 =\u003e 'l',\n        ],\n    8 =\u003e\n        [\n            0 =\u003e 'r',\n            1 =\u003e 'l',\n            2 =\u003e 'd',\n        ],\n];\n*/\n\n$string = 'hello world';\n\n// Better use preg_split() than str_split() in case of UTF8 strings.\n$chars = preg_split('/(?!^)(?=.)/u', $string);\n\n$ngrams = (new NGramsCyclic())-\u003engrams($chars, 3);\n\nprint_r(iterator_to_array($ngrams));\n/*\n[\n    0 =\u003e [\n            0 =\u003e 'h',\n            1 =\u003e 'e',\n            2 =\u003e 'l',\n        ],\n    1 =\u003e [\n            0 =\u003e 'e',\n            1 =\u003e 'l',\n            2 =\u003e 'l',\n        ],\n    2 =\u003e [\n            0 =\u003e 'l',\n            1 =\u003e 'l',\n            2 =\u003e 'o',\n        ],\n    3 =\u003e [\n            0 =\u003e 'l',\n            1 =\u003e 'o',\n            2 =\u003e ' ',\n        ],\n    4 =\u003e [\n            0 =\u003e 'o',\n            1 =\u003e ' ',\n            2 =\u003e 'w',\n        ],\n    5 =\u003e [\n            0 =\u003e ' ',\n            1 =\u003e 'w',\n            2 =\u003e 'o',\n        ],\n    6 =\u003e [\n            0 =\u003e 'w',\n            1 =\u003e 'o',\n            2 =\u003e 'r',\n        ],\n    7 =\u003e [\n            0 =\u003e 'o',\n            1 =\u003e 'r',\n            2 =\u003e 'l',\n        ],\n    8 =\u003e [\n            0 =\u003e 'r',\n            1 =\u003e 'l',\n            2 =\u003e 'd',\n        ],\n    9 =\u003e [\n            0 =\u003e 'l',\n            1 =\u003e 'd',\n            2 =\u003e 'h',\n        ],\n    10 =\u003e [\n            0 =\u003e 'd',\n            1 =\u003e 'h',\n            2 =\u003e 'e',\n        ],\n];\n*/\n```\n\nTo reduce to the maximum the memory footprint, the library returns Generators, if you want to get the complete resulting array, use [iterator_to_array()](https://secure.php.net/manual/en/function.iterator-to-array.php).\n\n## API\n\nFind the complete API documentation at [https://not-a-number.io/phpngrams](https://not-a-number.io/phpngrams).\n\n## Code quality and tests\n\nEvery time changes are introduced into the library, [Travis CI](https://travis-ci.org/drupol/phpngrams/builds) run the tests.\n\nThe library has tests written with [PHPSpec](http://www.phpspec.net/).\n\nFeel free to check them out in the `spec` directory. Run `composer phpspec` to trigger the tests.\n\n[PHPInfection](https://github.com/infection/infection) is used to ensure that your code is properly tested, run `composer infection` to test your code.\n\n# Contributing\n\nFeel free to contribute to this library by sending Github pull requests. I'm quite reactive :-)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdrupol%2Fphpngrams","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdrupol%2Fphpngrams","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdrupol%2Fphpngrams/lists"}