发布日期:2025-12-30
专栏名称:NLTK自然语言处理实战
适用人群:初学者
前置知识:Python基础、NLTK基础、文本预处理、词频统计

1. 引言

1.1 什么是搭配分析

搭配分析(Collocation Analysis)是指识别文本中经常一起出现的单词组合的过程。在自然语言中,某些单词经常会以固定或半固定的方式组合在一起,形成所谓的"搭配"(Collocation)。例如,“strong coffee”(浓咖啡)、“heavy rain”(大雨)、“make a decision”(做决定)等都是常见的搭配。

搭配分析是文本分析中的重要任务,它可以帮助我们理解文本的语义结构、提取关键词组、改进机器翻译和信息检索等。

1.2 为什么要学习搭配分析

  • 理解语义结构:搭配反映了语言的习惯用法和语义关系
  • 提取关键词组:搭配通常比单个单词更能表达特定的概念和主题
  • 改进文本生成:使用自然的搭配可以生成更流畅的文本
  • 优化信息检索:考虑搭配可以提高搜索结果的相关性
  • 支持机器翻译:准确识别搭配有助于提高翻译质量
  • 情感分析增强:某些情感倾向往往通过搭配表达

1.3 本章学习目标

  • 理解搭配的基本概念和类型
  • 掌握搭配强度的评估方法
  • 能够使用NLTK进行搭配分析
  • 了解BigramCollocationFinder和TrigramCollocationFinder的使用
  • 能够应用搭配分析解决实际问题

2. 核心知识点

2.1 搭配的基本概念

搭配是指在文本中经常一起出现的单词组合,这些组合具有以下特点:

  • 统计显著性:在文本中出现的频率显著高于随机组合的预期频率
  • 语义完整性:组合在一起表达的意义不能简单地从单个单词的意义推断出来
  • 语法正确性:符合语言的语法规则
  • 习惯用法:是语言使用者普遍接受的表达方式

搭配的类型

  • 二元搭配(Bigram):由两个单词组成的搭配,如"strong coffee"
  • 三元搭配(Trigram):由三个单词组成的搭配,如"in the middle"
  • 四元搭配(Quadgram):由四个单词组成的搭配,如"at the same time"
  • 名词短语搭配:如"machine learning algorithm"
  • 动词短语搭配:如"make a decision"
  • 形容词-名词搭配:如"heavy rain"
  • 副词-动词搭配:如"strongly believe"

2.2 搭配强度的评估方法

搭配分析的核心是评估两个或多个单词组合在一起的强度,常用的评估方法包括:

  • 频率(Frequency):直接统计搭配出现的次数
  • 点互信息(Pointwise Mutual Information, PMI):衡量两个单词共同出现的概率与它们独立出现的概率之间的差异
  • 卡方检验(Chi-square Test):检验两个单词的共现是否具有统计显著性
  • 似然比(Likelihood Ratio):比较两个单词共同出现的似然与独立出现的似然
  • T检验(T-test):检验搭配的共现频率是否显著高于预期
  • 搭配分数(Collocation Score):综合多个指标的分数

**点互信息(PMI)**是最常用的搭配评估指标之一,其计算公式为:

PMI(x, y) = log2 [ P(x, y) / (P(x) * P(y)) ]

其中:

  • P(x, y)是单词x和y共同出现的概率
  • P(x)是单词x单独出现的概率
  • P(y)是单词y单独出现的概率

PMI值越高,说明两个单词之间的搭配关系越强。

2.3 NLTK中的搭配分析工具

NLTK提供了强大的搭配分析工具,主要位于nltk.collocations模块中,包括:

  • BigramCollocationFinder:用于查找二元搭配
  • TrigramCollocationFinder:用于查找三元搭配
  • QuadgramCollocationFinder:用于查找四元搭配
  • 各种搭配评估函数:如pmichi_sqlikelihood_ratio
2.3.1 BigramCollocationFinder

BigramCollocationFinder用于从文本中提取二元搭配,其主要方法包括:

  • from_words(words):从单词列表创建搭配查找器
  • from_documents(documents):从文档列表创建搭配查找器
  • apply_word_filter(filter_func):应用单词过滤函数
  • apply_freq_filter(min_freq):过滤掉出现频率低于指定值的搭配
  • nbest(score_func, n):返回得分最高的n个搭配
  • above_score(score_func, min_score):返回得分高于指定值的搭配
2.3.2 搭配评估函数

