如何在标记化中将多词名称保留在一起? [英] How can I keep multi-word names in tokenization together?

查看:54
本文介绍了如何在标记化中将多词名称保留在一起?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 TF-IDF 功能对文档进行分类.一种方法:

from sklearn.feature_extraction.text import TfidfVectorizer导入字符串进口重新导入 nltk定义标记化(文档):文档 = document.lower()对于 string.punctuation 中的 punct_char:document = document.replace(punct_char, " ")document = re.sub('\s+', ' ', document).strip()令牌 = document.split(" ")# 包含比我想要的更多:# 从 spacy.lang.de.stop_words 导入 STOP_WORDS停用词 = nltk.corpus.stopwords.words('德语')令牌 = [令牌中的令牌,如果令牌不在停用词中]返回令牌# 我打算如何使用它变压器 = TfidfVectorizer(tokenizer=tokenize)示例 = "Jochen Schweizer ist eines der interessantesten Unternehmen der Welt, hat den Sitz allerdings nicht in der Schweizerischen Eidgenossenschaft."变压器.fit([示例])# 分词器示例打印(标记化(示例))

这个分词器的一个缺陷是它将属于一起的单词分开:Jochen Schweizer"和schweizerische Eidgenossenschaft".也缺少词形还原(词干提取).我想获得以下令牌:

["Jochen Schweizer", "interessantesten", "unternehmen", "Welt", "Sitz", "allerdings", "nicht", "Schweizerische Eidgenossenschaft"]

我知道 Spacy 可以识别那些命名实体 (NER):

import en_core_web_sm # python -m spacy 下载 en_core_web_sm --user解析器 = en_core_web_sm.load()doc = 解析器(示例)打印(doc.ents) # (Jochen Schweizer, Welt, Sitz)

是否有使用 spacy 以将命名实体单词保持在一起的方式进行标记的好方法?

解决方案

这个怎么样:

 使用 doc.retokenize() 作为 retokenizer:对于 doc.ents 中的 ent:retokenizer.merge(doc[ent.start:ent.end])

其实可以用spacy去掉标点符号&停用词,并进行词形还原!

parser = spacy.load('de_core_news_sm')定义标记化(文本):doc = 解析器(文本)使用 doc.retokenize() 作为 retokenizer:对于 doc.ents 中的 ent:retokenizer.merge(doc[ent.start:ent.end], attrs={"LEMMA": ent.text})如果不是 x.is_punct 而不是 x.is_stop,则返回 [x.lemma_ for x in doc]

示例:

<预><代码>>>>text = "Jochen Schweizer ist eines der interessantesten Unternehmen der Welt, hat den Sitz allerdings nicht in der Schweizerischen Eidgenossenschaft.>>>打印(标记化(文本))>>>[u'Jochen Schweizer', u'interessant', u'Unternehmen', u'Welt', u'Sitz', u'Schweizerischen Eidgenossenschaft']

I want to classify documents using TF-IDF features. One way to do it:

from sklearn.feature_extraction.text import TfidfVectorizer
import string
import re
import nltk


def tokenize(document):
    document = document.lower()
    for punct_char in string.punctuation:
        document = document.replace(punct_char, " ")
    document = re.sub('\s+', ' ', document).strip()

    tokens = document.split(" ")

    # Contains more than I want:
    # from spacy.lang.de.stop_words import STOP_WORDS
    stopwords = nltk.corpus.stopwords.words('german')
    tokens = [token for token in tokens if token not in stopwords]
    return tokens

# How I intend to use it
transformer = TfidfVectorizer(tokenizer=tokenize)

example = "Jochen Schweizer ist eines der interessantesten Unternehmen der Welt, hat den Sitz allerdings nicht in der Schweizerischen Eidgenossenschaft."
transformer.fit([example])

# Example of the tokenizer
print(tokenize(example))

One flaw of this tokenizer is that it splits words that belong together: "Jochen Schweizer" and "schweizerische Eidgenossenschaft". Also lemmatization (word stemming) is missing. I would like to get the following tokens:

["Jochen Schweizer", "interessantesten", "unternehmen", "Welt", "Sitz", "allerdings", "nicht", "Schweizerische Eidgenossenschaft"]

I know that Spacy can identify those named entities (NER):

import en_core_web_sm  # python -m spacy download en_core_web_sm --user
parser = en_core_web_sm.load()
doc = parser(example)
print(doc.ents)  # (Jochen Schweizer, Welt, Sitz)

Is there a good way to use spacy to tokenize in a way that keeps the named entity words together?

解决方案

How about this:

with doc.retokenize() as retokenizer:
    for ent in doc.ents:
        retokenizer.merge(doc[ent.start:ent.end])

In fact, you can use spacy to remove punctuations & stop words, and perform lemmatization too!

parser = spacy.load('de_core_news_sm')
def tokenize(text):
    doc = parser(text)
    with doc.retokenize() as retokenizer:
        for ent in doc.ents:
            retokenizer.merge(doc[ent.start:ent.end], attrs={"LEMMA": ent.text})
    return [x.lemma_ for x in doc if not x.is_punct and not x.is_stop]

Example:

>>> text = "Jochen Schweizer ist eines der interessantesten Unternehmen der Welt, hat den Sitz allerdings nicht in der Schweizerischen Eidgenossenschaft."
>>> print(tokenize(text))
>>> [u'Jochen Schweizer', u'interessant', u'Unternehmen', u'Welt', u'Sitz', u'Schweizerischen Eidgenossenschaft']

这篇关于如何在标记化中将多词名称保留在一起?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