1. 概览
自然语言处理 (NLP) 是对文本数据进行分析并从中获取数据洞见的研究。随着互联网上生成的文字内容不断增加,组织比以往任何时候都更希望利用其文本来获取与其业务相关的信息。
NLP 的应用场景非常广泛,包括语言翻译、情感分析、从头生成句子等。这是一个热门研究领域,正在改变我们处理文本的方式。
我们将探讨如何对大量文本数据大规模使用 NLP。这无疑是一项艰巨的任务!幸运的是,我们可以利用 Spark MLlib 和 spark-nlp 等库来简化此过程。
2. 我们的用例
我们(虚构)组织“FoodCorp”的首席数据科学家想要详细了解食品行业的趋势。我们可以访问 Reddit subreddit r/food 中的帖子形式的文本数据语料库,我们将使用这些数据来探索人们在谈论什么。
实现此目的的一种方法是通过一种称为“主题建模”的 NLP 方法。主题模型是一种统计方法,可以识别一组文档的语义趋势。换句话说,我们可以基于 Reddit“帖子”语料库构建一个主题模型,该模型将生成一个“主题”列表或描述趋势的字词组。
为了构建模型,我们将使用一种名为“隐含狄利克雷分布”(LDA) 的算法,该算法通常用于对文本进行聚类。如需查看 LDA 的精彩介绍,请点击此处。
3. 创建项目
如果您还没有 Google 账号(Gmail 或 Google Apps),则必须创建一个。登录 Google Cloud Platform 控制台 (console.cloud.google.com) 并创建一个新项目:



接下来,您需要在 Cloud 控制台中启用结算功能,才能使用 Google Cloud 资源。
在此 Codelab 中运行仅花费几美元,但是如果您决定使用更多资源或继续让它们运行,费用可能更高。PySpark-BigQuery 和 Spark-NLP Codelab 均在末尾说明了“清理”步骤。
Google Cloud Platform 的新用户有资格获享 $300 免费试用。
4. 设置环境
首先,我们需要启用 Dataproc 和 Compute Engine API。
点击屏幕左上角的菜单图标。

从下拉菜单中选择“API 管理器”。

点击启用 API 和服务。

在搜索框中搜索“Compute Engine”。在显示的结果列表中,点击“Google Compute Engine API”。

在 Google Compute Engine 页面上,点击启用

启用后,点击指向左侧的箭头即可返回。
现在,搜索“Google Dataproc API”并将其启用。

接下来,点击 Cloud 控制台右上角的按钮,打开 Cloud Shell:

