sophia_interfaces/
interpreter.rs

1
2use std::collections::HashMap;
3use std::fmt;
4use serde::{Serialize, Deserialize};
5use std::ffi::{CString, NulError};
6use std::os::raw::{c_char, c_uint};
7use std::ptr::null_mut;
8use super::OutputToken;
9
10#[derive(Serialize, Deserialize)]
11pub struct InterpretedOutput {
12    pub processing_time_ms: u128,
13    pub total_tokens: usize,
14    pub tokens: Vec<OutputToken>,
15    pub mwe: Vec<OutputToken>,
16    pub phrases: Vec<OutputPhrase>,
17    pub scores: HashMap<String, f32>,
18    pub audit: String,
19    //pub audit: Vec<OutputAuditPhrase>
20}
21
22#[derive(Serialize, Deserialize)]
23pub struct OutputPhrase {
24    pub start: usize,
25    pub end: usize,
26    pub split_token: Option<usize>,
27    pub nouns: Vec<OutputNoun>,
28    pub verbs: Vec<OutputVerb >,
29    pub tense: String,
30    pub person: String,
31    pub classification: String,
32    pub intent: String,
33}
34
35#[derive(Serialize, Deserialize)]
36pub struct OutputNoun {
37    pub head: usize,
38    pub compound_elements: Vec<usize>,
39    pub modifiers: Vec<OutputNounModifier>,
40    pub siblings: Vec<OutputNounSibling>,
41    pub owner: String,
42    pub prepositions: Vec<usize>,
43    pub determiners: Vec<usize>,
44    pub adjectives: Vec<OutputAdjective>
45}
46
47#[derive(Serialize, Deserialize)]
48pub struct OutputNounModifier {
49    pub position: usize,
50    pub compound_elements: Vec<usize>,
51    pub siblings: Vec<OutputNounSibling>,
52    pub prepositions: Vec<usize>,
53    pub determiners: Vec<usize>,
54    pub adjectives: Vec<OutputAdjective>
55}
56
57#[derive(Serialize, Deserialize)]
58pub struct OutputNounSibling {
59    pub position: usize,
60    pub is_excluded: bool,
61    pub determiners: Vec<usize>,
62    pub adjectives: Vec<OutputAdjective>,
63    pub seperators: Vec<usize>
64}
65
66#[derive(Serialize, Deserialize)]
67pub struct OutputVerb {
68    pub head: usize,
69    pub objects: Vec<usize>,
70    pub modifiers: Vec<OutputVerbModifier>,
71    pub siblings: Vec<OutputVerbSibling>,
72    pub auxillary_verbs: Vec<usize>,
73    pub prepositions: Vec<usize>,
74    pub determiners: Vec<usize>,
75    pub adverbs: Vec<usize>
76}
77
78#[derive(Serialize, Deserialize)]
79pub struct OutputVerbModifier {
80    pub position: usize,
81    pub objects: Vec<usize>,
82    pub auxillary_verbs: Vec<usize>,
83    pub prepositions: Vec<usize>,
84    pub adverbs: Vec<usize>
85}
86
87#[derive(Serialize, Deserialize)]
88pub struct OutputVerbSibling {
89    pub position: usize,
90    pub objects: Vec<usize>,
91    pub seperators: Vec<usize>
92}
93
94
95#[derive(Serialize, Deserialize)]
96pub struct OutputAdjective {
97    pub position: usize,
98    pub adverbs: Vec<usize>,
99    pub predicative_verbs: Vec<usize>
100}
101
102#[derive(Serialize, Deserialize)]
103pub struct OutputAuditPhrase {
104    pub phrase: String,
105    pub tense: String,
106    pub person: String,
107    pub classification: String,
108    pub intent: String,
109    pub nouns: Vec<OutputAuditItem>,
110    pub verbs: Vec<OutputAuditItem>
111}
112
113#[derive(Serialize, Deserialize)]
114pub struct OutputAuditItem {
115    pub word: String,
116    pub children: Vec<String>
117}
118
119impl fmt::Display for OutputAuditPhrase {
120    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
121        let mut res = vec![format!("Phrase: {}", self.phrase.to_string())];
122        //res.push(format!("    tense: {}, person: {}, classification: {}", self.tense, self.person, self.classification));
123        res.push(format!("    Intent: {}", self.intent));
124        for noun in self.nouns.iter() {
125            res.push(format!("Noun: {}", noun.word));
126            for child in noun.children.iter() {
127            let cline = if child.starts_with("mod sib") { format!("        {}", child) } else { format!("    {}", child) };
128                res.push(cline);
129            }
130        }
131
132        for verb in self.verbs.iter() {
133            res.push(format!("Verb: {}", verb.word));
134            for child in verb.children.iter() {
135                res.push(format!("    {}", child));
136            }
137        }
138
139        write!(f, "{}", res.join("\n").to_string())
140    }
141}
142
143
144