{"id":16718682,"url":"https://github.com/planeshifter/node-word2vec","last_synced_at":"2025-05-16T14:06:00.410Z","repository":{"id":24563784,"uuid":"27971109","full_name":"Planeshifter/node-word2vec","owner":"Planeshifter","description":"Node.js interface to the Google word2vec tool.","archived":false,"fork":false,"pushed_at":"2024-08-09T10:45:40.000Z","size":2290,"stargazers_count":355,"open_issues_count":15,"forks_count":56,"subscribers_count":13,"default_branch":"master","last_synced_at":"2025-05-12T22:41:07.585Z","etag":null,"topics":["nlp","word2vec"],"latest_commit_sha":null,"homepage":"","language":"C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2014-12-13T19:21:02.000Z","updated_at":"2025-04-17T08:38:43.000Z","dependencies_parsed_at":"2024-11-14T18:01:00.027Z","dependency_job_id":null,"html_url":"https://github.com/Planeshifter/node-word2vec","commit_stats":{"total_commits":104,"total_committers":5,"mean_commits":20.8,"dds":0.05769230769230771,"last_synced_commit":"25edc96223be3c196f38e719f01ef51181166901"},"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Planeshifter%2Fnode-word2vec","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Planeshifter%2Fnode-word2vec/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Planeshifter%2Fnode-word2vec/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Planeshifter%2Fnode-word2vec/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Planeshifter","download_url":"https://codeload.github.com/Planeshifter/node-word2vec/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254544146,"owners_count":22088807,"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","word2vec"],"created_at":"2024-10-12T21:38:05.400Z","updated_at":"2025-05-16T14:05:55.400Z","avatar_url":"https://github.com/Planeshifter.png","language":"C","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[![Dependencies][dependencies-image]][dependencies-url]\n\nnode-word2vec\n=============\n\n\u003e Node.js interface to the Google [word2vec](https://code.google.com/p/word2vec/) tool\n\n# What is it?\n\nThis is a Node.js interface to the *word2vec* tool developed at Google Research for \"efficient implementation of the continuous bag-of-words and skip-gram architectures for computing vector representations of words\", which can be used in a variety of NLP tasks. For further information about the *word2vec* project, consult [https://code.google.com/p/word2vec/](https://code.google.com/p/word2vec/).\n\n# Installation\n\nCurrently, `node-word2vec` is ONLY supported for Unix operating systems.\n\nInstall it via npm:\n``` bash\nnpm install word2vec\n```\n\nTo use it inside Node.js, require the module as follows:\n\n``` javascript\nvar w2v = require( 'word2vec' );\n```\n\n# Usage\n\n## API\n\n### .word2phrase( input, output, params, callback )\n\nFor applications where it is important that certain pairs of words are treated as a single term (e.g. \"Barack Obama\" or \"New York\" should be treated as one word), the text corpora used for training should be pre-processed via the *word2phrases* function. Words which frequently occur next to each other will be concatenated via an underscore, e.g. the words \"New\" and \"York\" if following next to each other might be transformed to a single word \"New_York\".\n\nInternally, this function calls the C command line application of the Google *word2vec* project. This allows it to make use of multi-threading and preserves the efficiency of the original C code. It processes the texts given by the `input` text document, writing the output to a file with the name given by `output`.\n\nThe `params` parameter expects a JS object optionally containing some of the following keys and associated values. If they are not supplied, the default values are used.\n\n\n| Key        |   Description           | Default Value |\n| ------------- |:-------------:| -----:|\n| minCount      | discard words appearing less than *minCount* times | 5 |\n| threshold      | determines the number of phrases, higher value means less phrases   | 100 |\n| debug         | sets debug mode      | 2 |\n| silent | sets whether any output should be printed to the console | false |\n\nAfter successful execution, the supplied `callback` function is invoked. It receives the number of the exit code as its first parameter.\n\n### .word2vec( input, output, params, callback )\n\nThis function calls Google's *word2vec* command line application and finds vector representations for the words in the `input` training corpus, writing the results to the `output` file. The output can then be loaded into node via the `loadModel` function, which exposes several methods to interact with the learned vector representations of the words.\n\nThe `params` parameter expects a JS object optionally containing some of the following keys and associated values. For those missing, the default values are used:\n\n| Key        |   Description           | Default Value |\n| ------------- |:-------------:| -----:|\n| size      | sets the size of word vectors| 100 |\n| window      | sets maximal skip length between words   | 5 |\n| sample         | sets threshold for occurrence of words. Those that appear with higher frequency in the training data will be randomly down-sampled; useful range is (0, 1e-5)      | 1e-3 |\n| hs | 1 = use  Hierarchical Softmax | 0 |\n| negative | number of negative examples; common values are 3 - 10 (0 = not used) | 5 |\n| threads | number of used threads | 12 |\n| iter | number of training iterations | 5 |\n| minCount | \tThis will discard words that appear less than *minCount* times | 5 |\n| alpha | sets the starting learning rate  |  0.025 for skip-gram and 0.05 for CBOW |\n| classes | output word classes rather than word vectors | 0 (vectors are written) |\n| debug | sets debug mode  | 2 |\n| binary | save the resulting vectors in binary mode | 0 (off) |\n| saveVocab | the vocabulary will be saved to *saveVocab* value |  |\n| readVocab | the vocabulary will be read from *readVocab* value , not constructed from the training data | |\n| cbow | use the continuous bag of words model | 1 (use 0 for skip-gram model) |\n| silent | sets whether any output should be printed to the console | false |\n\nAfter successful execution, the supplied `callback` function is invoked. It receives the number of the exit code as its first parameter.\n\n### .loadModel( file, callback )\n\nThis is the main function of the package, which loads a saved model file containing vector representations of words into memory. Such a file can be created by using the *word2vec* function. After the file is successfully loaded, the supplied callback function is fired, which following conventions has two parameters: `err` and `model`. If everything runs smoothly and no error occured, the first argument should be `null`. The `model` parameter is a model object holding all data and exposing the properties and methods explained in the *Model Object* section.\n\nExample:\n``` javascript\nw2v.loadModel( './vectors.txt', function( error, model ) {\n    console.log( model );\n});\n```\nSample Output:\n\n``` javascript\n{\n    getVectors: [Function],\n    distance: [Function: distance],\n    analogy: [Function: analogy],\n    words: '98331',\n    size: '200'\n}\n```\n\n## Model Object\n\n### Properties\n\n#### .words\n\nNumber of unique words in the training corpus.\n\n#### .size\n\nLength of the learned word vectors.\n\n### Methods\n\n#### .similarity( word1, word2 )\nCalculates the word similarity between `word1` and `word2`.\n\nExample:\n``` javascript\nmodel.similarity( 'ham', 'cheese' );\n```\n\nSample Output:\n``` javascript\n0.4907762118841032\n```\n\n#### .mostSimilar( phrase[, number] )\n\nCalculates the cosine distance between the supplied phrase (a `string` which is internally converted to an Array of words, which result in a *phrase vector*) and the other word vectors of the vocabulary. Returned are the `number` words with the highest similarity to the supplied phrase. If `number` is not supplied, by default the *40* highest scoring words are returned. If none of the words in the phrase appears in the dictionary, the function returns `null`. In all other cases, unknown words will be dropped in the computation of the cosine distance.\n\nExample:\n``` javascript\nmodel.mostSimilar( 'switzerland', 20 );\n```\nSample Output:\n\n``` javascript\n[\n    { word: 'chur', dist: 0.6070252929307018 },\n    { word: 'ticino', dist: 0.6049085549621765 },\n    { word: 'bern', dist: 0.6001648890419077 },\n    { word: 'cantons', dist: 0.5822226582323267 },\n    { word: 'z_rich', dist: 0.5671853621346818 },\n    { word: 'iceland_norway', dist: 0.5651901750812693 },\n    { word: 'aargau', dist: 0.5590524831511438 },\n    { word: 'aarau', dist: 0.555220055372284 },\n    { word: 'zurich', dist: 0.5401119092258485 },\n    { word: 'berne', dist: 0.5391358099043649 },\n    { word: 'zug', dist: 0.5375590160292268 },\n    { word: 'swiss_confederation', dist: 0.5365824598661265 },\n    { word: 'germany', dist: 0.5337325187293028 },\n    { word: 'italy', dist: 0.5309218588704736 },\n    { word: 'alsace_lorraine', dist: 0.5270204106304165 },\n    { word: 'belgium_denmark', dist: 0.5247942780963807 },\n    { word: 'sweden_finland', dist: 0.5241634037188426 },\n    { word: 'canton', dist: 0.5212495170066538 },\n    { word: 'anterselva', dist: 0.5186651140386938 },\n    { word: 'belgium', dist: 0.5150383129735169 }\n]\n```\n\n#### .analogy( word, pair[, number] )\n\nFor a pair of words in a relationship such as `man` and `king`, this function tries to find the term which stands in an analogous relationship to the supplied `word`. If `number` is not supplied, by default the *40* highest-scoring results are returned.\n\nExample:\n``` javascript\nmodel.analogy( 'woman', [ 'man', 'king' ], 10 );\n```\n\nSample Output:\n\n``` javascript\n[\n    { word: 'queen', dist: 0.5607083309028658 },\n    { word: 'queen_consort', dist: 0.510974781496456 },\n    { word: 'crowned_king', dist: 0.5060923120115347 },\n    { word: 'isabella', dist: 0.49319425034513376 },\n    { word: 'matilda', dist: 0.4931204901924969 },\n    { word: 'dagmar', dist: 0.4910608716969606 },\n    { word: 'sibylla', dist: 0.4832698899279795 },\n    { word: 'died_childless', dist: 0.47957251302898396 },\n    { word: 'charles_viii', dist: 0.4775804990655765 },\n    { word: 'melisende', dist: 0.47663194967001704 }\n]\n```\n\n#### .getVector( word )\n\nReturns the learned vector representations for the input `word`. If `word` does not exist in the vocabulary, the function returns `null`.\n\nExample:\n``` javascript\nmodel.getVector( 'king' );\n```\n\nSample Output:\n``` javascript\n{\n    word: 'king',\n    values: [\n        0.006371254151248689,\n        -0.04533821363410406,\n        0.1589142808632736,\n        ...\n        0.042080221123209825,\n        -0.038347102017109225\n    ]\n}\n```\n\n#### .getVectors( [words] )\n\nReturns the learned vector representations for the supplied words. If *words* is undefined, i.e. the function is evoked without passing it any arguments, it returns the vectors for all learned words. The returned value is an `array` of objects which are instances of the class `WordVec`.\n\nExample:\n``` javascript\nmodel.getVectors( [ 'king', 'queen', 'boy', 'girl' ] );\n```\n\nSample Output:\n``` javascript\n[\n    {\n        word: 'king',\n        values: [\n            0.006371254151248689,\n            -0.04533821363410406,\n            0.1589142808632736,\n            ...\n            0.042080221123209825,\n            -0.038347102017109225\n        ]\n    },\n    {\n        word: 'queen',\n        values: [\n            0.014399041122817985,\n            -0.000026896638109750347,\n            0.20398248693190596,\n            ...\n            -0.05329081648586445,\n            -0.012556868376422963\n        ]\n    },\n    {\n        word: 'girl',\n        values: [\n            -0.1247347144692245,\n            0.03834108759049417,\n            -0.022911846734360187,\n            ...\n            -0.0798994867922872,\n            -0.11387393949666696\n        ]\n    },\n    {\n        word: 'boy',\n        values: [\n            -0.05436531234037158,\n            0.008874993957578164,\n            -0.06711992414442335,\n            ...\n            0.05673998568026764,\n            -0.04885347925837509\n        ]\n    }\n]\n```\n\n#### .getNearestWord( vec )\n\nReturns the word which has the closest vector representation to the input `vec`. The function expects a word vector, either an instance of constructor `WordVector` or an array of Number values of length `size`. It returns the word in the vocabulary for which the distance between its vector and the supplied input vector is lowest.\n\nExample:\n``` javascript\nmodel.getNearestWord( model.getVector('empire') );\n```\n\nSample Output:\n``` javascript\n{ word: 'empire', dist: 1.0000000000000002 }\n```\n\n#### .getNearestWords( vec[, number] )\n\nReturns the words whose vector representations are closest to input `vec`. The first parameter of the function expects a word vector, either an instance of constructor `WordVector` or an array of Number values of length `size`. The second parameter, `number`, is optional and specifies the number of returned words. If not supplied, a default value of `10` is used.\n\nExample:\n``` javascript\nmodel.getNearestWords( model.getVector( 'man' ), 3 )\n```\n\nSample Output:\n``` javascript\n[\n    { word: 'man', dist: 1.0000000000000002 },\n    { word: 'woman', dist: 0.5731114915085445 },\n    { word: 'boy', dist: 0.49110060323870924 }\n]\n```\n\n## WordVector\n\n### Properties\n\n#### .word\nThe word in the vocabulary.\n\n#### .values\nThe learned vector representation for the word, an array of length `size`.\n\n### Methods\n\n#### .add( wordVector )\nAdds the vector of the input `wordVector` to the vector `.values`.\n\n#### .subtract( wordVector )\nSubtracts the vector of the input `wordVector` to the vector `.values`.\n\n## Unit Tests\n\nRun tests via the command `npm test`\n\n## Build from Source\n\nClone the git repository with the command\n\n``` bash\n$ git clone https://github.com/Planeshifter/node-word2vec.git\n```\n\nChange into the project directory and compile the C source files via\n\n``` bash\n$ cd node-word2vec\n$ make --directory=src\n```\n\n---\n## License\n\n[Apache v2](http://www.apache.org/licenses/LICENSE-2.0).\n\n[npm-image]: https://badge.fury.io/js/word2vec.svg\n[npm-url]: http://badge.fury.io/js/word2vec\n\n[travis-image]: https://travis-ci.org/Planeshifter/node-word2vec.svg\n[travis-url]: https://travis-ci.org/Planeshifter/node-word2vec\n\n[codecov-image]: https://img.shields.io/codecov/c/github/Planeshifter/node-word2vec/master.svg\n[codecov-url]: https://codecov.io/github/Planeshifter/node-word2vec?branch=master\n\n[dependencies-image]: https://david-dm.org/Planeshifter/node-word2vec.svg\n[dependencies-url]: https://david-dm.org/Planeshifter/node-word2vec\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fplaneshifter%2Fnode-word2vec","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fplaneshifter%2Fnode-word2vec","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fplaneshifter%2Fnode-word2vec/lists"}