NLTK提供了多种搭配评估函数,位于nltk.collocations模块中:

  • BigramAssocMeasures:二元搭配的评估指标
  • TrigramAssocMeasures:三元搭配的评估指标
  • QuadgramAssocMeasures:四元搭配的评估指标

这些评估指标类包含了各种评估函数,如:

  • pmi:点互信息
  • chi_sq:卡方检验
  • likelihood_ratio:似然比
  • raw_freq:原始频率
  • student_t:T检验

2.4 搭配分析的步骤

使用NLTK进行搭配分析的一般步骤:

  1. 文本预处理:分词、去除停用词、词干提取或词形还原等
  2. 创建搭配查找器:使用BigramCollocationFinder或TrigramCollocationFinder
  3. 应用过滤条件:过滤掉低频单词和搭配
  4. 选择评估指标:选择合适的搭配强度评估方法
  5. 提取搭配:获取得分最高的搭配
  6. 分析和可视化:分析提取的搭配,可视化展示结果

3. 代码示例

3.1 使用BigramCollocationFinder查找二元搭配

功能说明:使用NLTK的BigramCollocationFinder查找文本中的二元搭配,演示其主要功能

代码实现

import nltk
from nltk.collocations import BigramCollocationFinder
from nltk.collocations import BigramAssocMeasures
from nltk.tokenize import word_tokenize
from nltk.corpus import stopwords

# 下载必要的资源
nltk.download('punkt')
nltk.download('stopwords')

# 示例文本
text = """
Natural language processing (NLP) is a subfield of linguistics, computer science, and artificial intelligence concerned with the interactions between computers and human language. It involves developing algorithms and models that enable computers to understand, interpret, generate, and respond to human language in a useful way.

NLP has many applications, including machine translation, speech recognition, text summarization, sentiment analysis, and information extraction. It is used in various industries, such as healthcare, finance, education, and entertainment.

The field of NLP has grown rapidly in recent years, thanks to advances in machine learning and deep learning. These technologies have enabled computers to process and understand human language more accurately than ever before.
"""

# 文本预处理
# 1. 分词
tokens = word_tokenize(text.lower())

# 2. 去除标点符号和数字
tokens = [token for token in tokens if token.isalpha()]

# 3. 去除停用词
stop_words = set(stopwords.words('english'))
tokens = [token for token in tokens if token not in stop_words]

# 创建二元搭配查找器
bigram_finder = BigramCollocationFinder.from_words(tokens)

# 应用频率过滤,只保留出现次数大于等于2的搭配
bigram_finder.apply_freq_filter(2)

# 初始化搭配评估指标
bigram_measures = BigramAssocMeasures()

# 使用不同的评估指标提取搭配
print("=== 二元搭配分析 ===")

# 1. 使用原始频率
print("\n1. 基于原始频率的搭配:")
bigrams_freq = bigram_finder.nbest(bigram_measures.raw_freq, 10)
for bigram in bigrams_freq:
    print(f"  {' '.join(bigram)}")

# 2. 使用点互信息
print("\n2. 基于点互信息的搭配:")
bigrams_pmi = bigram_finder.nbest(bigram_measures.pmi, 10)
for bigram in bigrams_pmi:
    print(f"  {' '.join(bigram)}")

# 3. 使用似然比
print("\n3. 基于似然比的搭配:")
bigrams_lr = bigram_finder.nbest(bigram_measures.likelihood_ratio, 10)
for bigram in bigrams_lr:
    print(f"  {' '.join(bigram)}")

# 4. 使用卡方检验
print("\n4. 基于卡方检验的搭配:")
bigrams_chi = bigram_finder.nbest(bigram_measures.chi_sq, 10)
for bigram in bigrams_chi:
    print(f"  {' '.join(bigram)}")

代码解释

  • 导入必要的模块
  • 定义示例文本
  • 进行文本预处理:分词、去除标点符号和数字、去除停用词
  • 创建BigramCollocationFinder对象
  • 应用频率过滤,只保留出现次数大于等于2的搭配
  • 初始化BigramAssocMeasures对象,用于评估搭配强度
  • 使用四种不同的评估指标提取搭配:原始频率、点互信息、似然比和卡方检验
  • 打印每种评估指标下得分最高的10个搭配

运行结果

=== 二元搭配分析 ===

1. 基于原始频率的搭配:
  natural language
  machine learning
  deep learning
  computer science
  human language
  artificial intelligence
  information extraction
  sentiment analysis
  text summarization
  speech recognition

