Need help with getting a list of items, based on numbers in a table

I am making a module that suggests lists of random “content” for each user based on data collected from each user. The data is saved in “tags” and the higher the number for each tag, the more that user likes that particular topic. The person who uses the module in their code can create any number of tags, and assign strings representing content to each tag. I want it so that the person who has this in their code can tell the code which user they want the content for (userId), and how much content they want (number of “content” strings to output), and the code will return a list of strings, with a proportional number of content strings from each tag compared to that user’s value for each tag. For example, lets say a user has the following data…

tag1: 10,
tag2: 0,
tag3: 5,
tag4, 5

…And the game dev working with this code asks for 12 pieces of content for this user. That would mean the code should give them 6 strings from the tag1 category, 0 from the tag2 category, 3 from the tag3 category and 3 from the tag4 category.

I have a dictionary with some keys (tags) and values like this:

data = {
    [userId] = {
        [tag1] = 17
        [tag2] = 0
        [tag3] = 4
        [tag4] = 9
    }
}

I also have a table with strings (content) like this:

content = {
    [tag1] = {"pic1A", "pic1B", "pic1C", "pic1D", "pic1E", "pic1F"}
    [tag2] = {"pic2A", "pic2B", "pic2C", "pic2D", "pic2E", "pic2F"}
    [tag3] = {"pic3A", "pic3B", "pic3C", "pic3D", "pic3E", "pic3F"}
    [tag4] = {"pic4A", "pic4B", "pic4C", "pic4D", "pic4E", "pic4F"}
}

I am not sure how to make it so that an amount of content proportional to the users value from each category will be given. I am thinking of making the value of each tag into a decimal/percent out of that users total values, and then picking a certain number of content strings from that category based off of that decimal.
To stick with the example at the beginning, it would mean that tag1 would have 0.5 of the content, tag2 would have 0.0 of the content, tag3 would have 0.25 of the content, and tag4 would have 0.25 of the content.
I know how to make them into decimals, but I am unsure how to make the code pick a number of content for each tag based off of the decimals. All I need to get out of this is so that I can give the code a number corresponding to each of the tags and the total number of strings I want back, and it will tell me how many strings have to be from each category.

Im sorry if I wrote this really badly, any help is greatly appreciated! Lmk if I need to explain it better, im not great at explaining!

I see what you mean.

You have to change the numbers from numerical into decimal. Then multiply ntha with the output. Any numbers remaining can either be allocated randomly (best choice imo) or you can give it out proportionally or hand it to the highest number.

Sorry I am bad at explaining

Its a simple algorithm though.

Sum of all numbers in each tag - 20 = 5+5+0+10
Sum of all options - 12

Divide Sum of all options with sum on numbers from tags

12/20 = 0.6 You have a decimal

multiply decimal with number in each tag to get the respective proportional

0.6 * 10 = 6
0.6 * 5 = 3
0.6 * 0 = 0

If you get for any reason a leftover sum the leftovers and assign them randomly.

1 Like

Thank you for the help! After a LOT or work, I have this up and running. The only problem is, this does not take into account that I need to make it only give me a certain number of results. Right now, the results are proportional, but if I put high-ish numbers for the tags, I can get a dozen or more of strings returned, even if I only asked for 10. How would I make it only give me the amount of strings I asked for?

I can send the code I have now if needed, but it doesnt make a lot of sense, I have spend all day on this and I have so much code I have added, done wrong, redone and continued like that. It is hard for me to understand (I added comments after a bit) and I think it would be difficult to figure out. I am fixing it up right now so that it should be better soon-ish