news-deja-vu
Python package for News Deja Vu
News Deja Vu is a novel semantic search tool that leverages transformer large language models and a bi-encoder approach to identify historical news articles that share semantic similarities with modern news queries. News Déjà Vu first recognizes and masks entities, in order to focus on broader parallels rather than the specific named entities being discussed. Then, a contrastively trained, lightweight bi-encoder retrieves historical articles that are most similar semantically to a modern query.
Example Usage:
ner_model = 'bert-base-NER'
same_story_model = 'dell-research-harvard/same-story'
# Download historic news articles
corpus = download('american stories:1840')
# Perform NER inference
ner_output = ner_and_mask(corpus, ner_model, batch_size = batch_size)
# Embed with biencoder
embeddings = embed(ner_output, same_story_model)
# NER inference for query sentences
query_masked_input = ner_and_mask(sample_query_sentences, ner_model, batch_size = batch_size)
# Embed query sentences
query_embeddings = embed(query_masked_input, same_story_model)
# Search for closest matches in historical corpus
dist_list, nn_list = find_nearest_neighbours(query_embeddings, embeddings, k=1)
# Output results
results_dict = {i: {"query": sample_query_sentences[i], "neighbor": dataset[nn_list[i]]} for i in range(len(sample_query_sentences))}
with open('data/test_data/query_results_1840.json', 'w') as f:
json.dump(results_dict, f, indent = 4, default=str)
Or, in much simpler form:
corpus = download('american stories:1840')
results = search_same_story(sample_query_sentences, corpus, ner_model, same_story_model, k = 1)
Outputs are query texts matched with their nearest matches in the historical corpus.
Release files for newsdejavu 0.0.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| newsdejavu-0.0.3.tar.gz | 15.4 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| newsdejavu-0.0.3-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 31.4 kB
Release files / newsdejavu-0.0.3.tar.gz
| Download URL | newsdejavu-0.0.3.tar.gz |
|---|---|
| Size | 15.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
521712c094eb6acefc09350a4844c67cb9e44fda89dfa9c2e85d62f5914c1100
|
|
BLAKE2b-256 checksum How to use checksums |
c8cc96822ca808086857f31c9756085f6eb2edb1fe2dd2070aa6865c1d3428e6
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.1.1 pkginfo/1.4.2 requests/2.22.0 setuptools/45.2.0 requests-toolbelt/0.8.0 tqdm/4.64.1 CPython/3.8.10
|
Release files / newsdejavu-0.0.3-py3-none-any.whl
| Download URL | newsdejavu-0.0.3-py3-none-any.whl |
|---|---|
| Size | 16.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
d48e47f2252322939a4cca573021beea5c2fb87debb962494f701211d7dcc374
|
|
BLAKE2b-256 checksum How to use checksums |
e051e7df208117da324272e2fa3bb8de742a159f31f5e7b8eb003145293c8fe2
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.1.1 pkginfo/1.4.2 requests/2.22.0 setuptools/45.2.0 requests-toolbelt/0.8.0 tqdm/4.64.1 CPython/3.8.10
|