Giter Club home page Giter Club logo

document-similarity's Introduction

Document Similarity using Word2Vec

Calculate the similarity distance between documents using pre-trained word2vec model.

Usage

  • Load a pre-trained word2vec model. Note: You can use Google's pre-trained word2vec model, if you don't have one.

    from gensim.models.keyedvectors import KeyedVectors
    model_path = './data/GoogleNews-vectors-negative300.bin'
    w2v_model = KeyedVectors.load_word2vec_format(model_path, binary=True)
  • Once the model is loaded, it can be passed to DocSim class to calculate document similarities.

    from DocSim import DocSim
    ds = DocSim(w2v_model)
  • Calculate the similarity score between a source document & a list of target documents.

    source_doc = 'how to delete an invoice'
    target_docs = ['delete a invoice', 'how do i remove an invoice', 'purge an invoice']
    
    # This will return 3 target docs with similarity score
    sim_scores = ds.calculate_similarity(source_doc, target_docs)
    
    print(sim_scores)
  • Output is as follows:

      [ {'score': 0.99999994, 'doc': 'delete a invoice'}, 
      {'score': 0.79869318, 'doc': 'how do i remove an invoice'}, 
      {'score': 0.71488398, 'doc': 'purge an invoice'} ]
  • Note: You can optionally pass a threshold argument to the calculate_similarity() method to return only the target documents with similarity score above the threshold.

    sim_scores = ds.calculate_similarity(source_doc, target_docs, threshold=0.7)

Requirements

  • Python 3 only
  • gensim : to load the word2vec model
  • numpy : to calculate similarity scores

License

The MIT License

document-similarity's People

Contributors

v1shwa avatar rafaeelaudibert avatar pragatiahuja avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.