@@ -26,10 +26,37 @@ where
2626{
2727 /// Read embeddings in the fastText format.
2828 fn read_fasttext ( reader : & mut impl BufRead ) -> Result < Self > ;
29+
30+ /// Read embeddings in the fastText format lossily.
31+ ///
32+ /// In constrast to `read_fasttext`, this method does not fail
33+ /// on reading tokens with invalid UTF-8 byte sequences. Invalid
34+ /// UTF-8 sequences will be replaced by the unicode replacement
35+ /// character.
36+ fn read_fasttext_lossy ( reader : & mut impl BufRead ) -> Result < Self > ;
2937}
3038
3139impl ReadFastText for Embeddings < FastTextSubwordVocab , NdArray > {
32- fn read_fasttext ( mut reader : & mut impl BufRead ) -> Result < Self > {
40+ fn read_fasttext ( reader : & mut impl BufRead ) -> Result < Self > {
41+ Self :: read_fasttext_private ( reader, false )
42+ }
43+
44+ fn read_fasttext_lossy ( reader : & mut impl BufRead ) -> Result < Self > {
45+ Self :: read_fasttext_private ( reader, true )
46+ }
47+ }
48+
49+ /// Read embeddings in the fastText format.
50+ pub trait ReadFastTextPrivate
51+ where
52+ Self : Sized ,
53+ {
54+ /// Read embeddings in the fastText format.
55+ fn read_fasttext_private ( reader : & mut impl BufRead , lossy : bool ) -> Result < Self > ;
56+ }
57+
58+ impl ReadFastTextPrivate for Embeddings < FastTextSubwordVocab , NdArray > {
59+ fn read_fasttext_private ( mut reader : & mut impl BufRead , lossy : bool ) -> Result < Self > {
3360 let magic = reader
3461 . read_u32 :: < LittleEndian > ( )
3562 . map_err ( |e| ErrorKind :: io_error ( "Cannot fastText read magic" , e) ) ?;
@@ -54,7 +81,7 @@ impl ReadFastText for Embeddings<FastTextSubwordVocab, NdArray> {
5481
5582 let config = Config :: read ( & mut reader) ?;
5683
57- let vocab = read_vocab ( & config, & mut reader) ?;
84+ let vocab = read_vocab ( & config, & mut reader, lossy ) ?;
5885
5986 let is_quantized = reader
6087 . read_u8 ( )
@@ -271,7 +298,7 @@ where
271298}
272299
273300/// Read the vocabulary.
274- fn read_vocab < R > ( config : & Config , reader : & mut R ) -> Result < FastTextSubwordVocab >
301+ fn read_vocab < R > ( config : & Config , reader : & mut R , lossy : bool ) -> Result < FastTextSubwordVocab >
275302where
276303 R : BufRead ,
277304{
@@ -304,7 +331,7 @@ where
304331
305332 let mut words = Vec :: with_capacity ( size as usize ) ;
306333 for _ in 0 ..size {
307- let word = read_string ( reader, 0 , false ) ?;
334+ let word = read_string ( reader, 0 , lossy ) ?;
308335 reader
309336 . read_u64 :: < LittleEndian > ( )
310337 . map_err ( |e| ErrorKind :: io_error ( "Cannot read word frequency" , e) ) ?;
0 commit comments