Raised This Month: $51 Target: $400
 12% 

How to check letters and words in sentences?


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
abdobiskra
Veteran Member
Join Date: Jul 2014
Location: Algeria
Old 05-06-2023 , 16:53   How to check letters and words in sentences?
Reply With Quote #1

Hi,
1) I would like to know how can I know if there are extraneous words in a sentence (For example, using two languages in the same sentence in the chat)
2) How can I check if the letter is at the beginning, middle or end of a word? (in chat too)
__________________

Last edited by abdobiskra; 05-06-2023 at 16:53.
abdobiskra is offline
Send a message via Skype™ to abdobiskra
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 05-06-2023 , 17:12   Re: How to check letters and words in sentences?
Reply With Quote #2

You can use multiple arrays, one for each language, and load them with common words for that language. Then check if one or more words from any of the language arrays exist in the same body of text.

strfind() returns the position of a string in another string. It'll work for an individual character, too.
__________________

Last edited by Bugsy; 05-06-2023 at 17:46.
Bugsy is offline
abdobiskra
Veteran Member
Join Date: Jul 2014
Location: Algeria
Old 05-07-2023 , 12:37   Re: How to check letters and words in sentences?
Reply With Quote #3

Thanks It would be good if you give me an example so I'm not good at it.
__________________
abdobiskra is offline
Send a message via Skype™ to abdobiskra
lexzor
Veteran Member
Join Date: Nov 2020
Old 05-07-2023 , 13:45   Re: How to check letters and words in sentences?
Reply With Quote #4

look, here is an example.

the words are in 2 string cells.

at every say/say_team event, you hook a function that takes what player says

after this, you must iterate through all language variables, trying to find each element of them in the player sentence.

if it's find, make that variable true and break the loop.

PHP Code:
#include <amxmodx>
#include <regex>

#define TOTAL_LANGUAGES 2

new const ROMANIAN_WORDS[] =
{
    
"find",
    
"arunca"
}

new const 
ENGLISH_WORDS[] =
{
    
"gaseste",
    
"throw"
}


enum (+= 1)
{
    
ROMANIAN 0,
    
ENGLISH
}

public 
plugin_init()
{
    
register_clcmd("say""sayHook")
    
register_clcmd("say_team""sayHook")
}

public 
sayHook(id)
{
    static 
szArg[192]
    
read_args(szArgcharsmax(szArg))
    
remove_quotes(szArg)

    static 
bool:bLangs[TOTAL_LANGUAGES]

    for(new 
0sizeof(ROMANIAN_WORDS); j++)
    {
        if(
containi(szArgROMANIAN_WORDS[j]) != -1)
        {
            
bLangs[ROMANIAN] = true  
            
break
        }
    }

    for(new 
0sizeof(ENGLISH_WORDS); j++)
    {
        if(
containi(szArgENGLISH_WORDS[j]) != -1)
        {
            
bLangs[ENGLISH] = true
            
break
        }
    }

    
client_print(idprint_chat"Romanian words: %s; English words: %s"bLangs[ROMANIAN] ? "yes" "no"bLangs[ENGLISH] ? "yes" "no")

    return 
PLUGIN_CONTINUE;

lexzor is offline
abdobiskra
Veteran Member
Join Date: Jul 2014
Location: Algeria
Old 05-08-2023 , 00:00   Re: How to check letters and words in sentences?
Reply With Quote #5

What I'm looking for a little different is to specify the language and not specific words from a specific language. Is that possible? Especially with Arabic and English.
__________________
abdobiskra is offline
Send a message via Skype™ to abdobiskra
lexzor
Veteran Member
Join Date: Nov 2020
Old 05-08-2023 , 09:21   Re: How to check letters and words in sentences?
Reply With Quote #6

this is exact what that plugin does.


there are not default libraries or custom libraries that can detect the language (from what I know so far)

you have to create your own "language dictionary", like i did

PHP Code:
new const ROMANIAN_WORDS[] =
{
    
"find",
    
"arunca"
}

new const 
ENGLISH_WORDS[] =
{
    
"gaseste",
    
"throw"

these are "language dictionaries", you set specific words that can be used to detect the language of the user sentence.

also, for point 2 you can do something like

(not tested)
PHP Code:
for(new 0iPos = -1sizeof(ENGLISH_WORDS); j++)
    {
        if((
iPos containi(szArgENGLISH_WORDS[j])) != -1)
        {
            if(
iPos == 0)
            {
                
client_print(idprint_chat"Word %s is first in sentence"ENGLISH_WORDS[j])
            }
                
            if(
iPos == (strlen(szArg) - strlen(ENGLISH_WORDS[j])))
            {
                
client_print(idprint_chat"Word %s is the last in sentence"ENGLISH_WORDS[j])
            }

            
bLangs[ENGLISH] = true
            
break
        }
    } 
another way is to guess what language user will speak by his location using geolocation

edit: i don't know if it will work, but you can try using these: https://www.amxmodx.org/api/string_const

or what you can do is using regex with the next expression to detect if there are only arabic letters

PHP Code:
/[\u0600-\u06FF]/ 
i want to remind you that this is scripting help. this section is supposed to give you solutions, not complete a request.

Last edited by lexzor; 05-08-2023 at 09:29.
lexzor is offline
abdobiskra
Veteran Member
Join Date: Jul 2014
Location: Algeria
Old 05-08-2023 , 14:03   Re: How to check letters and words in sentences?
Reply With Quote #7

lexzor
1) To be more clear in the first question, I have an addition that arranges the Arabic letters from left to right, and it works normally in the same language, but if you enter one of the symbols or letters that are different to the language, it does not work and the letters become from right to left. I'd like to fix that by making it work either way.
2) As for the second question, the letters of the Arabic language, some of which are connected when forming a word, and the letters take a different form, and some of them are separate when they are alone or at the end of the word, for example, or alone as letters of the alphabet.
__________________
abdobiskra is offline
Send a message via Skype™ to abdobiskra
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 05-08-2023 , 23:15   Re: How to check letters and words in sentences?
Reply With Quote #8

