【问题标题】:How to tag sentences for spacy's Sence2vec implementation如何为 spacy Sense2vec 实现标记句子
【发布时间】:2017-09-24 03:19:13
【问题描述】:

SpaCy 实现了一个 sense2vec 词嵌入包,他们记录了 here

向量都是WORD|POS的形式。例如,句子

Dear local newspaper, I think effects computers have on people are great learning skills/affects because they give us time to chat with friends/new people, helps us learn about the globe(astronomy) and keeps us out of trouble

需要转换成

Dear|ADJ local|ADJ newspaper|NOUN ,|PUNCT I|PRON think|VERB effects|NOUN computers|NOUN have|VERB on|ADP people|NOUN are|VERB great|ADJ learning|NOUN skills/affects|NOUN because|ADP they|PRON give|VERB us|PRON time|NOUN to|PART chat|VERB with|ADP friends/new|ADJ people|NOUN ,|PUNCT helps|VERB us|PRON learn|VERB about|ADP the|DET globe(astronomy|NOUN )|PUNCT and|CONJ keeps|VERB us|PRON out|ADP of|ADP trouble|NOUN !|PUNCT

为了能够被 sense2vec 预训练的嵌入解释,并采用 sense2vec 格式。

如何做到这一点?

【问题讨论】:

    标签: python nlp spacy sense2vec


    【解决方案1】:

    基于SpaCy's bin/merge.py 实现,它完全符合需要:

    from spacy.en import English
    import re
    
    LABELS = {
        'ENT': 'ENT',
        'PERSON': 'ENT',
        'NORP': 'ENT',
        'FAC': 'ENT',
        'ORG': 'ENT',
        'GPE': 'ENT',
        'LOC': 'ENT',
        'LAW': 'ENT',
        'PRODUCT': 'ENT',
        'EVENT': 'ENT',
        'WORK_OF_ART': 'ENT',
        'LANGUAGE': 'ENT',
        'DATE': 'DATE',
        'TIME': 'TIME',
        'PERCENT': 'PERCENT',
        'MONEY': 'MONEY',
        'QUANTITY': 'QUANTITY',
        'ORDINAL': 'ORDINAL',
        'CARDINAL': 'CARDINAL'
    }
    
    
    
    nlp = False;
    def tag_words_in_sense2vec_format(passage):
        global nlp; 
        if(nlp == False): nlp = English()
        if isinstance(passage, str): passage = passage.decode('utf-8',errors='ignore');
        doc = nlp(passage);
        return transform_doc(doc);
    
    def transform_doc(doc):
        for index, ent in enumerate(doc.ents):
            ent.merge(ent.root.tag_, ent.text, LABELS[ent.label_])
            #if index % 100 == 0: print ("enumerating at entity index " + str(index));
        #for np in doc.noun_chunks:
        #    while len(np) > 1 and np[0].dep_ not in ('advmod', 'amod', 'compound'):
        #        np = np[1:]
        #    np.merge(np.root.tag_, np.text, np.root.ent_type_)
        strings = []
        for index, sent in enumerate(doc.sents):
            if sent.text.strip():
                strings.append(' '.join(represent_word(w) for w in sent if not w.is_space))
            #if index % 100 == 0: print ("converting at sentence index " + str(index));
        if strings:
            return '\n'.join(strings) + '\n'
        else:
            return ''
    def represent_word(word):
        if word.like_url:
            return '%%URL|X'
        text = re.sub(r'\s', '_', word.text)
        tag = LABELS.get(word.ent_type_, word.pos_)
        if not tag:
            tag = '?'
        return text + '|' + tag
    

    在哪里

    print(tag_words_in_sense2vec_format("Dear local newspaper, ..."))
    

    结果

     Dear|ADJ local|ADJ newspaper|NOUN ,|PUNCT ...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-01-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-18
      • 1970-01-01
      • 1970-01-01
      • 2019-08-13
      相关资源
      最近更新 更多