File tree Expand file tree Collapse file tree 1 file changed +29
-3
lines changed
src/main/java/com/hankcs/hanlp/model/hmm Expand file tree Collapse file tree 1 file changed +29
-3
lines changed Original file line number Diff line number Diff line change 12
12
13
13
import com .hankcs .hanlp .utility .MathUtility ;
14
14
15
+ import java .io .*;
15
16
import java .util .ArrayList ;
16
17
import java .util .Arrays ;
17
18
import java .util .Collection ;
@@ -44,9 +45,9 @@ public abstract class HiddenMarkovModel
44
45
*/
45
46
public HiddenMarkovModel (float [] start_probability , float [][] transition_probability , float [][] emission_probability )
46
47
{
47
- this .start_probability = start_probability ;
48
- this .transition_probability = transition_probability ;
49
- this .emission_probability = emission_probability ;
48
+ this .start_probability = ( float []) deepCopy ( start_probability ) ;
49
+ this .transition_probability = ( float [][]) deepCopy ( transition_probability ) ;
50
+ this .emission_probability = ( float [][]) deepCopy ( emission_probability ) ;
50
51
}
51
52
52
53
/**
@@ -298,4 +299,29 @@ protected static boolean similar(float[] A, float[] B)
298
299
if (Math .abs (A [i ] - B [i ]) > eta ) return false ;
299
300
return true ;
300
301
}
302
+
303
+ protected static Object deepCopy (Object object )
304
+ {
305
+ if (object == null )
306
+ {
307
+ return null ;
308
+ }
309
+ try
310
+ {
311
+ ByteArrayOutputStream bos = new ByteArrayOutputStream ();
312
+ ObjectOutputStream oos = new ObjectOutputStream (bos );
313
+ oos .writeObject (object );
314
+ oos .flush ();
315
+ oos .close ();
316
+ bos .close ();
317
+
318
+ byte [] byteData = bos .toByteArray ();
319
+ ByteArrayInputStream bais = new ByteArrayInputStream (byteData );
320
+ return new ObjectInputStream (bais ).readObject ();
321
+ }
322
+ catch (Exception e )
323
+ {
324
+ throw new RuntimeException (e );
325
+ }
326
+ }
301
327
}
You can’t perform that action at this time.
0 commit comments