1. NLP 基本概念

NLP: Natural Language Processing

常用的包安装:

numpy,NLTK( pip install nltk), scipy, gensim,tensorflow( pip install tensorflow or pip install tf-nightly-gpu/cpu)
Jieba: https://github.com/fxsjy/jieba.
stanford NLP: https://stanfordnlp.github.io/CoreNLP/
Hanlp: https://github.com/hankcs/HanLP

jieba case

#encoding=utf-8
import jieba
import re

dict_path = r'./data/dict.txt'
jieba.load_userdict(dict_path)

dict_fp = open(dict_path,'r',encoding='utf-8')
d = {}
[d.update({line: len(line.split(' ')[0])}) for line in dict_fp]
print(d.items()) #dict_items([('台中\n', 3), ('台中正确', 4)])
f = sorted(d.items(), key=lambda x:x[1], reverse=True) #key是一个函数,key=len按照长度排序
dict_fp.close()

new_dict = open('./data/dict1.txt','w',encoding='utf-8')
[new_dict.write(item[0]+'\n') for item in f]
new_dict.close()

[ jieba.suggest_freq(line.strip(),tune=True) for line in open('./data/dict1.txt','r',encoding='utf-8')]

if __name__ == '__main__':
    string = '台中正确应该不会被切开'
    words = jieba.cut(string, HMM=False)
    result = ' '.join(words)
    print(result)



stanford NLP case
from stanfordcorenlp import StanfordCoreNLP

nlp = StanfordCoreNLP(r'D:\NLP_sourceCode\stanfordcorenlp',lang='zh')

# step.1 启动 server
# Run a server using Chinese properties
# java -Xmx4g -cp "*" edu.stanford.nlp.pipeline.StanfordCoreNLPServer -serverProperties StanfordCoreNLP-chinese.properties -port 9000 -timeout 15000
# nlp = StanfordCoreNLP('http://localhost', port=9000)

sentence = '清华大学位于北京。'

print (nlp.word_tokenize(sentence))
print (nlp.pos_tag(sentence))
print (nlp.ner(sentence))
print (nlp.parse(sentence))
print (nlp.dependency_parse(sentence))

nlp.close()
如果所有设置都没有问题还是报错:
请注意:D:\Anaconda3\Lib\site-packages\stanfordcorenlp\corenlp.py
class StanfordCoreNLP:  #memory 默认是4g,但是我只有8g,运行剩余不够4g,所以需要改小
    def __init__(self, path_or_host, port=None, memory='4g', lang='en', timeout=1500, quiet=True,
                 logging_level=logging.WARNING):
        self.path_or_host = path_or_host
        self.port = port
        self.memory = memory
        self.lang = lang
        self.timeout = timeout
        self.quiet = quiet
        self.logging_level = logging_level

        logging.basicConfig(level=self.logging_level)
        ...

#memory 默认是4g,但是我只有8g,运行剩余不够4g,所以需要改小,但是速度会很慢,所以只有加内存条。

最终发现是jdk的问题。并不是上面的原因


#-*- coding:utf-8 -*-
from jpype import *
import os

root_path = r'D:\Anaconda3\Lib\site-packages\pyhanlp\static'
djclass_path = "-Djava.class.path="+root_path+os.sep+'hanlp-1.7.4.jar;'+root_path
jvmPath = getDefaultJVMPath()
startJVM(jvmPath, djclass_path,
         "-Xms1g",
         "-Xmx1g") # 启动JVM,Linux需替换分号;为冒号:

print("=" * 30 + "HanLP分词" + "=" * 30)
HanLP = JClass('com.hankcs.hanlp.HanLP')
# 中文分词
print(HanLP.segment('你好,欢迎在Python中调用HanLP的API'))
print("-" * 70)

print("=" * 30 + "标准分词" + "=" * 30)
StandardTokenizer = JClass('com.hankcs.hanlp.tokenizer.StandardTokenizer')
print(StandardTokenizer.segment('你好,欢迎在Python中调用HanLP的API'))
print("-" * 70)

# NLP分词NLPTokenizer会执行全部命名实体识别和词性标注
print("=" * 30 + "NLP分词" + "=" * 30)
NLPTokenizer = JClass('com.hankcs.hanlp.tokenizer.NLPTokenizer')
print(NLPTokenizer.segment('中国科学院计算技术研究所的宗成庆教授正在教授自然语言处理课程'))
print("-" * 70)

