f# 使用 ac# 库中的函数迭代两个数组
我有一个单词列表和相关词性标签列表。我想使用每个索引元组作为 .NET 函数的输入同时(匹配索引)迭代两者。这是最好的方法吗(它有效,但对我来说感觉不自然):
let taggingModel = SeqLabeler.loadModel(lthPath +
"models\penn_00_18_split_dict.model");
let lemmatizer = new Lemmatizer(lthPath + "v_n_a.txt")
let input = "the rain in spain falls on the plain"
let words = Preprocessor.tokenizeSentence( input )
let tags = SeqLabeler.tagSentence( taggingModel, words )
let lemmas = Array.map2 (fun x y -> lemmatizer.lookup(x,y)) words tags
I have a list of words and a list of associated part of speech tags. I want to iterate over both, simultaneously (matched index) using each indexed tuple as input to a .NET function. Is this the best way (it works, but doesn't feel natural to me):
let taggingModel = SeqLabeler.loadModel(lthPath +
"models\penn_00_18_split_dict.model");
let lemmatizer = new Lemmatizer(lthPath + "v_n_a.txt")
let input = "the rain in spain falls on the plain"
let words = Preprocessor.tokenizeSentence( input )
let tags = SeqLabeler.tagSentence( taggingModel, words )
let lemmas = Array.map2 (fun x y -> lemmatizer.lookup(x,y)) words tags
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的代码对我来说看起来相当不错 - 其中大部分涉及一些加载和初始化,因此您无法做太多事情来简化该部分。除了
Array.map2
之外,您还可以将Seq.zip
与Seq.map
结合使用 -zip
函数结合了将两个序列合并为一个包含具有匹配索引的元素对的序列:由于
lookup
函数采用您获得的元组作为参数,因此您可以编写:||> 运算符采用包含两个值的元组,并将它们作为两个参数传递给右侧的函数,这意味着
(a, b) ||> 。 f
表示fa b
。|>
运算符仅采用左侧的单个值,因此(a, b) |> f
表示f (a, b)
(如果函数f
需要元组而不是两个空格分隔的参数,则该函数可以工作)。如果您需要
lemmas
在末尾成为一个数组,则需要将Array.ofSeq
添加到处理管道的末尾(所有Seq
code> 函数使用序列,对应于IEnumerable
)另一种选择是使用序列表达式(您可以使用
[| .. |]
构造一个直接数组(如果您需要的话):是否使用序列表达式 - 这只是个人喜好。在这种情况下,第一个选项似乎更简洁,但对于不太熟悉部分函数应用程序等内容的人来说,序列表达式可能更具可读性(在使用
Seq.map
的较短版本中)Your code looks quite good to me - most of it deals with some loading and initialization, so there isn't much you could do to simplify that part. Alternatively to
Array.map2
, you could useSeq.zip
combined withSeq.map
- thezip
function combines two sequences into a single one that contains pairs of elements with matching indices:Since
lookup
function takes a tuple that you got as an argument, you could write:The
||>
operator used in the second version takes a tuple containing two values and passes them to the function on the right side as two arguments, meaning that(a, b) ||> f
meansf a b
. The|>
operator takes only a single value on the left, so(a, b) |> f
would meanf (a, b)
(which would work if the functionf
expected tuple instead of two, space separated, parameters).If you need
lemmas
to be an array at the end, you'll need to addArray.ofSeq
to the end of the processing pipeline (allSeq
functions work with sequences, which correspond toIEnumerable<T>
)One more alternative is to use sequence expressions (you can use
[| .. |]
to construct an array directly if that's what you need):Whether to use sequence expressions or not - that's just a personal preference. The first option seems to be more succinct in this case, but sequence expressions may be more readable for people less familiar with things like partial function application (in the shorter version using
Seq.map
)