Detecting a language in a general sense is something you need a more powerful engine to do (you know, like Google). If you have a plugin that you're trying to fix, it would have been good to know that (and for you to post it) so that we have better context of your request.
__________________

Last edited by fysiks; 05-08-2023 at 23:16.
fysiks is offline
abdobiskra
Veteran Member
Join Date: Jul 2014
Location: Algeria
Old 05-09-2023 , 01:14   Re: How to check letters and words in sentences?
Reply With Quote #9

Quote:
Originally Posted by fysiks View Post
Detecting a language in a general sense is something you need a more powerful engine to do (you know, like Google). If you have a plugin that you're trying to fix, it would have been good to know that (and for you to post it) so that we have better context of your request.
Here:
PHP Code:
new Separate_letters_Symbol[][] = {
    
"ط§""ط¨""طھ""ط«""ط¬""ط*""ط®""ط³""ط´""طµ""ط¶""ط¹",
    
"ط؛""ظپ""ظ‚""ظƒ""ظ…""ظ†""ظٹ""ط©""ظ‰""ظ‡""ظ„",
    
"ط¦"
}

new 
Connected_letters_Symbol[][] = {
    
    
"ï؛ژ""ï؛‘""ï؛—""ï؛›""ï؛ں""ï؛£""ï؛§""ï؛³""ï؛·""ï؛»""ï؛؟â€ژ""ﻋ",
    
"ï»ڈ""ﻓ""ï»—""ï»›""ﻣ""ﻧ""ﻳ""ï؛”""ï»°""ï»ھ""ï»ں",
    
"ï؛‹"
}

public 
plugin_init() {

    
register_clcmd"say""CheckMessage" )
    
register_clcmd"say_team""CheckMessage" )
}

public 
CheckMessage(id) {
    static 
said[192], said_to_utf16[192], said_to_utf8[192], name[33]
    
    
read_argssaidcharsmax(said) )
    
remove_quotessaid )
    
trimsaid )
    
//new last_letter = said[strlen(said) - 1]; // extract the last letter of the word
    //client_print(id, print_chat, "Last Letter **** |%s| ****", last_letter);
    
    
MultiByteToWideChar(saidsaid_to_utf16)
    
    if(
isEnglish(said_to_utf16))
        return 
PLUGIN_CONTINUE;
    
    
ReverseString(said_to_utf16)
    
    
WideCharToMultiByte(said_to_utf16said_to_utf8)
    
    for( new 
isizeof Separate_letters_Symboli++ ) {
                
        
replace_all(said_to_utf8 charsmax(said_to_utf8), Separate_letters_Symbol[i], Connected_letters_Symbol[i] )
    }
    
get_user_name(idnamecharsmax(name));
    
    
client_print(0print_chat"(AR) %s : %s ",namesaid_to_utf8 )
    
    return 
PLUGIN_HANDLED
}

stock ReverseString(toggle[]) 

    for(new 
strlen(toggle) - 10temp i--, j++) 
    { 
        
temp toggle[i]; 
        
toggle[i] = toggle[j]; 
        
toggle[j] = temp
    } 
}

