MIT License

技术解读

以下内容由 GitRead 中文社区 编辑团队基于项目 README 与官方资料整理, 结合中文开发者的常见使用场景补充技术选型建议。具体以项目官方文档为准。

medspacy 深度技术分析

项目背景与痛点

medspacy 是一个用于临床自然语言处理(NLP)的库,基于 spaCy 的基础上构建。它在医学文档处理方面的表现尤为出色,特别是在结构化文本中的实体识别、关系抽取以及特定于医学领域的自然语言处理任务。日常的医疗工作中,解析复杂的临床文档以提取疾病、症状、检查结果和处方信息等,细碎而琐碎。传统的方法如正则表达式和手动定义的规则不仅效率低下且容易出错。相比之下,medspacy 提供了更强大且精确的处理能力,这恰好解决了这一领域的核心痛点。

核心技术揭秘

medspacy 构建在 spaCy 之上,专为医疗文档处理而优化。它引入了大量医疗领域的增强功能,并扩展了 spaCy 的核心功能。通过定制化的组件与管道,它能够高效地处理复杂文本。以下是其大致实现架构图:

graph TD A[用户文本输入] --> B[预处理] B --> C[实体识别] C --> D[关系抽取] D --> E{"输出类型"} E -->|"医学实体"| F[实体列表] E -->|"关系结果"| G[关系列表]

图中展示了输入的文本如何经过预处理、实体识别和关系抽取的流程,最终输出实体列表和关系列表。图中的关系抽取部分对于捕获文本中实体间的关系尤其关键,这在医疗病历处理场景中至关重要。

功能亮点与差异

相比其他 NLP 库,medspacy 明显的亮点在于它专为医学领域设计,能够更好地识别专业术语和医学标准化表达。它集成了 UMLS 和其他医学数据库,提供了详尽的字典和规则库。此外,它还包含了一个健壮的关系抽取管道,能够更准确地捕捉医疗文档中的复杂关系。这些功能使得medspacy 在医疗 NLP 领域独树一帜,成为处理电子病历和临床文档的首选工具。

应用场景与落地建议

在生产环境中使用 medspacy,开发者需注意合理配置资源,特别是考虑其对内存的较高需求。适合应用于需要处理大量医学文档的场景,包括但不限于电子病历分类、医患交互监控、医疗知识图谱构建等。为优化性能,可以考虑在预处理阶段对文本做必要的清洗和标准化处理。

综合评价

优点:

  • 专为医学领域设计,能更精确地处理医学文档。
  • 集成丰富的医学知识库,包括 UMLS。
  • 强大的关系抽取模块,提取文档中复杂的医学实体关系。

缺点:

  • 对内存的占用较高。
  • 部署与优化需要一定的专业知识。

总体而言,medspacy 是当前医疗 NLP 一项重要工具,它在临床文档处理上的表现做出了卓绝的贡献,特别是在医学实体识别与关系抽取方面。对于医疗 NLP 领域的研究者与开发者,它是不可或缺的技术利器。

关于

GitHub 原始描述(英文)

Library for clinical NLP with spaCy.

描述取自 GitHub 官方 API,保持仓库作者书写的原文。

简要分析

热度分
907
价值分
434
活跃状态
活跃

语言占比

Dockerfile
Jupyter Notebook
Python

Release

README

License: MIT Build Status

medspacy

Library for clinical NLP with spaCy.

alt text

MedSpaCy is currently in beta.

Overview

MedSpaCy is a library of tools for performing clinical NLP and text processing tasks with the popular spaCy framework. The medspacy package brings together a number of other packages, each of which implements specific functionality for common clinical text processing specific to the clinical domain, such as sentence segmentation, contextual analysis and attribute assertion, and section detection.

medspacy is modularized so that each component can be used independently. All of medspacy is designed to be used as part of a spacy processing pipeline. Each of the following modules is available as part of medspacy:

  • medspacy.preprocess: Destructive preprocessing for modifying clinical text before processing
  • medspacy.sentence_splitter: Clinical sentence segmentation
  • medspacy.ner: Utilities for extracting concepts from clinical text
  • medspacy.context: Implementation of the ConText algorithm for detecting semantic modifiers and attributes of entities, including negation and uncertainty.
  • medspacy.section_detection: Clinical section detection and segmentation
  • medspacy.postprocess: Flexible framework for modifying and removing extracted entities
  • medspacy.io: Utilities for converting processed texts to structured data and interacting with databases
  • medspacy.visualization: Utilities for visualizing concepts and relationships extracted from text
  • SpacyQuickUMLS: UMLS concept extraction compatible with spacy and medspacy implemented by our fork of QuickUMLS. More detail on this component, how to use it, how to generate UMLS resources beyond the small UMLS sample can be found in this notebook.

