#11 2009-12-21 22:53

ThanhLoan
Member
Registered: 2009-12-17
Posts: 17

Re: RegEx to extract first letter of every words in a song name

Hi Stefan,
In your script, if I need to replace the special Vietnamese character 'Đ' by 'D', what do I need to add in the loop ?
This must be a simple IF statement but I don't know the syntax.
I tried to add the statement as follows :

    If FirstChar :='Đ' Then FirstChar :='D';
    FirstChars := FirstChars + FirstChar;

but I got an error message when compiling.


Please advise

Thank you so much.

Offline

#12 2009-12-21 23:28

Andrew
Senior Member
Registered: 2008-05-22
Posts: 542

Re: RegEx to extract first letter of every words in a song name

No need to apologize at all! It's just that I wanted to know if my script worked as well, 'cos if it didn't, I'd have removed it so as not to confuse someone else with a similar problem. Also, re. replacing all instances of 'Đ' with 'D', why not simply use another Replace rule? If you do want to include it in the script however, use this:

if(FirstChar = 'Ð') then FirstChar := 'D';

You almost had it, except that in Pascal '=' is the comparison operator and ':=' is the assignment operator.

Offline

#13 2009-12-21 23:40

Stefan
Moderator
From: Germany, EU
Registered: 2007-10-23
Posts: 1,161

Re: RegEx to extract first letter of every words in a song name

Hi Loan
":=" is an assignment command, var := 'new content'
You should us "=" to do an comparsion, IF var=1 Then

So If FirstChar ='Ð' Then FirstChar :='D';
should work. You can test that this works by using
    If FirstChar ='T' Then FirstChar :='D';
but as i see this didn't works with unicode 'Ð'

In some script languages one can use an construct like "/xnnnn" to access
an unicode char... i have to read the help/wiki to verify this.
Do you have the unicode number representation for 'Ð' ?

EDIT:
Ahh, i found it will searching the great help (thanks guys)
If WideToAnsi(FirstChar) ='Ð' Then FirstChar :='D';


Tested with
If WideToAnsi(FirstChar) ='Ð' Then FirstChar :='X';
Asia Karaoke 49_14-Chí Tâm+Ngọc Huyền-Tạ Từ Trong Đêm Dtest Tân Cổ Giao Duyên.mkv   
Asia Karaoke 49_14-Chí Tâm+Ngọc Huyền-Tạ Từ Trong Đêm Dtest Tân Cổ Giao Duyên-TTTXDTCGD.mkv

  for I:=0 to Length(Parts)-1 do
  begin
    FirstChar := WideCopy(Parts[i],1,1);
    If WideToAnsi(FirstChar) ='Ð' Then FirstChar :='D';
    FirstChars := FirstChars + FirstChar;
  end;

Read the  *WIKI* for HELP + MANUAL + Tips&Tricks.
If ReNamer had helped you, please *DONATE* to Denis or buy a PRO license. (Read *Lite vs Pro*)

Offline

#14 2009-12-22 02:35

ThanhLoan
Member
Registered: 2009-12-17
Posts: 17

Re: RegEx to extract first letter of every words in a song name

Thank you so much Stefan for your great findings..it works perfectly.
Andrew : I could have used  simple replace but the problem I am facing with is I want to keep the 'Ð' in the songname and only replace the 'Ð' in the abbreviated songname. I use the WD TV Player to search for abbreviated songname and the WD TV does not have on-screen Unicode keyboard.
Stefan : I have already renamed all my files and the new names now have the abbreviated (first letter of the words) songname.
Now I need to replace the special character in this abbreviated song name TTTÐTCGD. The special character can be at any position in the abbreviated songname.
The new name have 4 fields separated by '-' and the abbreviated songname is after the third '-' and just before '.mkv'
May I ask you to modify your script to do this for me please.

Once again, many thanks for your help.

Last edited by ThanhLoan (2009-12-22 15:38)

Offline

#15 2009-12-22 04:40

Stefan
Moderator
From: Germany, EU
Registered: 2007-10-23
Posts: 1,161

Re: RegEx to extract first letter of every words in a song name

I have tried to solve this by using
WideReplaceStr(Part2,'Ð','D');
WideReplaceText(Part2,'Ð','D');
ReplaceRegEx(Base,'Ð','D',false,false);
but without success  hmm

