Hanlp的学习

参考:HanLP 自然语言处理使用总结-优快云博客

参考:Sprint Boot 工程中HanLP配置相对路径,始终有问题的解决方案_springboot hanlp-优快云博客

<!--hanlp 依赖-->
        <dependency>
            <groupId>com.hankcs</groupId>
            <artifactId>hanlp</artifactId>
            <version>portable-1.8.4</version>
        </dependency>

public class ResourceFileIoAdapter implements IIOAdapter {
    @Override
    public InputStream open(String path) throws IOException {
        ClassPathResource resource = new ClassPathResource(path);
        // return Files.newInputStream(resource.getFile().toPath());
        // Linux环境下跑要把open()里改成这样:
        return resource.getInputStream();
    }

    @Override
    public OutputStream create(String path) throws IOException {
        ClassPathResource resource = new ClassPathResource(path);
        OutputStream os = new FileOutputStream(resource.getFile());
        return os;
    }
}

 http://nlp.hankcs.com/download.php?file=data
下载后


其中数据分为词典和模型,其中词典是词法分析必需的,模型是句法分析必需的,用户可以自行增删替换,如果不需要句法分析等功能的话,随时可以删除model文件夹 

实践使用

从文本中提取关键字

// 提取名词关键字
public static String extractOptimizedKeywords(String text, int keywordCount) {

        // 获取短语
        List<String> termList = HanLP.extractPhrase(text, 100);

        // 只保留都是名词的短语
        List<String> termNounsList = filterOnlyNounsFromPhrases(termList);
        System.out.println(termNounsList);
        // 计算TF-IDF值并排序
        Map<String, Double> tfidfMap = calculateTfidf(termNounsList, text);
        List<Map.Entry<String, Double>> sortedKeywords = tfidfMap.entrySet().stream()
                .sorted(Map.Entry.<String, Double>comparingByValue().reversed())
                .collect(Collectors.toList());

        // 返回出现次数最多的前keywordCount个复合关键字
        List<String> keywords = sortedKeywords.stream()
                .limit(keywordCount)
                .map(Map.Entry::getKey)
                .collect(Collectors.toList());

        // 将关键词列表转换为逗号分隔的字符串
        return String.join(", ", keywords);
    }


// 获取名词短语
public static List<String> filterOnlyNounsFromPhrases(List<String> phrases) {
        List<String> nounOnlyPhrases = new ArrayList<>();

        for (String phrase : phrases) {
            // 对每个短语进行分词和词性标注
            List<Term> terms = HanLP.segment(phrase);

            // 检查短语中是否所有词都是名词
            boolean allNouns = terms.stream()
                    .allMatch(term -> term.nature.toString().startsWith("n"));

            // 如果短语中所有词都是名词,则保留该短语
            if (allNouns) {
                nounOnlyPhrases.add(phrase);
            }
        }

        return nounOnlyPhrases;
    }

// 计算TF-IDF值并排序
private static Map<String, Double> calculateTfidf(List<String> terms, String corpus) {
        Map<String, Integer> termFrequency = terms.stream()
                .collect(Collectors.groupingBy(Function.identity(), Collectors.summingInt(v -> 1)));
        Map<String, Integer> documentFrequency = calculateDocumentFrequency(corpus);

        Map<String, Double> tfidf = new HashMap<>();
        for (Map.Entry<String, Integer> entry : termFrequency.entrySet()) {
            String term = entry.getKey();
            int freq = entry.getValue();
            int df = documentFrequency.getOrDefault(term, 1); // 确保df至少为1,避免log(0)
            double tf = (double) freq / terms.size();
            double idf = Math.log((double) 1 + corpus.length() / df); // 调整idf公式,用语料库长度作为文档数量的代理
            double tfidfValue = tf * idf;
            tfidf.put(term, tfidfValue);
        }

        return tfidf;
    }

    private static Map<String, Integer> calculateDocumentFrequency(String corpus) {
        List<Term> docTerms = HanLP.segment(corpus);
        return docTerms.stream()
                .filter(term -> term.nature.toString().startsWith("n")) // 使用startsWith("n")匹配任何名词性质
                .collect(Collectors.groupingBy(term -> term.word, Collectors.summingInt(v -> 1)));
    }

### HanLP 使用指南与功能介绍 HanLP 是一个由哈工大社会计算与信息检索研究中心开发的开源自然语言处理(NLP)工具包,支持多种 NLP 任务,包括分词、词性标注、命名实体识别(NER)、依存句法分析等。以下是关于 HanLP 的安装、使用方法以及功能的详细介绍。 #### 一、HanLP 安装指南 HanLP 提供了两个主要版本:HanLP 1.x 和 HanLP 2.x。两者的安装方式和依赖有所不同。 - **HanLP 1.x** 是基于 Java 的版本,适合 Python ≤ 3.8 的环境。可以通过 `pyhanlp` 包来调用 HanLP 1.x 的功能[^2]。 ```bash pip install pyhanlp ``` - **HanLP 2.x** 是基于 Python 的版本,支持更复杂的深度学习模型,例如 BERT、ELECTRA 和 ALBERT 等。HanLP 2.x 需要安装 PyTorch,并且建议配置 GPU 加速以提高性能[^3]。 ```bash pip install hanlp ``` 如果需要 GPU 支持,请确保系统已正确安装 NVIDIA 显卡驱动、CUDA 和 cuDNN,并在安装 PyTorch 时选择 GPU 版本。 #### 二、HanLP 使用方法 以下是一个简单的代码示例,展示如何加载 HanLP 2.x 并执行分词和词性标注任务: ```python import hanlp # 加载预训练模型 HanLP = hanlp.load(hanlp.pretrained.mtl.CLOSE_TOK_POS_NER_SRL_DEP_SDP_CON_ELECTRA_BASE_ZH) # [^1] # 输入文本 text = "北京大学位于北京市海淀区中关村" # 分词和词性标注 result = HanLP(text, tasks=['tok/fine', 'pos/ctb']) print(result) ``` #### 三、HanLP 功能介绍 HanLP 提供了丰富的 NLP 功能,主要包括以下几个方面: 1. **分词(Tokenization)** HanLP 支持粗分词(coarse-grained)和细分词(fine-grained),能够准确地对中文文本进行切分[^1]。 2. **词性标注(Part-of-Speech Tagging)** HanLP 提供了基于统计模型和深度学习模型的词性标注功能,适用于不同的应用场景。 3. **命名实体识别(Named Entity Recognition, NER)** HanLP 能够识别出文本中的实体,例如人名、地名、组织名等[^1]。 4. **依存句法分析(Dependency Parsing)** HanLP 提供了高效的依存句法分析工具,可以解析句子中词语之间的语法关系[^2]。 5. **语义角色标注(Semantic Role Labeling, SRL)** HanLP 能够识别句子中各个成分的语义角色,例如施事、受事等[^1]。 6. **关键词提取** HanLP 支持从文本中提取关键词,帮助用户快速了解文本的核心内容。 7. **机器翻译** 在某些扩展模块中,HanLP 还支持简单的机器翻译功能[^3]。 #### 四、注意事项 - 如果使用的是 HanLP 2.x,请确保系统满足其硬件和软件依赖条件,特别是对于 GPU 加速的支持。 - 对于 Python ≥ 3.9 的环境,建议直接使用 HanLP 2.x,因为 pyhanlp 可能存在兼容性问题。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值