OSDN Git Service

タスクトレイの右クリックで終了メニューを出すようにした。
[gikonavigoeson/gikonavi.git] / GikoBayesian.pas
index a63402d..a8f7c07 100644 (file)
@@ -4,9 +4,13 @@ unit GikoBayesian;
 \file          GikoBayesian.pas
 \brief \83x\83C\83W\83A\83\93\83t\83B\83\8b\83^
 
-$Id: GikoBayesian.pas,v 1.13 2004/11/01 05:18:21 yoffy Exp $
+
+$Id: GikoBayesian.pas,v 1.21 2006/06/26 14:57:15 h677 Exp $
 }
 
+//! \95½\89¼\96¼\82ð\8e«\8f\91\82É\8aÜ\82ß\82È\82¢
+{$DEFINE GIKO_BAYESIAN_NO_HIRAGANA_DIC}
+
 interface
 
 //==================================================
@@ -413,7 +417,7 @@ var
        wKanjiDelimiter         : TStringList;
        words                                                   : TStringList;
        aWord                                                   : string;
-       countInfo                                       : TWordCountInfo;
+//     countInfo                                       : TWordCountInfo;
 
        function cutBoth( _aWord : string; _delim : TStringList ) : string;
        var
@@ -483,6 +487,12 @@ var
        const
                _delim : string = #10;
        begin
+{$IFDEF GIKO_BAYESIAN_NO_HIRAGANA_DIC}
+               if mode = ModeWHira then begin
+                       Result := '';
+                       Exit;
+               end;
+{$ENDIF}
                if Ord( _mode ) >= Ord( ModeWGraph ) then begin
                        // \93ú\96{\8cê
                        // \83X\83y\81[\83X\82ð\8bl\82ß\82é
@@ -586,8 +596,10 @@ begin
        words := TStringList.Create;
        try
                mode := ModeWhite;
+{$IFNDEF GIKO_BAYESIAN_NO_HIRAGANA_DIC}
                wHiraDelimiter.Text := WHIRA_DELIMITER;
                wHiraFinalDelimiter.Text := WHIRA_FINAL_DELIMITER;
+{$ENDIF}
                wKanjiDelimiter.Text := WKANJI_DELIMITER;
                p                       := PChar( text );
                tail    := p + Length( text );
@@ -653,7 +665,9 @@ begin
                end;    // while
 
                if mode <> ModeWhite then begin
-                       aWord := Copy( last, 0, p - last );
+                       SetLength( aWord, p - last );
+                       CopyMemory( PChar( aWord ), last, p - last );
+
                        words.Text := changeMode( aWord, mode );
 
                        // \92P\8cê\93o\98^
@@ -686,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
@@ -721,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;
@@ -746,14 +768,23 @@ function TGikoBayesian.CalcGaryRobinson( wordCount : TWordCount ) : Extended;
                else if info.NormalWord = 0 then
                        Result := 0.99
                else
+               {
                        Result := ( info.ImportantWord / info.ImportantText ) /
                                ((info.NormalWord / info.NormalText ) +
                                 (info.ImportantWord / info.ImportantText));
+               }
+                       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;
        const
-               k = 0.00001;
+               k = 0.001;
        begin
                Result := ( (k * mean) + (cnt * n) ) / (k + cnt);
        end;
@@ -764,7 +795,7 @@ var
        mean                            : Extended;
        countInfo               : TWordCountInfo;
        i                                               : Integer;
-       P1, Q1, R1      : Extended;
+       P1, Q1{, R1}    : Extended;
        cnt                                     : Extended;
 begin
 
@@ -792,10 +823,15 @@ begin
        end;
        cnt := wordCount.Count;
        if cnt = 0 then
-               cnt := 1
-       else
-       P1 := 1 - Power( P1, 1 / cnt );
-       Q1 := 1 - Power( Q1, 1 / cnt );
+               cnt := 1;
+       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
@@ -825,41 +861,27 @@ function TGikoBayesian.CalcGaryRobinsonFisher(
                else if info.NormalWord = 0 then
                        Result := 0.99
                else
-                       Result := info.ImportantWord /
-                               (info.ImportantWord + info.NormalWord *
-                                info.ImportantText / info.NormalText);
+               {
+                       Result := ( info.ImportantWord / info.ImportantText ) /
+                               ((info.NormalWord / info.NormalText ) +
+                                (info.ImportantWord / info.ImportantText));
+               }
+                       Result := (info.ImportantWord * info.NormalText) /
+                               (info.NormalWord * info.ImportantText +
+                               info.ImportantWord * info.NormalText);
        end;
 
        function f( cnt : Integer; n, mean : Single ) : Extended;
        const
-               k = 0.00001;
+               k = 0.001;
        begin
                Result := ( (k * mean) + (cnt * n) ) / (k + cnt);
        end;
 
        function prbx( x2, degree : Extended ) : Extended;
-       var
-               m : Extended;
-               sum : Extended;
-               term : Extended;
-               i : extended;
        begin
 
-               m := x2 / 2;
-               sum := exp( -m );
-               term := -m;
-
-               i := 1;
-               while i < (degree / 2 - 1) do begin
-                       term := term + ln( m / i );
-                       sum := sum + exp( term );
-                       i := i + 1;
-               end;
-
-               if sum < 1 then
-                       Result := sum
-               else
-                       Result := 1.0;
+               Result := 0.5;
 
        end;
 
@@ -869,8 +891,8 @@ var
        mean                            : Extended;
        countInfo               : TWordCountInfo;
        i                                               : Integer;
-       normal                  : Extended;
-       important               : Extended;
+//     normal                  : Extended;
+//     important               : Extended;
        P1, Q1                  : Extended;
        cnt                                     : Extended;
 begin
@@ -889,41 +911,30 @@ begin
        end;
        mean := mean / wordCount.Count;
 
-       cnt := 0;
-(*
        P1 := 1;
        Q1 := 1;
-(*)
-       P1 := 0;
-       Q1 := 0;
-//*
        for i := 0 to wordCount.Count - 1 do begin
                countInfo       := TWordCountInfo( wordCount.Objects[ i ] );
                n                                               := f( countInfo.WordCount, narray[ i ], mean );
-               if countInfo <> nil then
-                       cnt := cnt + countInfo.WordCount;
-(*
-               P1 := P1 + Ln( 1 - n ) * countInfo.WordCount;
-               Q1 := Q1 + Ln( n ) * countInfo.WordCount;
-(*)
-               P1 := P1 + Ln( 1 - n );
-               Q1 := Q1 + Ln( n );
-//*)
+               P1 := P1 * ( 1 - n );
+               Q1 := Q1 * n;
        end;
+       cnt := wordCount.Count;
        if cnt = 0 then
                cnt := 1;
-//(*
-       P1 := prbx( -2 * P1, 2 * cnt );
-       Q1 := prbx( -2 * Q1, 2 * cnt );
-(*)
-       P1 := prbx( -2 * Ln( P1 ), 2 * cnt );
-       Q1 := prbx( -2 * Ln( Q1 ), 2 * cnt );
-//*)
-       if P1 + Q1 = 0 then begin
-               Result := 0.5
-       end else begin
-               Result := (1 + Q1 + P1) / 2;
+       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 );
+
+       Result := (1 + P1 - Q1) / 2;
 
 end;