5 \brief
\83x
\83C
\83W
\83A
\83\93\83t
\83B
\83\8b\83^
7 $Id: GikoBayesian.pas,v 1.2 2004/10/21 01:20:34 yoffy Exp $
12 //==================================================
14 //==================================================
17 //==================================================
19 //==================================================
21 {!***********************************************************
22 \brief
\92P
\8cê
\83v
\83\8d\83p
\83e
\83B
23 ************************************************************}
24 TWordInfo = class( TObject )
26 FNormalWord : Integer; //!<
\92Ê
\8fí
\82Ì
\92P
\8cê
\82Æ
\82µ
\82Ä
\93o
\8fê
\82µ
\82½
\89ñ
\90\94
27 FImportantWord : Integer; //!<
\92\8d\96Ú
\92P
\8cê
\82Æ
\82µ
\82Ä
\93o
\8fê
\82µ
\82½
\89ñ
\90\94
28 FNormalText : Integer; //!<
\92Ê
\8fí
\82Ì
\92P
\8cê
\82Æ
\82µ
\82Ä
\8aÜ
\82Ü
\82ê
\82Ä
\82¢
\82½
\95¶
\8fÍ
\82Ì
\90\94
29 FImportantText : Integer; //!<
\92\8d\96Ú
\92P
\8cê
\82Æ
\82µ
\82Ä
\8aÜ
\82Ü
\82ê
\82Ä
\82¢
\82½
\95¶
\8fÍ
\82Ì
\90\94
32 property NormalWord : Integer read FNormalWord write FNormalWord;
33 property ImportantWord : Integer read FImportantWord write FImportantWord;
34 property NormalText : Integer read FNormalText write FNormalText;
35 property ImportantText : Integer read FImportantText write FImportantText;
38 {!***********************************************************
39 \brief
\89ð
\90Í
\8dÏ
\82Ý
\92P
\8cê
\83v
\83\8d\83p
\83e
\83B
40 ************************************************************}
41 TWordCountInfo = class( TObject )
43 FWordCount : Integer; //!<
\92P
\8cê
\90\94
46 property WordCount : Integer read FWordCount write FWordCount;
49 {!***********************************************************
50 \brief
\89ð
\90Í
\8dÏ
\82Ý
\92P
\8cê
\83\8a\83X
\83g
51 ************************************************************}
52 // TWordCount = class( THashedStringList ) //
\8c\83\92x
53 TWordCount = class( TStringList ) //
\92x
55 destructor Destroy; override;
58 {!***********************************************************
59 \brief
\83t
\83B
\83\8b\83^
\83A
\83\8b\83S
\83\8a\83Y
\83\80
60 ************************************************************}
61 TGikoBayesianAlgorithm =
62 (gbaPaulGraham, gbaGaryRonbinson{, gbaGaryRonbinsonFisher});
64 {!***********************************************************
65 \brief
\83x
\83C
\83W
\83A
\83\93\83t
\83B
\83\8b\83^
66 ************************************************************}
67 TGikoBayesian = class( THashedStringList )
69 FFilePath : string; //!<
\93Ç
\82Ý
\8d\9e\82ñ
\82¾
\83t
\83@
\83C
\83\8b\83p
\83X
70 function GetObject( const name : string ) : TWordInfo;
71 procedure SetObject( const name : string; value : TWordInfo );
75 destructor Destroy; override;
77 //!
\83t
\83@
\83C
\83\8b\82©
\82ç
\8aw
\8fK
\97\9a\97ð
\82ð
\93Ç
\82Ý
\8fo
\82µ
\82Ü
\82·
78 procedure LoadFromFile( const filePath : string );
80 //!
\83t
\83@
\83C
\83\8b\82É
\8aw
\8fK
\97\9a\97ð
\82ð
\95Û
\91¶
\82µ
\82Ü
\82·
81 procedure SaveToFile( const filePath : string );
83 //!
\83t
\83@
\83C
\83\8b\82É
\8aw
\8fK
\97\9a\97ð
\82ð
\95Û
\91¶
\82µ
\82Ü
\82·
86 //!
\92P
\8cê
\82É
\91Î
\82·
\82é
\8fî
\95ñ
\82ð
\8eæ
\93¾
\82µ
\82Ü
\82·
87 property Objects[ const name : string ] : TWordInfo
88 read GetObject write SetObject; default;
90 //!
\95¶
\8fÍ
\82É
\8aÜ
\82Ü
\82ê
\82é
\92P
\8cê
\82ð
\83J
\83E
\83\93\83g
\82µ
\82Ü
\82·
93 wordCount : TWordCount );
96 \brief Paul Graham
\96@
\82É
\8aî
\82Ã
\82¢
\82Ä
\95¶
\8fÍ
\82Ì
\92\8d\96Ú
\93x
\82ð
\8c\88\92è
\82µ
\82Ü
\82·
97 \return
\95¶
\8fÍ
\82Ì
\92\8d\96Ú
\93x (
\92\8d\96Ú
\82É
\92l
\82µ
\82È
\82¢ 0.0
\81`1.0
\92\8d\96Ú
\82·
\82×
\82«)
99 function CalcPaulGraham( wordCount : TWordCount ) : Extended;
102 \brief GaryRobinson
\96@
\82É
\8aî
\82Ã
\82¢
\82Ä
\95¶
\8fÍ
\82Ì
\92\8d\96Ú
\93x
\82ð
\8c\88\92è
\82µ
\82Ü
\82·
103 \return
\95¶
\8fÍ
\82Ì
\92\8d\96Ú
\93x (
\92\8d\96Ú
\82É
\92l
\82µ
\82È
\82¢ 0.0
\81`1.0
\92\8d\96Ú
\82·
\82×
\82«)
105 function CalcGaryRobinson( wordCount : TWordCount ) : Extended;
107 // function CalcGaryRobinsonFisher( wordCount : TWordCount ) : Extended;
110 \brief
\95¶
\8fÍ
\82ð
\89ð
\90Í
111 \param text
\89ð
\90Í
\82·
\82é
\95¶
\8fÍ
112 \param wordCount
\89ð
\90Í
\82³
\82ê
\82½
\92P
\8cê
\83\8a\83X
\83g
\82ª
\95Ô
\82é
113 \param algorithm
\92\8d\96Ú
\93x
\82Ì
\8c\88\92è
\82É
\97p
\82¢
\82é
\83A
\83\8b\83S
\83\8a\83Y
\83\80\82ð
\8ew
\92è
\82µ
\82Ü
\82·
114 \return
\95¶
\8fÍ
\82Ì
\92\8d\96Ú
\93x (
\92\8d\96Ú
\82É
\92l
\82µ
\82È
\82¢ 0.0
\81`1.0
\92\8d\96Ú
\82·
\82×
\82«)
116 CountWord
\82Æ Calcxxxxx
\82ð
\82Ü
\82Æ
\82ß
\82Ä
\8eÀ
\8ds
\82·
\82é
\82¾
\82¯
\82Å
\82·
\81B
120 wordCount : TWordCount;
121 algorithm : TGikoBayesianAlgorithm = gbaGaryRonbinson
125 \brief
\8aw
\8fK
\82·
\82é
126 \param wordCount Parse
\82Å
\89ð
\90Í
\82³
\82ê
\82½
\92P
\8cê
\83\8a\83X
\83g
127 \param isImportant
\92\8d\96Ú
\82·
\82×
\82«
\95¶
\8fÍ
\82Æ
\82µ
\82Ä
\8ao
\82¦
\82é
\82È
\82ç True
130 wordCount : TWordCount;
131 isImportant : Boolean );
134 \brief
\8aw
\8fK
\8c\8b\89Ê
\82ð
\96Y
\82ê
\82é
135 \param wordCount Parse
\82Å
\89ð
\90Í
\82³
\82ê
\82½
\92P
\8cê
\83\8a\83X
\83g
136 \param isImportant
\92\8d\96Ú
\82·
\82×
\82«
\95¶
\8fÍ
\82Æ
\82µ
\82Ä
\8ao
\82¦
\82ç
\82ê
\82Ä
\82¢
\82½
\82È
\82ç True
137 \warning
\8aw
\8fK
\8dÏ
\82Ý
\82Ì
\95¶
\8fÍ
\82©
\82Ç
\82¤
\82©
\82Í
\8am
\94F
\8fo
\97\88\82Ü
\82¹
\82ñ
\81B<br>
138 Learn
\82µ
\82Ä
\82¢
\82È
\82¢
\95¶
\8fÍ
\82â isImportant
\82ª
\8aÔ
\88á
\82Á
\82Ä
\82¢
\82é
\95¶
\8fÍ
\82ð
139 Forget
\82·
\82é
\82Æ
\83f
\81[
\83^
\83x
\81[
\83X
\82ª
\94j
\91¹
\82µ
\82Ü
\82·
\81B<br>
140 \8aw
\8fK
\8dÏ
\82Ý
\82©
\82Ç
\82¤
\82©
\82Í
\93Æ
\8e©
\82É
\8aÇ
\97\9d\82µ
\82Ä
\82
\82¾
\82³
\82¢
\81B
142 \91S
\82Ä
\82Ì
\8aw
\8fK
\8c\8b\89Ê
\82ð
\83N
\83\8a\83A
\82·
\82é
\82í
\82¯
\82Å
\82Í
\82 \82è
\82Ü
\82¹
\82ñ
\81B<br>
143 wordCount
\82ð
\93¾
\82½
\95¶
\8fÍ (Parse
\82Ì text
\88ø
\90\94)
\82Ì
\8aw
\8fK
\8c\8b\89Ê
\82Ì
\82Ý
\83N
\83\8a\83A
\82µ
\82Ü
\82·
\81B<br><br>
145 \8eå
\82É
\92\8d\96Ú
\95¶
\8fÍ
\82Æ
\94ñ
\92\8d\96Ú
\95¶
\8fÍ
\82ð
\90Ø
\82è
\91Ö
\82¦
\82é
\82½
\82ß
\82É Forget -> Learn
\82Ì
\8f\87\82Å
\8eg
\97p
\82µ
\82Ü
\82·
\81B
148 wordCount : TWordCount;
149 isImportant : Boolean );
152 //==================================================
154 //==================================================
165 b1, b2, b3, b4, b5, b6, b7, b8, b9, b10 : Int64; // benchmark
169 GIKO_BAYESIAN_FILE_VERSION = '1.0';
170 kYofKanji : TSysCharSet = [#$80..#$A0, #$E0..#$ff];
172 //************************************************************
174 //************************************************************
176 //==============================
178 //==============================
179 function RemoveToken(var s: string;const delimiter: string): string;
183 p := AnsiPos(delimiter, s);
187 Result := Copy(s, 1, p - 1);
188 s := Copy(s, Length(Result) + Length(delimiter) + 1, Length(s));
191 //==============================
193 //==============================
194 function AbsSort( p1, p2 : Pointer ) : Integer;
199 v1 := Abs( Single( p1 ) - 0.5 );
200 v2 := Abs( Single( p2 ) - 0.5 );
210 //************************************************************
212 //************************************************************
213 destructor TWordCount.Destroy;
218 for i := Count - 1 downto 0 do
219 if Objects[ i ] <> nil then
226 //************************************************************
227 // TGikoBayesian class
228 //************************************************************
230 //==============================
232 //==============================
233 constructor TGikoBayesian.Create;
237 b1:=0; b2:=0; b3:=0; b4:=0; b5:=0; b6:=0; b7:=0; b8:=0; b9:=0; b10:=0;
240 Duplicates := dupIgnore;
245 //==============================
247 //==============================
248 destructor TGikoBayesian.Destroy;
253 for i := Count - 1 downto 0 do
254 if inherited Objects[ i ] <> nil then
255 inherited Objects[ i ].Free;
261 procedure TGikoBayesian.LoadFromFile( const filePath : string );
270 FFilePath := filePath;
272 if not FileExists( filePath ) then
275 sl := TStringList.Create;
277 sl.LoadFromFile( filePath );
279 for i := 1 to sl.Count - 1 do begin
281 name := RemoveToken( s, #1 );
282 info := TWordInfo.Create;
283 info.NormalWord := StrToIntDef( '$' + RemoveToken( s, #1 ), 0 );
284 info.ImportantWord := StrToIntDef( '$' + RemoveToken( s, #1 ), 0 );
285 info.NormalText := StrToIntDef( '$' + RemoveToken( s, #1 ), 0 );
286 info.ImportantText := StrToIntDef( '$' + RemoveToken( s, #1 ), 0 );
288 AddObject( name, info );
296 procedure TGikoBayesian.SaveToFile( const filePath : string );
305 ShowMessage(IntToStr(b1)+'/'+IntToStr(b2)+'/'+IntToStr(b3)+'/'+IntToStr(b4)+
306 '/'+IntToStr(b5)+'/'+IntToStr(b6));
309 FFilePath := filePath;
311 sl := TStringList.Create;
314 sl.Add( GIKO_BAYESIAN_FILE_VERSION );
316 for i := 0 to Count - 1 do begin
317 info := TWordInfo( inherited Objects[ i ] );
318 s := Strings[ i ] + #1
319 + Format('%x', [info.NormalWord]) + #1
320 + Format('%x', [info.ImportantWord]) + #1
321 + Format('%x', [info.NormalText]) + #1
322 + Format('%x', [info.ImportantText]);
327 sl.SaveToFile( filePath );
334 procedure TGikoBayesian.Save;
337 if FFilePath <> '' then
338 SaveToFile( FFilePath );
342 //==============================
344 //==============================
345 function TGikoBayesian.GetObject( const name : string ) : TWordInfo;
350 idx := IndexOf( name );
354 Result := TWordInfo( inherited Objects[ idx ] );
358 //==============================
360 //==============================
361 procedure TGikoBayesian.SetObject( const name : string; value : TWordInfo );
366 idx := IndexOf( name );
368 AddObject( name, value )
370 inherited Objects[ idx ] := value;
375 //==============================
377 //==============================
378 procedure TGikoBayesian.CountWord(
380 wordCount : TWordCount );
382 Modes = (ModeWhite, ModeGraph, ModeAlpha, ModeNum, ModeHanKana,
383 ModeWGraph, ModeWAlpha, ModeWNum,
384 ModeWHira, ModeWKata, ModeWKanji);
386 p, tail, last : PChar;
387 mode, newMode : Modes;
391 delimiter : TStringList;
394 countInfo : TWordCountInfo;
399 KAKUJOSI = '
\82ð' + #10 + '
\82É' + #10 + '
\82ª' + #10 + '
\82Æ' + #10 + '
\82©
\82ç' +
400 #10 + '
\82Å' + #10 + '
\82Ö' + #10 + '
\82æ
\82è' + #10 + '
\82Ü
\82Å';
403 delimiter := TStringList.Create;
405 //***
\91¬
\93x
\83e
\83X
\83g
\92\86
406 wordCount.Duplicates := dupIgnore;
407 wordCount.CaseSensitive := True;
408 wordCount.Capacity := 1000;
409 wordCount.Sorted := True;
413 delimiter.Text := KAKUJOSI;
414 SetLength( aWord, 256 );
416 tail := p + Length( text );
419 while p < tail do begin
421 QueryPerformanceCounter( t1 );
424 //
\95¶
\8e\9a\82Ì
\83^
\83C
\83v
\82ð
\94»
\95Ê
425 //
\81¦
\8bå
\93Ç
\93_
\82Í ModeGraph
\82É
\82È
\82é
\82Ì
\82Å
\8cÂ
\95Ê
\82É
\91Î
\89\9e\82µ
\82È
\82
\82Ä
\82à
\82¢
\82¢
426 if p^ in kYofKanji then begin
427 if p + 1 < tail then begin
428 ch := (PByte( p )^ shl 8) or PByte( p + 1 )^;
430 $8140: newMode := ModeWhite;
431 $8141..$824e: newMode := ModeWGraph;
432 $824f..$8258: newMode := ModeWNum;
433 $8260..$829a: newMode := ModeWAlpha;
434 $829f..$82f1: newMode := ModeWHira;
435 $8340..$8396: newMode := ModeWKata;
436 else newMode := ModeWKanji;
439 newMode := ModeWhite;
444 //
\8bæ
\90Ø
\82è
\82É
\82È
\82é
\95¶
\8e\9a\82ª
\82 \82é
\82©
\8c\9f\8d¸
\82·
\82é
445 if p + 3 < tail then begin // 3 = delimiter
\82Ì
\8dÅ
\91å
\8e\9a\90\94 - 1
446 for i := 0 to delimiter.Count - 1 do begin
448 p, PChar( delimiter[ i ] ), Length( delimiter[ i ] ) ) then begin
450 chSize := Length( delimiter[ i ] );
457 #$0..#$20, #$7f: newMode := ModeWhite;
458 '0'..'9': newMode := ModeNum;
459 'a'..'z', 'A'..'Z': newMode := ModeAlpha;
460 #$A6..#$DD: newMode := ModeHanKana;
461 else newMode := ModeGraph;
467 QueryPerformanceCounter( t2 ); b1 := b1 + (t2 - t1);
470 if (mode <> newMode) or delimited then begin
472 //
\95¶
\8e\9a\82Ì
\83^
\83C
\83v
\82ª
\95Ï
\8dX
\82³
\82ê
\82½
473 //
\82à
\82µ
\82
\82Í
\8bæ
\90Ø
\82è
\82É
\82È
\82é
\95¶
\8e\9a\82É
\91\98\8bö
\82µ
\82½
474 if mode <> ModeWhite then begin
476 QueryPerformanceCounter( t1 );
478 aWord := Copy( last, 0, p - last ); //
\8c\83\92x
479 // SetLength( aWord, p - last );
480 // CopyMemory( PChar( aWord ), last, p - last );
482 QueryPerformanceCounter( t2 ); b2 := b2 + (t2 - t1);
484 idx := wordCount.IndexOf( aWord ); //
\8c\83\92x
486 QueryPerformanceCounter( t1 ); b3 := b3 + (t1 - t2);
488 if idx < 0 then begin
489 countInfo := TWordCountInfo.Create;
490 wordCount.AddObject( aWord, countInfo );
492 countInfo := TWordCountInfo( wordCount.Objects[ idx ] );
494 countInfo.WordCount := countInfo.WordCount + 1;
496 QueryPerformanceCounter( t2 ); b4 := b4 + (t2 - t1);
508 if mode <> ModeWhite then begin
509 aWord := Copy( last, 0, p - last );
510 idx := wordCount.IndexOf( aWord );
511 if idx < 0 then begin
512 countInfo := TWordCountInfo.Create;
513 wordCount.AddObject( aWord, countInfo );
515 countInfo := TWordCountInfo( wordCount.Objects[ idx ] );
517 countInfo.WordCount := countInfo.WordCount + 1;
525 //==============================
527 //==============================
528 function TGikoBayesian.CalcPaulGraham( wordCount : TWordCount ) : Extended;
530 function p( const aWord : string ) : Single;
534 info := Objects[ aWord ];
537 else if info.NormalWord = 0 then
539 else if info.ImportantWord = 0 then
542 Result := ( info.ImportantWord / info.ImportantText ) /
543 ((info.NormalWord * 2 / info.NormalText ) +
544 (info.ImportantWord / info.ImportantText));
556 if wordCount.Count = 0 then
559 narray := TList.Create;
561 for i := 0 to wordCount.Count - 1 do begin
562 narray.Add( Pointer( p( wordCount[ i ] ) ) );
565 narray.Sort( AbsSort );
569 i := min( SAMPLE_COUNT, narray.Count );
572 s := s * Single( narray[ i ] );
573 q := q * (1 - Single( narray[ i ] ));
576 Result := s / (s + q);
583 //==============================
585 //==============================
586 function TGikoBayesian.CalcGaryRobinson( wordCount : TWordCount ) : Extended;
588 function p( const aWord : string ) : Single;
592 info := Objects[ aWord ];
595 else if info.ImportantWord = 0 then
597 else if info.NormalWord = 0 then
600 Result := ( info.ImportantWord / info.ImportantText ) /
601 ((info.NormalWord / info.NormalText ) +
602 (info.ImportantWord / info.ImportantText));
605 function f( cnt : Integer; n, mean : Single ) : Extended;
609 Result := ( (k * mean) + (cnt * n) ) / (k + cnt);
614 narray : array of Single;
616 countInfo : TWordCountInfo;
619 important : Extended;
623 if wordCount.Count = 0 then begin
628 SetLength( narray, wordCount.Count );
630 for i := 0 to wordCount.Count - 1 do begin
631 n := p( wordCount[ i ] );
635 mean := mean / wordCount.Count;
640 for i := 0 to wordCount.Count - 1 do begin
641 countInfo := TWordCountInfo( wordCount.Objects[ i ] );
642 n := f( countInfo.WordCount, narray[ i ], mean );
643 normal := normal * n;
644 important := important * (1 - n);
645 if countInfo <> nil then
646 cnt := cnt + countInfo.WordCount;
650 normal := 1 - Exp( Ln( normal ) * (1 / cnt) );
651 important := 1 - Exp( Ln( important ) * (1 / cnt) );
653 n := (important - normal+ 0.00001) / (important + normal + 0.00001);
654 Result := (1 + n) / 2;
658 //==============================
660 //==============================
661 function TGikoBayesian.Parse(
663 wordCount : TWordCount;
664 algorithm : TGikoBayesianAlgorithm = gbaGaryRonbinson
673 QueryPerformanceCounter( t1 );
675 CountWord( text, wordCount );
677 QueryPerformanceCounter( t2 ); b5 := b5 + (t2 - t1);
680 gbaPaulGraham: Result := CalcPaulGraham( wordCount );
681 gbaGaryRonbinson: Result := CalcGaryRobinson( wordCount );
685 QueryPerformanceCounter( t1 ); b6 := b6 + (t1 - t2);
690 //==============================
692 //==============================
693 procedure TGikoBayesian.Learn(
694 wordCount : TWordCount;
695 isImportant : Boolean );
698 wordinfo : TWordInfo;
699 countinfo : TWordCountInfo;
703 for i := 0 to wordCount.Count - 1 do begin
704 aWord := wordCount[ i ];
705 wordinfo := Objects[ aWord ];
706 if wordinfo = nil then begin
707 wordinfo := TWordInfo.Create;
708 Objects[ aWord ] := wordinfo;
711 countinfo := TWordCountInfo( wordCount.Objects[ i ] );
712 if isImportant then begin
713 wordinfo.ImportantWord := wordinfo.ImportantWord + countinfo.WordCount;
714 wordinfo.ImportantText := wordinfo.ImportantText + 1;
716 wordinfo.NormalWord := wordinfo.NormalWord + countinfo.WordCount;
717 wordinfo.NormalText := wordinfo.NormalText + 1;
723 //==============================
725 //==============================
726 procedure TGikoBayesian.Forget(
727 wordCount : TWordCount;
728 isImportant : Boolean );
731 wordinfo : TWordInfo;
732 countinfo : TWordCountInfo;
736 for i := 0 to wordCount.Count - 1 do begin
737 aWord := wordCount[ i ];
738 wordinfo := Objects[ aWord ];
739 if wordinfo = nil then
742 countinfo := TWordCountInfo( wordCount.Objects[ i ] );
743 if isImportant then begin
744 wordinfo.ImportantWord := wordinfo.ImportantWord - countinfo.WordCount;
745 wordinfo.ImportantText := wordinfo.ImportantText - 1;
747 wordinfo.NormalWord := wordinfo.NormalWord - countinfo.WordCount;
748 wordinfo.NormalText := wordinfo.NormalText - 1;