Crates.io | rnltk |
lib.rs | rnltk |
version | 0.4.0 |
source | src |
created_at | 2022-10-29 02:36:40.425679 |
updated_at | 2023-04-21 13:12:30.610356 |
description | Natural Language Toolkit for Rust |
homepage | |
repository | https://github.com/michael-long88/rnltk/ |
max_upload_size | |
id | 700796 |
size | 486,961 |
This crate is designed to create a general tooklit for natural language processing, a current deficiency in the Rust ecosystem.
Project can be found on crates.io.
Check out the examples folder to see how to create a sentiment lexicon and get the arousal level for a term.
The sentiment analysis was originally designed by Dr. Christopher Healey and then ported to Rust for the purpose of this project.
Basic tokenization is supported right now (string to sentences, string to tokens, term frequencies), but there are plans to expand this to include stop word removal as well.
Stemming currently uses modified code from rust-stem, but this may switch to the rust-stemmers crate after further research.
More information on the stemming algorithm can be found here.
Term frequency–inverse document frequency (TF-IDF) is an algorithm used to find document similarity. Creating a TF-IDF matrix takes place over two steps:
The weighted, normalized matrix can then be used to find the cosine similarity between documents. Normally, calculating the cosine similarity of two document vectors would look like $\cos(\theta) = \frac{D_i \cdot D_j}{|D_i| |D_j|}$. Since the matrix is already normalized, this simplifies to $\cos(\theta) = D_i \cdot D_j$.
The resulting $MxM$ matrix, where $M$ is the number of columns from the TF-IDF matrix, has 1's along the diagonal since the similarity of a document with itself is 1. The intersections of rows and columns, $M_{i,j}$, is the cosine similarity value between $D_i$ and $D_j$.
Latent Semantic Analysis (LSA) finds document similarity based on the idea of concepts. LSA starts with the $m \times n$ TF-IDF matrix and uses Singular Value Decomposition (SVD) to reduce dimensionality of the matrix. The $k$ largest singular values are chosen to produce a reduced ${V_k}^T$ matrix, with $1 \le k \le n$. Each document column in the ${V_k}^T$ matrix is normalized and then we dot product them together. To shift the resulting dot product from a range of [-1...-1] to [0...1], we add 1 to the dot product and then divide by 2 ($\frac{1 + \cos(\theta)}{2}$).
The resulting $MxM$ matrix, where $M$ is the number of columns from the TF-IDF matrix, has 1's along the diagonal since the similarity of a document with itself is 1. The intersections of rows and columns, $M_{i,j}$, is the cosine similarity value between $D_i$ and $D_j$.