# Getting the value of sentences

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?

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}}

• (+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 Jan 30 '17 at 4:18
• @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 Jan 30 '17 at 4:26

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


• 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 Jan 30 '17 at 2:18
• 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 Jan 30 '17 at 2:19
• 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 Jan 30 '17 at 2:21
• Please try now. I did it quickly but you can test it :) – Nasser Jan 30 '17 at 2:29