我试图通过将每个句子传递给外部库Sentient中的analyze函数来对小说小说进行情感分析。我反复收到以下错误:
(Enum.EmptyError) empty error
(elixir) lib/enum.ex:1590: Enum.reduce/2
(elixir) lib/enum.ex:1184: Enum."-map/2-lists^map/1-0-"/2
(sentiment) lib/sentiment.ex:23: Sentiment.analyze/1
(sentiment) lib/sentiment.ex:6: Sentiment.run/1
(stdlib) erl_eval.erl:670: :erl_eval.do_apply/6
(elixir) lib/code.ex:168: Code.eval_string/3
据我所知,我没有将空字符串传递给我的函数。我一直在使用包含以下内容的测试文件:
This is a test. This sentence is sad. This sentence is happy. The end.
这是我的代码:
defmodule Sentiment do
def run(file) do
file
|> fileread
|> analyze
|>IO.inspect
end
def fileread(file) do
file
|> File.stream!
|> Stream.map(&String.trim_trailing(&1))
|> Enum.map(&String.replace(&1, ~r/[-@#$%^&*()=_+|;':",<>']/, ""))
|> Stream.map(&String.split(&1,~r/[\p{P}\p{S}]+/, trim: true))
|> Enum.to_list
|> List.flatten
|> IO.inspect
end
def analyze(list) do
list
|> Enum.map(&Sentient.analyze(&1))
|> IO.inspect
end
end
我在文件读取函数的末尾放置了一个IO.inspect,以检查列表中是否有空条目。没有,这是我fileread
函数的结果:
mix run -e 'Sentiment.run("test.txt")'
["This is a test", " This sentence is sad", " This sentence is happy",
" The end"]
所以,我真的不确定为什么我会收到这个错误。有人有什么建议吗?
答案 0 :(得分:2)
这是sentient
中的错误:
$ mix run -e 'Sentient.analyze("This is a test")'
** (Enum.EmptyError) empty error
(elixir) lib/enum.ex:1722: Enum.reduce/2
(stdlib) erl_eval.erl:670: :erl_eval.do_apply/6
(elixir) lib/code.ex:170: Code.eval_string/3
(elixir) lib/enum.ex:645: Enum."-each/2-lists^foreach/1-0-"/2
(elixir) lib/enum.ex:645: Enum.each/2
(mix) lib/mix/tasks/run.ex:78: Mix.Tasks.Run.run/1
在Git仓库中看起来像作者fixed但是从未在hex.pm上发布它。使用当前的git版本,上面的语句不会抛出任何错误。
错误是旧版本使用空列表调用Enum.reduce/2
,如果列表为空,则会记录该错误。他们使用前面提到的提交中的默认累加器值将其更改为Enum.reduce/3
,处理空列表就好了。