- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
对于词形还原 spacy 有一个 lists of words :形容词、副词、动词...以及异常(exception)情况列表: adverbs_irreg... 对于常规的,有一组 rules
让我们以“更宽”这个词为例
因为它是一个形容词,所以词形还原的规则应该来自这个列表:
ADJECTIVE_RULES = [
["er", ""],
["est", ""],
["er", "e"],
["est", "e"]
]
最佳答案
让我们从类定义开始:https://github.com/explosion/spaCy/blob/develop/spacy/lemmatizer.py
类(class)
它从初始化 3 个变量开始:
class Lemmatizer(object):
@classmethod
def load(cls, path, index=None, exc=None, rules=None):
return cls(index or {}, exc or {}, rules or {})
def __init__(self, index, exceptions, rules):
self.index = index
self.exc = exceptions
self.rules = rules
self.exc
对于英语,我们看到它指向
https://github.com/explosion/spaCy/tree/develop/spacy/lang/en/lemmatizer/init.py它从目录
https://github.com/explosion/spaCy/tree/master/spacy/en/lemmatizer 加载文件的位置
_morphy
规则来自
nltk
https://github.com/nltk/nltk/blob/develop/nltk/corpus/reader/wordnet.py#L1749
Morphy
软件
https://wordnet.princeton.edu/man/morphy.7WN.html
spacy
包含了一些不是来自普林斯顿莫菲的标点规则:
PUNCT_RULES = [
["“", "\""],
["”", "\""],
["\u2018", "'"],
["\u2019", "'"]
]
*_irreg.py
中。
spacy
中的文件,而且它们看起来也来自普林斯顿 Wordnet。
.exc
的一些镜像,就很明显了。 (排除)文件(例如
https://github.com/extjwnl/extjwnl-data-wn21/blob/master/src/main/resources/net/sf/extjwnl/data/wordnet/wn21/adj.exc ),如果您下载
wordnet
包裹来自
nltk
,我们看到它是同一个列表:
alvas@ubi:~/nltk_data/corpora/wordnet$ ls
adj.exc cntlist.rev data.noun index.adv index.verb noun.exc
adv.exc data.adj data.verb index.noun lexnames README
citation.bib data.adv index.adj index.sense LICENSE verb.exc
alvas@ubi:~/nltk_data/corpora/wordnet$ wc -l adj.exc
1490 adj.exc
spacy
lemmatizer
index
,我们看到它也来自 Wordnet,例如
https://github.com/explosion/spaCy/tree/develop/spacy/lang/en/lemmatizer/_adjectives.py以及
nltk
中重新分发的 wordnet 副本:
alvas@ubi:~/nltk_data/corpora/wordnet$ head -n40 data.adj
1 This software and database is being provided to you, the LICENSEE, by
2 Princeton University under the following license. By obtaining, using
3 and/or copying this software and database, you agree that you have
4 read, understood, and will comply with these terms and conditions.:
5
6 Permission to use, copy, modify and distribute this software and
7 database and its documentation for any purpose and without fee or
8 royalty is hereby granted, provided that you agree to comply with
9 the following copyright notice and statements, including the disclaimer,
10 and that the same appear on ALL copies of the software, database and
11 documentation, including modifications that you make for internal
12 use or for distribution.
13
14 WordNet 3.0 Copyright 2006 by Princeton University. All rights reserved.
15
16 THIS SOFTWARE AND DATABASE IS PROVIDED "AS IS" AND PRINCETON
17 UNIVERSITY MAKES NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR
18 IMPLIED. BY WAY OF EXAMPLE, BUT NOT LIMITATION, PRINCETON
19 UNIVERSITY MAKES NO REPRESENTATIONS OR WARRANTIES OF MERCHANT-
20 ABILITY OR FITNESS FOR ANY PARTICULAR PURPOSE OR THAT THE USE
21 OF THE LICENSED SOFTWARE, DATABASE OR DOCUMENTATION WILL NOT
22 INFRINGE ANY THIRD PARTY PATENTS, COPYRIGHTS, TRADEMARKS OR
23 OTHER RIGHTS.
24
25 The name of Princeton University or Princeton may not be used in
26 advertising or publicity pertaining to distribution of the software
27 and/or database. Title to copyright in this software, database and
28 any associated documentation shall at all times remain with
29 Princeton University and LICENSEE agrees to preserve same.
00001740 00 a 01 able 0 005 = 05200169 n 0000 = 05616246 n 0000 + 05616246 n 0101 + 05200169 n 0101 ! 00002098 a 0101 | (usually followed by `to') having the necessary means or skill or know-how or authority to do something; "able to swim"; "she was able to program her computer"; "we were at last able to buy a car"; "able to get a grant for the project"
00002098 00 a 01 unable 0 002 = 05200169 n 0000 ! 00001740 a 0101 | (usually followed by `to') not having the necessary means or skill or know-how; "unable to get to town without a car"; "unable to obtain funds"
00002312 00 a 02 abaxial 0 dorsal 4 002 ;c 06037666 n 0000 ! 00002527 a 0101 | facing away from the axis of an organ or organism; "the abaxial surface of a leaf is the underside or side facing away from the stem"
00002527 00 a 02 adaxial 0 ventral 4 002 ;c 06037666 n 0000 ! 00002312 a 0101 | nearest to or facing toward the axis of an organ or organism; "the upper side of a leaf is known as the adaxial surface"
00002730 00 a 01 acroscopic 0 002 ;c 06066555 n 0000 ! 00002843 a 0101 | facing or on the side toward the apex
00002843 00 a 01 basiscopic 0 002 ;c 06066555 n 0000 ! 00002730 a 0101 | facing or on the side toward the base
00002956 00 a 02 abducent 0 abducting 0 002 ;c 06080522 n 0000 ! 00003131 a 0101 | especially of muscles; drawing away from the midline of the body or from an adjacent part
00003131 00 a 03 adducent 0 adductive 0 adducting 0 003 ;c 06080522 n 0000 + 01449236 v 0201 ! 00002956 a 0101 | especially of muscles; bringing together or drawing toward the midline of the body or toward an adjacent part
00003356 00 a 01 nascent 0 005 + 07320302 n 0103 ! 00003939 a 0101 & 00003553 a 0000 & 00003700 a 0000 & 00003829 a 0000 | being born or beginning; "the nascent chicks"; "a nascent insurgency"
00003553 00 s 02 emergent 0 emerging 0 003 & 00003356 a 0000 + 02625016 v 0102 + 00050693 n 0101 | coming into existence; "an emergent republic"
00003700 00 s 01 dissilient 0 002 & 00003356 a 0000 + 07434782 n 0101 | bursting open with force, as do some ripe seed vessels
spacy
lemmatizer 的使用主要来自普林斯顿 WordNet 和他们的 Morphy 软件,我们可以继续看实际实现如何
spacy
使用索引和异常(exception)应用规则。
Lemmatizer
类(class):
def lemmatize(string, index, exceptions, rules):
string = string.lower()
forms = []
# TODO: Is this correct? See discussion in Issue #435.
#if string in index:
# forms.append(string)
forms.extend(exceptions.get(string, []))
oov_forms = []
for old, new in rules:
if string.endswith(old):
form = string[:len(string) - len(old)] + new
if not form:
pass
elif form in index or not form.isalpha():
forms.append(form)
else:
oov_forms.append(form)
if not forms:
forms.extend(oov_forms)
if not forms:
forms.append(string)
return set(forms)
lemmatize
Lemmatizer
之外的方法课?
@staticmethod
and @classmethod
关于函数和类为什么已经解耦可能还有其他考虑
spacy
lemmatize() 函数针对
morphy()
nltk 中的函数(最初来自
http://blog.osteele.com/2004/04/pywordnet-20/ 创建于十多年前),
morphy()
,Oliver Steele 的 WordNet morphy 的 Python 移植主要流程是:
spacy
,鉴于
TODO
,它可能仍在开发中在线
https://github.com/explosion/spaCy/blob/develop/spacy/lemmatizer.py#L76
nltk
morphy
的实现做同样的事情,例如
>>> from nltk.stem import WordNetLemmatizer
>>> wnl = WordNetLemmatizer()
>>> wnl.lemmatize('alvations')
'alvations'
morphy
和
spacy
决定分配给这个词的词性。在这方面,
spacy
puts some linguistics rule in the Lemmatizer()
to decide whether a word is the base form and skips the lemmatization entirely if the word is already in the infinitive form (is_base_form()) ,如果要对语料库中的所有单词进行词形还原,并且其中很大一部分是不定式(已经是词素形式),这将节省很多。
spacy
中是可能的因为它允许 lemmatizer 访问与某些形态规则密切相关的 POS。而对于
morphy
尽管可以使用细粒度的 PTB POS 标签找出一些形态,但仍然需要一些努力来对它们进行分类以了解哪些形式是不定式的。
4d2d7d586608ddc0bcb2857fb3c2d0d4c151ebfc
)相关
关于python - spacy lemmatizer 如何工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43795249/
我有一段文本和索引条目,其中一些指示出现在文本中的重要多词表达 (MWE)(例如生物学文本的“海绵骨”)。我想使用这些条目在 spaCy 中构建自定义匹配器,以便我可以识别文本中出现的 MWE。一个附
我想在 Spacy 中使用德语 lemmatizer,但我对结果感到非常惊讶: import spacy nlp = spacy.load("de_dep_news_trf") [token.lemm
要将我的句子拆分为标记,我正在执行以下操作,这很慢 import spacy nlp = spacy.load("en_core_web_lg") text = "This is a test.
我已经使用空间很长一段时间了,我真的很喜欢这种置换 有没有一种方法可以让我们在网页中从我的数据集中提供多个文本,如一个小箭头,以重定向到下一条记录并标记实体。 我使用的代码如下。 def valida
我有变量 trainData它具有以下简化格式。 [ ('Paragraph_A', {"entities": [(15, 26, 'DiseaseClass'), (443, 449, 'Disea
我正在尝试测试在另一台计算机上运行的模型,但是当我尝试将其导入我的笔记本时,出现以下错误:ModuleNotFoundError:没有名为“spacy.pipeline.pipes”的模块; 'spa
我正在尝试测试在另一台计算机上运行的模型,但是当我尝试将其导入我的笔记本时,出现以下错误:ModuleNotFoundError:没有名为“spacy.pipeline.pipes”的模块; 'spa
当处理数百万文档并将它们保存为空间文档以供以后使用(更多处理、可视化、提取特征)时,一种明显的扩展解决方案是并行/分布式处理。这意味着每个并行进程都将拥有自己的 Vocab,这些 Vocab 可能会随
我正在使用 Spacy 大型模型,但它错误地使用与我的领域无关的类别标记实体,例如“艺术作品”可能导致它无法识别本应属于组织的内容。 是否可以限制 NER 仅返回人员、位置和组织? 最佳答案 简答:
我正在像这样使用 SpaCy 创建一个短语匹配器: import spacy from spacy.matcher import PhraseMatcher nlp = spacy.load("en"
我正在尝试使用 spaCy Matcher 工作获得以下简单示例: import en_core_web_sm from spacy.matcher import Matcher nlp = en_c
它没有出现在 pip list zeke$ pip list | grep spacy spacy (1.7.3) 如何获取模型名称? 我试过了,还是不行 echo "spaCy model:" py
我在 "Training an additional entity type" 中有新 NER 类型的训练数据spaCy 文档的部分。 TRAIN_DATA = [ ("Horses are
给定一个 token ,它是具有多个 token 的命名实体的一部分,是否有直接方法来获取该实体的跨度? 例如,考虑这个有两个词命名实体的句子: >>> doc = nlp("This year wa
如何限制 Spacy 使用的 CPU 数量? 我想从大量句子中提取词性和命名实体。由于 RAM 的限制,我首先使用 Python NLTK 将我的文档解析为句子。然后我遍历我的句子并使用 nlp.pi
显然 for doc in nlp.pipe(sequence) 比运行 for el in order: doc = nlp(el) .. 我遇到的问题是我的序列实际上是一个元组序列,其中包含用于将
显然 for doc in nlp.pipe(sequence) 比运行 for el in order: doc = nlp(el) .. 我遇到的问题是我的序列实际上是一个元组序列,其中包含用于将
我已经下载了 spaCy,但每次尝试 nlp = spacy.load("en_core_web_lg") 命令时,我都会收到此错误: OSError:[E050] 找不到模型“en_core_web
到目前为止,我一直在使用 spacy 2.3.1,并为我的自定义语言类(class)训练并保存了几个管道。但是现在使用 spacy 3.0 和 spacy.load('model-path') 我遇到
我安装了 spacy 使用 python3 install spacy 并使用下载了两个英文模型 python3 -m spacy download en 和 python3 -m spacy dow
我是一名优秀的程序员,十分优秀!