Future work will include I/O, relations extraction, and pre-trained clinical models.

Latest release 1.3.1 (11/21/2024)

What’s new in 1.3.1:

    • Optimized database I/O to write concepts into SQLite in batches, which has been tested on mariadb (not integrated into main branch yet).
    • Reconfigured requirements.txt and updated medspacydependencies to support later versions of spaCy up to 3.8.2.
    • Due to deprecated and incompatible configurations, we have stopped supporting Python 3.6 and 3.7.

As of 10/2/2021 (version 0.2.0.0), medspaCy supports spaCy v3

Language support

As of May 2024, medspacy has been restructured to allow distributing rules and resources in languages besides English.

Please note that some languages are effectively “empty” from the perspective of medspacy. We’ve had requests to allow some of these languages, but we have no rules since the medspacy team works primarily in English. Please see the table below for a summary of what is available in each language, as they each vary. As of this writing, the languages have been sorted in order of what rules/maturity they currently support. If you are reading this and you have rules or would like to develop rules in another language, please consider contributing. We can help get your work integrated.

Language ConText rules Section Rules QuickUMLS sample (and unit test)
English (en) Yes Yes Yes
French (fr) Yes Very few Yes
Dutch (nl) Yes No No
Spanish (es) No Very few Yes
Polish (pl) No No No
Portuguese (pt) No No Yes
Italian (it) No No Yes
German (de) No No No

Regarding the rules that are available in these languages, a few citations to mention:

Usage

Installation

You can install medspacy using setup.py:

python setup.py install

Or with pip:

pip install medspacy

To install a previous version which uses spaCy 2:

pip install medspacy==medspacy 0.1.0.2

Requirements

The following packages are required and installed when medspacy is installed:

If you download other models, you can use them by providing the model itself or model name to medspacy.load(model_name):

import spacy; import medspacy
# Option 1: Load default
nlp = medspacy.load()

# Option 2: Load from existing model
nlp = spacy.load("en_core_web_sm", disable={"ner"})
nlp = medspacy.load(nlp)

# Option 3: Load from model name
nlp = medspacy.load("en_core_web_sm", disable={"ner"})

Basic Usage

Here is a simple example showing how to implement and visualize a simple rule-based pipeline using medspacy:

import medspacy
from medspacy.ner import TargetRule
from medspacy.visualization import visualize_ent

# Load medspacy model
nlp = medspacy.load()
print(nlp.pipe_names)

text = """
Past Medical History:
1. Atrial fibrillation
2. Type II Diabetes Mellitus

Assessment and Plan:
There is no evidence of pneumonia. Continue warfarin for Afib. Follow up for management of type 2 DM.
"""

# Add rules for target concept extraction
target_matcher = nlp.get_pipe("medspacy_target_matcher")
target_rules = [
    TargetRule("atrial fibrillation", "PROBLEM"),
    TargetRule("atrial fibrillation", "PROBLEM", pattern=[{"LOWER": "afib"}]),
    TargetRule("pneumonia", "PROBLEM"),
    TargetRule("Type II Diabetes Mellitus", "PROBLEM", 
              pattern=[
                  {"LOWER": "type"},
                  {"LOWER": {"IN": ["2", "ii", "two"]}},
                  {"LOWER": {"IN": ["dm", "diabetes"]}},
                  {"LOWER": "mellitus", "OP": "?"}
              ]),
    TargetRule("warfarin", "MEDICATION")
]
target_matcher.add(target_rules)

doc = nlp(text)
visualize_ent(doc)

Output: alt text

For more detailed examples and explanations of each component, see the notebooks folder.

Citing medspaCy

If you use medspaCy in your work, consider citing our paper! Presented at the AMIA Annual Symposium 2021, preprint available on Arxiv.

H. Eyre, A.B. Chapman, K.S. Peterson, J. Shi, P.R. Alba, M.M. Jones, T.L. Box, S.L. DuVall, O. V Patterson,
Launching into clinical space with medspaCy: a new clinical text processing toolkit in Python,
AMIA Annu. Symp. Proc. 2021 (in Press. (n.d.). 
http://arxiv.org/abs/2106.07799.
@Article{medspacy,
   Author="Eyre, H.  and Chapman, A. B.  and Peterson, K. S.  and Shi, J.  and Alba, P. R.  and Jones, M. M.  and Box, T. L.  and DuVall, S. L.  and Patterson, O. V. ",
   Title="{{L}aunching into clinical space with medspa{C}y: a new clinical text processing toolkit in {P}ython}",
   Journal="AMIA Annu Symp Proc",
   Year="2021",
   Volume="2021",
   Pages="438--447"
}

}

Made with medSpaCy

Here are some links to projects or tutorials which use medSpacy. If you have a project which uses medSpaCy which you’d like to use, let us know!

评论

暂无评论