我们将设置一些环境变量,以便在继续学习 Codelab 的过程中引用。首先,为我们要创建的 Dataproc 集群选择一个名称,例如“my-cluster”,并在您的环境中进行设置。您可以随意使用任何喜欢的名称。
CLUSTER_NAME=my-cluster
接下来,从此处提供的可用区中选择一个。示例:us-east1-b.
REGION=us-east1
最后,我们需要设置作业要从中读取数据的源存储桶。我们在存储桶 bm_reddit 中提供了抽样数据,但如果您在完成本实验之前完成了使用 PySpark 预处理 BigQuery 数据实验,也可以使用您在该实验中生成的数据。
BUCKET_NAME=bm_reddit
配置好环境变量后,运行以下命令来创建 Dataproc 集群:
gcloud beta dataproc clusters create ${CLUSTER_NAME} \
--region ${REGION} \
--metadata 'PIP_PACKAGES=google-cloud-storage spark-nlp==2.7.2' \
--worker-machine-type n1-standard-8 \
--num-workers 4 \
--image-version 1.4-debian10 \
--initialization-actions gs://dataproc-initialization-actions/python/pip-install.sh \
--optional-components=JUPYTER,ANACONDA \
--enable-component-gateway
我们来逐一了解这些命令:
gcloud beta dataproc clusters create ${CLUSTER_NAME}:将使用您之前提供的名称启动 Dataproc 集群的创建。我们在此处添加了 beta,以启用 Dataproc 的 Beta 版功能,例如组件网关(我们将在下文讨论)。
--zone=${ZONE}:此参数用于设置集群的位置。
--worker-machine-type n1-standard-8:这是用于工作器的机器类型。
--num-workers 4:我们的集群将有四个工作器。
--image-version 1.4-debian9:这表示我们将使用的 Dataproc 映像版本。
--initialization-actions ...:初始化操作是在创建集群和工作器时执行的自定义脚本。这些文件可以由用户创建并存储在 GCS 存储桶中,也可以从公共存储桶 dataproc-initialization-actions 中引用。此处包含的初始化操作将允许使用 Pip 安装 Python 软件包,如 --metadata 标志所提供。
--metadata 'PIP_PACKAGES=google-cloud-storage spark-nlp':这是要安装到 Dataproc 中的软件包的空格分隔列表。在本例中,我们将安装 google-cloud-storage Python 客户端库和 spark-nlp。
--optional-components=ANACONDA:可选组件是与 Dataproc 搭配使用的常见软件包,会在创建期间自动安装在 Dataproc 集群上。与初始化操作相比,使用可选组件的优势包括启动时间更短,并且针对特定 Dataproc 版本进行了测试。总体而言,它们更可靠。
--enable-component-gateway:此标志可让我们利用 Dataproc 的组件网关来查看常见界面,例如 Zeppelin、Jupyter 或 Spark History。注意:其中一些需要关联的可选组件。
如需更深入地了解 Dataproc,请查看此 Codelab。
接下来,在 Cloud Shell 中运行以下命令,克隆包含示例代码的代码库,并切换到正确的目录:
cd
git clone https://github.com/GoogleCloudPlatform/cloud-dataproc
cd cloud-dataproc/codelabs/spark-nlp
5. Spark MLlib
Spark MLlib 是一个用 Apache Spark 编写的可扩缩机器学习库。MLlib 能够利用 Spark 的高效性,并结合一系列经过微调的机器学习算法,来分析海量数据。它提供 Java、Scala、Python 和 R API。在本 Codelab 中,我们将重点介绍 Python。
MLlib 包含大量转换器和估算器。转换器是一种可以改变或更改数据的工具,通常使用 transform() 函数;而估算器是一种预构建的算法,您可以使用它来训练数据,通常使用 fit() 函数。
转换器的示例包括:
- 词元化(根据一串字词创建数字向量)
- 独热编码(创建稀疏向量,其中数字表示字符串中出现的字词)
- 停用词移除器(移除不会为字符串增加语义价值的字词)
估算器的示例包括:
- 分类(这是苹果还是橙子?)
- 回归(这个苹果应该卖多少钱?)
- 聚类(所有苹果彼此之间有多相似?)
- 决策树(如果颜色是橙色,那么它就是橙子。否则就是苹果)
- 降维(我们能否从数据集中移除特征,但仍能区分苹果和橙子?)。
MLlib 还包含用于机器学习中其他常见方法的工具,例如超参数调优和选择以及交叉验证。
此外,MLlib 还包含 Pipelines API,可让您使用不同的转换器构建可重新执行的数据转换流水线。
6. Spark-NLP
Spark-nlp 是由 John Snow Labs 创建的一个库,用于使用 Spark 执行高效的自然语言处理任务。它包含名为注释器的内置工具,可用于执行以下常见任务:
- 词元化(根据一串字词创建数字向量)
- 创建词嵌入(通过向量定义词之间的关系)
- 词性标签(哪些词是名词?Which are verbs?)
虽然不在本 Codelab 的讨论范围之内,但 spark-nlp 也能与 TensorFlow 很好地集成。
或许最重要的是,Spark-NLP 提供了可轻松插入 MLlib 流水线的组件,从而扩展了 Spark MLlib 的功能。
7. 自然语言处理的最佳实践
在从数据中提取有用信息之前,我们需要先做一些准备工作。我们将采取的预处理步骤如下:
标记化
通常,我们要做的第一件事就是对数据进行“词元化”。这涉及获取数据并根据“词元”或字词将其拆分。通常,我们会在这一步中移除标点符号,并将所有字词都设为小写。例如,假设我们有以下字符串:What time is it?。经过词元化后,该句子将包含四个词元:“what" , "time", "is", "it".。我们不希望模型将 what 一词视为两个不同的大小写形式不同的词。此外,标点符号通常无法帮助我们更好地从字词中学习推理,因此我们也会将其去除。
标准化
我们通常希望对数据进行“归一化”。这会将含义相似的字词替换为相同的字词。例如,如果在文本中识别出“fought”(战斗)、“battled”(战斗)和“dueled”(决斗)这些词,则标准化可能会将“battled”和“dueled”替换为“fought”。
词干提取
词干提取会将字词替换为其词根含义。例如,“car”“cars'”和“car's”这些词都会被替换为“car”,因为这些词的词根都表示相同的意思。
移除停用词
停用词是指“和”和“的”等通常不会为句子的语义增加价值的词。我们通常希望移除这些内容,以减少文本数据集中的噪声。
8. 运行作业
我们来看看要运行的作业。您可以在 cloud-dataproc/codelabs/spark-nlp/topic_model.py 中找到该代码。请至少花几分钟时间通读该代码和相关注释,了解其执行的操作。我们还会在下方重点介绍其中一些部分:
# Python imports
import sys
# spark-nlp components. Each one is incorporated into our pipeline.
from sparknlp.annotator import Lemmatizer, Stemmer, Tokenizer, Normalizer
from sparknlp.base import DocumentAssembler, Finisher
# A Spark Session is how we interact with Spark SQL to create Dataframes
from pyspark.sql import SparkSession
# These allow us to create a schema for our data
from pyspark.sql.types import StructField, StructType, StringType, LongType
# Spark Pipelines allow us to sequentially add components such as transformers
from pyspark.ml import Pipeline
# These are components we will incorporate into our pipeline.
from pyspark.ml.feature import StopWordsRemover, CountVectorizer, IDF
# LDA is our model of choice for topic modeling
from pyspark.ml.clustering import LDA
# Some transformers require the usage of other Spark ML functions. We import them here
from pyspark.sql.functions import col, lit, concat
# This will help catch some PySpark errors
from pyspark.sql.utils import AnalysisException
# Assign bucket where the data lives
try:
bucket = sys.argv[1]
except IndexError:
print("Please provide a bucket name")
sys.exit(1)
# Create a SparkSession under the name "reddit". Viewable via the Spark UI
spark = SparkSession.builder.appName("reddit topic model").getOrCreate()
# Create a three column schema consisting of two strings and a long integer
fields = [StructField("title", StringType(), True),
StructField("body", StringType(), True),
StructField("created_at", LongType(), True)]
schema = StructType(fields)
# We'll attempt to process every year / month combination below.
years = ['2016', '2017', '2018', '2019']
months = ['01', '02', '03', '04', '05', '06',
'07', '08', '09', '10', '11', '12']
# This is the subreddit we're working with.
subreddit = "food"
# Create a base dataframe.
reddit_data = spark.createDataFrame([], schema)
# Keep a running list of all files that will be processed
files_read = []
for year in years:
for month in months:
# In the form of <project-id>.<dataset>.<table>
gs_uri = f"gs://{bucket}/reddit_posts/{year}/{month}/{subreddit}.csv.gz"
# If the table doesn't exist we will simply continue and not
# log it into our "tables_read" list
try:
reddit_data = (
spark.read.format('csv')
.options(codec="org.apache.hadoop.io.compress.GzipCodec")
.load(gs_uri, schema=schema)
.union(reddit_data)
)
files_read.append(gs_uri)
except AnalysisException:
continue
if len(files_read) == 0:
print('No files read')
sys.exit(1)
# Replacing null values with their respective typed-equivalent is usually
# easier to work with. In this case, we'll replace nulls with empty strings.
# Since some of our data doesn't have a body, we can combine all of the text
# for the titles and bodies so that every row has useful data.
df_train = (
reddit_data
# Replace null values with an empty string
.fillna("")
.select(
# Combine columns
concat(
# First column to concatenate. col() is used to specify that we're referencing a column
col("title"),
# Literal character that will be between the concatenated columns.
lit(" "),
# Second column to concatenate.
col("body")
# Change the name of the new column
).alias("text")
)
)
# Now, we begin assembling our pipeline. Each component here is used to some transformation to the data.
# The Document Assembler takes the raw text data and convert it into a format that can
# be tokenized. It becomes one of spark-nlp native object types, the "Document".
document_assembler = DocumentAssembler().setInputCol("text").setOutputCol("document")
# The Tokenizer takes data that is of the "Document" type and tokenizes it.
# While slightly more involved than this, this is effectively taking a string and splitting
# it along ths spaces, so each word is its own string. The data then becomes the
# spark-nlp native type "Token".
tokenizer = Tokenizer().setInputCols(["document"]).setOutputCol("token")
# The Normalizer will group words together based on similar semantic meaning.
normalizer = Normalizer().setInputCols(["token"]).setOutputCol("normalizer")
# The Stemmer takes objects of class "Token" and converts the words into their
# root meaning. For instance, the words "cars", "cars'" and "car's" would all be replaced
# with the word "car".
stemmer = Stemmer().setInputCols(["normalizer"]).setOutputCol("stem")
# The Finisher signals to spark-nlp allows us to access the data outside of spark-nlp
# components. For instance, we can now feed the data into components from Spark MLlib.
finisher = Finisher().setInputCols(["stem"]).setOutputCols(["to_spark"]).setValueSplitSymbol(" ")
# Stopwords are common words that generally don't add much detail to the meaning
# of a body of text. In English, these are mostly "articles" such as the words "the"
# and "of".
stopword_remover = StopWordsRemover(inputCol="to_spark", outputCol="filtered")
# Here we implement TF-IDF as an input to our LDA model. CountVectorizer (TF) keeps track
# of the vocabulary that's being created so we can map our topics back to their
# corresponding words.
# TF (term frequency) creates a matrix that counts how many times each word in the
# vocabulary appears in each body of text. This then gives each word a weight based
# on its frequency.
tf = CountVectorizer(inputCol="filtered", outputCol="raw_features")
# Here we implement the IDF portion. IDF (Inverse document frequency) reduces
# the weights of commonly-appearing words.
idf = IDF(inputCol="raw_features", outputCol="features")
# LDA creates a statistical representation of how frequently words appear
# together in order to create "topics" or groups of commonly appearing words.
lda = LDA(k=10, maxIter=10)
# We add all of the transformers into a Pipeline object. Each transformer
# will execute in the ordered provided to the "stages" parameter
pipeline = Pipeline(
stages = [
document_assembler,
tokenizer,
normalizer,
stemmer,
finisher,
stopword_remover,
tf,
idf,
lda
]
)
# We fit the data to the model.
model = pipeline.fit(df_train)
# Now that we have completed a pipeline, we want to output the topics as human-readable.
# To do this, we need to grab the vocabulary generated from our pipeline, grab the topic
# model and do the appropriate mapping. The output from each individual component lives
# in the model object. We can access them by referring to them by their position in
# the pipeline via model.stages[<ind>]
# Let's create a reference our vocabulary.
vocab = model.stages[-3].vocabulary
# Next, let's grab the topics generated by our LDA model via describeTopics(). Using collect(),
# we load the output into a Python array.
raw_topics = model.stages[-1].describeTopics().collect()
# Lastly, let's get the indices of the vocabulary terms from our topics
topic_inds = [ind.termIndices for ind in raw_topics]
# The indices we just grab directly map to the term at position <ind> from our vocabulary.
# Using the below code, we can generate the mappings from our topic indices to our vocabulary.
topics = []
for topic in topic_inds:
_topic = []
for ind in topic:
_topic.append(vocab[ind])
topics.append(_topic)
# Let's see our topics!
for i, topic in enumerate(topics, start=1):
print(f"topic {i}: {topic}")
运行作业
现在,我们来运行作业。继续运行以下命令:
gcloud dataproc jobs submit pyspark --cluster ${CLUSTER_NAME}\
--region ${REGION}\
--properties=spark.jars.packages=com.johnsnowlabs.nlp:spark-nlp_2.11:2.7.2\
--driver-log-levels root=FATAL \
topic_model.py \
-- ${BUCKET_NAME}
此命令可让我们利用 Dataproc Jobs API。通过添加命令 pyspark,我们向集群表明这是一个 PySpark 作业。我们提供集群名称、可选参数(可从此处获取)以及包含作业的文件名称。在本例中,我们提供了 --properties 参数,以便更改 Spark、Yarn 或 Dataproc 的各种属性。我们将更改 Spark 属性 packages,以便通知 Spark 我们希望将 spark-nlp 包含在作业中。我们还提供了参数 --driver-log-levels root=FATAL,该参数将抑制 PySpark 的大部分日志输出(错误除外)。一般来说,Spark 日志往往比较杂乱。
最后,-- ${BUCKET} 是 Python 脚本本身的命令行参数,用于提供存储桶名称。请注意 -- 和 ${BUCKET} 之间的空格。
运行作业几分钟后,我们应该会看到包含模型的输出:

太棒啦!!您能否通过查看模型的输出来推断趋势?那我们的呢?
从上述输出中,可以推断出主题 8 与早餐食品有关,主题 9 与甜点有关。
9. 清理
为避免在完成本快速入门后您的 GCP 账号产生不必要的费用,请执行以下操作:
如果您是专门为此 Codelab 创建的项目,也可以选择删除该项目:
- 在 GCP 控制台中,前往项目页面。
- 在项目列表中,选择要删除的项目,然后点击删除。
- 在对话框中输入项目 ID,然后点击关停以删除项目。
许可
本作品已获得知识共享署名 3.0 通用许可和 Apache 2.0 许可。