Skip to content
This repository has been archived by the owner on Jun 3, 2024. It is now read-only.

Commit

Permalink
fix: embbed tagger assets in binary
Browse files Browse the repository at this point in the history
  • Loading branch information
oknozor committed Oct 27, 2023
1 parent 6259ea4 commit 5853b80
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 67 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ jobs:
MIMIR__ELASTICSEARCH__URL: 'http://es1:9200'
with:
command: llvm-cov
args: --workspace --lcov --output-path lcov.info -- --nocapture
args: --workspace --lcov --output-path lcov.info

- name: Upload coverage to Codecov
uses: codecov/codecov-action@v3
Expand Down
1 change: 1 addition & 0 deletions crates/libs/tagger/src/tagger/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ pub enum AddressTag {
}

impl Tagger for AddressTagger {
const ASSETS: &'static str = "";
type Output = Result<Option<AddressTag>, TaggerError>;
fn tag(&self, input: &str, _: Option<u32>) -> Self::Output {
let mut tag = None;
Expand Down
19 changes: 5 additions & 14 deletions crates/libs/tagger/src/tagger/brand.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
use crate::tagger::{Tagger, TaggerAutocomplete};
use crate::ASSETS_PATH;
use bk_tree::BKTree;
use once_cell::sync::Lazy;
use serde::Deserialize;
use std::fs;
use std::path::PathBuf;
use trie_rs::{Trie, TrieBuilder};

use crate::tagger::{Tagger, TaggerAutocomplete};

pub static BRAND_AUTOCOMPLETE_TAGGER: Lazy<BrandAutocompleteTagger> = Lazy::new(|| {
let assets =
ASSETS_PATH.get_or_init(|| PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("assets"));
let path = assets.join("brand.json");
let brands = fs::read(path).expect("brands data");
let brands: Vec<Brand> = serde_json::from_slice(&brands).expect("json brand data");
let brands: Vec<Brand> = serde_json::from_str(BrandTagger::ASSETS).expect("json brand data");
let mut tree = TrieBuilder::new();

brands
Expand Down Expand Up @@ -42,11 +36,7 @@ impl TaggerAutocomplete for BrandAutocompleteTagger {
}

pub static BRAND_TAGGER: Lazy<BrandTagger> = Lazy::new(|| {
let assets =
ASSETS_PATH.get_or_init(|| PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("assets"));
let path = assets.join("brand.json");
let brands = fs::read(path).expect("brands data");
let brands: Vec<Brand> = serde_json::from_slice(&brands).expect("json brand data");
let brands: Vec<Brand> = serde_json::from_str(BrandTagger::ASSETS).expect("json brand data");
let mut tree = BKTree::default();

brands
Expand All @@ -72,6 +62,7 @@ pub struct BrandTagger {
}

impl Tagger for BrandTagger {
const ASSETS: &'static str = include_str!("../../assets/brand.json");
type Output = bool;
fn tag(&self, input: &str, tolerance: Option<u32>) -> bool {
self.inner
Expand Down
22 changes: 7 additions & 15 deletions crates/libs/tagger/src/tagger/category.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@
use crate::tagger::{Tagger, TaggerAutocomplete};
use crate::ASSETS_PATH;
use std::collections::HashMap;

use bk_tree::BKTree;
use once_cell::sync::Lazy;
use serde::Deserialize;
use std::collections::HashMap;
use std::fs;
use std::path::PathBuf;
use trie_rs::{Trie, TrieBuilder};

use crate::tagger::{Tagger, TaggerAutocomplete};

pub static CATEGORY_AUTOCOMPLETE_TAGGER: Lazy<CategoryAutocompleteTagger> = Lazy::new(|| {
let assets =
ASSETS_PATH.get_or_init(|| PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("assets"));
let path = assets.join("categories.json");
let categories = fs::read(path).expect("category data");
let categories: Vec<Category> =
serde_json::from_slice(&categories).expect("json category data");
serde_json::from_str(CategoryTagger::ASSETS).expect("json category data");
let mut keywords = TrieBuilder::new();
let mut category_map = HashMap::new();
for category in categories.into_iter() {
Expand Down Expand Up @@ -60,12 +55,8 @@ impl TaggerAutocomplete for CategoryAutocompleteTagger {
}

pub static CATEGORY_TAGGER: Lazy<CategoryTagger> = Lazy::new(|| {
let assets =
ASSETS_PATH.get_or_init(|| PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("assets"));
let path = assets.join("categories.json");
let categories = fs::read(path).expect("category data");
let categories: Vec<Category> =
serde_json::from_slice(&categories).expect("json category data");
serde_json::from_str(CategoryTagger::ASSETS).expect("json category data");
let mut keywords = BKTree::default();
let mut category_map = HashMap::new();
for category in categories.into_iter() {
Expand Down Expand Up @@ -104,6 +95,7 @@ pub struct CategoryTagger {
}

impl Tagger for CategoryTagger {
const ASSETS: &'static str = include_str!("../../assets/categories.json");
type Output = Option<String>;

fn tag(&self, input: &str, tolerance: Option<u32>) -> Self::Output {
Expand Down
62 changes: 25 additions & 37 deletions crates/libs/tagger/src/tagger/location.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
use crate::tagger::Tagger;
use crate::tokens::normalize_diacritics;
use crate::ASSETS_PATH;
use bk_tree::BKTree;
use once_cell::sync::Lazy;
use serde::Deserialize;
use std::fs;
use std::path::PathBuf;

use crate::tagger::Tagger;
use crate::tokens::normalize_diacritics;

pub static COUNTRY_TAGGER: Lazy<CountryTagger> = Lazy::new(|| {
let assets =
ASSETS_PATH.get_or_init(|| PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("assets"));
let path = assets.join("countries.json");
let locations = fs::read(path).expect("countries data");
let locations: Countries = serde_json::from_slice(&locations).expect("valid json locations");
let locations: Countries =
serde_json::from_str(CountryTagger::ASSETS).expect("valid json locations");

let mut tree = BKTree::default();

Expand All @@ -26,11 +21,8 @@ pub static COUNTRY_TAGGER: Lazy<CountryTagger> = Lazy::new(|| {
});

pub static STATE_TAGGER: Lazy<StateTagger> = Lazy::new(|| {
let assets =
ASSETS_PATH.get_or_init(|| PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("assets"));
let path = assets.join("states.json");
let locations = fs::read(path).expect("state data");
let locations: States = serde_json::from_slice(&locations).expect("valid json locations");
let locations: States =
serde_json::from_str(StateTagger::ASSETS).expect("valid json locations");

let mut tree = BKTree::default();

Expand All @@ -44,11 +36,8 @@ pub static STATE_TAGGER: Lazy<StateTagger> = Lazy::new(|| {
});

pub static DISTRICT_TAGGER: Lazy<DistrictTagger> = Lazy::new(|| {
let assets =
ASSETS_PATH.get_or_init(|| PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("assets"));
let path = assets.join("districts.json");
let locations = fs::read(path).expect("district data");
let locations: Districts = serde_json::from_slice(&locations).expect("valid json locations");
let locations: Districts =
serde_json::from_str(DistrictTagger::ASSETS).expect("valid json locations");

let mut tree = BKTree::default();

Expand All @@ -62,13 +51,7 @@ pub static DISTRICT_TAGGER: Lazy<DistrictTagger> = Lazy::new(|| {
});

pub static CITY_TAGGER: Lazy<CityTagger> = Lazy::new(|| {
let assets =
ASSETS_PATH.get_or_init(|| PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("assets"));
println!("LES ASSETS");
println!("{:?}", assets);
let path = assets.join("cities.json");
let locations = fs::read(path).expect("cities data");
let locations: Cities = serde_json::from_slice(&locations).expect("valid json locations");
let locations: Cities = serde_json::from_str(CityTagger::ASSETS).expect("valid json locations");

let mut tree = BKTree::default();

Expand All @@ -82,12 +65,8 @@ pub static CITY_TAGGER: Lazy<CityTagger> = Lazy::new(|| {
});

pub static CITY_DISTRICT_TAGGER: Lazy<CityDistrictTagger> = Lazy::new(|| {
let assets =
ASSETS_PATH.get_or_init(|| PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("assets"));
let path = assets.join("cities_districts.json");
let locations = fs::read(path).expect("city district data");
let locations: CitiesDistricts =
serde_json::from_slice(&locations).expect("valid json locations");
serde_json::from_str(CityDistrictTagger::ASSETS).expect("valid json locations");

let mut tree = BKTree::default();

Expand All @@ -104,11 +83,8 @@ pub static CITY_DISTRICT_TAGGER: Lazy<CityDistrictTagger> = Lazy::new(|| {
});

pub static SUBURBS_TAGGER: Lazy<SuburbTagger> = Lazy::new(|| {
let assets =
ASSETS_PATH.get_or_init(|| PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("assets"));
let path = assets.join("suburbs.json");
let locations = fs::read(path).expect("suburb data");
let locations: Suburbs = serde_json::from_slice(&locations).expect("valid json locations");
let locations: Suburbs =
serde_json::from_str(SuburbTagger::ASSETS).expect("valid json locations");

let mut tree = BKTree::default();

Expand Down Expand Up @@ -182,6 +158,8 @@ pub struct CountryTagger {
}

impl Tagger for CityTagger {
const ASSETS: &'static str = include_str!("../../assets/cities.json");

type Output = bool;
fn tag(&self, input: &str, tolerance: Option<u32>) -> bool {
self.inner
Expand All @@ -192,6 +170,8 @@ impl Tagger for CityTagger {
}

impl Tagger for StateTagger {
const ASSETS: &'static str = include_str!("../../assets/states.json");

type Output = bool;
fn tag(&self, input: &str, tolerance: Option<u32>) -> bool {
self.inner
Expand All @@ -202,6 +182,8 @@ impl Tagger for StateTagger {
}

impl Tagger for DistrictTagger {
const ASSETS: &'static str = include_str!("../../assets/districts.json");

type Output = bool;
fn tag(&self, input: &str, tolerance: Option<u32>) -> bool {
self.inner
Expand All @@ -212,6 +194,8 @@ impl Tagger for DistrictTagger {
}

impl Tagger for CityDistrictTagger {
const ASSETS: &'static str = include_str!("../../assets/cities_districts.json");

type Output = bool;
fn tag(&self, input: &str, tolerance: Option<u32>) -> bool {
self.inner
Expand All @@ -222,6 +206,8 @@ impl Tagger for CityDistrictTagger {
}

impl Tagger for SuburbTagger {
const ASSETS: &'static str = include_str!("../../assets/suburbs.json");

type Output = bool;
fn tag(&self, input: &str, tolerance: Option<u32>) -> bool {
self.inner
Expand All @@ -232,6 +218,8 @@ impl Tagger for SuburbTagger {
}

impl Tagger for CountryTagger {
const ASSETS: &'static str = include_str!("../../assets/countries.json");

type Output = bool;
fn tag(&self, input: &str, tolerance: Option<u32>) -> bool {
self.inner
Expand Down
1 change: 1 addition & 0 deletions crates/libs/tagger/src/tagger/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ pub trait TaggerAutocomplete {
/// Utility trait to implement tagging logic, not that the Output type can
/// be anything if additional info needs to be conveyed.
pub trait Tagger {
const ASSETS: &'static str;
type Output;
/// Apply implementor tagging with the given levenshtein distance.
fn tag(&self, input: &str, tolerance: Option<u32>) -> Self::Output;
Expand Down

0 comments on commit 5853b80

Please sign in to comment.