streaming.dsl.mmlib.algs.SQLFeatureExtractInPlace.scala Maven / Gradle / Ivy
The newest version!
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package streaming.dsl.mmlib.algs
import java.util.regex.Pattern
import org.apache.commons.lang3.StringUtils
import org.apache.spark.sql.{DataFrame, SaveMode, SparkSession, functions => F}
import org.apache.spark.sql.expressions.UserDefinedFunction
import streaming.dsl.mmlib.SQLAlg
import streaming.dsl.mmlib.algs.MetaConst.getMetaPath
/**
* Created by dxy_why on 2018/8/20.
*/
class SQLFeatureExtractInPlace extends SQLAlg with Functions {
/**
* 是否含有电话
*
* @return
*/
def phoneExisted = F.udf((doc: String) => {
SQLFeatureExtractInPlace.EXISTED_PHONE_REGEX.map(regex => {
val pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE & Pattern.DOTALL)
val matcher = pattern.matcher(doc)
matcher.find()
}).contains(true)
})
/**
* 是否含有邮箱地址
*
* @return
*/
def emailExisted = F.udf((doc: String) => {
SQLFeatureExtractInPlace.EXISTED_EMAIL_REGEX.map(regex => {
val pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE & Pattern.DOTALL)
val matcher = pattern.matcher(doc)
matcher.find()
}).contains(true)
})
/**
* 是否还有qq微信号
*
* @return
*/
def qqwechatExisted = F.udf((doc: String) => {
SQLFeatureExtractInPlace.EXISTED_QQWECHAT_REGEX.map(regex => {
val pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE & Pattern.DOTALL)
val matcher = pattern.matcher(doc)
matcher.find()
}).contains(true)
})
/**
* url数量
*
* @return
*/
def urlNumber = F.udf((doc: String) => {
StringUtils.countMatches(doc, "http")
})
/**
* 图片数量
*
* @return
*/
def picNumber = F.udf((doc: String) => {
SQLFeatureExtractInPlace.PIC_NUMBER_REGEX.map(regex => {
val pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE & Pattern.DOTALL)
val matcher = pattern.matcher(doc)
var count = 0
while (matcher.find()) {
count += 1
}
count
}).head
})
def cleanEmotionAndSpecChar = F.udf((doc: String) => {
val regEx_emotion = """
")
.replaceAll("&", "&").replaceAll("@", "@")
/**
* 去除新版app内自带的用户标签
*/
val regEx_user = """[\s\S]*?
"""
val p_user = Pattern.compile(regEx_user, Pattern.CASE_INSENSITIVE)
val m_user = p_user.matcher(htmlStr)
m_user.replaceAll("")
})
def cleanDoc = F.udf((doc: String) => {
/**
* 去除html标签
*/
// 定义script的正则表达式
val regEx_script = "