[英]Need Help Converting some F# code to C#
I am doing a Machine Learning using F# book. 我正在使用F#书进行机器学习。 I am converting the F# code to C# and doing the tutorials as I do not understand F# that much.
我将F#代码转换为C#并进行了教程,因为我不太了解F#。 I really got stuck in a part of code and wonder if someone can tell me how to write the code in c#.
我真的陷入了代码的一部分,想知道是否有人可以告诉我如何用C#编写代码。 Even just the explanation of what the code does in F# would be helpful.. The code I am stuck is
即使只是解释代码在F#中的功能也将有所帮助。
let tokenScore (group:DocsGroup) (token:Token) =
if group.TokenFrequencies.ContainsKey token
then log group.TokenFrequencies.[token]
else 0.0
let score (document:TokenizedDoc) (group:DocsGroup) =
let scoreToken = tokenScore group
log group.Proportion +
(document |> Seq.sumBy scoreToken)
I'm really stuck with the 我真的坚持
let scoreToken = tokenScore group
log group.Proportion +
(document |> Seq.sumBy scoreToken)
This is the tutorial in the Machine Learning Projects for .Net Developers - Chapter 2. If you can help me that would be great. 这是.Net开发人员机器学习项目中的教程-第2章。如果可以帮助我,那真是太好了。 Thanks in advance
提前致谢
F# functions are curried . F#函数是咖喱的 。 That means that if a function is declared with two parameters, and you call it with just one parameter, the result is another function , of one parameter.
这意味着,如果使用两个参数声明一个函数,而仅用一个参数调用它,则结果是一个参数的另一个函数 。 Here's an example:
这是一个例子:
let add x y = x + y
let n = add 2 3 // Now n has the value 5
let addFive = add 5 // This is a *function* that takes one parameter
let altAddFive y = add 5 y // Another way to define the same thing
let result = addFive 3 // Now result has the value 8
let altResult = altAddFive 3 // This one also has the value 8
So in the score
function, the name scoreToken
now refers to a function that takes one parameter, and then calls tokenScore
with two parameters: group
, and the value it was just passed. 因此,在
score
函数中,名称scoreToken
现在是指一个具有一个参数的函数,然后使用两个参数调用tokenScore
: group
和刚刚传递的值。 In other words, the definition of scoreToken
could have been written as: 换句话说,
scoreToken
的定义可以写成:
let scoreToken token = tokenScore group token
And that would have been the same thing. 那将是同一回事。
Now for the next line. 现在下一行。 First,
log
is the natural logarithm function , usually written as ln
in mathematical notation. 首先,
log
是自然对数函数 ,通常以数学符号ln
表示。 It takes a single floating-point number, and returns a floating-point number. 它采用一个浮点数,并返回一个浮点数。 The rules of operator precedence in F# are that function calls are higher precedence than operators, so
log 3.0 + 5.0
would be interpreted as (log 3.0) + 5.0
, which would not be the same value as log 8.0
. 运算符优先级的F#中的规则是该函数调用比运营商优先级更高 ,因此
log 3.0 + 5.0
将被解释为(log 3.0) + 5.0
,这不会是相同的值作为log 8.0
。 Therefore, the following lines: 因此,以下几行:
log group.Proportion +
(document |> Seq.sumBy scoreToken)
are equivalent to this: 等效于:
let score1 = log group.Proportion
let score2 = document |> Seq.sumBy scoreToken
score1 + score2
Finally, the document |> Seq.sumBy scoreToken
line may also be confusing you. 最后,
document |> Seq.sumBy scoreToken
行也可能使您感到困惑。 There are two parts to it. 它有两个部分。 First,
Seq.sumBy
is the equivalent of the two-parameter version of the LINQ Sum
method , but the parameter order in F# is the opposite of the one in C#. 首先,
Seq.sumBy
与LINQ Sum
方法的两参数版本等效,但是F#中的参数顺序与C#中的参数顺序相反。 The F# Seq.sumBy
function takes two parameters; F#
Seq.sumBy
函数采用两个参数; first, the function that should be applied to each element to produce a value, and second, the sequence of elements. 首先,应应用于每个元素以产生值的函数,其次是元素序列。 Ie, if you were calling it without that
|>
operator, it would look like this: 即,如果您在不使用
|>
运算符的情况下调用它,则它将看起来像这样:
Seq.sumBy scoreToken document
This would go through the document
sequence (which is of type TokenizedDoc
and, given the way it's used, clearly can be treated as a sequence of tokens). 这将遍历
document
序列(其类型为TokenizedDoc
并且鉴于其使用方式,显然可以视为令牌序列)。 For each token in the document, the scoreToken
function would be called (if you'll remember, calling scoreToken t
, where t
is some token, is the same as calling tokenScore group t
). 对于文档中的每个令牌,将调用
scoreToken
函数(如果您还记得,调用scoreToken t
,其中t
是某些令牌,与调用tokenScore group t
相同)。 That call would produce a floating-point value. 该调用将产生一个浮点值。 Finally, all the floating-point values will be added together to produce the final score.
最后,所有浮点值将被加在一起以产生最终分数。
However, in F#, it's traditional to use the "pipe" operator |>
when summing sequences or doing similar things: document |> Seq.sumBy scoreToken
is exactly equivalent to Seq.sumBy scoreToken document
. 但是,在F#中,传统上在对序列求和或执行类似操作时使用“ pipe”运算符
|>
: document |> Seq.sumBy scoreToken
与Seq.sumBy scoreToken document
完全等效。 The |>
operator is rather well explained in the answer to another SO question so I won't duplicate that answer here. |>
运算符在另一个SO问题的答案中作了很好的解释,因此在此我将不重复该答案。
So what this function is doing is taking the natural log of the group.Proportion
value, and adding it to the score of each token in the document. 因此,此功能正在执行的操作是获取
group.Proportion
值的自然对数,并将其添加到文档中每个标记的分数中。 The result of this addition is the last expression in the function, so that's the result of the function: in F#, unlike C#, you don't need to type return
at the end of functions. 加法的结果是函数中的最后一个表达式,所以这就是函数的结果:在F#中,与C#不同,您不需要在函数末尾键入
return
。 (The return
keyword has a different meaning in F#, which I won't get into now as that's a whole different topic). (
return
关键字在F#中具有不同的含义,由于这是一个完全不同的主题,因此我现在不再赘述)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.