我从the SpaCy docs获取了一些代码,允许您为文本分配自定义依赖项标签,我想用它来解释用户的意图。它主要工作,但例如当我运行代码时,它标记"删除"作为' ROOT'它应该在哪里标记为' INTENT'就像它在deps
字典中显示的那样。
from __future__ import unicode_literals, print_function
import plac
import random
import spacy
from pathlib import Path
# training data: texts, heads and dependency labels
# for no relation, we simply chose an arbitrary dependency label, e.g. '-'
TRAIN_DATA = [
("How do I delete my account?", {
'heads': [3, 3, 3, 3, 5, 3, 3], # index of token head
'deps': ['ROOT', '-', '-', 'INTENT', '-', 'OBJECT', '-']
}),
("How do I add a balance?", {
'heads': [3, 3, 3, 3, 5, 3, 3],
'deps': ['ROOT', '-', '-', 'INTENT', '-', 'OBJECT', '-']
}),
("How do I deposit my funds into my bank account?", {
'heads': [3, 3, 3, 3, 5, 3, 3, 9, 9, 6, 3],
'deps': ['ROOT', '-', '-', 'INTENT', '-', '-', '-', '-', '-', 'OBJECT', '-']
}),
("How do I fill out feedback forms?", {
'heads': [3, 3, 3, 3, 3, 6, 3, 3],
'deps': ['ROOT', '-', '-', 'INTENT', '-', '-', 'OBJECT', '-']
}),
#("How does my profile impact my score?", {
#'heads': [4, 4, 4, 4, 4, 6, 4, 4],
#'deps': ['ROOT', '-', '-', '-', 'INTENT', '-', 'OBJECT' '-']
#}),
("What are the fees?", {
'heads': [1, 1, 3, 1, 1],
'deps': ['ROOT', '-', '-', 'INTENT', '-']
}),
("How do I update my profile picture?", {
'heads': [3, 3, 3, 3, 6, 6, 3, 3],
'deps': ['ROOT', '-', '-', 'INTENT', '-', 'OBJECT', 'OBJECT', '-']
}),
("How do I add a referral to the marketplace?", {
'heads': [3, 3, 3, 3, 5, 3, 3, 8, 6, 3],
'deps': ['ROOT', '-', '-', 'INTENT', '-', 'OBJECT', '-', '-', 'OBJECT', '-']
}),
]
@plac.annotations(
model=("Model name. Defaults to blank 'en' model.", "option", "m", str),
output_dir=("Optional output directory", "option", "o", Path),
n_iter=("Number of training iterations", "option", "n", int))
def main(model=None, output_dir=None, n_iter=5):
"""Load the model, set up the pipeline and train the parser."""
if model is not None:
nlp = spacy.load(model) # load existing spaCy model
print("Loaded model '%s'" % model)
else:
nlp = spacy.blank('en') # create blank Language class
print("Created blank 'en' model")
# We'll use the built-in dependency parser class, but we want to create a
# fresh instance – just in case.
if 'parser' in nlp.pipe_names:
nlp.remove_pipe('parser')
parser = nlp.create_pipe('parser')
nlp.add_pipe(parser, first=True)
#add new labels to the parser
for text, annotations in TRAIN_DATA:
for dep in annotations.get('deps', []):
parser.add_label(dep)
other_pipes = [pipe for pipe in nlp.pipe_names if pipe != 'parser']
with nlp.disable_pipes(*other_pipes): # only train parser
optimizer = nlp.begin_training()
for itn in range(n_iter):
random.shuffle(TRAIN_DATA)
losses = {}
for text, annotations in TRAIN_DATA:
nlp.update([text], [annotations], sgd=optimizer, losses=losses)
print(losses)
# test the trained model
test_model(nlp)
# save model to output directory
if output_dir is not None:
output_dir = Path(output_dir)
if not output_dir.exists():
output_dir.mkdir()
nlp.to_disk(output_dir)
print("Saved model to", output_dir)
# test the saved model
print("Loading from", output_dir)
nlp2 = spacy.load(output_dir)
test_model(nlp2)
def test_model(nlp):
texts = ["How do I delete my account?"]
docs = nlp.pipe(texts)
for doc in docs:
print(doc.text)
print([(t.text, t.dep_, t.head.text) for t in doc if t.dep_ != '-'])
if __name__ == '__main__':
plac.call(main)
这是输出:
How do I delete my account?
[(u'How', u'ROOT', u'delete'), (u'delete', u'ROOT', u'delete'), (u'account', u'OBJECT', u'delete')]
答案 0 :(得分:2)
我认为你的问题的根源是依赖树的根被自动标记为'ROOT'
,
(并且依赖树的根被定义为其头部本身的标记)。
可能的解决方法是在训练数据中添加人工根:
("root How do I delete my account?", {
'heads': [0, 4, 4, 4, 0, 6, 4, 4], # index of token head
'deps': ['ROOT', '-', '-', '-', 'INTENT', '-', 'OBJECT', '-']
})
(同时在您的测试示例中添加符号root
:texts = ["root How do I delete my account?"]
)
通过这些更改,如果您训练模型的时间足够长,您将获得:
root How do I delete my account?
[('root', 'ROOT', 'root'), ('delete', 'INTENT', 'root'), ('account', 'OBJECT', 'delete')]