【问题标题】:Spacy Verb highlight?Spacy动词亮点?
【发布时间】:2021-06-23 19:37:20
【问题描述】:

就像 spacy displacy 在 html 中呈现实体高亮一样。

import spacy
from spacy import displacy
nlp = spacy.load('en')
doc1 = nlp(u'This is a google sentence.')
doc2 = nlp(u'This is another sentence.')
html = displacy.render([doc1, doc2], style='ent', page=True)

如何突出显示给定文本中的所有动词。

from __future__ import unicode_literals
import spacy,en_core_web_sm
import textacy
nlp = en_core_web_sm.load()
sentence = 'The cat jumped quickly over the wall.'
doc = textacy.Doc(sentence, lang='en_core_web_sm')
for token in doc:
    if (token.pos_ == 'VERB'):
        print(token.text)

这里的输出 Jumped 将以绿色突出显示!怎么样?

类似的东西

http://www.expresso-app.org/

【问题讨论】:

    标签: nlp spacy


    【解决方案1】:

    您可以通过在render() 或serve() 上设置manual=True 来使用displace ent 界面突出显示自定义实体。 这是一个简单的例子:

    sentence = [{'text': 'The cat jumped quickly over the wall.',
        'ents': [{'start': 8, 'end':14, 'label': 'VERB'}],
        'title': None}]
    
    displacy.render(sentence, style='ent', manual=True)
    

    此外,要获取所需格式的数据,您可以进行依赖解析并在其上使用PhraseMatcher 以获取start 和end 值。

    【讨论】:

      【解决方案2】:

      您可以使用带有参数style='ent' 和manual=True 的显示的渲染或服务方法来突出显示自定义实体。

      from spacy import displacy
      import re
      
      text = 'The quick brown fox jumps over the lazy dog'
      match_phrases = ['brown fox','lazy dog']
      matches = []
      
      for match_phrase in match_phrases:
          for item in re.finditer(match_phrase,text):
              match = {}
              match['start'], match['end'] = item.span() 
              match['label'] = '\u2713' # The tag/label that you would like to display
              matches.append(match)
              
      sentence = [{
          'text': text,
          'ents': matches
      }]
      
      displacy.render(sentence, style='ent', manual=True)
      

      【讨论】:

        猜你喜欢
        • 2019-01-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-04-18
        • 1970-01-01
        • 1970-01-01
        • 2018-07-01
        • 1970-01-01
        相关资源
        最近更新 更多