SRC:DELPHI TConvertText
От: &reY Украина http://www.livejournal.com/~1000turov/
Дата: 12.11.02 11:23
Оценка: 6 (2)
{ ****************************************************************** }
{                                                                    }
{   VCL component TConvertText                                       }
{                                                                    }
{                                                                    }
{   Code generated by Component Create for Delphi                    }
{                                                                    }
{   Generated from source file shutdown.cd                           }
{   on 29 Mar 2001 at 21:36                                          }
{                                                                    }
{   Copyright © 2001 by VIC   Stepanets Serge                        }
{   mail : vic_774@rambler.ru                                        }
{ ****************************************************************** }

Unit ConvertText;

interface

uses
  windows, sysutils, classes, dsgnintf;

type
  //TCode = (win, koi, iso, dos);
  TCode = (dos, win, koi, iso, gost, mac);
  //TCode = (dos, win, koi, iso, gost, mac, bolg);

  TMode = (Auto , Manual);
const
  //CodeStrings: array [TCode] of String[3] = ('win','koi','iso','dos');
  CodeStrings: array [TCode] of String[4] = ('dos','win','koi','iso','gost','mac');
  //CodeStrings: array [TCode] of String[4] = ('dos','win','koi','iso','gost','mac','bolg');
type
  TConvertText = class(TComponent)
  private
    { Private Declarations }
    FConvertMode: TMode;
    FFromCharSet: TCode;
    FToCharSet: TCode;
    FText: string;
  protected
    { Protected Declarations }
  public
    { Public Declarations }
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
    procedure Run;
  published
    property ConvertMode: TMode read FConvertMode write FConvertMode;
    property FromCharSet: TCode read FFromCharSet write FFromCharSet;
    property ToCharSet: TCode read FToCharSet write FToCharSet;
    property Text: string read FText write FText;
  end;

procedure Register;

implementation

{--------------------------------------}
{ Convert.Create                       }
{--------------------------------------}
constructor TConvertText.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  FConvertMode := Auto;
  FFromCharSet := win;
  FToCharSet := win;
end;

{--------------------------------------}
{ Convert.Destroy                      }
{--------------------------------------}
destructor TConvertText.Destroy;
begin
  inherited Destroy;
end;

{--------------------------------------}
{ Convert.Execute                      }
{--------------------------------------}

procedure TConvertText.Run;
label _convert;
{
type
  TCode = (win, koi, iso, dos);
const
  CodeStrings: array [TCode] of String = ('win','koi','iso','dos');
}
var
  str:    array [TCode] of string;
  norm:   array ['А'..'я'] of single;
  code1:  TCode;
  code2:  TCode;
//  code_1: TCode;
//  code_2: TCode;
  count:  array [char] of integer;
  d:      single;
  tmp:    single;
  s:      string;
  so:     string;
  chars:  array [char] of char;
  c:      char;
  i:      integer;