stock MultiByteToWideChar(const mbszInput[], wcszOutput[])
{
    
    new 
nOutputChars 0
    for (new 
0mbszInput[n] != EOSn++) { 
        if (
mbszInput[n] < 0x80) { // 0... 1-byte ASCII 
            
wcszOutput[nOutputChars] = mbszInput[n]; 
            } else if ((
mbszInput[n] & 0xE0) == 0xC0) { // 110... 2-byte UTF-8 
            
wcszOutput[nOutputChars] = (mbszInput[n] & 0x1F) << 6// Upper 5 bits 
            
            
if ((mbszInput[1] & 0xC0) == 0x80) { // Is 10... ? 
                
wcszOutput[nOutputChars] |= mbszInput[++n] & 0x3F// Lower 6 bits 
                
} else { // Decode error 
                
wcszOutput[nOutputChars] = '?'
            } 
            } else if ((
mbszInput[n] & 0xF0) == 0xE0) { // 1110... 3-byte UTF-8 
            
wcszOutput[nOutputChars] = (mbszInput[n] & 0xF) << 12// Upper 4 bits 
            
            
if ((mbszInput[1] & 0xC0) == 0x80) { // Is 10... ? 
                
wcszOutput[nOutputChars] |= (mbszInput[++n] & 0x3F) << 6// Middle 6 bits 
                
                
if ((mbszInput[1] & 0xC0) == 0x80) { // Is 10... ? 
                    
wcszOutput[nOutputChars] |= mbszInput[++n] & 0x3F// Lower 6 bits 
                    
} else { // Decode error 
                    
wcszOutput[nOutputChars] = '?'
                } 
                } else { 
// Decode error 
                
wcszOutput[nOutputChars] = '?'
            } 
            } else { 
// Decode error 
            
wcszOutput[nOutputChars] = '?'
        } 
        
        
nOutputChars++; 
    } 
    
wcszOutput[nOutputChars] = EOS
}
stock WideCharToMultiByte(const wcszInput[], mbszOutput[])

    new 
nOutputChars 0
    for (new 
0wcszInput[n] != EOSn++) { 
        if (
wcszInput[n] < 0x80) { 
            
mbszOutput[nOutputChars++] = wcszInput[n]; 
            } else if (
wcszInput[n] < 0x800) { 
            
mbszOutput[nOutputChars++] = (wcszInput[n] >> 6) | 0xC0
            
mbszOutput[nOutputChars++] = (wcszInput[n] & 0x3F) | 0x80
            } else { 
            
mbszOutput[nOutputChars++] = (wcszInput[n] >> 12) | 0xE0
            
mbszOutput[nOutputChars++] = ((wcszInput[n] >> 6) & 0x3F) | 0x80
            
mbszOutput[nOutputChars++] = (wcszInput[n] & 0x3F) | 0x80
        } 
    } 
    
mbszOutput[nOutputChars] = EOS
}

isEnglish(const szString[])
{
    new 
0;
    new 
ch;
    while((
ch szString[i]) != EOS)
    {
        if(
0x21 <= ch <= 0x7F
            return 
true;
            
        
i++;
    }    
    return 
false;

strlen()Not supports multi-byte characters (UTF-8 ).
I want an example of how strfind() can determine the position of a letter in a word?
I want to replace the word except for the last letter, for example, how do I do that?
__________________

Last edited by abdobiskra; 05-09-2023 at 02:20.
abdobiskra is offline
Send a message via Skype™ to abdobiskra
abdobiskra
Veteran Member
Join Date: Jul 2014
Location: Algeria
Old 06-23-2023 , 11:42   Re: How to check letters and words in sentences?
Reply With Quote #10

Is it possible to specify the characters before and after them by moving to the byte level?
I'm not good at it but it seems the most appropriate way..
PHP Code:
        for (new 0sizeof(said_to_utf8); i++)
        {
            
            if ((
said_to_utf8[i] == 0x20 || said_to_utf8[i] == 0xEF) && (said_to_utf8[i+1] == 0xBB ||  said_to_utf8[i+1] == 0xBA))
            {
                if (
said_to_utf8[i+2] == 0x8B//" أ" alif letter
                
{
                    
said_to_utf8[i+2] = 0x8A;
                }
            }
        } 
I have already replaced the character, now I want to specify what comes before and after it?
__________________
abdobiskra is offline
Send a message via Skype™ to abdobiskra
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 13:12.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode