0

I try to loop through some json (clustered twees from twitter) and count how often particular keywords (hashtags) are present so I can create an ordered list of frequent words.
this (19)
that (9)
hat (3)

this I've done by creating

var hashtags = [];  

first time I add a new word i add th word and give the value 1

hashtags[new_tag] = 1;

the next time a find the same worj I just add to the number

hashtags[hashtag]+=1;

the result is a simple structure with words and values. I can list out what I need with

$.each(hashtags, function(i, val){
    console.log(i+ " - "+ val);
})

Now I realize I also nee to know what clusters these words where found in. So; I thing I need to add a list (array) to my "hashtags".

I guess what I try to create is a json structure like this:

hashtags: {"this": 19, clusters: [1,3,8]},{"that": 9, clusters: [1,2]}

How do I add the arrays to the hashtags object?

2 Answers 2

2

First create an object for holding the hashtags:

var hashtags = {};

Then if an hashtag has never been seen yet, initialize it:

hashtags[new_tag] = {
    count: 0,
    clusters: []
};

Increment counts:

hashtags[new_tag].count += 1;

Add cluster:

hashtags[new_tag].clusters.push(the_cluster);
Sign up to request clarification or add additional context in comments.

1 Comment

Perfect. It now both makes sense to me now, and the code works smoothly! Thanks.
1

What you want is an Object ({ }) for hashtags since you are using strings as keys, not an Array ([ ]) which you would be appropriate if you were using index as the key. I'd structure it like this:

var hashtags = {
    "this": { count: 19, clusters: [1, 3, 8] },
    "that": { count: 9, clusters: [1, 2] }
};

So, when you add a new hash tag, do it like this:

hashtags[new_tag] = { count: 1, clusters: [cluster] },

And, when you increment a hash tag, do this:

hashtags[hashtag].count++;
hashtags[hashtag].clusters.push(cluster);

1 Comment

Thanks for your comment, your explanation of the difference in notation between object {} and array [] made all the difference in my understanding of this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.