自然言语处理学习,HanLp2在Docker中安装运转的记录
作者头像
  • 联e会
  • 2021-03-05 12:04:50 9

安装与运行HanLP2.1于Docker中的步骤

下载Python镜像

首先,拉取一个Python 3.7.10版本的镜像:

bash docker pull python:3.7.10-buster

运行容器

启动一个新的容器并进入其中:

bash docker run -itd --name virhuiai-nlp-try2 python:3.7.10-buster docker exec -it virhuiai-nlp-try2 /bin/sh

提高apt更新速度

为了加快更新速度,我们可以通过更换镜像源来实现。以下是具体步骤:

  1. 查看当前的sources.list文件内容:

    bash cat /etc/apt/sources.list

  2. 备份原有的sources.list文件:

    bash mv /etc/apt/sources.list /etc/apt/sources.list.back.20210303

  3. 创建新的sources.list文件并添加清华镜像源:

    bash touch /etc/apt/sources.list echo 'deb https://mirrors.tuna.tsinghua.edu.cn/debian/ buster main contrib non-free' >> /etc/apt/sources.list echo '# deb-src https://mirrors.tuna.tsinghua.edu.cn/debian/ buster main contrib non-free' >> /etc/apt/sources.list echo 'deb https://mirrors.tuna.tsinghua.edu.cn/debian/ buster-updates main contrib non-free' >> /etc/apt/sources.list echo '# deb-src https://mirrors.tuna.tsinghua.edu.cn/debian/ buster-updates main contrib non-free' >> /etc/apt/sources.list echo 'deb https://mirrors.tuna.tsinghua.edu.cn/debian/ buster-backports main contrib non-free' >> /etc/apt/sources.list echo '# deb-src https://mirrors.tuna.tsinghua.edu.cn/debian/ buster-backports main contrib non-free' >> /etc/apt/sources.list echo 'deb https://mirrors.tuna.tsinghua.edu.cn/debian-security buster/updates main contrib non-free' >> /etc/apt/sources.list echo '# deb-src https://mirrors.tuna.tsinghua.edu.cn/debian-security buster/updates main contrib non-free' >> /etc/apt/sources.list

  4. 更新并安装必要的软件:

    bash set -eux apt-get update apt-get install -y --no-install-recommends vim mlocate unzip tree wget rm -rf /var/lib/apt/lists/* apt-get clean

  5. 更新文件索引:

    bash updatedb

使用国内镜像

为了提高Python包的安装速度,我们可以使用国内镜像源:

临时方法

使用指定镜像源安装包:

bash pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple requests

一次处理

设置全局配置:

bash pip3 config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple pip3 config set global.trusted-host mirrors.aliyun.com

查看配置变化:

bash cat /root/.config/pip/pip.conf

安装HanLP

使用阿里云镜像源安装HanLP:

bash pip3 config set global.index-url https://mirrors.aliyun.com/pypi/simple pip3 config set global.trusted-host mirrors.aliyun.com pip3 --no-cache-dir --default-timeout=6000 install hanlp

快速上手

导入HanLP库并加载预训练模型:

python import hanlp HanLP = hanlp.load(hanlp.pretrained.mtl.CLOSE_TOK_POS_NER_SRL_DEP_SDP_CON_ELECTRA_SMALL_ZH)

处理下载问题

如果遇到下载速度慢的问题,可以先在宿主机上下载模型文件,然后将其复制到容器中:

```bash

在宿主机

docker cp /Volumes/RamDisk/closetokposnersrldepsdpconelectrasmall20210111_124159.zip virhuiai-nlp-try2:/root/.hanlp/mtl/ ```

导出镜像

最后,将当前容器导出为一个tar文件:

bash docker export -o /Users/virhuiaivirhuiai/Documents/阅读中/自然语言处理/hanlp2_python37.tar virhuiai-nlp-try2

完整版HanLP

如果需要完整版的HanLP,可以使用以下命令:

bash pip install hanlp[full]

希望以上步骤对你有所帮助。如果有任何问题,请随时联系我。

    本文来源:图灵汇
责任编辑: : 联e会
声明:本文系图灵汇原创稿件,版权属图灵汇所有,未经授权不得转载,已经协议授权的媒体下载使用时须注明"稿件来源:图灵汇",违者将依法追究责任。
    分享
中安装运言语自然处理记录HanLp2Docker学习
    下一篇