The Ultimate Site of Worms Armageddon

Other Things => Off Topic => Topic started by: THeDoGG on July 24, 2012, 09:05 PM

Title: web development / code challenge
Post by: THeDoGG on July 24, 2012, 09:05 PM
Hello!
Is there any good developer around ?

I have a little challenge, there is something I'm trying to achieve and I'd like some advice for it.
Not looking for a final solution, but just some clues that could help me to go in the good way :)

It's about writing a function, and I've never written any :)

Here is my problem :
I've got a DB with several items, and for each of them I'd like to generate a "random" description, but that would actually always be the same !
So it wouldnt really be a random one actually :)

For each of the items, ive got several values: id, title, duration, etc ...
I was thinking that I could play with the ID, getting the number and depending on it generating a description. Good idea or not ?
I need this function to be light, because it's gonna calculate this generated description each time the page is rendering !

I've currently already got my description generator, but its writting the result directly in MySQL, and totally randomly. But it was the only way I've found to be sure the description would always stay the same.
And it's a bit painfull to have to re-write in the DB each time I want to add new items into my DB..

Does anyone has a good advice to give me ? :)
Title: Re: web development / code challenge
Post by: zippeurfou on July 24, 2012, 09:21 PM
Quote
"random" description, but that would actually always be the same

it's not a random anymore ? :o

What language ? php ?
If I understood what you said this might help you.
A way i can think of is. Everytime there is a new entry in your db. Trigger it and run your "generateArandomNotSoRandomFunction" that will take in an array of premade random description and add it to a Map that has your id as a key and the index (or whatever you want) as the value. There are plenty of way of doing it (you can mix up everything in one array if you want to save data or use two array has i said if you don't mind data but use maybe more cpu depend if you aim something or not). Up to you. :)
Title: Re: web development / code challenge
Post by: Rok on July 24, 2012, 09:23 PM
Sounds like hashing would do the job?
Title: Re: web development / code challenge
Post by: zippeurfou on July 24, 2012, 09:31 PM
Sounds like hashing would do the job?
Indeed, my bad I am always thinking "Java" (don't kill me :D) but indeed I was thinking @Hashing also :)
Title: Re: web development / code challenge
Post by: THeDoGG on July 24, 2012, 09:31 PM
Quote
"random" description, but that would actually always be the same

it's not a random anymore ? :o

What language ? php ?
If I understood what you said this might help you.
A way i can think of is. Everytime there is a new entry in your db. Trigger it and run your "generateArandomNotSoRandomFunction" that will take in an array of premade random description and add it to a Map that has your id as a key and the index (or whatever you want) as the value. There are plenty of way of doing it (you can mix up everything in one array if you want to save data or use two array has i said if you don't mind data but use maybe more cpu depend if you aim something or not). Up to you. :)

Yes its not really random :) By random I mean that for each items I use a serie of keyword nun, verb, adjective, etc .. that i have put in arrays, and i mix them all together, I'd like that for each item it takes different word from those array.
I would like to run the function not when there is a new entry in the db, but when the page is rendering, in the client side ! So that I can manage my DB and get sure each of the new items will get fresh "randomly" generated static description. But actually maybe it's not such a good idea to it this way ?

About the language its ruby for a rails app.

@Rok, I've already heard about hashing, but I have no clue of what it is xD
Title: Re: web development / code challenge
Post by: Rok on July 24, 2012, 09:38 PM
What is this "description"? How does it need to look like? Write an example of input/output.
Title: Re: web development / code challenge
Post by: zippeurfou on July 24, 2012, 09:43 PM
this can help you for  Hashing (http://en.wikipedia.org/wiki/Hash_function).
I am not sure to really understand.
Do you mean that if the client reload the page all these word  will change ?
Let me understand it.
let's use this code (it's C).

char randomWords[][10] = {"TheDogg", "Love", "Agnos", "Dick"};

you want the description to be a mix of these words "TheDogg", "Love", "Agnos", "Dick" with space at between everyword.

The output could be TheDogg Agnos Love Dick or Agnos Loves Dick TheDogg and so on.
right ?


Title: Re: web development / code challenge
Post by: THeDoGG on July 24, 2012, 09:55 PM
Do you mean that if the client reload the page all these word  will change ?

I want exactly the opposite :) Each time the client reload it should keep the same description. But it still should be a different one for each item
It's for SEO purpose, google will not like that the description change each time it visits the page :)

for example, to generate my descriptions, ive got 3 arrays :

["thedogg", "zippeurfou", "rok"]
["loves", "enjoy", "hates"]
["cherries", "apples", "bananas"]

for item #1: thedogg enjoy apples
for item #2: zippeurfou hates cherries
for item #3: rok loves bananas

but actually item #3 could also be "thedogg hates cherries" or anything else since i get sure it would ALWAYS be the same generated description
Title: Re: web development / code challenge
Post by: zippeurfou on July 24, 2012, 09:59 PM
If you want to do it client sided.
You'll have to use cookies to do it. If they disable it, you'll be kinda f@#!ed. I might be wrong but I don't see any otherway :(.
Title: Re: web development / code challenge
Post by: THeDoGG on July 24, 2012, 10:02 PM
wait, talking about client side was a mistake, after thinking !
Could be in server side :)
Title: Re: web development / code challenge
Post by: MonkeyIsland on July 25, 2012, 06:46 AM
May I ask why you need to do such thing? Filling page for SEO?
Title: Re: web development / code challenge
Post by: THeDoGG on July 25, 2012, 09:46 AM
May I ask why you need to do such thing? Filling page for SEO?

