4
$\begingroup$

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?

$\endgroup$
4
$\begingroup$

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

$\endgroup$
  • $\begingroup$ (+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. $\endgroup$ – kglr Jan 30 '17 at 4:18
  • $\begingroup$ @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. $\endgroup$ – ciao Jan 30 '17 at 4:26
3
$\begingroup$

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

$\endgroup$
  • $\begingroup$ 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? $\endgroup$ – user46215 Jan 30 '17 at 2:18
  • $\begingroup$ Yes, there is away, but you need to make sure the specification of the problem is complete. This was not mentioned in your post :) $\endgroup$ – Nasser Jan 30 '17 at 2:19
  • $\begingroup$ 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. $\endgroup$ – user46215 Jan 30 '17 at 2:21
  • $\begingroup$ Please try now. I did it quickly but you can test it :) $\endgroup$ – Nasser Jan 30 '17 at 2:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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