print("=" * 30 + "索引分词" + "=" * 30)
IndexTokenizer = JClass('com.hankcs.hanlp.tokenizer.IndexTokenizer')
termList = IndexTokenizer.segment("主副食品");
for term in termList:
    print(str(term) + " [" + str(term.offset) + ":" + str(term.offset + len(term.word)) + "]")
print("-" * 70)

print("=" * 30 + " CRF分词" + "=" * 30)
print("-" * 70)

print("=" * 30 + " 极速词典分词" + "=" * 30)
SpeedTokenizer = JClass('com.hankcs.hanlp.tokenizer.SpeedTokenizer')
print(NLPTokenizer.segment('江西鄱阳湖干枯,中国最大淡水湖变成大草原'))
print("-" * 70)

print("=" * 30 + " 自定义分词" + "=" * 30)
CustomDictionary = JClass('com.hankcs.hanlp.dictionary.CustomDictionary')
CustomDictionary.add('攻城狮')
CustomDictionary.add('单身狗')
HanLP = JClass('com.hankcs.hanlp.HanLP')
print(HanLP.segment('攻城狮逆袭单身狗,迎娶白富美,走上人生巅峰'))
print("-" * 70)

print("=" * 20 + "命名实体识别与词性标注" + "=" * 30)
NLPTokenizer = JClass('com.hankcs.hanlp.tokenizer.NLPTokenizer')
print(NLPTokenizer.segment('中国科学院计算技术研究所的宗成庆教授正在教授自然语言处理课程'))
print("-" * 70)

document = "水利部水资源司司长陈明忠9月29日在国务院新闻办举行的新闻发布会上透露," \
    "根据刚刚完成了水资源管理制度的考核,有部分省接近了红线的指标," \
           "有部分省超过红线的指标。对一些超过红线的地方,陈明忠表示,对一些取用水项目进行区域的限批," \
           "严格地进行水资源论证和取水许可的批准。"
print("=" * 30 + "关键词提取" + "=" * 30)
print(HanLP.extractKeyword(document, 8))
print("-" * 70)

print("=" * 30 + "自动摘要" + "=" * 30)
print(HanLP.extractSummary(document, 3))
print("-" * 70)


text = r"算法工程师\n 算法(Algorithm)是一系列解决问题的清晰指令,也就是说,能够对一定规范的输入,在有限时间内获得所要求的输出。如果一个算法有缺陷,或不适合于某个问题,执行这个算法将不会解决这个问题。不同的算法可能用不同的时间、空间或效率来完成同样的任务。一个算法的优劣可以用空间复杂度与时间复杂度来衡量。算法工程师就是利用算法处理事物的人。\n \n 1职位简介\n 算法工程师是一个非常高端的职位;\n 专业要求:计算机、电子、通信、数学等相关专业;\n 学历要求:本科及其以上的学历,大多数是硕士学历及其以上;\n 语言要求:英语要求是熟练,基本上能阅读国外专业书刊;\n 必须掌握计算机相关知识,熟练使用仿真工具MATLAB等,必须会一门编程语言。\n\n2研究方向\n 视频算法工程师、图像处理算法工程师、音频算法工程师 通信基带算法工程师\n \n 3目前国内外状况\n 目前国内从事算法研究的工程师不少,但是高级算法工程师却很少,是一个非常紧缺的专业工程师。算法工程师根据研究领域来分主要有音频/视频算法处理、图像技术方面的二维信息算法处理和通信物理层、雷达信号处理、生物医学信号处理等领域的一维信息算法处理。\n 在计算机音视频和图形图像技术等二维信息算法处理方面目前比较先进的视频处理算法:机器视觉成为此类算法研究的核心;另外还有2D转3D算法(2D-to-3D conversion),去隔行算法(de-interlacing),运动估计运动补偿算法(Motion estimation/Motion Compensation),去噪算法(Noise Reduction),缩放算法(scaling),锐化处理算法(Sharpness),超分辨率算法(Super Resolution),手势识别(gesture recognition),人脸识别(face recognition)。\n 在通信物理层等一维信息领域目前常用的算法:无线领域的RRM、RTT,传送领域的调制解调、信道均衡、信号检测、网络优化、信号分解等。\n 另外数据挖掘、互联网搜索算法也成为当今的热门方向。\n"
print("=" * 30 + "短语提取" + "=" * 30)

print(HanLP.extractPhrase(text, 10))
print("-" * 70)

shutdownJVM()

https://github.com/zmjames2000

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值