cortical_io/
lib.rs

1extern crate core;
2
3use serde::{Deserialize, Serialize};
4
5#[cfg(feature = "client")]
6pub use client::Cortical;
7
8use crate::similarity::FingerprintSimilarity;
9
10pub mod similarity;
11
12#[cfg(feature = "image")]
13pub mod image;
14#[cfg(feature = "client")]
15pub mod client;
16pub mod density;
17pub mod find_peaks;
18
19#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
20pub struct Retina {
21    #[serde(rename = "retinaName")]
22    pub retina_name: String,
23    #[serde(rename = "numberOfColumns")]
24    pub number_of_columns: u32,
25    #[serde(rename = "numberOfTermsInRetina")]
26    pub number_of_terms_in_retina: u64,
27    pub description: String,
28    #[serde(rename = "numberOfRows")]
29    pub number_of_rows: u32,
30}
31
32#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
33pub struct Fingerprint {
34    pub positions: Vec<u32>,
35}
36
37impl Fingerprint {
38    pub fn expand(&self, len: usize) -> Vec<u8> {
39        let mut expanded = vec![0; len];
40
41        for pos in self.positions.iter() {
42            expanded[*pos as usize] = 1;
43        }
44
45        expanded
46    }
47
48    pub fn expand_t<T: num::Float>(&self, len: usize) -> Vec<T> {
49        let zero = T::from(0).unwrap();
50        let one = T::from(1).unwrap();
51
52        let mut expanded: Vec<T> = vec![zero; len];
53
54        for pos in self.positions.iter() {
55            expanded[*pos as usize] = one;
56        }
57
58        expanded
59    }
60
61    pub fn compare(&self, other: &Fingerprint) -> FingerprintSimilarity {
62        FingerprintSimilarity::new(self, other)
63    }
64}
65
66impl From<Fingerprint> for Vec<f64> {
67    fn from(fingerprint: Fingerprint) -> Self {
68        fingerprint
69            .expand_t::<f64>(128 * 128)
70    }
71}
72
73#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
74pub struct TextSlice {
75    pub text: String,
76    pub fingerprint: Option<Fingerprint>,
77}
78
79#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
80pub struct TextSliceRequest {
81    pub retina_name: String,
82    pub start_index: usize,
83    pub max_results: usize,
84    pub get_fingerprint: bool,
85}
86
87impl Default for TextSliceRequest {
88    fn default() -> Self {
89        Self {
90            retina_name: "en_general".to_string(),
91            start_index: 0,
92            max_results: 10,
93            get_fingerprint: false,
94        }
95    }
96}
97
98impl TextSliceRequest {
99    pub fn new() -> Self {
100        Self::default()
101    }
102
103    pub fn with_retina_name(mut self, retina_name: &str) -> Self {
104        self.retina_name = retina_name.to_string();
105        self
106    }
107
108    pub fn with_start_index(mut self, start_index: usize) -> Self {
109        self.start_index = start_index;
110        self
111    }
112
113    pub fn with_max_results(mut self, max_results: usize) -> Self {
114        self.max_results = max_results;
115        self
116    }
117
118    pub fn with_get_fingerprint(mut self, get_fingerprint: bool) -> Self {
119        self.get_fingerprint = get_fingerprint;
120        self
121    }
122}
123
124#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
125pub enum PosType {
126    #[serde(rename = "ADJECTIVE")]
127    Adjective,
128    #[serde(rename = "NOUN")]
129    Noun,
130    #[serde(rename = "NUMBER")]
131    Number,
132    #[serde(rename = "SYMBOL")]
133    Symbol,
134    #[serde(rename = "VERB")]
135    Verb,
136    #[serde(rename = "UNKNOWN")]
137    Unknown,
138}
139
140#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
141pub struct TextEnvelope {
142    pub text: String,
143}
144
145impl TextEnvelope {
146    pub fn new(text: &str) -> Self {
147        Self {
148            text: text.to_string(),
149        }
150    }
151}
152
153#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
154pub struct CreateCategoryFilterRequest {
155    #[serde(rename = "categoryName")]
156    pub category_name: Option<String>,
157    #[serde(rename = "positiveExamples")]
158    pub positive_examples: Vec<TextEnvelope>,
159    #[serde(rename = "negativeExamples")]
160    pub negative_examples: Vec<TextEnvelope>,
161}
162
163#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
164pub struct CreateCategoryFilterResponse {
165    #[serde(rename = "categoryName")]
166    pub category_name: String,
167    pub positions: Vec<u32>,
168}
169
170#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
171pub struct LanguageResponse {
172    pub language: Option<String>,
173    pub iso_tag: Option<String>,
174    pub wiki_url: Option<String>,
175}
176
177#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
178pub struct CompareResponse {
179    #[serde(rename = "sizeLeft")]
180    pub size_left: u32,
181    #[serde(rename = "sizeRight")]
182    pub size_right: u32,
183    #[serde(rename = "weightedScoring")]
184    pub weighted_scoring: f64,
185    #[serde(rename = "euclideanDistance")]
186    pub euclidean_distance: f64,
187    #[serde(rename = "jaccardDistance")]
188    pub jaccard_distance: f64,
189    #[serde(rename = "overlappingAll")]
190    pub overlapping_all: u32,
191    #[serde(rename = "overlappingLeftRight")]
192    pub overlapping_left_right: f64,
193    #[serde(rename = "overlappingRightLeft")]
194    pub overlapping_right_left: f64,
195    #[serde(rename = "cosineSimilarity")]
196    pub cosine_similarity: f64,
197}
198
199#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
200pub struct GetTermsRequest {
201    pub retina_name: String,
202    pub term: Option<String>,
203    pub start_index: Option<u32>,
204    pub max_results: Option<u32>,
205    pub get_fingerprint: bool,
206}
207
208#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
209pub struct Term {
210    pub pos_types: Option<Vec<PosType>>,
211    pub df: Option<f64>,
212    pub score: Option<f64>,
213    pub fingerprint: Option<Fingerprint>,
214    pub term: Option<String>,
215}
216
217pub type GetTermsResponse = Vec<Term>;
218
219#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
220pub struct GetTermsContextsRequest {
221    pub retina_name: String,
222    pub term: String,
223    pub start_index: Option<u32>,
224    pub max_results: Option<u32>,
225    pub get_fingerprint: bool,
226}
227
228#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
229pub struct TermContext {
230    pub fingerprint: Option<Fingerprint>,
231    pub context: Option<String>,
232    pub context_label: Option<String>,
233}
234
235pub type GetTermsContextsResponse = Vec<TermContext>;
236
237#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
238pub struct GetTermsSimilarTermsRequest {
239    pub retina_name: String,
240    pub term: String,
241    pub context_id: Option<String>,
242    pub pos_type: Option<PosType>,
243    pub start_index: Option<u32>,
244    pub max_results: Option<u32>,
245    pub get_fingerprint: bool,
246}