vector-space-model-similarity
v1.0.161
Published
this module is using for calculating the cosine similarity and vector space model using tfidf
Downloads
9
Maintainers
Readme
Library for calculate vector space model using cosine similarity.. for now i test this library using indonesian languange. so i didn't testing it with english data. but i was added english lemmatize document. but maybe i will change it to porter algorithm
How to install
npm install vector-space-model-similarity --save
How to use
first we import our function
import { VSM } from 'vector-space-model-similarity
next we define documents, it's an array
const documents = ["rumah saya penuh makanan", "saya suka makan nasi", "nasi berawal dari beras"] // define our variable
next we call VSM Class and define our object from VSM class
const document = new VSM(documents); // define our object of VSM
const idf = document.getIdfVectorized(); // return an array ob object, the key is tokenize our documents and the value is the
const query = new VSM(["sistem cerdas"], idf); // we define our object again, it's for query. and we pass our idf constant variable
const cosine = Cosine(query.getPowWeightVectorized()[0], document.getPowWeightVectorized()); // calculating cosine similarity
descriptions
example of vsm calculating using excel.
VSM
import { VSM } from 'vector-space-model-similarity
VSM is a class that extends from Tfidf class. VSM has one constructor and in the constructor it has two parameter. the first parameter is an important parameter and the second is optional. it's the parameter
documents: string[], idfVector:any[] = []
documents represented about our document, and idfVector is the idf from our vector of IDF number. idfVector is important if you want to search data from query. you must pass idfVector from the idf you got from documents before. to get idfVector use this function.
getIdfVectorized will return this array. but not array of number, it's array of object. the key is the word and the value is the IDF value
getIdfVectorized() // <-- this is method from TFIDF Class.
getWeightVectorized() will return idf value. and the return is an multidimension array
getWeightVectorized() // <-- return weight of documents
getPowWeightVectorized() will return Exponent of IDF from the documents
getPowWeightVectorized() // <-- return weight of documents
Cosine
when you was got documents and query vector idf you can use this function
import { Cosine } from 'vector-space-model-similarity
Cosine library has two parameters. the first paramter is a query, and the second is a documents
queries:any[], documents:any[][] // <=== the parameters
number[] // <=== the return
after you get exponent of document idf from getPowWeightVectorized() you can use this function
query is single dimension of array, and documents is a multi dimension of array. becasue getPowWeightVectorized() return multidimension array and the query parameter required singledimension of array you must pass the first index of your array. e.g :
const document = new VSM([
"sistem cerdas adalah kumpulan elemen",
"adalah kumpulan elemen yang saling berinteraksi",
"Sistem berinteraksi untuk mencapai tujuan"
]);
const idf = document.getIdfVectorized();
const query = new VSM(["sistem cerdas"], idf);
const cosine = Cosine(query.getPowWeightVectorized()[0], document.getPowWeightVectorized()); // output : [ 4.457087767265072, 0, 0.4853443577859814 ]