5 \brief
\83x
\83C
\83W
\83A
\83\93\83t
\83B
\83\8b\83^
7 $Id: GikoBayesian.pas,v 1.1 2004/10/20 18:25:00 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 //==================================================
160 GIKO_BAYESIAN_FILE_VERSION = '1.0';
161 kYofKanji : TSysCharSet = [#$80..#$A0, #$E0..#$ff];
163 //************************************************************
165 //************************************************************
167 //==============================
169 //==============================
170 function RemoveToken(var s: string;const delimiter: string): string;
174 p := AnsiPos(delimiter, s);
178 Result := Copy(s, 1, p - 1);
179 s := Copy(s, Length(Result) + Length(delimiter) + 1, Length(s));
182 //==============================
184 //==============================
185 function AbsSort( p1, p2 : Pointer ) : Integer;
190 v1 := Abs( Single( p1 ) - 0.5 );
191 v2 := Abs( Single( p2 ) - 0.5 );
201 //************************************************************
203 //************************************************************
204 destructor TWordCount.Destroy;
209 for i := Count - 1 downto 0 do
210 if Objects[ i ] <> nil then
217 //************************************************************
218 // TGikoBayesian class
219 //************************************************************
221 //==============================
223 //==============================
224 constructor TGikoBayesian.Create;
227 Duplicates := dupIgnore;
232 //==============================
234 //==============================
235 destructor TGikoBayesian.Destroy;
240 for i := Count - 1 downto 0 do
241 if inherited Objects[ i ] <> nil then
242 inherited Objects[ i ].Free;
248 procedure TGikoBayesian.LoadFromFile( const filePath : string );
257 if not FileExists( filePath ) then
260 sl := TStringList.Create;
262 sl.LoadFromFile( filePath );
264 for i := 1 to sl.Count - 1 do begin
266 name := RemoveToken( s, #1 );
267 info := TWordInfo.Create;
268 info.NormalWord := StrToIntDef( '$' + RemoveToken( s, #1 ), 0 );
269 info.ImportantWord := StrToIntDef( '$' + RemoveToken( s, #1 ), 0 );
270 info.NormalText := StrToIntDef( '$' + RemoveToken( s, #1 ), 0 );
271 info.ImportantText := StrToIntDef( '$' + RemoveToken( s, #1 ), 0 );
273 AddObject( name, info );
281 procedure TGikoBayesian.SaveToFile( const filePath : string );
289 sl := TStringList.Create;
292 sl.Add( GIKO_BAYESIAN_FILE_VERSION );
294 for i := 0 to Count - 1 do begin
295 info := TWordInfo( inherited Objects[ i ] );
296 s := Strings[ i ] + #1
297 + Format('%x', [info.NormalWord]) + #1
298 + Format('%x', [info.ImportantWord]) + #1
299 + Format('%x', [info.NormalText]) + #1
300 + Format('%x', [info.ImportantText]);
305 sl.SaveToFile( filePath );
312 procedure TGikoBayesian.Save;
315 if FFilePath <> '' then
316 SaveToFile( FFilePath );
320 //==============================
322 //==============================
323 function TGikoBayesian.GetObject( const name : string ) : TWordInfo;
328 idx := IndexOf( name );
332 Result := TWordInfo( inherited Objects[ idx ] );
336 //==============================
338 //==============================
339 procedure TGikoBayesian.SetObject( const name : string; value : TWordInfo );
344 idx := IndexOf( name );
346 AddObject( name, value )
348 inherited Objects[ idx ] := value;
353 //==============================
355 //==============================
356 procedure TGikoBayesian.CountWord(
358 wordCount : TWordCount );
360 Modes = (ModeWhite, ModeGraph, ModeAlpha, ModeNum, ModeHanKana,
361 ModeWGraph, ModeWAlpha, ModeWNum,
362 ModeWHira, ModeWKata, ModeWKanji);
364 p, tail, last : PChar;
365 mode, newMode : Modes;
369 delimiter : TStringList;
372 countInfo : TWordCountInfo;
374 KAKUJOSI = '
\82ð' + #10 + '
\82É' + #10 + '
\82ª' + #10 + '
\82Æ' + #10 + '
\82©
\82ç' +
375 #10 + '
\82Å' + #10 + '
\82Ö' + #10 + '
\82æ
\82è' + #10 + '
\82Ü
\82Å';
378 delimiter := TStringList.Create;
380 //***
\91¬
\93x
\83e
\83X
\83g
\92\86
381 wordCount.Duplicates := dupIgnore;
382 wordCount.CaseSensitive := True;
383 wordCount.Capacity := 1000;
384 wordCount.Sorted := True;
388 delimiter.Text := KAKUJOSI;
389 SetLength( aWord, 256 );
391 tail := p + Length( text );
394 while p < tail do begin
396 //
\95¶
\8e\9a\82Ì
\83^
\83C
\83v
\82ð
\94»
\95Ê
397 //
\81¦
\8bå
\93Ç
\93_
\82Í ModeGraph
\82É
\82È
\82é
\82Ì
\82Å
\8cÂ
\95Ê
\82É
\91Î
\89\9e\82µ
\82È
\82
\82Ä
\82à
\82¢
\82¢
398 if p^ in kYofKanji then begin
399 if p + 1 < tail then begin
400 ch := (PByte( p )^ shl 8) or PByte( p + 1 )^;
402 $8140: newMode := ModeWhite;
403 $8141..$824e: newMode := ModeWGraph;
404 $824f..$8258: newMode := ModeWNum;
405 $8260..$829a: newMode := ModeWAlpha;
406 $829f..$82f1: newMode := ModeWHira;
407 $8340..$8396: newMode := ModeWKata;
408 else newMode := ModeWKanji;
411 newMode := ModeWhite;
416 //
\8bæ
\90Ø
\82è
\82É
\82È
\82é
\95¶
\8e\9a\82ª
\82 \82é
\82©
\8c\9f\8d¸
\82·
\82é
417 if p + 3 < tail then begin // 3 = delimiter
\82Ì
\8dÅ
\91å
\8e\9a\90\94 - 1
418 for i := 0 to delimiter.Count - 1 do begin
420 p, PChar( delimiter[ i ] ), Length( delimiter[ i ] ) ) then begin
422 chSize := Length( delimiter[ i ] );
429 #$0..#$20, #$7f: newMode := ModeWhite;
430 '0'..'9': newMode := ModeNum;
431 'a'..'z', 'A'..'Z': newMode := ModeAlpha;
432 #$A6..#$DD: newMode := ModeHanKana;
433 else newMode := ModeGraph;
439 if (mode <> newMode) or delimited then begin
441 //
\95¶
\8e\9a\82Ì
\83^
\83C
\83v
\82ª
\95Ï
\8dX
\82³
\82ê
\82½
442 //
\82à
\82µ
\82
\82Í
\8bæ
\90Ø
\82è
\82É
\82È
\82é
\95¶
\8e\9a\82É
\91\98\8bö
\82µ
\82½
443 if mode <> ModeWhite then begin
444 aWord := Copy( last, 0, p - last ); //
\8c\83\92x
445 // SetLength( aWord, p - last );
446 // CopyMemory( PChar( aWord ), last, p - last );
447 idx := wordCount.IndexOf( aWord ); //
\8c\83\92x
448 if idx < 0 then begin
449 countInfo := TWordCountInfo.Create;
450 wordCount.AddObject( aWord, countInfo );
452 countInfo := TWordCountInfo( wordCount.Objects[ idx ] );
454 countInfo.WordCount := countInfo.WordCount + 1;
465 if mode <> ModeWhite then begin
466 aWord := Copy( last, 0, p - last );
467 idx := wordCount.IndexOf( aWord );
468 if idx < 0 then begin
469 countInfo := TWordCountInfo.Create;
470 wordCount.AddObject( aWord, countInfo );
472 countInfo := TWordCountInfo( wordCount.Objects[ idx ] );
474 countInfo.WordCount := countInfo.WordCount + 1;
482 //==============================
484 //==============================
485 function TGikoBayesian.CalcPaulGraham( wordCount : TWordCount ) : Extended;
487 function p( const aWord : string ) : Single;
491 info := Objects[ aWord ];
494 else if info.NormalWord = 0 then
496 else if info.ImportantWord = 0 then
499 Result := ( info.ImportantWord / info.ImportantText ) /
500 ((info.NormalWord * 2 / info.NormalText ) +
501 (info.ImportantWord / info.ImportantText));
513 if wordCount.Count = 0 then
516 narray := TList.Create;
518 for i := 0 to wordCount.Count - 1 do begin
519 narray.Add( Pointer( p( wordCount[ i ] ) ) );
522 narray.Sort( AbsSort );
526 i := min( SAMPLE_COUNT, narray.Count );
529 s := s * Single( narray[ i ] );
530 q := q * (1 - Single( narray[ i ] ));
533 Result := s / (s + q);
540 //==============================
542 //==============================
543 function TGikoBayesian.CalcGaryRobinson( wordCount : TWordCount ) : Extended;
545 function p( const aWord : string ) : Single;
549 info := Objects[ aWord ];
552 else if info.ImportantWord = 0 then
554 else if info.NormalWord = 0 then
557 Result := ( info.ImportantWord / info.ImportantText ) /
558 ((info.NormalWord / info.NormalText ) +
559 (info.ImportantWord / info.ImportantText));
562 function f( cnt : Integer; n, mean : Single ) : Extended;
566 Result := ( (k * mean) + (cnt * n) ) / (k + cnt);
571 narray : array of Single;
573 countInfo : TWordCountInfo;
576 important : Extended;
580 if wordCount.Count = 0 then begin
585 SetLength( narray, wordCount.Count );
587 for i := 0 to wordCount.Count - 1 do begin
588 n := p( wordCount[ i ] );
592 mean := mean / wordCount.Count;
597 for i := 0 to wordCount.Count - 1 do begin
598 countInfo := TWordCountInfo( wordCount.Objects[ i ] );
599 n := f( countInfo.WordCount, narray[ i ], mean );
600 normal := normal * n;
601 important := important * (1 - n);
602 if countInfo <> nil then
603 cnt := cnt + countInfo.WordCount;
607 normal := 1 - Exp( Ln( normal ) * (1 / cnt) );
608 important := 1 - Exp( Ln( important ) * (1 / cnt) );
610 n := (important - normal+ 0.00001) / (important + normal + 0.00001);
611 Result := (1 + n) / 2;
615 //==============================
617 //==============================
618 function TGikoBayesian.Parse(
620 wordCount : TWordCount;
621 algorithm : TGikoBayesianAlgorithm = gbaGaryRonbinson
625 CountWord( text, wordCount );
627 gbaPaulGraham: Result := CalcPaulGraham( wordCount );
628 gbaGaryRonbinson: Result := CalcGaryRobinson( wordCount );
634 //==============================
636 //==============================
637 procedure TGikoBayesian.Learn(
638 wordCount : TWordCount;
639 isImportant : Boolean );
642 wordinfo : TWordInfo;
643 countinfo : TWordCountInfo;
647 for i := 0 to wordCount.Count - 1 do begin
648 aWord := wordCount[ i ];
649 wordinfo := Objects[ aWord ];
650 if wordinfo = nil then begin
651 wordinfo := TWordInfo.Create;
652 Objects[ aWord ] := wordinfo;
655 countinfo := TWordCountInfo( wordCount.Objects[ i ] );
656 if isImportant then begin
657 wordinfo.ImportantWord := wordinfo.ImportantWord + countinfo.WordCount;
658 wordinfo.ImportantText := wordinfo.ImportantText + 1;
660 wordinfo.NormalWord := wordinfo.NormalWord + countinfo.WordCount;
661 wordinfo.NormalText := wordinfo.NormalText + 1;
667 //==============================
669 //==============================
670 procedure TGikoBayesian.Forget(
671 wordCount : TWordCount;
672 isImportant : Boolean );
675 wordinfo : TWordInfo;
676 countinfo : TWordCountInfo;
680 for i := 0 to wordCount.Count - 1 do begin
681 aWord := wordCount[ i ];
682 wordinfo := Objects[ aWord ];
683 if wordinfo = nil then
686 countinfo := TWordCountInfo( wordCount.Objects[ i ] );
687 if isImportant then begin
688 wordinfo.ImportantWord := wordinfo.ImportantWord - countinfo.WordCount;
689 wordinfo.ImportantText := wordinfo.ImportantText - 1;
691 wordinfo.NormalWord := wordinfo.NormalWord - countinfo.WordCount;
692 wordinfo.NormalText := wordinfo.NormalText - 1;