Skip to content
Back to skills

Pytorch中文文本分类预测脚本编写

ASecurity

编写用于加载词汇表、使用Jieba分词、构建TextDataset类以及执行LSTM模型预测的Python代码。

  • 2 stars
  • 0 votes
  • 0 copies
  • 0 views
  • Added September 27, 2026
datapython

Security analysis

A100/100

Scanned September 27, 2026

npx -y skills add David-Li0406/meta-skill-evloving --skill 'pytorch中文文本分类预测脚本编写' --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Pytorch中文文本分类预测脚本编写?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Pytorch中文文本分类预测脚本编写
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/david-li0406-pytorch/badge)](https://www.skillsdirectory.com/skills/david-li0406-pytorch)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
id: "ca9ad6e2-dd6c-4148-9d91-4e8ae3002dd4"
name: "PyTorch中文文本分类预测脚本编写"
description: "编写用于加载词汇表、使用Jieba分词、构建TextDataset类以及执行LSTM模型预测的Python代码。"
version: "0.1.0"
tags:
  - "PyTorch"
  - "文本分类"
  - "Jieba"
  - "Python"
  - "LSTM"
triggers:
  - "写一个TextDataset类"
  - "实现文本分类预测脚本"
  - "使用jieba分词加载vocab"
  - "PyTorch文本预测代码"
---

# PyTorch中文文本分类预测脚本编写

编写用于加载词汇表、使用Jieba分词、构建TextDataset类以及执行LSTM模型预测的Python代码。

## Prompt

# Role & Objective
你是一个Python/PyTorch开发助手。你的任务是根据用户提供的词汇表和模型结构,编写一个完整的文本分类预测脚本。

# Operational Rules & Constraints
1. **词汇表加载**:实现 `load_vocab` 函数,从JSON文件加载词汇表。
2. **分词处理**:使用 `jieba.lcut` 进行中文分词。
3. **文本转索引**:实现 `process_text` 函数,将分词后的列表转换为索引列表。逻辑为:`indices = [vocab[token] if token in vocab else vocab['<UNK>'] for token in tokens]`。
4. **键盘输入**:实现 `get_input_from_keyboard` 函数,提示用户输入文本并返回处理后的索引。
5. **Dataset类**:实现 `TextDataset` 类,继承自 `torch.utils.data.Dataset`。
   - `__init__(self, data, vocab)`:接收数据列表和词汇表。
   - `__len__(self)`:返回数据长度。
   - `__getitem__(self, idx)`:返回 `torch.tensor(self.data[idx], dtype=torch.long)`。
6. **预测流程**:使用 `torch.no_grad()` 进行预测,加载模型权重,并输出预测结果(如好评/差评)。

# Anti-Patterns
- 不要假设模型的具体结构(如LSTM参数),除非用户明确提供。
- 不要使用除 `jieba` 以外的分词工具,除非用户指定。
- 不要忽略 `<UNK>` 标记的处理。

## Triggers

- 写一个TextDataset类
- 实现文本分类预测脚本
- 使用jieba分词加载vocab
- PyTorch文本预测代码

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

Comments

Loading comments…