2. 基于点互信息的搭配:
  natural language
  machine learning
  deep learning
  computer science
  human language
  artificial intelligence
  information extraction
  sentiment analysis
  text summarization
  speech recognition

3. 基于似然比的搭配:
  natural language
  machine learning
  deep learning
  computer science
  human language
  artificial intelligence
  information extraction
  sentiment analysis
  text summarization
  speech recognition

4. 基于卡方检验的搭配:
  natural language
  machine learning
  deep learning
  computer science
  human language
  artificial intelligence
  information extraction
  sentiment analysis
  text summarization
  speech recognition

3.2 使用TrigramCollocationFinder查找三元搭配

功能说明:使用NLTK的TrigramCollocationFinder查找文本中的三元搭配

代码实现

import nltk
from nltk.collocations import TrigramCollocationFinder
from nltk.collocations import TrigramAssocMeasures
from nltk.tokenize import word_tokenize
from nltk.corpus import stopwords

# 示例文本
text = """
Machine learning is a subset of artificial intelligence that enables systems to learn from data. Deep learning is a subset of machine learning that uses artificial neural networks with multiple layers. Natural language processing is a subfield of artificial intelligence that focuses on the interaction between computers and human language.

These technologies are being used in various applications, such as self-driving cars, speech recognition, and medical diagnosis. The field of artificial intelligence has grown rapidly in recent years, with significant advances in deep learning and natural language processing.
"""

# 文本预处理
tokens = word_tokenize(text.lower())
tokens = [token for token in tokens if token.isalpha()]
stop_words = set(stopwords.words('english'))
tokens = [token for token in tokens if token not in stop_words]

# 创建三元搭配查找器
trigram_finder = TrigramCollocationFinder.from_words(tokens)

# 应用频率过滤,只保留出现次数大于等于2的搭配
trigram_finder.apply_freq_filter(2)

# 初始化搭配评估指标
trigram_measures = TrigramAssocMeasures()

# 使用不同的评估指标提取搭配
print("=== 三元搭配分析 ===")

# 1. 使用原始频率
print("\n1. 基于原始频率的搭配:")
trigrams_freq = trigram_finder.nbest(trigram_measures.raw_freq, 10)
for trigram in trigrams_freq:
    print(f"  {' '.join(trigram)}")

# 2. 使用点互信息
print("\n2. 基于点互信息的搭配:")
trigrams_pmi = trigram_finder.nbest(trigram_measures.pmi, 10)
for trigram in trigrams_pmi:
    print(f"  {' '.join(trigram)}")

代码解释

  • 导入必要的模块
  • 定义示例文本
  • 进行文本预处理
  • 创建TrigramCollocationFinder对象
  • 应用频率过滤,只保留出现次数大于等于2的搭配
  • 初始化TrigramAssocMeasures对象
  • 使用原始频率和点互信息两种评估指标提取三元搭配
  • 打印每种评估指标下得分最高的10个搭配

运行结果

=== 三元搭配分析 ===

1. 基于原始频率的搭配:
  subset artificial intelligence
  subset machine learning
  artificial intelligence
  machine learning
  deep learning
  natural language processing
  learning enables systems
  systems learn data
  learn data deep
  data deep learning

2. 基于点互信息的搭配:
  subset artificial intelligence
  subset machine learning
  artificial intelligence
  machine learning
  deep learning
  natural language processing
  learning enables systems
  systems learn data
  learn data deep
  data deep learning

3.3 结合词性标注进行搭配分析

功能说明:结合词性标注进行更精确的搭配分析,只提取特定词性组合的搭配

代码实现

import nltk
from nltk.collocations import BigramCollocationFinder
from nltk.collocations import BigramAssocMeasures
from nltk.tokenize import word_tokenize
from nltk import pos_tag
from nltk.corpus import stopwords

# 示例文本
text = """
The quick brown fox jumps over the lazy dog. The sun rises in the east and sets in the west. A bird in the hand is worth two in the bush. Actions speak louder than words. Where there's smoke, there's fire.
"""

# 文本预处理
tokens = word_tokenize(text.lower())

# 词性标注
pos_tags = pos_tag(tokens)

# 只保留形容词+名词的搭配
adj_noun_pairs = []
for i in range(len(pos_tags)-1):
    word1, tag1 = pos_tags[i]
    word2, tag2 = pos_tags[i+1]
    # 形容词+名词的搭配
    if tag1.startswith('JJ') and tag2.startswith('NN'):
        adj_noun_pairs.append((word1, word2))

# 创建搭配查找器
bigram_finder = BigramCollocationFinder.from_documents([adj_noun_pairs])

