简体   繁体   English

Scala将项目列表映射到值

[英]Scala Map a list of items to a value

I have a list of bigrams of a sentence and another original list of relevantbigrams , I want to check that if any of the relevantbigrams are present in the sentences then I want to return the sentence. 我有一个句子的双字母组列表和另一个有关 bigrams的原始列表,我想检查一下句子中是否存在任何有关 bigrams,然后我想返回该句子。 I was thinking of implementing it as follows: map each of the bigrams in the list to the sentence they come from then do a search on the key an return the value. 我正在考虑按以下方式实现它:将列表中的每个双字母组映射到它们来自的句子,然后在键上进行搜索并返回值。

example: 例:

relevantbigrams = (This is, is not, not what)
bigrams List(list(This of, of no, no the),list(not what, what is))

So each list is a bigram of separate sentences. 因此,每个列表都是单独句子的二元组。 Here "not what" from the second sentence matches, so I would like to return the second sentence. 这里第二句的“不是什么”匹配,所以我想返回第二句。 I am planning to have a map of Map("This of" -> "This of no the", "of no" ->"This of no the", "not what"->"not what is"). 我正准备有一个Map(“ This of”->“ This of no the”,“ of no”->“ This of no the”,“ not what”->“ not what is”)地图。 etc. and return the sentences that match on relevant bigram, so here I return "not what is" 等等,并返回与相关双字词匹配的句子,所以在这里我返回“不是什么”

This is my code: 这是我的代码:

val bigram = usableTweets.map(x =>Tokenize(x).sliding(2).flatMap{case Vector(x,y) => List(x+" "+y)}.map(z => z, x))
for(i<- 0 to relevantbigram.length)
    if(bigram.contains(relevantbigram(i)))) bigram.get(relevantbigram(i))
    else useableTweets.head

You got the order or flatMap and map the wrong way around: 您得到了order或flatMap并以错误的方式map

val bigramMap = usableTweets.flatMap { x => 
    x.split(" ").sliding(2).
      map(bg => bg.mkString(" ") -> x)
} toMap

Then you can do your search like this: 然后,您可以像这样进行搜索:

relevantbigrams collect { rb if theMap contains rb => bigramMap(rb) }

Or 要么

val found = 
  for { 
    rb <- relevantbigrams
    sentence <- theMap get rb
  } yield sentence

Both should give you a list, but from your code it appears you want to default to the first sentence if your search found nothing: 两者都应该给您一个列表,但是从您的代码看来,如果您的搜索没有发现,您想默认为第一句话:

found.headOption.getOrElse(usableTweets.head)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM