我打算使用spacy和textacy来识别英语中的句子结构。
例如: 猫坐在垫子上-SVO,猫跳了起来,拿起了饼干-SVV0。那只猫吃了饼干和饼干。-SVOO。
该程序应该读取一个段落并以SVO,SVOO,SVVO或其他自定义结构返回每个句子的输出。
到目前为止的努力:
# -*- coding: utf-8 -*-
#!/usr/bin/env python
from __future__ import unicode_literals
# Load Library files
import en_core_web_sm
import spacy
import textacy
nlp = en_core_web_sm.load()
SUBJ = ["nsubj","nsubjpass"]
VERB = ["ROOT"]
OBJ = ["dobj", "pobj", "dobj"]
text = nlp(u'The cat sat on the mat. The cat jumped and picked up the biscuit. The cat ate biscuit and cookies.')
sub_toks = [tok for tok in text if (tok.dep_ in SUBJ) ]
obj_toks = [tok for tok in …Run Code Online (Sandbox Code Playgroud) 我有一个例句。“开门。” 我解析了一个句子以获取括号内的解析输出,如下所示。
(S(VP(VB打开)(NP(DT the)(NN门)))(..))
我需要提取产生已解析输出的CFG语法规则。我可以这样手动将它们写出来:
grammar = CFG.fromstring("""
S -> VP NP
NP -> Det N
VP -> V
Det ->'the '
N -> 'door'
V -> 'Open'
""")
Run Code Online (Sandbox Code Playgroud)
但这很耗时,如何自动生成带括号的语法规则?
我使用颜色窃贼从图像中提取调色板。
如何创建一个rgb值的图像作为调色板?
from colorthief import ColorThief
color_thief = ColorThief('C:\Users\username\Desktop\index.jpg')
# get the dominant color
dominant_color = color_thief.get_color(quality=1)
print dominant_color
# build a color palette
palette = color_thief.get_palette(color_count=2)
print palette
Run Code Online (Sandbox Code Playgroud)
输出:
(82, 129, 169)
[(82, 129, 169), (218, 223, 224), (147, 172, 193), (168, 197, 215), (117, 170, 212)]
Run Code Online (Sandbox Code Playgroud)
预期输出类似于http://www.color-hex.com/color-palette/895,即一系列彩色矩形
目的是根据 POS 标签进行大写,我可以通过以下链接来实现。
尝试使用 spacy 获得类似的结果?
def truecase(doc):
truecased_sents = [] # list of truecased sentences
tagged_sent = token.tag_([word.lower() for token in doc])
normalized_sent = [w.capitalize() if t in ["NN","NNS"] else w for (w,t) in tagged_sent]
normalized_sent[0] = normalized_sent[0].capitalize()
string = re.sub(" (?=[\.,'!?:;])", "", ' '.join(normalized_sent))
return string
Run Code Online (Sandbox Code Playgroud)
它抛出这个错误
tagged_sent = token.tag_([word.lower() for token in doc])
NameError: global name 'token' is not defined
Run Code Online (Sandbox Code Playgroud)
如何将 token 声明为全局并解决这个问题。我的做法正确吗?