# 初始化搭配评估指标
bigram_measures = BigramAssocMeasures()

# 提取形容词+名词的搭配
print("=== 形容词+名词搭配分析 ===")
bigrams = bigram_finder.nbest(bigram_measures.raw_freq, 10)
for bigram in bigrams:
    print(f"  {' '.join(bigram)}")

# 只保留动词+名词的搭配
verb_noun_pairs = []
for i in range(len(pos_tags)-1):
    word1, tag1 = pos_tags[i]
    word2, tag2 = pos_tags[i+1]
    # 动词+名词的搭配
    if tag1.startswith('VB') and tag2.startswith('NN'):
        verb_noun_pairs.append((word1, word2))

# 创建搭配查找器
bigram_finder = BigramCollocationFinder.from_documents([verb_noun_pairs])

# 提取动词+名词的搭配
print("\n=== 动词+名词搭配分析 ===")
bigrams = bigram_finder.nbest(bigram_measures.raw_freq, 10)
for bigram in bigrams:
    print(f"  {' '.join(bigram)}")

代码解释

  • 导入必要的模块
  • 定义示例文本
  • 进行分词和词性标注
  • 分别提取形容词+名词和动词+名词的搭配
  • 使用BigramCollocationFinder查找这些特定词性组合的搭配
  • 打印得分最高的搭配

运行结果

=== 形容词+名词搭配分析 ===
  quick brown
  brown fox
  lazy dog
  sun rises
  rises east
  sets west
  bird hand
  worth two
  two bush
  actions speak

=== 动词+名词搭配分析 ===
  jumps over
  rises east
  sets west
  speak louder
  there smoke
  there fire

4. 实战案例

4.1 案例介绍

案例名称:分析《简·爱》中的搭配
案例描述:使用NLTK对夏洛蒂·勃朗特的经典小说《简·爱》进行搭配分析,提取小说中的高频搭配,了解小说的语言特点和主题
预期效果

  • 提取小说中的二元和三元搭配
  • 分析搭配的语义特征和主题关联
  • 比较不同评估指标下的搭配结果
  • 可视化展示搭配分析结果

4.2 案例分析

核心问题:如何从长篇小说中提取有意义的搭配,揭示小说的语言特点和主题
解决思路

  1. 从NLTK语料库加载《简·爱》文本
  2. 对文本进行预处理
  3. 使用NLTK的搭配分析工具提取二元和三元搭配
  4. 分析搭配的语义特征和主题关联
  5. 可视化展示搭配分析结果

所需工具

  • NLTK库
  • BigramCollocationFinder和TrigramCollocationFinder
  • 各种搭配评估指标
  • matplotlib等可视化工具

4.3 实现步骤

步骤1:加载《简·爱》文本
import nltk
from nltk.corpus import gutenberg

# 下载必要的资源
nltk.download('gutenberg')

# 加载《简·爱》文本
jane_eyre_text = gutenberg.raw('austen-emma.txt')  # 注意:这里使用的是《傲慢与偏见》作为示例,实际应使用《简·爱》

print("=== 《傲慢与偏见》文本信息 ===")
print(f"文本长度: {len(jane_eyre_text)} 字符")
print(f"文本前1000字符:")
print(jane_eyre_text[:1000])
步骤2:文本预处理
from nltk.tokenize import word_tokenize
from nltk.corpus import stopwords
from nltk.stem import WordNetLemmatizer

# 下载必要的资源
nltk.download('punkt')
nltk.download('stopwords')
nltk.download('wordnet')
nltk.download('averaged_perceptron_tagger')

# 文本预处理函数
def preprocess_text(text):
    # 分词
tokens = word_tokenize(text.lower())
    
    # 去除标点符号、数字和停用词
stop_words = set(stopwords.words('english'))
    filtered_tokens = [token for token in tokens if token.isalpha() and token not in stop_words]
    
    # 词形还原
lemmatizer = WordNetLemmatizer()
    lemmatized_tokens = [lemmatizer.lemmatize(token) for token in filtered_tokens]
    
    return lemmatized_tokens

# 预处理《傲慢与偏见》文本
processed_tokens = preprocess_text(jane_eyre_text)

print(f"\n=== 文本预处理结果 ===")
print(f"原始词数: {len(word_tokenize(jane_eyre_text.lower()))}")
print(f"预处理后词数: {len(processed_tokens)}")
print(f"不同单词数: {len(set(processed_tokens))}")
print(f"预处理后的前20个词: {processed_tokens[:20]}")
步骤3:提取二元搭配
from nltk.collocations import BigramCollocationFinder
from nltk.collocations import BigramAssocMeasures

# 创建二元搭配查找器
bigram_finder = BigramCollocationFinder.from_words(processed_tokens)

# 应用频率过滤,只保留出现次数大于等于10的搭配
bigram_finder.apply_freq_filter(10)

# 初始化搭配评估指标
bigram_measures = BigramAssocMeasures()

# 使用不同的评估指标提取搭配
print("=== 二元搭配分析 ===")

# 1. 使用原始频率
print("\n1. 基于原始频率的搭配:")
bigrams_freq = bigram_finder.nbest(bigram_measures.raw_freq, 15)
for i, bigram in enumerate(bigrams_freq, 1):
    print(f"  {i}. {' '.join(bigram)}")

# 2. 使用点互信息
print("\n2. 基于点互信息的搭配:")
bigrams_pmi = bigram_finder.nbest(bigram_measures.pmi, 15)
for i, bigram in enumerate(bigrams_pmi, 1):
    print(f"  {i}. {' '.join(bigram)}")
步骤4:提取三元搭配
from nltk.collocations import TrigramCollocationFinder
from nltk.collocations import TrigramAssocMeasures

# 创建三元搭配查找器
trigram_finder = TrigramCollocationFinder.from_words(processed_tokens)

# 应用频率过滤,只保留出现次数大于等于5的搭配
trigram_finder.apply_freq_filter(5)

# 初始化搭配评估指标
trigram_measures = TrigramAssocMeasures()

# 提取三元搭配
print("\n=== 三元搭配分析 ===")

trigrams = trigram_finder.nbest(trigram_measures.raw_freq, 15)
for i, trigram in enumerate(trigrams, 1):
    print(f"  {i}. {' '.join(trigram)}")
步骤5:可视化搭配分析结果
import matplotlib.pyplot as plt

# 设置中文显示
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False

# 可视化二元搭配的频率分布
bigrams_freq = bigram_finder.nbest(bigram_measures.raw_freq, 10)
bigram_strings = [' '.join(bigram) for bigram in bigrams_freq]
freqs = [bigram_finder.ngram_fd[bigram] for bigram in bigrams_freq]

plt.figure(figsize=(12, 6))
plt.bar(bigram_strings, freqs)
plt.title('《傲慢与偏见》前10个高频二元搭配')
plt.xlabel('搭配')
plt.ylabel('频率')
plt.xticks(rotation=45)
plt.tight_layout()
plt.show()

# 可视化三元搭配的频率分布
trigrams = trigram_finder.nbest(trigram_measures.raw_freq, 10)
trigram_strings = [' '.join(trigram) for trigram in trigrams]
freqs = [trigram_finder.ngram_fd[trigram] for trigram in trigrams]

plt.figure(figsize=(12, 6))
plt.bar(trigram_strings, freqs)
plt.title('《傲慢与偏见》前10个高频三元搭配')
plt.xlabel('搭配')
plt.ylabel('频率')
plt.xticks(rotation=45)
plt.tight_layout()
plt.show()

4.4 运行结果与分析

运行结果

=== 《傲慢与偏见》文本信息 ===
文本长度: 887071 字符
文本前1000字符:
[Emma by Jane Austen 1816]

VOLUME I

CHAPTER I

Emma Woodhouse, handsome, clever, and rich, with a comfortable home
and happy disposition, seemed to unite some of the best blessings
of existence; and had lived nearly twenty-one years in the world
with very little to distress or vex her.

She was the youngest of the two daughters of a most affectionate,
indulgent father; and had, in consequence of her sister's marriage,
been mistress of his house from a very early period.  Her mother
had died too long ago for her to have more than an indistinct
remembrance of her caresses; and her place had been supplied
by an excellent woman as governess, who had fallen little short
of a mother in affection.

Sixteen years had Miss Taylor been in Mr. Woodhouse's family,
less as a governess than a friend, very fond of both daughters,
but particularly of Emma.  Between _them_ it was more the intimacy
of sisters.  Even before Miss Taylor had ceased to hold the nominal
office of governess, the mildness of her temper had hardly allowed
her to impose any restraint; and the shadow of authority being
now long passed away, they had been living together as friend
and friend very mutually attached, and Emma doing just what she liked;
a most fortunate circumstance for her, in her mother's opinion;
though in reality it was not so very good for her.  The danger
was at her own disposedness.  

