summarizer/
lib.rs

1mod tokenizer;
2mod summarizer;
3
4use self::summarizer::Summarizer;
5
6pub fn summarize( text: &str , reduction_factor: f32 ) -> String {
7    Summarizer::compute(text, reduction_factor)
8}
9
10pub fn par_summarize( text: &str , reduction_factor: f32 ) -> String {
11    Summarizer::par_compute(text, reduction_factor)
12}
13
14/// functions exposing Rust methods as C interfaces
15/// These methods are accessible with the ABI (compiled object code)
16mod c_binding {
17
18    use std::ffi::CString;
19    use crate::summarizer::Summarizer;
20
21    #[no_mangle]
22    pub extern "C" fn summarize( text: *const u8 , length: usize , reduction_factor: f32 ) -> *const u8 {
23        unsafe {
24            match std::str::from_utf8( std::slice::from_raw_parts( text , length ) ) {
25                Ok( text ) => {
26                    let summary = Summarizer::compute(text, reduction_factor) ;
27                    let c_summary = CString::new( summary ).unwrap() ;
28                    let c_summary_ptr = c_summary.as_ptr() ; 
29
30                    // Eliminate `c_summary` from reference/ownership tracking
31                    // hence transferring the ownership to the calling program
32                    std::mem::forget( c_summary );
33
34                    c_summary_ptr.cast::<u8>()
35                } , 
36                Err( e ) => {
37                    // Return an empty string as a summary if error occurred
38                    let c_summary = CString::new( e.to_string() ).unwrap() ;
39                    c_summary.as_ptr().cast::<u8>()
40                }
41            }
42        }    
43    }
44
45    #[no_mangle]
46    pub extern "C" fn par_summarize( text: *const u8 , length: usize , reduction_factor: f32 ) -> *const u8 {
47        unsafe {
48            match std::str::from_utf8( std::slice::from_raw_parts( text , length ) ) {
49                Ok( text ) => {
50                    let summary = Summarizer::par_compute(text, reduction_factor) ;
51                    let c_summary = CString::new( summary ).unwrap() ;
52                    let c_summary_ptr = c_summary.as_ptr() ; 
53
54                    // Eliminate `c_summary` from reference/ownership tracking
55                    // hence transferring the ownership to the calling program
56                    std::mem::forget( c_summary ) ;
57                    
58                    c_summary_ptr.cast::<u8>()
59                } , 
60                Err( e ) => {
61                    let c_summary = CString::new( e.to_string() ).unwrap() ;
62                    c_summary.as_ptr().cast::<u8>()
63                }
64            }
65        }    
66    }
67
68}
69
70/// JNI methods for using in Android
71/// `Cargo.toml` has a conditional dependence of `jni` for this module
72#[cfg(feature="android")]
73mod android {
74
75    extern crate jni ; 
76    use jni::objects::{JClass, JString};
77    use jni::sys::jfloat;
78    use jni::JNIEnv;
79    use crate::summarize ;
80    use crate::par_summarize ;
81
82    #[no_mangle]
83    pub extern "C" fn Java_com_projects_ml_summarizer_Summarizer_summarize<'a>(
84        mut env: JNIEnv<'a>,
85        _: JClass<'a>,
86        text: JString<'a>,
87        reduction_factor: jfloat
88    ) -> JString<'a> {
89        let text: String = env
90            .get_string(&text)
91            .expect("Could not open text in summarize")
92            .into();
93        let summary = summarize( text.as_str() , reduction_factor ) ; 
94        let output = env
95            .new_string( summary )
96            .expect("Could not create output string");
97        output
98    }
99
100    #[no_mangle]
101    pub extern "C" fn Java_com_projects_ml_summarizer_Summarizer_parallelSummarize<'a>(
102        mut env: JNIEnv<'a>,
103        _: JClass<'a>,
104        text: JString<'a>,
105        reduction_factor: jfloat
106    ) -> JString<'a> {
107        let text: String = env
108            .get_string(&text)
109            .expect("Could not open text in par_summarize")
110            .into();
111        let summary = par_summarize( text.as_str() , reduction_factor ) ; 
112        let output = env
113            .new_string( summary )
114            .expect("Could not create output string");
115        output
116    }
117
118}