11use std:: collections:: { HashMap , HashSet } ;
22use std:: iter:: FromIterator ;
33use std:: ops:: Range ;
4- use std:: sync:: { Arc , Mutex } ;
54
65use itertools:: Itertools ;
76use regex:: Regex ;
@@ -26,31 +25,19 @@ lazy_static! {
2625}
2726
2827impl BuiltinEntityParser {
29- pub fn get ( lang : Language ) -> Arc < BuiltinEntityParser > {
30- lazy_static ! {
31- static ref CACHED_PARSERS : Mutex <HashMap <String , Arc <BuiltinEntityParser >>> =
32- Mutex :: new( HashMap :: new( ) ) ;
33- }
34-
35- CACHED_PARSERS
36- . lock ( )
37- . unwrap ( )
38- . entry ( lang. to_string ( ) )
39- . or_insert_with ( || {
40- let supported_entity_kinds = BuiltinEntityKind :: supported_entity_kinds ( lang) ;
41- let ordered_entity_kinds = OutputKind :: all ( )
42- . iter ( )
43- . map ( |output_kind| output_kind. into_builtin ( ) )
44- . filter ( |builtin_entity_kind| supported_entity_kinds. contains ( & builtin_entity_kind) )
45- . collect ( ) ;
28+ pub fn new ( lang : Language ) -> Self {
29+ let supported_entity_kinds = BuiltinEntityKind :: supported_entity_kinds ( lang) ;
30+ let ordered_entity_kinds = OutputKind :: all ( )
31+ . iter ( )
32+ . map ( |output_kind| output_kind. into_builtin ( ) )
33+ . filter ( |builtin_entity_kind| supported_entity_kinds. contains ( & builtin_entity_kind) )
34+ . collect ( ) ;
4635
47- Arc :: new ( BuiltinEntityParser {
48- parser : build_parser ( lang. into_builtin ( ) ) . unwrap ( ) ,
49- lang,
50- supported_entity_kinds : ordered_entity_kinds,
51- } )
52- } )
53- . clone ( )
36+ BuiltinEntityParser {
37+ parser : build_parser ( lang. into_builtin ( ) ) . unwrap ( ) ,
38+ lang,
39+ supported_entity_kinds : ordered_entity_kinds,
40+ }
5441 }
5542
5643 pub fn extract_entities (
@@ -182,7 +169,7 @@ mod test {
182169
183170 #[ test]
184171 fn test_entities_extraction ( ) {
185- let parser = BuiltinEntityParser :: get ( Language :: EN ) ;
172+ let parser = BuiltinEntityParser :: new ( Language :: EN ) ;
186173 assert_eq ! (
187174 vec![ BuiltinEntityKind :: Number , BuiltinEntityKind :: Time ] ,
188175 parser
@@ -225,7 +212,7 @@ mod test {
225212
226213 #[ test]
227214 fn test_entities_extraction_for_non_space_separated_languages ( ) {
228- let parser = BuiltinEntityParser :: get ( Language :: JA ) ;
215+ let parser = BuiltinEntityParser :: new ( Language :: JA ) ;
229216 let expected_time_value = InstantTimeValue {
230217 value : "2013-02-10 00:00:00 +01:00" . to_string ( ) ,
231218 grain : Grain :: Day ,
@@ -268,7 +255,7 @@ mod test {
268255 #[ test]
269256 fn test_entity_examples_should_be_parsed ( ) {
270257 for language in Language :: all ( ) {
271- let parser = BuiltinEntityParser :: get ( * language) ;
258+ let parser = BuiltinEntityParser :: new ( * language) ;
272259 for entity_kind in BuiltinEntityKind :: all ( ) {
273260 for example in entity_kind. examples ( * language) {
274261 let results = parser. extract_entities ( example, Some ( & [ * entity_kind] ) ) ;
0 commit comments