Mathematica Stack Exchange is a question and answer site for users of Wolfram Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a list, importance, that assigns a "value" to each word in some text, like such:

{ {the, 0.9}, {and, 1}, {red, 2.1} }

And I also have a list of sentences, with no periods. What I am trying to do is get the 'value' of each of sentences, by taking each word, finding it's 'value' in importance, and then finds the sum of the values of all the words in that sentence.

Here is an example input/output:

{"the and", "red the", "and and"}

{ {"the and", 1.9}, {"red the", 3}, {"and and", 2} }

I have tried all sorts of things with Table and Map, but nothing has even come close. Is there an elegant way to do this?

share|improve this question

Function, takes a string sentence and importance list:

fn = {#1,Tr[Replace[StringSplit[#1], Append[Rule @@@ #2, _ -> 0], 1]]} &;

Set up some dummy data, map function over list of inputs:

imp={{"the", 0.9}, {"and", 1}, {"red", 2.1}};
input = {"the and thou", "red the book", "and and ahh one, two, three"};
fn[#, imp] & /@ input

{{"the and thou", 1.9}, {"red the book", 3.}, {"and and ahh one, two, three", 2}}

share|improve this answer
    
(+1) a typo in the last line: imp should be importance. Also, since importance is defined as a list of rules, you don't need Rule@@@ in the first line. – kglr 7 hours ago
    
@kglr - thanks for catch, actually an anti-typo: I'd lazily cut-n-pasted the definition in answer, but used the one in OP format (not a rules list). Fixed. – ciao 7 hours ago

One possible way

importance = {"the" -> 0.9, "and" -> 1, "red" -> 2.1};
input = {"the and me", "red the pluto", "and and mikey mouse"};
doIt[str_String] := Module[{m},
  m = StringSplit[str];
  m = Plus @@ (m /. importance);
  m = Cases[m, x_ /; NumericQ[x]];
  {str, m}
  ];

doIt[#] & /@ input

Mathematica graphics

share|improve this answer
    
If there are words in input that aren't in importance, it will just add them, so the result will be something like 1.8+by+something. Is there a way to make all those other words just 0? So the result would, in that example, just be 1.8? – user46215 9 hours ago
    
Yes, there is away, but you need to make sure the specification of the problem is complete. This was not mentioned in your post :) – Nasser 9 hours ago
    
Oh sorry! I didn't mean to say that it wasn't right - this answer is brilliant, thank you. I was just wondering. This only came up after I tested your code and realized some of the words were missing. I'm sure I know how to do it, but I'm sure it's not the most elegant way, either. – user46215 9 hours ago
    
Please try now. I did it quickly but you can test it :) – Nasser 9 hours ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.