begin
  if FConvertMode <> Auto then goto _convert;
  so := FText;

  norm['А'] := 0.001;
  norm['Б'] := 0;
  norm['В'] := 0.002;
  norm['Г'] := 0;
  norm['Д'] := 0.001;
  norm['Е'] := 0.001;
  norm['Ж'] := 0;
  norm['З'] := 0;
  norm['И'] := 0.001;
  norm['Й'] := 0;
  norm['К'] := 0.001;
  norm['Л'] := 0;
  norm['М'] := 0.001;
  norm['Н'] := 0.001;
  norm['О'] := 0.001;
  norm['П'] := 0.002;
  norm['Р'] := 0.002;
  norm['С'] := 0.001;
  norm['Т'] := 0.001;
  norm['У'] := 0;
  norm['Ф'] := 0;
  norm['Х'] := 0;
  norm['Ц'] := 0;
  norm['Ч'] := 0.001;
  norm['Ш'] := 0.001;
  norm['Щ'] := 0;
  norm['Ъ'] := 0;
  norm['Ы'] := 0;
  norm['Ь'] := 0;
  norm['Э'] := 0.001;
  norm['Ю'] := 0;
  norm['Я'] := 0;
  norm['а'] := 0.057;
  norm['б'] := 0.01;
  norm['в'] := 0.031;
  norm['г'] := 0.011;
  norm['д'] := 0.021;
  norm['е'] := 0.067;
  norm['ж'] := 0.007;
  norm['з'] := 0.013;
  norm['и'] := 0.052;
  norm['й'] := 0.011;
  norm['к'] := 0.023;
  norm['л'] := 0.03;
  norm['м'] := 0.024;
  norm['н'] := 0.043;
  norm['о'] := 0.075;
  norm['п'] := 0.026;
  norm['р'] := 0.038;
  norm['с'] := 0.034;
  norm['т'] := 0.046;
  norm['у'] := 0.016;
  norm['ф'] := 0.001;
  norm['х'] := 0.006;
  norm['ц'] := 0.002;
  norm['ч'] := 0.011;
  norm['ш'] := 0.004;
  norm['щ'] := 0.004;
  norm['ъ'] := 0;
  norm['ы'] := 0.012;
  norm['ь'] := 0.012;
  norm['э'] := 0.003;
  norm['ю'] := 0.005;
  norm['я'] := 0.015;
  {
  Str[win] := 'АаБбВвГгДдЕеЖжЗзИиЙйКкЛлМмНнОоПпРрСсТтУуФфХхЦцЧчШшЩщЪъЫыЬьЭэЮюЯя';
  Str[koi] := 'юЮаАбБцЦдДеЕфФгГхХиИйЙкКлЛмМнНоОпПяЯрРсСтТуУжЖвВьЬыЫзЗшШэЭщЩчЧъЪ';
  Str[iso] := 'РрСсТтУуФфХхЦцЧчШшЩщЪъЫыЬьЭэЮюЯяа№бёвђгѓдєеѕжізїијйљкњлћмќн§оўпџ';
  Str[dos] := 'Ђ ЃЎ‚ўѓЈ„¤…Ґ†¦‡§€Ё‰©ЉЄ‹"Њ¬ЌЋ®ЏЇђа'+Chr(39)+'б'+Chr(39)+'в“г”д•е–ж—з?и™йљк›лњмќнћоџп';
  }
  // CONVERT from/to WIN(cp1251), ALT(cp866), KOI-8(cp787), MAC, ISO 8859-5, ѓЋ‘’ */

  Str[dos]  := '.+-|ЂЃ‚ѓ„…†‡€‰Љ‹ЊЌЋЏђ‘’“”•–—?™љ›њќћџ ЎўЈ¤Ґ¦§Ё©Є«¬®Ї°±Ііґµ¶·ё№є»јЅѕїАБВГДЕЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯабвгдежзийклмнопрстуфхцчшщъыьэюя';
  Str[win]  := '.+-|АБВГДЕЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯабвгдежзийклмноп...|++++++|+++++++++-++++++++-+++++++++++++.....рстуфхцчшщъыьэюяЁё..ЈҐ.......¤..';
  Str[koi]  := '.+-|бвчздецъйклмнопртуфхжигюыэящшьасБВЧЗДЕЦЪЙКЛМНОПРђ‘’Ѓ‡Іґ§¦µЎЁ®¬ѓ„‰€†ЂЉЇ°«Ґ»ё± ѕ№є¶·Є©ў¤Ѕј…‚ЌЊЋЏ‹ТУФХЖИГЮЫЭЯЩШЬАСіЈ™?“›џ—њћ•–љќ”ї';
  Str[iso]  := '.+-|°±Ііґµ¶·ё№є»јЅѕїАБВГДЕЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯ°±Ііґµ¶·ё№є»јЅѕїАБВГДЕЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯабвгдежзийклмнопўстуфхцчшщъыьэюя';
  Str[gost] := '.+-|ПРСµ¶·ёТУФХЅѕЖЗЦЙ»јИНєЛ№КМО°±ІЧШЪїЩАДіВґБГЕЫЬЭЮЯЂЃ‚ѓ„…†‡€‰Љ‹ЊЌЋЏђ‘’“”•–—?™љ›њќћџ ЎўЈ¤Ґ¦§Ё©Є«¬®Їабвгдежзийклмнопрстуфхцчшщъыьэюя';
  Str[mac]  := '.+-|ЂЃ‚ѓ„…†‡€‰Љ‹ЊЌЋЏђ‘’“”•–—?™љ›њќћџабвгдежзийклмнопЪЪЫ|+#ёё#######В++++С+##################++ЫЬЭЮЯрстуфхцчшщъыьэюЯЭЮўЈ¤Ґ¦§Ё©Ц±ЬяҐя';
  //Str[bolg] := '.+-|ЂЃ‚ѓ„…†‡€‰Љ‹ЊЌЋЏђ‘’“”•–—?™љ›њќћџ ЎўЈ¤Ґ¦§Ё©Є«¬®ЇРСТУФабвЖЗЧШгдПАБВГДЕежИЙКЛМНОзийклмнопЦЩЪЫЬЭЮЯ°±Ііґµ¶·ё№є»јЅѕїрстуфхцчшщъыьэюя';


  for c := #0 to #255 do
    Chars[c] := c;

  //code_1 := win;
  //code_2 := win;
  tmp := 0;
  s := so;
  fillchar(count, sizeof(count), 0);
  for i := 1 to Length(s) do
    inc(count[s[i]]);
  for c := 'А' to 'я' do
    tmp := tmp + sqr(count[c] / Length(s) - norm[c]);
  for code1 := low(TCode) to high(TCode) do begin
    for code2 := low(TCode) to high(TCode) do begin
      if code1 = code2 then continue;

      s := so;
      for i := 1 to Length(Str[win]) do
        Chars[Str[code2][i]] := Str[code1][i];
      for i := 1 to Length(s) do
        s[i] := Chars[s[i]];
      fillchar(count, sizeof(count), 0);
      for i := 1 to Length(s) do
        inc(count[s[i]]);
      d := 0;
      for c := 'А' to 'я' do
        d := d + sqr(count[c] / Length(s) - norm[c]);
      if d < tmp then begin
        //code_1 := code1;
        //code_2 := code2;
        FFromCharSet := code2;
        FToCharSet := code1;
        tmp := d;
      end;
    end;
  end;

//
//        конвертация текста
//

_convert:
  //code_2 := FFromCharSet;
  //code_1 := FToCharSet;
  s := FText;
  if FToCharSet <> FFromCharSet then begin
    for c := #0 to #255 do
      Chars[c] := c;

    for i := 1 to Length(Str[win]) do
        Chars[Str[FFromCharSet][i]] := Str[FToCharSet][i];

    for i := 1 to Length(s) do
      s[i] := Chars[s[i]];
  end;
  //Form1.Caption := CodeStrings[code_2] + ' ' + CodeStrings[code_1];

  FText := s;
end;

{--------------------------------------}
{ Register                             }
{--------------------------------------}
procedure Register;
begin
  RegisterComponents('VIC', [TConvertText]);
end;

end.
 
Подождите ...
Wait...
Пока на собственное сообщение не было ответов, его можно удалить.