【发布时间】:2019-03-26 04:19:03
【问题描述】:
我最近认识了Spacy,并且对这个 Python 库非常感兴趣。但是,在我的规范中,我打算从输入句子中提取复合名词-形容词对作为关键短语。我认为Spacy 提供了许多实用程序来处理 NLP 任务,但没有为我想要的任务找到满意的线索。我在SO、related post 中查看了一个非常相似的帖子,解决方案效率不高,不适用于自定义输入语句。
这里是一些输入语句:
sentence_1="My problem was with DELL Customer Service"
sentence_2="Obviously one of the most important features of any computer is the human interface."
sentence_3="The battery life seems to be very good and have had no issues with it."
这是我尝试过的代码:
import spacy, en_core_web_sm
nlp=en_core_web_sm.load()
def get_compound_nn_adj(doc):
compounds_nn_pairs = []
parsed=nlp(doc)
compounds = [token for token in sent if token.dep_ == 'compound']
compounds = [nc for nc in compounds if nc.i == 0 or sent[nc.i - 1].dep_ != 'compound']
if compounds:
for token in compounds:
pair_1, pair_2 = (False, False)
noun = sent[token.i:token.head.i + 1]
pair_1 = noun
if noun.root.dep_ == 'nsubj':
adj_list = [rt for rt in noun.root.head.rights if rt.pos_ == 'ADJ']
if adj_list:
pair_2 = adj_list[0]
if noun.root.dep_ == 'dobj':
verb_root = [vb for vb in noun.root.ancestors if vb.pos_ == 'VERB']
if verb_root:
pair_2 = verb_root[0]
if pair_1 and pair_2:
compounds_nn_pairs.append(pair_1, pair_2)
return compounds_nn_pairs
我推测应该在辅助函数上方应用什么样的修饰,因为它没有返回我预期的复合名词-形容词对。有谁对Spacy有好的经验吗?如何改进上述草图解决方案?有更好的主意吗?
期望的输出:
我希望从每个输入句子中得到复合名词-形容词对,如下所示:
desired_output_1="DELL Customer Service"
desired_output_2="human interface"
desired_output_3="battery life"
有什么办法可以得到预期的输出?上述实施需要什么样的更新?还有什么想法吗?提前致谢!
【问题讨论】:
标签: python sentiment-analysis spacy