OSDN Git Service

1.60.2.794
[gikonavigoeson/gikonavi.git] / GikoBayesian.pas
index c7f5606..55791a3 100644 (file)
@@ -4,7 +4,8 @@ unit GikoBayesian;
 \file          GikoBayesian.pas
 \brief \83x\83C\83W\83A\83\93\83t\83B\83\8b\83^
 
-$Id: GikoBayesian.pas,v 1.16 2004/11/01 10:32:02 yoffy Exp $
+
+$Id: GikoBayesian.pas,v 1.22 2009/01/31 15:47:15 h677 Exp $
 }
 
 //! \95½\89¼\96¼\82ð\8e«\8f\91\82É\8aÜ\82ß\82È\82¢
@@ -309,12 +310,12 @@ begin
 
                for i := 1 to sl.Count - 1 do begin
                        s := sl[ i ];
-                       name := RemoveToken( s, #1 );
+                       name := GikoBayesian.RemoveToken( s, #1 );
                        info := TWordInfo.Create;
-                       info.NormalWord                 := StrToIntDef( '$' + RemoveToken( s, #1 ), 0 );
-                       info.ImportantWord      := StrToIntDef( '$' + RemoveToken( s, #1 ), 0 );
-                       info.NormalText                 := StrToIntDef( '$' + RemoveToken( s, #1 ), 0 );
-                       info.ImportantText      := StrToIntDef( '$' + RemoveToken( s, #1 ), 0 );
+                       info.NormalWord                 := StrToIntDef( '$' + GikoBayesian.RemoveToken( s, #1 ), 0 );
+                       info.ImportantWord      := StrToIntDef( '$' + GikoBayesian.RemoveToken( s, #1 ), 0 );
+                       info.NormalText                 := StrToIntDef( '$' + GikoBayesian.RemoveToken( s, #1 ), 0 );
+                       info.ImportantText      := StrToIntDef( '$' + GikoBayesian.RemoveToken( s, #1 ), 0 );
 
                        AddObject( name, info );
                end;
@@ -416,7 +417,7 @@ var
        wKanjiDelimiter         : TStringList;
        words                                                   : TStringList;
        aWord                                                   : string;
-       countInfo                                       : TWordCountInfo;
+//     countInfo                                       : TWordCountInfo;
 
        function cutBoth( _aWord : string; _delim : TStringList ) : string;
        var
@@ -699,10 +700,15 @@ function TGikoBayesian.CalcPaulGraham( wordCount : TWordCount ) : Extended;
                        Result := 0.01
                else if info.ImportantWord + info.NormalWord * 2 < 5 then
                        Result := 0.5
-               else
-                       Result := ( info.ImportantWord / info.ImportantText ) /
-                               ((info.NormalWord * 2 / info.NormalText ) +
-                                (info.ImportantWord / info.ImportantText));
+               else begin
+                       try
+                               Result := ( info.ImportantWord / info.ImportantText ) /
+                                       ((info.NormalWord * 2 / info.NormalText ) +
+                                        (info.ImportantWord / info.ImportantText));
+                       except
+               on EZeroDivide do Result := 0.99;
+                       end;
+               end;
        end;
 
 var
@@ -734,8 +740,11 @@ begin
                        s := s * Single( narray[ i ] );
                        q := q * (1 - Single( narray[ i ] ));
                end;
-
-               Result := s / (s + q);
+               try
+                       Result := s / (s + q);
+               except
+            Result := 0.5;
+               end;
        finally
                narray.Free;
        end;
@@ -764,9 +773,13 @@ function TGikoBayesian.CalcGaryRobinson( wordCount : TWordCount ) : Extended;
                                ((info.NormalWord / info.NormalText ) +
                                 (info.ImportantWord / info.ImportantText));
                }
-                       Result := (info.ImportantWord * info.NormalText) /
-                               (info.NormalWord * info.ImportantText +
-                               info.ImportantWord * info.NormalText);
+                       try
+                               Result := (info.ImportantWord * info.NormalText) /
+                                       (info.NormalWord * info.ImportantText +
+                                       info.ImportantWord * info.NormalText);
+                       except
+                               Result := 0.5;
+                       end;
        end;
 
        function f( cnt : Integer; n, mean : Single ) : Extended;
@@ -782,7 +795,7 @@ var
        mean                            : Extended;
        countInfo               : TWordCountInfo;
        i                                               : Integer;
-       P1, Q1, R1      : Extended;
+       P1, Q1{, R1}    : Extended;
        cnt                                     : Extended;
 begin
 
@@ -811,8 +824,14 @@ begin
        cnt := wordCount.Count;
        if cnt = 0 then
                cnt := 1;
-       P1 := 1 - Power( P1, 1 / cnt );
-       Q1 := 1 - Power( Q1, 1 / cnt );
+       try
+               P1 := 1 - Power( P1, 1 / cnt );
+       except
+       end;
+       try
+               Q1 := 1 - Power( Q1, 1 / cnt );
+       except
+       end;
 
        if P1 + Q1 = 0 then begin
                Result := 0.5
@@ -872,8 +891,8 @@ var
        mean                            : Extended;
        countInfo               : TWordCountInfo;
        i                                               : Integer;
-       normal                  : Extended;
-       important               : Extended;
+//     normal                  : Extended;
+//     important               : Extended;
        P1, Q1                  : Extended;
        cnt                                     : Extended;
 begin
@@ -903,8 +922,14 @@ begin
        cnt := wordCount.Count;
        if cnt = 0 then
                cnt := 1;
-       P1 := Power( P1, 1 / cnt );
-       Q1 := Power( Q1, 1 / cnt );
+       try
+               P1 := Power( P1, 1 / cnt );
+       except
+       end;
+       try
+               Q1 := Power( Q1, 1 / cnt );
+       except
+       end;
 
        P1 := 1 - prbx( -2 * Ln( P1 ), 2 * cnt );
        Q1 := 1 - prbx( -2 * Ln( Q1 ), 2 * cnt );