- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
考虑句子
msg = 'I got this URL https://stackoverflow.com/questions/47637005/handmade-estimator-modifies-parameters-in-init/47637293?noredirect=1#comment82268544_47637293 freed'
接下来,我使用开箱即用的英语 spaCy
处理句子:
import spacy
nlp = spacy.load('en')
doc = nlp(msg)
让我们回顾一下输出:[(t, t.lemma_, t.pos_, t.tag_, t.dep_) for t in doc]
:
[(I, '-PRON-', 'PRON', 'PRP', 'nsubj'),
(got, 'get', 'VERB', 'VBD', 'ROOT'),
(this, 'this', 'DET', 'DT', 'det'),
(URL, 'url', 'NOUN', 'NN', 'compound'),
(https://stackoverflow.com/questions/47637005/handmade-estimator-modifies-parameters-in-init/47637293?noredirect=1#comment82268544_47637293,
'https://stackoverflow.com/questions/47637005/handmade-estimator-modifies-parameters-in-init/47637293?noredirect=1#comment82268544_47637293',
'NOUN',
'NN',
'nsubj'),
(freed, 'free', 'VERB', 'VBN', 'ccomp')]
我想改进对 URL 片段的处理。特别是,我想:
lemma
设置为 stackoverflow.com
标签
设置为URL
如何使用 spaCy
来实现?我想使用正则表达式(如建议的 here )来确定字符串是否为 URL 并获取域。到目前为止,我没能找到方法。
编辑 我想我需要的是自定义组件。但是,似乎没有办法将基于正则表达式(或任何其他)的可调用项放置为 patterns
。 .
最佳答案
您可以使用自定义分词器指定 URL 正则表达式,例如来自 https://spacy.io/usage/linguistic-features#native-tokenizers
import regex as re
from spacy.tokenizer import Tokenizer
prefix_re = re.compile(r'''^[\[\("']''')
suffix_re = re.compile(r'''[\]\)"']$''')
infix_re = re.compile(r'''[-~]''')
simple_url_re = re.compile(r'''^https?://''')
def custom_tokenizer(nlp):
return Tokenizer(nlp.vocab, prefix_search=prefix_re.search,
suffix_search=suffix_re.search,
infix_finditer=infix_re.finditer,
token_match=simple_url_re.match)
nlp = spacy.load('en')
nlp.tokenizer = custom_tokenizer(nlp)
msg = 'I got this URL https://stackoverflow.com/questions/47637005/handmade-estimator-modifies-parameters-in-init/47637293?noredirect=1#comment82268544_47637293 freed'
for i, token in enumerate(nlp(msg)):
print(i, ':\t', token)
[输出]:
0 : I
1 : got
2 : this
3 : URL
4 : https://stackoverflow.com/questions/47637005/handmade-estimator-modifies-parameters-in-init/47637293?noredirect=1#comment82268544_47637293
5 : freed
您可以检查 token 是否类似于 URL,例如
for i, token in enumerate(nlp(msg)):
print(token.like_url, ':\t', token.lemma_)
[输出]:
False : -PRON-
False : get
False : this
False : url
True : https://stackoverflow.com/questions/47637005/handmade-estimator-modifies-parameters-in-init/47637293?noredirect=1#comment82268544_47637293
False : free
doc = nlp(msg)
for i, token in enumerate(doc):
if token.like_url:
token.tag_ = 'URL'
print([token.tag_ for token in doc])
[输出]:
['PRP', 'VBD', 'DT', 'NN', 'URL', 'VBN']
使用正则表达式 https://regex101.com/r/KfjQ1G/1 :
doc = nlp(msg)
for i, token in enumerate(doc):
if re.match(r'(?:http[s]:\/\/)stackoverflow.com.*', token.lemma_):
token.lemma_ = 'stackoverflow.com'
print([token.lemma_ for token in doc])
[输出]:
['-PRON-', 'get', 'this', 'url', 'stackoverflow.com', 'free']
关于python - 使用 spaCy 为 URL 定制标签和词法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48112057/
我目前被指派去调查并以某种方式找到一种“定制”(对身份验证、常量、消息等进行修改)OpenSSH 的方法,并且作为一个“基于网络”的人,我真的不知道从哪儿开始。因此非常感谢评论。 问题: 我从 her
我拥有的 excel 文件超过 1,000,000 行和 26 列。 下面是用于查找特定数据的代码,并根据该数据创建一个新文件,目前创建一个新文件大约需要 15 分钟 请如果有专家可以帮助我更快地处理
我正在处理Zend_Form现在,我很难弄清楚如何: 使用表单按钮的自定义图像, 在特定位置插入文本和链接(就我而言,我想在提交按钮之前添加“忘记密码?”链接)。 我已通读手册,但没有看到任何相关内容
是否可以将图像添加到 UISwitch 背景,例如当状态为 ON 时(作为一个背景)和当状态为 OFF 时(另一个背景图像)? 最佳答案 要更改背景颜色(不是图像),您只需执行以下操作即可。这会更改领
到目前为止,我刚刚开始使用 Octave 并在我的终端上运行它。 每次打开提示符时,我的命令行都以: octave-3.4.0:1> 因此我使用以下内容来使其更短且更易于阅读: PS1('>> ')
在阅读Struts2文档时,我遇到了下面引用的段落 customizing controller - Struts 1 lets to customize the request processor
我正在尝试自定义 jQuery Tag-It 小部件 (http://aehlke.github.com/tag-it/) 以实现以下两种行为: 1)允许在标签中使用逗号(我可以通过自定义trigge
我是整个 Emacs 的新手,让我着迷的一件事是开箱即用的 Emacs 在编程时不会让您陷入困境。我主要使用 Python 和 C++ 进行编程,然后按回车键将光标发送回新行的第 1 列,而不是让你停
我有这些行 y DB,我想按以下顺序排序,并包含字符和数字。 Score 列是一个 varchar。获胜者和失败者也在分数栏中。 得分 WINNER 100+ 100 90 80+ 80 50 LOS
我正在使用 Bootstrap,您如何自定义轮播? 有什么建议吗? https://v4-alpha.getbootstrap.com/components/carousel/ 最佳答案 .activ
我有一个投票设置,使用脚本将其拉入我的 WP 页面。通过http://quipol.com/ EG 我已经通过 firebug 找到了样式并在其中相应地自定义了它们,但我想知道是否有一种方法可以实现
美好的一天。 如果 JLabel 和 JTextField 字体大小可以根据需要更改,是否也可以更改 JTable 的列名称和元素的字体样式(大小、外观、颜色)? 添加更多内容,我正在使用 Windo
进一步回答我的问题Java JFilechooser 。建议扩展 BasicFileChooserUI,重写 create/getModel 并提供 BasicDirectoryModel 的实现。
我想制作(好吧..正在制作..)一个标签页。我用 border-top:none 属性制作了一个“选项卡框”,所以它看起来像是选项卡的一部分,在里面我有一个表格。 我想知道,有没有办法删除表格标题的所
我有大量的项目正在进行中,还有几个解决方案(它们是项目“池”的子集)。有时拥有一个仅用于特定测试的 .sln 是件好事。 问题: NUGet 分别绑定(bind)到每个解决方案。 NUGet 喜欢在
我计划编写一些 git 钩子(Hook)作为一个项目,将用户的操作记录在数据库中。然后可以使用该数据库查询他的所有事件。我尝试记录的操作是 promise pull 推送 merge 分支机构 我想把
大家好,我是张飞洪,感谢您的阅读,我会不定期和你分享学习心得,希望我的文章能成为你成长路上的垫脚石,让我们一起精进。 在本文中,我们将学习中间件,以及如何使用它进一步定制应用程序。我
我正在尝试使用 yasg 自定义我的 api 文档。 首先,我想确定我自己的部分的命名,以及本部分应包含哪些端点。似乎部分的命名是基于不属于最长公共(public)前缀的第一个前缀,例如: 如果我们有
我需要(即客户要求)提供自定义键盘,供用户在文本字段和区域中输入文本。我已经有一些可以执行键盘操作并将测试附加到文本字段的东西,但是我想让它更通用并让它像标准的 iphone 键盘一样工作,即当用户选
我有一个项目,它在特定位置(不是/src/resources)包含资源(模板文件)。我希望在运行 package-bin 时将这些资源打包。 我看到了 package-options 和 packag
我是一名优秀的程序员,十分优秀!