繁体   English   中英

Scala将[Seq [string]转换为[String]? (定格后的TF-IDF)

[英]Scala Convert [Seq[string] to [String]? (TF-IDF after lemmatization)

我尝试学习scala,特别是文本挖掘(词法化,TF-IDF矩阵和LSA)。

我有一些文本想作定形并进行分类(LSA)。 我在cloudera上使用spark。

因此,我使用了stanfordCore NLP功能:

    def plainTextToLemmas(text: String, stopWords: Set[String]): Seq[String] = {
    val props = new Properties()
    props.put("annotators", "tokenize, ssplit, pos, lemma")
    val pipeline = new StanfordCoreNLP(props)
    val doc = new Annotation(text)
    pipeline.annotate(doc)
    val lemmas = new ArrayBuffer[String]()
    val sentences = doc.get(classOf[SentencesAnnotation])
    for (sentence <- sentences; token <-sentence.get(classOf[TokensAnnotation])) {
    val lemma = token.get(classOf[LemmaAnnotation])
    if (lemma.length > 2 && !stopWords.contains(lemma)) {
    lemmas += lemma.toLowerCase
    }
    }
    lemmas
    }

之后,我尝试制作一个TF-IDF矩阵,但这是我的问题:Stanford函数以[Seq [string]形式制作RDD。 但是,我有一个错误。 我需要以[String]形式(而不是[Seq [string]]形式)使用RDD。

val (termDocMatrix, termIds, docIds, idfs) = termDocumentMatrix(lemmatized-text, stopWords, numTerms, sc)

有人知道如何将[Seq [string]]转换为[String]吗?

或者我需要更改我的请求之一?

谢谢您的帮助。 抱歉,这是一个愚蠢的问题,对于英语。

再见

我不确定这种去词性是什么,但是就使字符串脱离序列而言,您可以执行seq.mkString("\\n") (或将“ \\ n”替换为所需的任何其他分隔符),或只是seq.mkString如果要合并而没有任何分隔符)。

另外,不要使用可变结构,这在scala中是不好的味道:

val lemmas = sentences
  .map(_.get(classOf[TokensAnnotation]))
  .map(_.get(classOf[LemmaAnnotation]))
  .filter(_.length > 2)
  .filterNot(stopWords)
  .mkString

暂无
暂无

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

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