n-gram/词频/词性/词向量区别以及组合使用案例

n-gram(2-gram):捕捉文本中的短语和模式。
词频:反映文本中单词的重要性和分布。
词性:提供额外的上下文信息。
词向量:捕捉单词之间的语义关系。
特征组合示例:

n-gram特征:
我 喜欢
喜欢 吃
吃 苹果
词频特征:
我:2次
喜欢:2次
吃:2次
苹果:2次
词性特征:
我:代词
喜欢:动词
吃:动词
苹果:名词
词向量特征:
我:[向量表示]
喜欢:[向量表示]
吃:[向量表示]
苹果:[向量表示]

特征融合示例:

定义一个函数,用于将文本转换为特征向量

def text_to_features(text, word_to_index, bigrams, trigrams, word_freq, pos_tags):
# 初始化特征向量
features = []

# 将文本转换为单词列表
words = text.split()

# 添加n-gram特征
for bigram in bigrams:
    if bigram in words:
        features.append(1)
    else:
        features.append(0)
for trigram in trigrams:
    if trigram in words:
        features.append(1)
    else:
        features.append(0)

# 添加词频特征
for word in words:
    features.append(word_freq[word])

# 添加词性特征
for pos in pos_tags:
    features.append(pos)

# 添加词向量特征
for word in words:
    if word in word_to_index:
        features.append(word_to_index[word])
    else:
        features.append(0)  # 或者使用一个特殊的向量表示未知的词

# 返回特征向量
return features

示例文本

text = “我今天心情很好,阳光明媚。”

假设我们有一个预先构建的词典和索引

word_to_index = {“我”: 0, “今天”: 1, “心情”: 2, “很好”: 3, “阳光”: 4, “明媚”: 5}
bigrams = [“我今天”, “今天心情”, “心情很好”, “很好阳光”, “阳光明媚”]
trigrams = [“我今天心情”, “今天心情很好”, “心情很好阳光”, “很好阳光明媚”]
word_freq = {“我”: 1, “今天”: 1, “心情”: 1, “很好”: 1, “阳光”: 1, “明媚”: 1}
pos_tags = [“代词”, “时间词”, “名词”, “副词”, “名词”, “形容词”]

转换文本为特征向量

features = text_to_features(text, word_to_index, bigrams, trigrams, word_freq, pos_tags)

打印特征向量

print(features)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Ai玩家hly

年少且带锋芒,擅行侠仗义之事

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值