Author Topic: Converting chat to URL efficiently.  (Read 1326 times)

I was trying to make a script that would convert text from chat to URL, but I've hit snags. One snag is that characters such as ":" "<" and ">" break the URL and make it look uglier. Another is that I must account for the fact each message can only contain two URLs.

I find it hard to figure out a simple method to achieve this and am hoping someone here may help. Here's what I've tried to compose, but I became lost:

Code: [Select]
function urlChat(%t) {
%output = "http://";
for(%i = 0; %i <= getWordCount(%t); %i++) {
%w = getWord(%t, %i);
if(strStr(%w, ":") > -1)
%output = %output @ " " @ %w @ " http://";
else
%output = %output @ "_" @ %w @ " ";
}
}
« Last Edit: October 20, 2010, 05:26:57 PM by MegaScientifical »

Why not just chat normally?

Wow. You have the most convoluted thought process I've ever seen.

Wow. You have the most convoluted thought process I've ever seen.

I know, that's why I came here. I don't get what the forget I'm doing. The only reason I'd even bring up such a useless script here is because the process to make it is somehow confusing me so much.

Whatever you learn from this will very unlikely be used for anything useful.

I'm just trying to figure out why I can't figure this out. I might not use it in coding, but I need to hold logical thinking... I think this is why I have problems with Pre-calculus... Thought process. Here's what I just threw together, but even if it functioned perfectly from what's there, it doesn't account for limiting to only two URLs:

Code: [Select]
function urlChat(%t) {
%output = "http://";
for(%i = 0; %i <= getWordCount(%t); %i++) {
%w = getWord(%t, %i);
if(strStr(%w, ":") > -1)
%output = %output @ " " @ %w @ " http://";
else
%output = %output @ "_" @ %w @ " ";
}
}

This sort of thing would be far easier with a regex...

I thought I had the answer but reading your opening post forgeted with my mind.

I rewrote the topic post. Hopefully it is a little more understandable.

I need an example of a multitasking code like this... it has to keep track of some things in an efficient fashion, and I can't figure that out logically. :panda:

I've only tested this briefly, so just tell me if you come across any errors.
What it does is it converts as much as it can (see: code) into "URL chat."

I don't know if it's all that optimized, as it's sixty lines of loopy goodness.

Code: [Select]
function makeURLd(%str)
{
%cnt = getWordCount(%str);

for(%i = 0; %i < %cnt; %i++)
{
if(strPos(getWord(%str,%i),":") != -1)
%idx[%i] = 1;
}

for(%i = 0; %i < %cnt; %i++)
{
%wrd = getWord(%str,%i);
%tmp = %tmp @ %wrd;

if(!%idx[%i + 1] && !%idx[%i])
%tmp = %tmp @ "\x90";
else
%tmp = %tmp @ " ";
}

%tmp = getSubStr(%tmp,0,strLen(%tmp) - 1);
%cnt = getWordCount(%tmp);

for(%i = 0; %i < %cnt; %i++)
{
%wrd = getWord(%tmp,%i);

if(strPos(%wrd,":") != -1)
continue;

%len = strLen(%wrd);

if(%len > %len1)
{
%len2 = %len1;
%idx2 = %idx1;

%len1 = %len;
%idx1 = %i;
}
else if(%len > %len2)
{
%len2 = %len;
%idx2 = %i;
}
}

for(%i = 0; %i < %cnt; %i++)
{
%wrd = getWord(%tmp,%i);

if(%i $= %idx1 || %i $= %idx2)
%url = %url @ "http://" @ strReplace(%wrd,"\x90","_") @ " ";
else
%url = %url @ strReplace(%wrd,"\x90"," ") @ " ";
}

return getSubStr(%url,0,strLen(%url) - 1);
}

Input:

Quote
hi connect to 127.0.0.1:1234 please :) thank you!

Output:

Quote
http://hi_connect_to 127.0.0.1:1234 please :) http://thank_you!

(It didn't prefix "please" with http:// because the "thank you!" phrase was longer. All this code assumes you were correct when you said that Blockland chat allows for two URLs - no more, no less - and affects up to two of the longest colonless phrases. I haven't actually tested it ingame, only via the console (dedicated LAN's), so if you're wrong about that then the code probably doesn't work ingame. Can be altered though.)

What are you trying to do? If you gave an example or two of input and output that you want, it would be easier to help.

I just tested it ingame and saw that the chat only supports one URL by default. Just take out the code that I put in to account for the second longest portion and it works perfectly. (If you don't remove it, it still works perfectly as long as the chat supports two URLs, which is what it was originally designed for.)

It should still find the longest portion. Just not make more than one URL. And I must still point out that I said:

Quote
[01:12:15] MegaScience: Trigun :(

It came out:

Quote
[01:12:15] MegaScience: Trigun http://:(

in chat... Might need that fixed.

It should still find the longest portion. Just not make more than one URL.

If you remove the second longest one then it still finds the first longest one. Like I said, it works.

And I must still point out that I said:

It came out:

in chat... Might need that fixed.

It shows up as "http://Trigun :(" for me (how it should). You're probably implementing it wrong.