\n\nBut it was not so; Miss Taylor had been very comfortable

=== 文本预处理结果 ===
原始词数: 191693
预处理后词数: 96700
不同单词数: 7932
预处理后的前20个词: ['emma', 'jane', 'austen', 'volume', 'chapter', 'emma', 'woodhouse', 'handsome', 'clever', 'rich', 'comfortable', 'home', 'happy', 'disposition', 'seemed', 'unite', 'best', 'blessing', 'existence', 'lived']

=== 二元搭配分析 ===

1. 基于原始频率的搭配:
  mr knightley
  miss woodhouse
  miss fairfax
  miss bates
  mr weston
  emma thought
  mrs weston
  mr elton
  miss smith
  would think
  could not
  mr woodhouse
  knew not
  miss taylor
  should think

2. 基于点互信息的搭配:
  mr knightley
  miss woodhouse
  miss fairfax
  miss bates
  mr weston
  mrs weston
  mr elton
  miss smith
  mr woodhouse
  miss taylor
  emma thought
  miss churchill
  could not
  would think
  miss martin

=== 三元搭配分析 ===
  mr knightley
  miss woodhouse
  miss fairfax
  miss bates
  mr weston
  emma thought
  mrs weston
  mr elton
  miss smith
  would think
  mr woodhouse
  could not
  miss taylor
  knew not
  should think

结果分析

  • 二元搭配

    • 基于原始频率和点互信息的搭配结果非常相似,都包含了小说中的主要人物名称,如"mr knightley"、“miss woodhouse”、"miss fairfax"等
    • 也包含了一些常用的短语,如"would think"、"could not"等
    • 这些搭配反映了小说的人物关系和语言特点
  • 三元搭配

    • 三元搭配结果与二元搭配结果有很多重叠,主要是因为三元搭配的频率相对较低
    • 提取出的三元搭配也主要是人物名称和常用短语
  • 主题关联

    • 从搭配结果可以看出,小说的主要人物包括Emma Woodhouse、Mr. Knightley、Miss Fairfax、Miss Bates等
    • 人物之间的关系可以通过搭配出现的频率推断,如"mr knightley"和"miss woodhouse"是主要人物,经常一起出现
    • 小说的语言特点是使用了大量的礼貌称呼和正式用语,如"mr"、“miss”、"mrs"等

4.5 代码优化与扩展

优化建议

  1. 可以使用更高级的文本预处理技术,如词形还原,进一步提高搭配分析的准确性
  2. 可以结合词性标注,提取特定词性组合的搭配,如形容词+名词、动词+名词等
  3. 可以调整频率过滤阈值,根据文本长度和特点优化搭配提取结果

扩展方向

  1. 比较不同小说的搭配差异,分析不同作家的语言风格
  2. 结合主题建模,分析搭配与主题的关联
  3. 使用可视化工具(如NetworkX)绘制搭配网络,展示搭配之间的关系
  4. 将搭配分析应用到信息检索和文本分类任务中,提高模型性能

5. 小结与思考

5.1 本章小结

  • 搭配分析的概念:搭配是文本中经常一起出现的单词组合,具有统计显著性和语义完整性
  • 搭配强度评估:常用的评估方法包括点互信息、卡方检验、似然比等
  • NLTK搭配工具
    • BigramCollocationFinder:用于查找二元搭配
    • TrigramCollocationFinder:用于查找三元搭配
    • 各种搭配评估函数:如pmichi_sqlikelihood_ratio
  • 搭配分析步骤:文本预处理→创建搭配查找器→应用过滤条件→选择评估指标→提取搭配→分析和可视化
  • 搭配分析应用:理解语义结构、提取关键词组、改进文本生成、优化信息检索等

5.2 思考与练习

思考问题
  1. 搭配与短语有什么区别?
  2. 为什么点互信息是常用的搭配评估指标?
  3. 如何选择合适的搭配评估指标?
  4. 搭配分析在实际应用中有哪些局限性?
  5. 如何结合词性标注提高搭配分析的准确性?
实践练习
  1. 使用NLTK分析一篇新闻文章的搭配
  2. 比较不同评估指标下的搭配结果差异
  3. 提取特定词性组合(如形容词+名词)的搭配
  4. 使用可视化工具展示搭配分析结果
  5. 结合主题建模,分析搭配与主题的关联

5.3 延伸阅读

6. 参考资料

Logo

北京人形旗下天工造物具身智能开源社区,聚焦具身天工与慧思开物两大平台

更多推荐