Jump to content

Match Words in Array & Retrieve Array Values


anevins

Recommended Posts

Hello,

 

I have an array $sentiment_example of strings and associated values.

I have another array $tweet_words of strings.

 

I want to check if a string in $sentiment_example matches a string in $tweet_words, then extract the associated value of that string from $sentiment_example.

 

Here's some code to help envisage it;


foreach ( $tweet_words as $tweet_word ) {

  if ( in_array ( $tweet_word, $sentiment_example ) ) {
    
    // But how do I extract the sentiment value for this word?

  }

}

 

Can anyone give me some tips, please?

Link to comment
Share on other sites

$sentiment_example = array("Wrapped" => 6, "in" => 3, "my" => 5, "blanket" => 7, "always" => 4, "makes" => 6, "me" => 6,
						  "feel" => 5, "a" => 1, "little" => 2, "better" => 7, "when" => 3, "I'm" => 4, "not" => 3,
						  "feeling" => 5, "to" => 1, "well" => 6);		

 

$tweet_words is just the same as $sentiment_example but without the value, so just 17 strings.

Link to comment
Share on other sites

I'm trying to simulate a sentiment analysis.

 

A sentence is broken up into individual words.

Those individual words ought to be compared against an array, which will contain strings of separate words from the entire English language, with a defined sentimental value assigned to each word.

 

It's one of the stages to find sentiment in a tweet, from Twitter.

Link to comment
Share on other sites

So are you saying $tweet_words looks like this?

 

$tweet_words = array("Wrapped", "in", "my", "blanket", "always", "makes", "me",
  "feel", "a", "little", "better", "when", "I'm", "not",
  "feeling", "to", "well");

 

If so, what's the problem with posting it?

If not, what's the problem with posting it?

 

You do want help, don't you?

Link to comment
Share on other sites

@Jesirose

 

Thanks for the help.

 

@Barand

 

I was hurried into responses, grateful for the quick responses, so I thought to save time rather than typing out that similar array.

I don't actually have the array $tweet_words, I have the string and I've put it into an array of separate strings per word, so I couldn't copy & paste it from my code.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.