自然语言处理中的文本特征提取与模型构建
1. Spark中TF.IDF计算与搜索功能实现
在Spark的MLlib里,存在计算TF.IDF的阶段。若某列包含字符串数组,可使用 CountVectorizer 或者 HashingTF 来获取词频(tf)值。 HashingTF 运用了哈希技巧,预先确定一个向量空间,再把单词哈希到该空间。若发生哈希冲突,这些单词会被视作相同的。这使得我们能在内存效率和准确性之间进行权衡,当预先设定的向量空间增大时,输出向量会变大,但冲突的可能性会降低。
以下是构建搜索功能的代码示例:
def process_query(query, pipeline):
data = spark.createDataFrame([(query,)], ['text'])
return pipeline.transform(data).first()['normalized']
def get_filter_set(processed_query):
filter_set = set()
# find all the documents that contain any of the terms
return filter_set
def get_score(index, terms):
return # return a single score
def display(index, score, terms):
hits = [term for ter
超级会员免费看
订阅专栏 解锁全文

被折叠的 条评论
为什么被折叠?



