fasttext 安装_基于FastText进行文本分类

第一步:安装fasttext可以参考:https://github.com/facebookresearch/fastText#building-fasttextliunx版本下操作:$ git clone https://github.com/facebookresearch/fastText.git$ cd fastText$ pip install .安装成功后的导入:新建te...
摘要由CSDN通过智能技术生成

a07ca86d5c6876b56d9a9d87ad83dfea.png
第一步:安装fasttext 可以参考:https://github.com/facebookresearch/fastText#building-fasttext
liunx版本下操作:
$ git clone https://github.com/facebookresearch/fastText.git
$ cd fastText
$ pip install .

安装成功后的导入:
新建test.py文件,写入:
import fastText.FastText as fasttext(可能会瞟红线)
新增:最近发现fasttext的github更新了,引入方式发生了变化,如果上述引入报错,改成 import fasttext.FastText as fasttext

新增:现在安装直接 pip install fasttext,导入直接 import fasttext 就行

保存后退出并运行:
python3 test.py
没报错说明安装成功
第二步:准备数据集
  • 我这里用的是清华的新闻数据集(由于完整数据集较大,这里只取部分数据)
  • 数据链接:点击获取网盘数据 提取码:byoi(data.txt为数据集,stopwords.txt为停用词)
  • 下载好后的数据格式为:

45a3add553966ad06d7c24a5c9431d55.png
  • 对应的标签分别为(由于只是用小部分数据,所以data.txt只包含部分标签):
mapper_tag = {
    
    '财经': 'Finance',
    '彩票': 'Lottery',
    '房产': 'Property',
    '股票': 'Shares',
    '家居': 'Furnishing',
    '教育': 'Education',
    '科技': 'Technology',
    '社会': 'Sociology',
    '时尚': 'Fashion',
    '时政': 'Affairs',
    '体育': 'Sports',
    '星座': 'Constellation',
    '游戏': 'Game',
    '娱乐': 'Entertainment'
}
第三步:数据预处理
  • 由于data.txt已经经过了分词和去停用词的处理,所以这里只需要对数据进行切割为训练集和测试集即可。
  • 分词和去停用词的工具代码(运行时不需要执行此部分代码):
import re
from types import MethodType, FunctionType

import jieba


def clean_txt(raw):
    fil = re.compile(r"[^0-9a-zA-Zu4e00-u9fa5]+")
    return fil.sub(' ', raw)

def seg(sentence, sw, apply=None):
    if isinstance(apply, FunctionType) or isinstance(apply, MethodType):
        sentence = apply(sentence)
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值