@@ -17,6 +17,7 @@ public partial class FastTextWrapper : IDisposable
1717 private static readonly Encoding _utf8 = Encoding . UTF8 ;
1818
1919 private IntPtr _fastText ;
20+ private bool _modelLoaded = false ;
2021 private int _maxLabelLen ;
2122
2223 /// <summary>
@@ -47,6 +48,7 @@ public void LoadModel(string path)
4748 {
4849 LoadModel ( _fastText , path ) ;
4950 _maxLabelLen = GetMaxLabelLenght ( _fastText ) ;
51+ _modelLoaded = true ;
5052 }
5153
5254 /// <summary>
@@ -55,6 +57,8 @@ public void LoadModel(string path)
5557 /// <returns>Labels.</returns>
5658 public unsafe string [ ] GetLabels ( )
5759 {
60+ CheckModelLoaded ( ) ;
61+
5862 IntPtr labelsPtr ;
5963 int numLabels = GetLabels ( _fastText , new IntPtr ( & labelsPtr ) ) ;
6064
@@ -77,10 +81,8 @@ public unsafe string[] GetLabels()
7781 /// <returns>Single prediction.</returns>
7882 public unsafe Prediction PredictSingle ( string text )
7983 {
80- if ( _maxLabelLen == 0 )
81- {
82- throw new InvalidOperationException ( "Model not loaded!" ) ;
83- }
84+ CheckModelLoaded ( ) ;
85+ CheckModelLabels ( ) ;
8486
8587 IntPtr labelPtr ;
8688 float prob = PredictSingle ( _fastText , _utf8 . GetBytes ( text ) , new IntPtr ( & labelPtr ) ) ;
@@ -99,10 +101,8 @@ public unsafe Prediction PredictSingle(string text)
99101 /// <returns>Multiple predictions.</returns>
100102 public unsafe Prediction [ ] PredictMultiple ( string text , int number )
101103 {
102- if ( _maxLabelLen == 0 )
103- {
104- throw new InvalidOperationException ( "Model not loaded!" ) ;
105- }
104+ CheckModelLoaded ( ) ;
105+ CheckModelLabels ( ) ;
106106
107107 var probs = new float [ number ] ;
108108 IntPtr labelsPtr ;
@@ -129,6 +129,8 @@ public unsafe Prediction[] PredictMultiple(string text, int number)
129129 /// <returns>A single averaged vector.</returns>
130130 public unsafe float [ ] GetSentenceVector ( string text )
131131 {
132+ CheckModelLoaded ( ) ;
133+
132134 IntPtr vecPtr ;
133135 int dim = GetSentenceVector ( _fastText , _utf8 . GetBytes ( text ) , new IntPtr ( & vecPtr ) ) ;
134136
@@ -168,6 +170,7 @@ public void Train(string inputPath, string outputPath, SupervisedArgs args)
168170
169171 TrainSupervised ( _fastText , inputPath , outputPath , argsStruct , args . LabelPrefix ) ;
170172 _maxLabelLen = GetMaxLabelLenght ( _fastText ) ;
173+ _modelLoaded = true ;
171174 }
172175
173176 /// <summary>
@@ -212,6 +215,7 @@ public void Train(string inputPath, string outputPath, FastTextArgs args)
212215
213216 Train ( _fastText , inputPath , outputPath , argsStruct , args . LabelPrefix , args . PretrainedVectors ) ;
214217 _maxLabelLen = GetMaxLabelLenght ( _fastText ) ;
218+ _modelLoaded = true ;
215219 }
216220
217221 /// <inheritdoc />
@@ -226,6 +230,22 @@ public void Dispose()
226230 _fastText = IntPtr . Zero ;
227231 }
228232
233+ private void CheckModelLabels ( )
234+ {
235+ if ( _maxLabelLen == 0 )
236+ {
237+ throw new InvalidOperationException ( "Loaded model doesn't contain supervised labels. Maybe you loaded an unsupervised model?" ) ;
238+ }
239+ }
240+
241+ private void CheckModelLoaded ( )
242+ {
243+ if ( ! _modelLoaded )
244+ {
245+ throw new InvalidOperationException ( "Model not loaded!" ) ;
246+ }
247+ }
248+
229249 private void ValidatePaths ( string input , string output , string pretrained )
230250 {
231251 if ( string . IsNullOrEmpty ( input ) || ! File . Exists ( input ) )
0 commit comments