本篇文章将分享5个很棒然而却不被常被提及的Python库,这些库能够帮你解决各种自然语言解决(NLP)工作。

Contractions

Contractions它能够扩大常见的英语缩写和俚语。并且能够疾速、高效的解决大多数边缘状况,例如短少撇号。

例如:以前须要编写一长串正则表达式来扩大文本数据中的(即 don’t → do not;can’t → cannot;haven’t → have not)。Contractions就能够解决这个问题

pip install contractions

应用样例

import contractionss = "ive gotta go! i'll see yall later."text = contractions.fix(s, slang=True)print(text)

后果

ORIGINAL: ive gotta go! i’ll see yall later.OUTPUT: I have got to go! I will see you all later.

文本预处理的一个重要局部是创立一致性并在不失去太多意义的状况下缩小单词列表。词袋模型和 TF-IDF 创立大型稠密矩阵,其中每个变量都是语料库中一个不同的词汇词。将缩略语进行还原能够进一步升高维度,还能够有助于过滤停用词。

Distilbert-Punctuator

将失落的标点符号的文本进行断句并增加标点符号……听起来很容易,对吧?对于计算机来说,做到这一点必定要简单得多。

Distilbert-punctuator 是我能找到的惟一能够执行此工作的 Python 库。而且还超级准!这是因为它应用了 BERT 的精简变体。在联合 20,000 多篇新闻文章和 4,000 份 TED Talk 抄本后,对模型进行了进一步微调,以检测句子边界。在插入句尾标点符号(例如句号)时,模型还会适当地将下一个起始字母大写。

装置

pip install distilbert-punctuator

这个库须要相当多的依赖项,如果只是想测试,能够在 Google Colab 上试用。

应用样例

from dbpunctuator.inference import Inference, InferenceArgumentsfrom dbpunctuator.utils import DEFAULT_ENGLISH_TAG_PUNCTUATOR_MAPargs = InferenceArguments(        model_name_or_path="Qishuai/distilbert_punctuator_en",        tokenizer_name="Qishuai/distilbert_punctuator_en",        tag2punctuator=DEFAULT_ENGLISH_TAG_PUNCTUATOR_MAP    )punctuator_model = Inference(inference_args=args,                              verbose=False)text = ["""however when I am elected I vow to protect our American workforceunlike my opponent I have faith in our perseverance our sense of trust and our democratic principles will you support me"""]print(punctuator_model.punctuation(text)[0])

后果

ORIGINAL: however when I am elected I vow to protect our American workforceunlike my opponent I have faith in our perseverance our sense of trust and our democratic principles will you support meOUTPUT:However, when I am elected, I vow to protect our American workforce. Unlike my opponent, I have faith in our perseverance, our sense of trust and our democratic principles. Will you support me?

如果你只是心愿文本数据在语法上更加正确和易于展现。无论工作是修复凌乱的 Twitter 帖子还是聊天机器人音讯,这个库都适宜你。

Textstat

Textstat 是一个易于应用的轻量级库,可提供无关文本数据的各种指标,例如浏览程度、浏览工夫和字数。

pip install textstat

应用样例

import textstattext = """Love this dress! it's sooo pretty. i happened to find it in a store, and i'm glad i did bc i never would have ordered it online bc it's petite. """# Flesch reading ease scoreprint(textstat.flesch_reading_ease(text))  # 90-100 | Very Easy  # 80-89  | Easy  # 70-79  | Fairly Easy  # 60-69  | Standard  # 50-59  | Fairly Difficult  # 30-49  | Difficult  # <30    | Very Confusing# Reading time (output in seconds)# Assuming 70 milliseconds/characterprint(textstat.reading_time(text, ms_per_char=70))# Word count print(textstat.lexicon_count(text, removepunct=True))

后果

ORIGINAL:Love this dress! it's sooo pretty. i happened to find it in a store, and i'm glad i did bc i never would have ordered it online bc it's petite.OUTPUTS:74.87 # reading score is considered 'Fairly Easy'7.98  # 7.98 seconds to read30    # 30 words

这个库还为这些指标减少了一个额定的剖析层。例如,一个八卦杂志上的名人新闻文章的数据集。应用textstat,你会发现浏览速度更快更容易浏览的文章更受欢迎,留存率更高。

Gibberish-Detector

这个低代码库的次要目标是检测难以了解的单词(或胡说八道)。它在大量英语单词上训练的模型。

pip install gibberish-detector

装置实现后还须要本人训练模型,但这非常简单,只需一分钟。训练步骤如下:

  1. 从这里下载名为 big.txt 的训练语料库
  2. 关上你的 CLI 并 cd 到 big.txt 所在的目录
  3. 运行以下命令:gibberish-detector train .\big.txt > gibberish-detector.model

这将在当前目录中创立一个名为 gibberish-detector.model 的文件。

应用样例

from gibberish_detector import detector# load the gibberish detection modelDetector = detector.create_from_model('.\gibberish-detector.model')text1 = "xdnfklskasqd"print(Detector.is_gibberish(text1))text2 = "apples"print(Detector.is_gibberish(text2))

后果

True  # xdnfklskasqd (this is gibberish)False # apples (this is not)

它能够帮忙我从数据集中删除不良察看后果。还能够实现对用户输出的错误处理。例如,如果用户在您的 Web 应用程序上输出无意义的胡说八道文本,这时能够返回一条谬误音讯。

NLPAug

最好的要留到最初。

首先,什么是数据加强?它是通过增加现有数据的略微批改的副原本扩大训练集大小的任何技术。当现有数据的多样性无限或不均衡时,通常应用数据加强。对于计算机视觉问题,加强用于通过裁剪、旋转和扭转图像的亮度来创立新样本。对于数值数据,能够应用聚类技术创立合成实例。

然而如果咱们正在解决文本数据呢?这就是 NLPAug 的用武之地。该库能够通过替换或插入语义关联的单词来裁减文本。通过应用像 BERT 这样的预训练语言模型来进行数据的加强,这是一种弱小的办法,因为它思考了单词的上下文。依据设置的参数,能够应用前 n 个类似词来批改文本。

预训练的词嵌入,如 Word2Vec 和 GloVe,也可用于用同义词替换词。

pip install nlpaug

应用样例

import nlpaug.augmenter.word as naw# main parameters to adjustACTION = 'substitute' # or use 'insert'TOP_K = 15 # randomly draw from top 15 suggested wordsAUG_P = 0.40 # augment 40% of words within textaug_bert = naw.ContextualWordEmbsAug(    model_path='bert-base-uncased',     action=ACTION,     top_k=TOP_K,    aug_p=AUG_P    )text = """Come into town with me today to buy food!"""augmented_text = aug_bert.augment(text, n=3) # n: num. of outputsprint(augmented_text)

后果

ORIGINAL:Come into town with me today to buy food!OUTPUTS:• drove into denver with me today to purchase groceries!• head off town with dad today to buy coffee!• come up shop with mom today to buy lunch!

假如你正在应用一个具备 15k 条侧面评论和仅 4k 条负面评论的数据集上训练监督分类模型。重大不均衡的数据集会在训练期间产生对少数类(侧面评估)的模型偏差。

简略地复制少数类的示例(负面评论)不会向模型增加任何新信息。相同,利用 NLPAug 的高级文本加强性能来减少多样性的少数类。该技术已被证实能够进步 AUC 和 F1-Score。

论断

作为数据科学家、Kaggle 参与者或个别程序员,重要的是咱们须要找到更多的工具来简化咱们的工作流程。这样能够利用这些库来解决问题,加强咱们的数据集,并花更多工夫思考解决方案而不是编写代码。

作者:Michael Markin