I want to match on list of maps that looks like:
[
%{
predictions: [
%{label: "neutral", score: 0.9985172152519226},
%{label: "negative", score: 0.0010232925415039062},
%{label: "positive", score: 4.594727361109108e-4}
]
},...
]
ger_sentiments = Enum.map(ger_predictions, fn x -> SentimentScore.score(x.predictions) end)
To sum up the individual values. With a function that looks like:
defmodule SentimentScore do
def score(prediction) do
prediction
|> Enum.map(fn p ->
cond do
p.label == "POS" -> p.score
p.label == "positive" -> p.score
p.label == "NEG" -> -p.score
p.label == "negative" -> -p.score
p.label == "NEU" -> 0
p.label == "neutral" -> 0
end
end)
|> Enum.sum()
end
end
I can do as want. I just want it to look more like Elixir code and apply matching_
defmodule SentimentScore do
def score(prediction) do
prediction
|> Enum.map(fn p ->
cond do
%{label: "POS"} = p -> p.score
%{label: "positive"} = p -> p.score
%{label: "NEG"} = p -> -p.score
%{label: "negative"} = p -> -p.score
%{label: "NEU"} = p -> 0
%{label: "neutral"} = p -> 0
end
end)
|> Enum.sum()
end
end
But here I get a matching error:
(MatchError) no match of right hand side value: %{label: "neutral", score: 0.9985172152519226}
What is the errror I overlooked?