f# 使用 ac# 库中的函数迭代两个数组

发布于 2024-09-01 17:45:27 字数 513 浏览 3 评论 0原文

我有一个单词列表和相关词性标签列表。我想使用每个索引元组作为 .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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

§对你不离不弃 2024-09-08 17:45:27

您的代码对我来说看起来相当不错 - 其中大部分涉及一些加载和初始化,因此您无法做太多事情来简化该部分。除了 Array.map2 之外,您还可以将 Seq.zipSeq.map 结合使用 - zip 函数结合了将两个序列合并为一个包含具有匹配索引的元素对的序列:

let lemmas = Seq.zip words tags 
          |> Seq.map (fun (x, y) -> lemmatizer.lookup (x, y)) 

由于 lookup 函数采用您获得的元组作为参数,因此您可以编写:

// standard syntax using the pipelining operator
let lemmas = Seq.zip words tags |> Seq.map lemmatizer.lookup

// .. an alternative syntax doing exactly the same thing
let lemmas = (words, tags) ||> Seq.zip |> Seq.map lemmatizer.lookup

||> 运算符采用包含两个值的元组,并将它们作为两个参数传递给右侧的函数,这意味着 (a, b) ||> 。 f 表示 fa b|> 运算符仅采用左侧的单个值,因此 (a, b) |> f 表示 f (a, b) (如果函数 f 需要元组而不是两个空格分隔的参数,则该函数可以工作)。

如果您需要 lemmas 在末尾成为一个数组,则需要将 Array.ofSeq 添加到处理管道的末尾(所有 Seq code> 函数使用序列,对应于 IEnumerable

另一种选择是使用序列表达式(您可以使用 [| .. |] 构造一个直接数组(如果您需要的话):

let lemmas = [| for wt in Seq.zip words tags do // wt is tuple (string * string)
                  yield lemmatizer.lookup wt |] 

是否使用序列表达式 - 这只是个人喜好。在这种情况下,第一个选项似乎更简洁,但对于不太熟悉部分函数应用程序等内容的人来说,序列表达式可能更具可读性(在使用 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 use Seq.zip combined with Seq.map - the zip function combines two sequences into a single one that contains pairs of elements with matching indices:

let lemmas = Seq.zip words tags 
          |> Seq.map (fun (x, y) -> lemmatizer.lookup (x, y)) 

Since lookup function takes a tuple that you got as an argument, you could write:

// standard syntax using the pipelining operator
let lemmas = Seq.zip words tags |> Seq.map lemmatizer.lookup

// .. an alternative syntax doing exactly the same thing
let lemmas = (words, tags) ||> Seq.zip |> Seq.map lemmatizer.lookup

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 means f a b. The |> operator takes only a single value on the left, so (a, b) |> f would mean f (a, b) (which would work if the function f expected tuple instead of two, space separated, parameters).

If you need lemmas to be an array at the end, you'll need to add Array.ofSeq to the end of the processing pipeline (all Seq functions work with sequences, which correspond to IEnumerable<T>)

One more alternative is to use sequence expressions (you can use [| .. |] to construct an array directly if that's what you need):

let lemmas = [| for wt in Seq.zip words tags do // wt is tuple (string * string)
                  yield lemmatizer.lookup wt |] 

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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文