extract-human-name
从任意文本中提取、描述和构建人类作者的名字。告诉一个来自组织(“The New York Times”)的人(“Dr. John Doe”),将一个名字解析成它的各个部分,拆分多作者署名,并格式化apa风格的引用——不需要网络、模型和浏览器。
bun add extract-human-name
import { extractHumanName } from "extract-human-name";
extractHumanName("by: Dr. John Q. Public Jr.");
// {
// author_cite: "Public, John Q., Jr.",
// author_short: "Public",
// author_type: 1
// }
extractHumanName("John Doe and Jane Smith");
// { author_cite: "Doe, John & Smith, Jane", author_short: "Doe & Smith", author_type: 2 }
extractHumanName("The New York Times");
// { author_cite: "The New York Times", author_short: "The New York Times", author_type: 4 }
API
extractHumanName(author, options?)
主入口。清理输入条 by: 前缀,取消空白),将署名拆分为单个作者,将每个作者分类为个人或组织,并编译引用表单。
| 选项 | 默认的 | |
|---|---|---|
formatCiteShortenAuthor | false | 把名字缩写成首字母: "John Quincy Doe" → "Doe, J. Q." |
maxAuthorsBeforeEtAl | 2 | 列出这么多作者,然后再折叠到 "First et al." |
author_type 价值观:
| 价值 | 意义 |
|---|---|
0 | 输入为空或不可解析 |
1 | 一个人 |
2 | 正好有两位作者 |
3 | 三位或三位以上作者 |
4 | 一个单一的组织 |
extractHumanNameParts(input)
将一个名称解析为 { title, firstname, middle, lastname, honorific }. 处理 "Last, First" 倒装,多字族前缀(Ludwig van Beethoven →姓 "van Beethoven"),引用别名(John "The Rocket" Doe),并修复了全上/全下输入的情况。
cleanProfessionalQualifications(authorName)
去掉尊称、头衔和专业资格("Dr. John Smith, Professor" → "John Smith").
splitMultipleAuthors(authorString)
将署名分隔为单独的名称。处理 "Last, First & Last, First",以逗号结尾的系列 and, &/and/分号分隔符和减号 et al.。
isOrganization(nameString)
个人对组织启发式:严格的首字母缩略词(WHO),一个组织字典(决定性术语,或成对的模糊术语,如 Morgan Stanley)、资格反条款(Professor …), "Last, First" 倒置,组织短语,单词计数和名称数据库。
名称数据库:slim default vs。 /full
默认条目是slim:确实如此 不 绑定92k条目名称JSON (human-names-92k.json,约1.1 mb)。个人对组织的检测运行在一个紧凑的内置列表上,该列表包含约5000个给定的名字和5000个常见的姓氏以及单词启发式。加载完整的数据库 只在需要的时候两种方式之一:
// Lazy-load it from the jsDelivr CDN (one shared request, cached;
// resolves null on failure, so a CDN outage never breaks extraction)
import { extractHumanName, loadHumanNamesDB } from "extract-human-name";
await loadHumanNamesDB(); // or { url, fetch } to self-host
// Or bundle it: same API, names database registered on import
import { extractHumanName } from "extract-human-name/full";
setHumanNamesDB(db) 注册一份你已经持有的副本,然后 getHumanNamesDB() 读取当前的。两个条目共享一个数据库,因此导入 extract-human-name/full 一旦启用它为苗条的导入。
在加载数据库后,一个单词在压缩列表之外的名字仍然被识别为一个人:
await loadHumanNamesDB();
extractHumanName("Zephyr Quill Xylo").author_type; // 1 (person), not 4 (organization)
演示
的引文选项卡 extract-pdf网站 (/demo#cite)根据新闻、期刊、组织和博客url运行名称识别。
测试
cd packages/extract-human-name && bun run test
涵盖名称解析、署名拆分、组织检测(有和没有数据库) extractHumanName 引用表单和惰性加载器(单个共享请求、失败回退、重试、 /full 登记)。