{"id":16718699,"url":"https://github.com/planeshifter/text-miner","last_synced_at":"2025-08-12T02:05:48.655Z","repository":{"id":17823216,"uuid":"20723961","full_name":"Planeshifter/text-miner","owner":"Planeshifter","description":"text mining utilities for Node.js","archived":false,"fork":false,"pushed_at":"2023-01-21T21:42:36.000Z","size":251,"stargazers_count":141,"open_issues_count":6,"forks_count":19,"subscribers_count":8,"default_branch":"master","last_synced_at":"2025-04-09T15:06:43.709Z","etag":null,"topics":["nlp","text-mining"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","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/Planeshifter.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-06-11T12:18:18.000Z","updated_at":"2025-01-24T13:00:09.000Z","dependencies_parsed_at":"2023-02-12T12:46:05.374Z","dependency_job_id":null,"html_url":"https://github.com/Planeshifter/text-miner","commit_stats":null,"previous_names":[],"tags_count":23,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Planeshifter%2Ftext-miner","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Planeshifter%2Ftext-miner/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Planeshifter%2Ftext-miner/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Planeshifter%2Ftext-miner/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Planeshifter","download_url":"https://codeload.github.com/Planeshifter/text-miner/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248055284,"owners_count":21040157,"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":["nlp","text-mining"],"created_at":"2024-10-12T21:38:11.408Z","updated_at":"2025-04-09T15:06:48.305Z","avatar_url":"https://github.com/Planeshifter.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![NPM version][npm-image]][npm-url]\n[![Build Status][travis-image]][travis-url]\n[![Coverage Status][codecov-image]][codecov-url]\n\ntext-miner\n==========\n\n\u003e text mining utilities for node.js\n\n# Introduction\n\nThe text-miner package can be easily installed via npm:\n\n``` bash\nnpm install text-miner\n```\n\nTo require the module in a project, we can use the expression\n\n``` javascript\nvar tm = require( 'text-miner' );\n```\n\n## Corpus\n\nThe fundamental data type in the `text-miner` module is the *Corpus*. An instance of this class wraps a collection of documents\nand provides several methods to interact with this collection and perform post-processing tasks such as stemming,\nstopword removal etc.\n\nA new corpus is created by calling the constructor\n\n``` javascript\nvar my_corpus = new tm.Corpus([]);\n```\n\nwhere `[]` is an array of text documents which form the data of the corpus. The class supports method chaining, such that mutliple methods can be invoked after each other, e.g.\n\n``` javascript\nmy_corpus\n\t.trim()\n\t.toLower()\n```\n\nThe following methods and properties are part of the Corpus class:\n\n### Methods\n\n#### `.addDoc(doc)`\n\nAdd a single document to the corpus. Has to be a string.\n\n#### `.addDocs(docs)`\n\nAdds a collection of documents (in form of an array of strings) to the corpus.\n\n#### `.clean()`\nStrips extra whitespace from all documents, leaving only at most one whitespace between any two other characters.\n\n#### `.map(fun)`\nApplies the function supplied to `fun` to each document in the corpus and maps each document to the result of its respective\nfunction call.\n\n#### `.removeInterpunctuation()`\nRemoves interpunctuation characters (! ? . , ; -) from all documents.\n\n#### `.removeNewlines()`\nRemoves newline characters (\\n) from all documents.\n\n#### `.removeWords(words[, case_insensitive])`\nRemoves all words in the supplied `words` array from all documents. This function is usually invoked to remove stopwords. For convenience,\nthe *text-miner* package ships with a list of stopwords for different languages. These are stored in the\n`STOPWORDS` object of the module.\n\nCurrently, stopwords for the following languages are included:\n\n``` javascript\nSTOPWORDS.DE\nSTOPWORDS.EN\nSTOPWORDS.ES\nSTOPWORDS.IT\n```\n\nAs a concrete example, we could remove all english stopwords from corpus `my_corpus` as follows:\n\n``` javascript\nmy_corpus.removeWords( tm.STOPWORDS.EN )\n```\n\nThe second (optional) parameter of the function `case_insensitive` expects a Boolean indicating whether to ignore cases or not.\nThe default value is `false`.\n\n#### `.removeDigits()`\n\nRemoves any digits occuring in the texts.\n\n#### `.removeInvalidCharacters()`\n\nRemoves all characters which are unknown or unrepresentable in Unicode.\n\n#### `.stem(type)`\nPerforms stemming of the words in each document. Two stemmers are supported: Porter and Lancaster. The former is the default\noption. Passing \"Lancaster\" to the `type` parameter of the function ensured that the latter one is used.\n\n#### `.toLower()`\nConverts all characters in the documents to lower-case.\n\n#### `.toUpper()`\nConverts all characters in the documents to upper-case.\n\n#### `.trim()`\nStrips off whitespace at the beginning and end of each document.\n\n## DocumentTermMatrix / TermDocumentMatrix\n\nWe can pass a corpus to the constructor `DocumentTermMatrix` in order to create a document-term-matrix or a term-document matrix. Objects derived from either share the same methods, but differ in how the underlying matrix is represented: A `DocumentTermMatrix` has documents on its rows and columns corresponding to words, whereas a `TermDocumentMatrix` has rows corresponding to words and columns to documents.\n\n``` javascript\nvar terms = new tm.DocumentTermMatrix( my_corpus );\n```\n\nAn instance of either `DocumentTermMatrix` or `TermDocumentMatrix` has the following properties:\n\n### Properties\n\n#### `.vocabulary`\nAn array holding all the words occuring in the corpus, in order corresponding to the column entries of the document-term matrix.\n\n#### `.data`\nThe document-term or term-document matrix, implemented as a nested array in JavaScript. Rows correspond to individual documents, while each column index corresponds to the respective word in `vocabulary`. Each entry of `data` holds the number of counts the word appears in the respective documents. The array is sparse, such that each entry which is undefined corresponds to a value of zero.\n\n#### `.nDocs`\nThe number of documents in the term matrix\n\n#### `.nTerms`\nThe number of distinct words appearing in the documents\n\n### Methods\n\n#### `.findFreqTerms( n )`\n\nReturns all terms in alphabetical ordering which appear `n` or more times in the corpus. The return value is an array of objects of the form\n`{word: \"\u003cword\u003e\", count: \u003cnumber\u003e}`.\n\n#### `.removeSparseTerms( percent )`\n\nRemove all words from the document-term matrix which appear in less than `percent` of the documents.\n\n#### `.weighting( fun )`\n\nApply a weighting scheme to the entries of the document-term matrix. The `weighting` method expects a function as its argument, which is then applied to each entry of the document-term matrix. Currently, the function `weightTfIdf`, which calculates the term-frequency inverse-document-frequency (TfIdf) for each word, is the only built-in weighting function.  \n\n#### `.fill_zeros()`\n\nTurn the document-term matrix `dtm` into a non-sparse matrix by replacing each value which is `undefined` by zero and save the result.\n\n## Utils\n\nThe module exports several other utility functions.\n\n#### `.expandContractions( str )`\n\nReplaces all occuring English contractions by their expanded equivalents, e.g. \"don't\" is changed to\n\"do not\". The resulting string is returned.\n\n#### `.weightTfIdf( terms )`\n\nWeights document-term or term-document matrix `terms` by term frequency - inverse document frequency. *Mutates* the input `DocumentTermMatrix` or `TermDocumentMatrix` object.\n\n## Data\n\n#### .STOPWORDS\n\nAn object with four keys: `DE`, `EN`, `ES` and `IT`, each of which is an `array` of stopwords for the German, English, Spanish and Italian language, respectively.\n\n``` javascript\n{\n\t\"EN\": [\n\t\t\"a\",\n\t\t\"a's\",\n\t\t\"able\",\n\t\t\"about\",\n\t\t\"above\",\n\t\t// (...)  \n\t],\n\t\"DE\": [\n\t\t// (...)\n\t],\n\t// (...)\n}\n```\n\n#### .CONTRACTIONS\n\nThe keys of the `CONTRACTIONS` object are the contracted expressions and the corresponding values are `arrays` of the possible expansions.\n\n``` javascript\n{\n\t\"ain't\": [\"am not\", \"are not\", \"is not\", \"has not\",\"have not\"],\n\t\"aren't\": [\"are no\", \"am not\"],\n\t\"can't\": [\"cannot\"],\n\t// (...)\n}\n```\n\n## Unit Tests\n\nRun tests via the command `npm test`\n\n---\n## License\n\n[MIT license](http://opensource.org/licenses/MIT).\n\n[npm-image]: https://badge.fury.io/js/text-miner.svg\n[npm-url]: http://badge.fury.io/js/text-miner\n\n[travis-image]: https://travis-ci.org/Planeshifter/text-miner.svg\n[travis-url]: https://travis-ci.org/Planeshifter/text-miner\n\n[codecov-image]: https://img.shields.io/codecov/c/github/Planeshifter/text-miner/master.svg\n[codecov-url]: https://codecov.io/github/Planeshifter/text-miner?branch=master\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fplaneshifter%2Ftext-miner","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fplaneshifter%2Ftext-miner","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fplaneshifter%2Ftext-miner/lists"}