Maybe Denis can re-check this in January if this is an bug or i am to stupid. (But unfortunately i lost the code)


So i have tested the Part2 char-by-char if it is 'Ð', and this seams to work:

(always test with demo files first)

// http://www.den4b.com/forum/viewtopic.php?pid=4133#p4133
//Change one special unicode char after last '-' to ansi (Ð to D):
//FROM:
//Asia Karaoke 49_14-Chí Tâm+Ng?c Huy?n-T? T? Trong Ðêm Dtest Tân C? Giao Duyên-TTTÐDTCGD.mkv
//TO:
//Asia Karaoke 49_14-Chí Tâm+Ng?c Huy?n-T? T? Trong Ðêm Dtest Tân C? Giao Duyên-TTTDDTCGD.mkv

var
  I: Integer;
  Parts: TStringsArray;
  Base, Part1, Part2, Part2new, NextChar: WideString;
  
begin
  Base := WideExtractBaseName(FileName);
  
  //Find last '-' by greedy RegEx:
  Parts := SubMatchesRegEx(Base, '(.+-)(.+)', FALSE);
  If (Length(Parts) <=0) then exit;
  
  //Split file name into two:
  Part1 := WideCopy(Base, 1, Length(Parts[0]));
  Part2 := WideCopy(Base, Length(Parts[0]) +1, 999);

  //WideShowMessage(Part1 + ' ~~~ ' + Part2);
  //Part1 => Asia Karaoke 49_14-Chí Tâm+Ng?c Huy?n-T? T? Trong Ðêm Dtest Tân C? Giao Duyên-
  //Part2 => TTTÐDTCGD

  //check each char of Part2 for 'Ð':
  For I:=1 to Length(Part2)-1 do
  begin
    NextChar := Part2[i];
    If WideToAnsi(NextChar) ='Ð' Then NextChar :='D';
    Part2new := Part2new + NextChar;
  end;
  
  //Test:
  WideShowMessage(Part1 + Part2new + WideExtractFileExt(FileName));
  
  //Enable renaming:
  //FileName := Part1  + Part2new + WideExtractFileExt(FileName);
  Part2new := '';  
end.

Last edited by Stefan (2009-12-22 04:45)


Read the  *WIKI* for HELP + MANUAL + Tips&Tricks.
If ReNamer had helped you, please *DONATE* to Denis or buy a PRO license. (Read *Lite vs Pro*)

Offline

#16 2009-12-22 15:43

ThanhLoan
Member
Registered: 2009-12-17
Posts: 17

Re: RegEx to extract first letter of every words in a song name

Magical Stefan ! Thank you so much !
I would like to take the opportunity to convey all my best wishes to Stefan, Andrew and the whole team.
Have a wonderful Holiday Season and a very Happy New year !

Offline

#17 2009-12-22 16:51

ThanhLoan
Member
Registered: 2009-12-17
Posts: 17

Re: RegEx to extract first letter of every words in a song name

Hi Stefan,
I tried out the script and it worked fine with with the special Unicode 'Đ'.
However, some other special Unicode characters are not recognized in the Pascalscript rule.
It shows '?' for those.

Any idea ?

Here are the special characters I'd like to replace :

Đđ ÁÀẢÃẠĂÂẨẪẤẤ ÉÈÊỂỄẾỀÍ ÓÒỎÕÔỐỒỔỖƠỚỜỞỠ ÚỦỦŨỨỪ ÝỲỶỸ

and the portion of the script for this purpose :

// D
    If WideToAnsi(NextChar) ='Ð' Then NextChar :='D';
    If WideToAnsi(NextChar) ='đ' Then NextChar :='D';
// A
    If WideToAnsi(NextChar) ='À' Then NextChar :='A';
    If WideToAnsi(NextChar) ='Á' Then NextChar :='A';
    If WideToAnsi(NextChar) ='Ả' Then NextChar :='A';
    If WideToAnsi(NextChar) ='Ã' Then NextChar :='A';
    If WideToAnsi(NextChar) ='Ạ' Then NextChar :='A';
    If WideToAnsi(NextChar) ='Ă' Then NextChar :='A';
    If WideToAnsi(NextChar) ='Â' Then NextChar :='A';
    If WideToAnsi(NextChar) ='Ẩ' Then NextChar :='A';
    If WideToAnsi(NextChar) ='Ẫ' Then NextChar :='A';
    If WideToAnsi(NextChar) ='Ấ' Then NextChar :='A';
    If WideToAnsi(NextChar) ='Ầ' Then NextChar :='A';
// E
    If WideToAnsi(NextChar) ='É' Then NextChar :='E';
    If WideToAnsi(NextChar) ='È' Then NextChar :='E';
    If WideToAnsi(NextChar) ='Ê' Then NextChar :='E';
    If WideToAnsi(NextChar) ='Ể' Then NextChar :='E';
    If WideToAnsi(NextChar) ='Ễ' Then NextChar :='E';
    If WideToAnsi(NextChar) ='Ế' Then NextChar :='E';
    If WideToAnsi(NextChar) ='Ề' Then NextChar :='E';
// I
    If WideToAnsi(NextChar) ='Í' Then NextChar :='I';
// O
    If WideToAnsi(NextChar) ='Ó' Then NextChar :='O';
    If WideToAnsi(NextChar) ='Ò' Then NextChar :='O';
    If WideToAnsi(NextChar) ='Ỏ' Then NextChar :='O';
    If WideToAnsi(NextChar) ='Õ' Then NextChar :='O';
    If WideToAnsi(NextChar) ='Ô' Then NextChar :='O';
    If WideToAnsi(NextChar) ='Ố' Then NextChar :='O';
    If WideToAnsi(NextChar) ='Ồ' Then NextChar :='O';
    If WideToAnsi(NextChar) ='Ổ' Then NextChar :='O';
    If WideToAnsi(NextChar) ='Ỗ' Then NextChar :='O';
    If WideToAnsi(NextChar) ='Ơ' Then NextChar :='O';
    If WideToAnsi(NextChar) ='Ớ' Then NextChar :='O';
    If WideToAnsi(NextChar) ='Ờ' Then NextChar :='O';
    If WideToAnsi(NextChar) ='Ở' Then NextChar :='O';
    If WideToAnsi(NextChar) ='Ỡ' Then NextChar :='O';
// U
    If WideToAnsi(NextChar) ='Ú' Then NextChar :='U';
    If WideToAnsi(NextChar) ='Ù' Then NextChar :='U';
    If WideToAnsi(NextChar) ='Ủ' Then NextChar :='U';
    If WideToAnsi(NextChar) ='Ũ' Then NextChar :='U';
    If WideToAnsi(NextChar) ='Ứ' Then NextChar :='U';
    If WideToAnsi(NextChar) ='Ừ' Then NextChar :='U';
// Y
    If WideToAnsi(NextChar) ='Ý' Then NextChar :='Y';
    If WideToAnsi(NextChar) ='Ỳ' Then NextChar :='Y';
    If WideToAnsi(NextChar) ='Ỷ' Then NextChar :='Y';
    If WideToAnsi(NextChar) ='Ỹ' Then NextChar :='Y';

and here is what Renamer Pascalscript rule shows :

//D
    If WideToAnsi(NextChar) ='Ð' Then NextChar :='D';
    If WideToAnsi(NextChar) ='d' Then NextChar :='D';
//A
    If WideToAnsi(NextChar) ='À' Then NextChar :='A';
    If WideToAnsi(NextChar) ='Á' Then NextChar :='A';
    If WideToAnsi(NextChar) ='?' Then NextChar :='A';
    If WideToAnsi(NextChar) ='Ã' Then NextChar :='A';
    If WideToAnsi(NextChar) ='?' Then NextChar :='A';
    If WideToAnsi(NextChar) ='A' Then NextChar :='A';
    If WideToAnsi(NextChar) ='Â' Then NextChar :='A';
    If WideToAnsi(NextChar) ='?' Then NextChar :='A';
    If WideToAnsi(NextChar) ='?' Then NextChar :='A';
    If WideToAnsi(NextChar) ='?' Then NextChar :='A';
    If WideToAnsi(NextChar) ='?' Then NextChar :='A';
