Python环境中HanLP安装与使用

本文根据github官方文档整理,介绍了在Terminal使用pip安装Hanlp的方法,其依赖深度学习技术,要求Python 3.6至3.10。还给出了第一个Hanlp demo示例,并详细解释了多个Demo方法,如计算句子数、获取指定前缀元素、文本格式转换等。

根据github最新官方文档整理

1 在Terminal使用pip安装

依赖PyTorch、TensorFlow等深度学习技术,适合专业NLP工程师、研究者以及本地海量数据场景。要求Python 3.6至3.10,支持Windows,推荐*nix。可以在CPU上运行,推荐GPU/TPU。安装PyTorch版:

安装时请关闭节点代理

  • STEP1
pip install hanlp

回显内容:

(MyTest) C:\Users\Lenovo\PycharmProjects\MyTest>pip install hanlp
Collecting hanlp
  Downloading hanlp-2.1.0b52-py3-none-any.whl (651 kB)
     ---------------------------------------- 651.5/651.5 kB 1.2 MB/s eta 0:00:00
Collecting pynvml
  Downloading pynvml-11.5.0-py3-none-any.whl (53 kB)
     ---------------------------------------- 53.1/53.1 kB ? eta 0:00:00
Collecting transformers>=4.1.1
  Downloading transformers-4.30.2-py3-none-any.whl (7.2 MB)
     ---------------------------------------- 7.2/7.2 MB 5.5 MB/s eta 0:00:00
Collecting hanlp-trie>=0.0.4
  Downloading hanlp_trie-0.0.5.tar.gz (6.7 kB)
  Preparing metadata (setup.py) ... done
Collecting toposort==1.5
  Downloading toposort-1.5-py2.py3-none-any.whl (7.6 kB)
Collecting hanlp-common>=0.0.19
  Downloading hanlp_common-0.0.19.tar.gz (28 kB)
  Preparing metadata (setup.py) ... done
Collecting termcolor
  Downloading termcolor-2.3.0-py3-none-any.whl (6.9 kB)
Requirement already satisfied: hanlp-downloader in c:\users\lenovo\anaconda3\envs\mytest\lib\site-packages (from hanlp) (0.0.25)
Collecting torch>=1.6.0
  Downloading torch-1.13.1-cp37-cp37m-win_amd64.whl (162.6 MB)
     ---------------------------------------- 162.6/162.6 MB 6.3 MB/s eta 0:00:00
Collecting sentencepiece>=0.1.91
  Downloading sentencepiece-0.1.99-cp37-cp37m-win_amd64.whl (977 kB)
     ---------------------------------------- 977.7/977.7 kB 10.3 MB/s eta 0:00:00
Collecting phrasetree
  Downloading phrasetree-0.0.8.tar.gz (42 kB)
     ---------------------------------------- 42.2/42.2 kB 2.0 MB/s eta 0:00:00
  Preparing metadata (setup.py) ... done
Collecting typing-extensions
  Downloading typing_extensions-4.7.1-py3-none-any.whl (33 kB)
Collecting regex!=2019.12.17
  Downloading regex-2023.10.3-cp37-cp37m-win_amd64.whl (269 kB)
     ---------------------------------------- 269.9/269.9 kB 17.3 MB/s eta 0:00:00
Collecting filelock
  Downloading filelock-3.12.2-py3-none-any.whl (10 kB)
Collecting importlib-metadata
  Downloading importlib_metadata-6.7.0-py3-none-any.whl (22 kB)
Collecting huggingface-hub<1.0,>=0.14.1
  Downloading huggingface_hub-0.16.4-py3-none-any.whl (268 kB)
     ---------------------------------------- 268.8/268.8 kB 8.3 MB/s eta 0:00:00
Requirement already satisfied: requests in c:\users\lenovo\anaconda3\envs\mytest\lib\site-packages (from transformers>=4.1.1->hanlp) (2.31.0)
Collecting tqdm>=4.27
  Downloading tqdm-4.66.1-py3-none-any.whl (78 kB)
     ---------------------------------------- 78.3/78.3 kB ? eta 0:00:00
Collecting numpy>=1.17
  Downloading numpy-1.21.6-cp37-cp37m-win_amd64.whl (14.0 MB)
     ---------------------------------------- 14.0/14.0 MB 11.7 MB/s eta 0:00:00
Collecting tokenizers!=0.11.3,<0.14,>=0.11.1
  Downloading tokenizers-0.13.3-cp37-cp37m-win_amd64.whl (3.5 MB)
     ---------------------------------------- 3.5/3.5 MB 12.3 MB/s eta 0:00:00
Collecting safetensors>=0.3.1
  Downloading safetensors-0.4.0-cp37-none-win_amd64.whl (277 kB)
     ---------------------------------------- 277.3/277.3 kB 17.8 MB/s eta 0:00:00
Collecting pyyaml>=5.1
  Downloading PyYAML-6.0.1-cp37-cp37m-win_amd64.whl (153 kB)
     ---------------------------------------- 153.2/153.2 kB 9.5 MB/s eta 0:00:00
Collecting packaging>=20.0
  Downloading packaging-23.2-py3-none-any.whl (53 kB)
     ---------------------------------------- 53.0/53.0 kB ? eta 0:00:00
Collecting fsspec
  Downloading fsspec-2023.1.0-py3-none-any.whl (143 kB)
     ---------------------------------------- 143.0/143.0 kB ? eta 0:00:00
Collecting colorama
  Downloading colorama-0.4.6-py2.py3-none-any.whl (25 kB)
Collecting zipp>=0.5
  Downloading zipp-3.15.0-py3-none-any.whl (6.8 kB)
Requirement already satisfied: certifi>=2017.4.17 in c:\users\lenovo\anaconda3\envs\mytest\lib\site-packages (from requests->transformers>=4.1.1->hanlp)
 (2022.12.7)
Requirement already satisfied: charset-normalizer<4,>=2 in c:\users\lenovo\anaconda3\envs\mytest\lib\site-packages (from requests->transformers>=4.1.1->
hanlp) (3.3.2)
Requirement already satisfied: urllib3<3,>=1.21.1 in c:\users\lenovo\anaconda3\envs\mytest\lib\site-packages (from requests->transformers>=4.1.1->hanlp)
 (2.0.7)
Requirement already satisfied: idna<4,>=2.5 in c:\users\lenovo\anaconda3\envs\mytest\lib\site-packages (from requests->transformers>=4.1.1->hanlp) (3.4)

Building wheels for collected packages: hanlp-common, hanlp-trie, phrasetree
  Building wheel for hanlp-common (setup.py) ... done
  Created wheel for hanlp-common: filename=hanlp_common-0.0.19-py3-none-any.whl size=30650 sha256=d3135f8a0e8bde4ff02320c6c84f1d809a9357f9ae2524a5bd99d4
a096d2db2e
  Stored in directory: c:\users\lenovo\appdata\local\pip\cache\wheels\f2\70\bf\57226335746d58210d202e3a64428b8e3b4d57ca373f26d77b
  Building wheel for hanlp-trie (setup.py) ... done
  Created wheel for hanlp-trie: filename=hanlp_trie-0.0.5-py3-none-any.whl size=68
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

程序终结者

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值