2017-07-16 102 views
0

我尝试学习scala和特定文本minning(词形化,TF-IDF矩阵和LSA)。Scala将[Seq [string]转换为[String]? (在词形化后的TF-IDF)

我有一些文本我想要lemmatize并作出分类(LSA)。我在cloudera上使用spark。

所以我用了stanfordCore NLP fonction:

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矩阵,但这里是我的问题: 斯坦福fonction使在RDD [序列[字符串]形成。 但是,我有一个错误。 我需要以[String]形式(而不是[Seq [string]]形式)使用RDD。

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

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

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

感谢您的帮助。 对不起,如果这是一个愚蠢的问题和英语。

再见

+0

对不起,我需要澄清我的问题。在[Seq [字符串形式]]中,词典化函数做了一个RDD,但我只需要一个[字符串形式]给tf-idf。你知道一个形式为[String]的词形化功能吗? –

回答

0

我不知道这个词形还原啄是什么,但只要做一个串出一个序列,你可以做seq.mkString("\n")(或替换“\ n”和你想要的任何其他分隔符) ,或者只需要seq.mkString,如果你想要它合并没有任何分隔符。

另外,不要使用可变的结构,它在斯卡拉味道不好:

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