新网创想网站建设,新征程启航

为企业提供网站建设、域名注册、服务器等服务

Spark中文分词-创新互联

Spark 中文分词

一、导入需要的分词包

创新互联建站长期为上1000+客户提供的网站建设服务,团队从业经验10年,关注不同地域、不同群体,并针对不同对象提供差异化的产品和服务;打造开放共赢平台,与合作伙伴共同营造健康的互联网生态环境。为临猗企业提供专业的做网站、网站建设,临猗网站改版等技术服务。拥有十余年丰富建站经验和众多成功案例,为您定制开发。
import org.ansj.domain.Term
import org.ansj.recognition.impl.StopRecognition
import org.ansj.splitWord.analysis.ToAnalysis

二、停用词过滤

def filter(stopWords: Array[String]): StopRecognition = {
    // add stop words 
    val filter = new StopRecognition
    filter.insertStopNatures("w") // filter punctuation
    filter.insertStopNatures("m") // filter m pattern
    filter.insertStopNatures("null") // filter null
    filter.insertStopNatures("
") // filter
filter.insertStopRegexes("^[a-zA-Z]{1,}") //filter English alphabet filter.insertStopRegexes("^[0-9]+") //filter number filter.insertStopRegexes("[^a-zA-Z0-9\\u4e00-\\u9fa5]+") filter.insertStopRegexes("\t") for (x <- stopWords) { filter.insertStopWords(x) } filter }

三、分词

def getWords(text: String, filter: StopRecognition): ArrayBuffer[String] = {
    val words = new mutable.ArrayBuffer[String]()
    val terms: java.util.List[Term] =  ToAnalysis.parse(text).recognition(filter).getTerms
    for (i <- 0 until terms.size()) {
        val word = terms.get(i).getName
        if (word.length >= MIN_WORD_LENGTH) {
           words += word
        }
    }
   words
}

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


分享文章:Spark中文分词-创新互联
当前路径:http://www.wjwzjz.com/article/ceccjd.html
在线咨询
服务热线
服务热线:028-86922220
TOP