Its not about filling a whole page, its about giving to each item a <meta> description that is unique. Getting sure it wont display any "duplicate" content that would also be present in another site.
Like this is should get better ranking in Google and sure to not be in "omitted" search results.

Any idea to help me MI ?
Title: Re: web development / code challenge
Post by: Impossible on July 25, 2012, 12:33 PM
In many languages there is function Choose, here's info from one of them

Code: [Select]
Real-valued function
Choose(val1,val2,val3,...) Returns one of the arguments choosen randomly. The function can have up to 16 arguments.
Maybe something such as that exist on language you use? So you could write IDs, and they would choose randomly..
Title: Re: web development / code challenge
Post by: Abnaxus on July 25, 2012, 12:33 PM
I didn't really got it: if you can explain me in French, I might be able to help you.
Title: Re: web development / code challenge
Post by: MonkeyIsland on July 26, 2012, 07:07 AM
THeDoGG, my advise is that do not try to make a function to generate unique description. Such things are not flexible and will cause even more trouble. For example:

1. What if you need to specifically change a description for whatever reason in the future?
2. What if you need to search within your nonsense description for whatever reason?
3. What if a description is really really crappy and you want to refresh it?

If it was me, I would create a totally random generating function and insert the output in the database. That way everything would be in my control.
Title: Re: web development / code challenge
Post by: Abnaxus on July 26, 2012, 09:20 AM
At your place MI, I'd do a class with everything you need, and so if I need to change something on an object, I'd just make/call a method. :p

This way, you can generate your page through objects.
And so if they change, the page will also change.

I'm a bit rusty with PHP, but I think it's kinda easy to do so.
Title: Re: web development / code challenge
Post by: THeDoGG on July 26, 2012, 04:37 PM
THeDoGG, my advise is that do not try to make a function to generate unique description. Such things are not flexible and will cause even more trouble. For example:

1. What if you need to specifically change a description for whatever reason in the future?
2. What if you need to search within your nonsense description for whatever reason?
3. What if a description is really really crappy and you want to refresh it?

If it was me, I would create a totally random generating function and insert the output in the database. That way everything would be in my control.

Well, what you suggest is what I already done actually, generating description and writting them in the DB !
But now I have to do it again for new items ! Which is a pain, I need to get sure to not override existing ones. Maybe I should get better in SQL scripts skills tho :)

About the points you mentionned, I will not have to change a spefiic description!
Im treating millions of items, I can't take care of them individually.
And most of the result are actually giving "senseful" descriptions :D Thats magic !

@Abnaxus:
Lets talk in french when we meet in #AG, gonna be easier ;)
Btw its not PHP but Ruby !


@Impossible
Seems like a good function! But didnt find it for ruby :(