我开发了一个应用程序,它在(标记化的)文本中构建单词对,并产生每对发生的次数(即使同一对词出现多次,也可以,因为它将在算法的后期得到均衡).
我用的时候
elements groupBy()
Run Code Online (Sandbox Code Playgroud)
我想按元素的内容进行分组,所以我写了以下内容:
def self(x: (String, String)) = x
/**
* Maps a collection of words to a map where key is a pair of words and the
* value is number of
* times this pair
* occurs in the passed array
*/
def producePairs(words: Array[String]): Map[(String,String), Double] = {
var table = List[(String, String)]()
words.foreach(w1 =>
words.foreach(w2 =>
table = table ::: List((w1, w2))))
val grouppedPairs = table.groupBy(self)
val size = int2double(grouppedPairs.size)
return grouppedPairs.mapValues(_.length / size) …Run Code Online (Sandbox Code Playgroud) scala ×1