小编yhy*_*ord的帖子

How to make method return the same generic as the input?

I want to split a string delimited by commas and use the result as either a Seq or a Set:

def splitByComma(commaDelimited: String): Array[String]
  = commaDelimited.trim.split(',')

def splitByCommaAsSet(commaDelimited: String): Set[String]
  = splitByComma(commaDelimited).toSet

def splitByCommaAsSeq(commaDelimited: String): Seq[String]
  = splitByComma(commaDelimited).toSeq

val foods = "sushi,taco,burrito"
val foodSet = splitByCommaAsSet(foods)
// foodSet: scala.collection.immutable.Set[String] = Set(sushi, taco, burrito)
val foodSeq = splitByCommaAsSeq(foods)
// foodSeq: Seq[String] = List(sushi, taco, burrito)
Run Code Online (Sandbox Code Playgroud)

However, this is quite repetitive. Ideally, I would want to have something like genericSplitByComma[Set](foods) that …

generics polymorphism scala type-conversion scala-collections

13
推荐指数
3
解决办法
868
查看次数

用中文和英文标记文本不恰当地将英文单词分成字母

当标记包含中文和英文的文本时,结果会将英文单词分成字母,这不是我想要的.请考虑以下代码:

from nltk.tokenize.stanford_segmenter import StanfordSegmenter
segmenter = StanfordSegmenter()
segmenter.default_config('zh')
print(segmenter.segment('?????Melissa Dell'))
Run Code Online (Sandbox Code Playgroud)

输出将是???? ? M e l i s s a D e l l.如何修改此行为?

nlp tokenize nltk stanford-nlp python-3.x

5
推荐指数
1
解决办法
997
查看次数