//E
    If WideToAnsi(NextChar) ='É' Then NextChar :='E';
    If WideToAnsi(NextChar) ='È' Then NextChar :='E';
    If WideToAnsi(NextChar) ='Ê' Then NextChar :='E';
    If WideToAnsi(NextChar) ='?' Then NextChar :='E';
    If WideToAnsi(NextChar) ='?' Then NextChar :='E';
    If WideToAnsi(NextChar) ='?' Then NextChar :='E';
    If WideToAnsi(NextChar) ='?' Then NextChar :='E';
//I
    If WideToAnsi(NextChar) ='Í' Then NextChar :='I';
//O
    If WideToAnsi(NextChar) ='Ó' Then NextChar :='O';
    If WideToAnsi(NextChar) ='Ò' Then NextChar :='O';
    If WideToAnsi(NextChar) ='?' Then NextChar :='O';
    If WideToAnsi(NextChar) ='Õ' Then NextChar :='O';
    If WideToAnsi(NextChar) ='Ô' Then NextChar :='O';
    If WideToAnsi(NextChar) ='?' Then NextChar :='O';
    If WideToAnsi(NextChar) ='?' Then NextChar :='O';
    If WideToAnsi(NextChar) ='?' Then NextChar :='O';
    If WideToAnsi(NextChar) ='?' Then NextChar :='O';
    If WideToAnsi(NextChar) ='O' Then NextChar :='O';
    If WideToAnsi(NextChar) ='?' Then NextChar :='O';
    If WideToAnsi(NextChar) ='?' Then NextChar :='O';
    If WideToAnsi(NextChar) ='?' Then NextChar :='O';
    If WideToAnsi(NextChar) ='?' Then NextChar :='O';
//U
    If WideToAnsi(NextChar) ='Ú' Then NextChar :='U';
    If WideToAnsi(NextChar) ='Ù' Then NextChar :='U';
    If WideToAnsi(NextChar) ='?'Then NextChar :='U';
    If WideToAnsi(NextChar) ='U' Then NextChar :='U';
    If WideToAnsi(NextChar) ='?' Then NextChar :='U';
    If WideToAnsi(NextChar) ='?' Then NextChar :='U';
// Y
    If WideToAnsi(NextChar) ='Ý' Then NextChar :='Y';
    If WideToAnsi(NextChar) ='?' Then NextChar :='Y';
    If WideToAnsi(NextChar) ='?' Then NextChar :='Y';
    If WideToAnsi(NextChar) ='?' Then NextChar :='Y';
    Part2new := Part2new + NextChar;

Seems that Renamer can't rezcognize certain Unicode characters...

Offline

#18 2009-12-22 17:00

ThanhLoan
Member
Registered: 2009-12-17
Posts: 17

Re: RegEx to extract first letter of every words in a song name

Just one more observation...
The script was pasted OK into Pascalscript but the '?'s  show up at compiling...

Offline

#19 2009-12-22 18:03

Stefan
Moderator
From: Germany, EU
Registered: 2007-10-23
Posts: 1,161

Re: RegEx to extract first letter of every words in a song name

You just asked for the 'Ð' ,   roll  if you want to convert
all chars to ANSI (at it seams as i just fly over your long list) i would try:

  For I:=1 to Length(Part2)-1 do
  begin
    Part2new := Part2new + WideToAnsi(Part2[i]);
  end;

and WideToAnsi() will convert all chars of Part2 into ANSI.
(Not tested yet)



TL> Seems that Renamer can't rezcognize certain Unicode characters...
I don't know. I never have the need to use unicode, maybe other experts will turn in after
the season holidays to bring in some light?



TL> Have a wonderful Holiday Season and a very Happy New year !
You too.


Read the  *WIKI* for HELP + MANUAL + Tips&Tricks.
If ReNamer had helped you, please *DONATE* to Denis or buy a PRO license. (Read *Lite vs Pro*)

Offline

#20 2009-12-22 18:15

Stefan
Moderator
From: Germany, EU
Registered: 2007-10-23
Posts: 1,161

Re: RegEx to extract first letter of every words in a song name

Oh, and i just see in my last scripts this
For I:=1 to Length(Part2)-1 do
which is wrong as we forget the very last char to convert. It should be in the last scripts
For I:=1 to Length(Part2) do


Read the  *WIKI* for HELP + MANUAL + Tips&Tricks.
If ReNamer had helped you, please *DONATE* to Denis or buy a PRO license. (Read *Lite vs Pro*)

Offline

Board footer

Powered by FluxBB