English Text in capital letters to lower case

8 replies
Goto Page
To the start Previous 1 Next To the start
Up
GeoB
Moderator
Offline Off
Was thinking around about making a simple script which converts all the text in capital letters to lower case one. string.lower will do the job as it is already explained there in Lua pil tutorial that it converts any type of arguments or sentences into lower case.

To begin with, here's the code:
Code:
1
2
3
4
5
6
function NoCaps(id, text)
     OutPutSayFunction(id, text:lower())
     msg2(id,string.char(169) .. "240128128Please refrain at using Caps-Lock while typing! All the text in capital letters are now converted to lower case.")
end

addhook("say","NoCaps")

As you can see, in 2 line the output function has two arguments. The ID of a player and the text defined to string.lower whenever a player says something but in caps though so it'll be converted. Although there's a problem with this script. CS2D triggered an error and Console printed that on 2 line, OutPutSayFunction attempted to call globally (a nil value). I already know why it does come up with this issue so you don't have to tell me that. The thing is, I don't have a clue what's the best trick to check for the output when a player types in capital letters. Was also rolling around with cs2d lua cmd msg as well and adding string.lower in it yet no results whatsoever.

I've seen also a thread where someone asked a script similar of this. Though the user who posted the code is pretty much similar in coding terms (outputsayfunction thingy).

Any thoughts about this? Is there a way somehow?
13.02.16 06:37:18 pm
Up
_Yank
User
Offline Off
So you want to check if the player wrote any capital letter on his message ?
13.02.16 06:56:39 pm
Up
GeoB
Moderator
Offline Off
Yes. Whenever someone tries to type something but in capital letters it'll be converted in small / lower case.
13.02.16 06:58:26 pm
Up
TimeQuesT
User
Offline Off
Here we go!

Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
--Returns 'true' whenever the amount of found capital letters per cent is bigger or equal than the parameter percent
--range for percent is 0.0 to 1.0
--teh_string is the string to search through

function IsCapital(teh_string, percent)
     local capital_found = 0.0;
     for i = 1, string.len(teh_string) do
          local bait = string.byte(teh_string, i);
          if (bait >= 65 and bait <= 90) then --'A' to 'Z'
               capital_found = capital_found + 1;
          end
     end

     return ((capital_found / string.len(teh_string)) >= percent);
end


Example usage >


(Be aware that special letters like space or brackets are included in the calculation. You can optimize this function by exclude them from the calculation. Simply count how many Arabic letters are in there.)
Muh
13.02.16 07:10:26 pm
Up
Or you can use this code below, which searches and detects for the capital letters from the given start point.
As I put a variable to have the start's point value. 2 means it starts from the second letter in the text you are saying.
I know it's a bit basic and somehow buggy.

Code:
1
2
3
4
5
6
7
8
9
start_point = 2;

addhook("say", "_say")
function _say(id, txt)
     if string.find(txt, "%u", start_point) then
          msg("Please don't use the Caps lock!");
          return 1;
     end
end
I don't know what I'm doing with my life.
13.02.16 07:17:44 pm
Up
VADemon
User
Offline Off
Code:
1
2
3
4
5
6
text = "HellO:D My FrieNd!"
local count = 0
for ucase in text:gmatch("%u") do 
     print(ucase)
     count = count + 1
end

I thought user TimeQuesT's solution might not be very fast since it's using Lua to iterate through single characters, so I came up with gmatch matching upper-case letters. Should be faster.
Moreover, probably it has less potential bugs because it doesn't determine itself which letters are to be considered upper-case, instead Lua does.

UPD: @user THEMUD: Are you gonna forbid using any uppercase letters with string.find?
13.02.16 07:23:50 pm
Up
GeoB
Moderator
Offline Off
Thanks for the quick answers so far. Tried some of scripts posted above and it work. I guess now I got the point somehow to check when a player types a text in upper case.
13.02.16 07:25:26 pm
Up
TimeQuesT
User
Offline Off
user VADemon is right, but since there is not a message every frame and messages are limited in size this will not have any critical impact on the performance.

user GeoB you might replace the "counting" part in "IsCapital" with the method user VADemon mentioned.
Muh
13.02.16 10:50:30 pm
Up
TopNotch
User
Offline Off
@user GeoB: You might check this, if you want to know more about string patterns.
To the start Previous 1 Next To the start