脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - 五个很少被提到但能提高NLP工作效率的Python库

五个很少被提到但能提高NLP工作效率的Python库

2021-12-27 23:49deephub Python

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

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

五个很少被提到但能提高NLP工作效率的Python库

Contractions

Contractions它可以扩展常见的英语缩写和俚语。 并且可以快速、高效的处理大多数边缘情况,例如缺少撇号。

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

  1. pip install contractions

使用样例

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

结果

  1. ORIGINAL: ive gotta go! i’ll see yall later.
  2. 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 抄本后,对模型进行了进一步微调,以检测句子边界。 在插入句尾标点符号(例如句号)时,模型还会适当地将下一个起始字母大写。

安装

  1. pip install distilbert-punctuator

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

使用样例

  1. from dbpunctuator.inference import Inference, InferenceArguments
  2. from dbpunctuator.utils import DEFAULT_ENGLISH_TAG_PUNCTUATOR_MAP
  3. args = InferenceArguments(
  4. model_name_or_path="Qishuai/distilbert_punctuator_en",
  5. tokenizer_name="Qishuai/distilbert_punctuator_en",
  6. tag2punctuator=DEFAULT_ENGLISH_TAG_PUNCTUATOR_MAP
  7. )
  8. punctuator_model = Inference(inference_args=args,
  9. verbose=False)
  10. text = [
  11. """
  12. however when I am elected I vow to protect our American workforce
  13. unlike my opponent I have faith in our perseverance our sense of trust and our democratic principles will you support me
  14. """
  15. ]
  16. print(punctuator_model.punctuation(text)[0])

结果

  1. ORIGINAL:
  2. however when I am elected I vow to protect our American workforce
  3. unlike my opponent I have faith in our perseverance our sense of trust and our democratic principles will you support me
  4. OUTPUT:
  5. 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 是一个易于使用的轻量级库,可提供有关文本数据的各种指标,例如阅读水平、阅读时间和字数。

  1. pip install textstat

使用样例

  1. import textstat
  2. text = """
  3. 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.
  4. """
  5. # Flesch reading ease score
  6. print(textstat.flesch_reading_ease(text))
  7. # 90-100 | Very Easy
  8. # 80-89 | Easy
  9. # 70-79 | Fairly Easy
  10. # 60-69 | Standard
  11. # 50-59 | Fairly Difficult
  12. # 30-49 | Difficult
  13. # <30 | Very Confusing
  14. # Reading time (output in seconds)
  15. # Assuming 70 milliseconds/character
  16. print(textstat.reading_time(text, ms_per_char=70))# Word count
  17. print(textstat.lexicon_count(text, removepunct=True))

结果

  1. ORIGINAL:
  2. 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.
  3. OUTPUTS:
  4. 74.87 # reading score is considered 'Fairly Easy'
  5. 7.98 # 7.98 seconds to read
  6. 30 # 30 words

这个库还为这些指标增加了一个额外的分析层。 例如,一个八卦杂志上的名人新闻文章的数据集。使用textstat,你会发现阅读速度更快更容易阅读的文章更受欢迎,留存率更高。

Gibberish-Detector

这个低代码库的主要目的是检测难以理解的单词(或胡言乱语)。 它在大量英语单词上训练的模型。

  1. pip install gibberish-detector

安装完成后还需要自己训练模型,但这非常简单,只需一分钟。 训练步骤如下:

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

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

使用样例

  1. from gibberish_detector import detector
  2. # load the gibberish detection model
  3. Detector = detector.create_from_model('.\gibberish-detector.model')
  4. text1 = "xdnfklskasqd"
  5. print(Detector.is_gibberish(text1))
  6. text2 = "apples"
  7. print(Detector.is_gibberish(text2))

结果

  1. True # xdnfklskasqd (this is gibberish)
  2. False # apples (this is not)

它可以帮助我从数据集中删除不良观察结果。还可以实现对用户输入的错误处理。 例如,如果用户在您的 Web 应用程序上输入无意义的胡言乱语文本,这时可以返回一条错误消息。

NLPAug

最好的要留到最后。

首先,什么是数据增强?它是通过添加现有数据的稍微修改的副本来扩展训练集大小的任何技术。当现有数据的多样性有限或不平衡时,通常使用数据增强。对于计算机视觉问题,增强用于通过裁剪、旋转和改变图像的亮度来创建新样本。对于数值数据,可以使用聚类技术创建合成实例。

但是如果我们正在处理文本数据呢?这就是 NLPAug 的用武之地。该库可以通过替换或插入语义关联的单词来扩充文本。通过使用像 BERT 这样的预训练语言模型来进行数据的增强,这是一种强大的方法,因为它考虑了单词的上下文。根据设置的参数,可以使用前 n 个相似词来修改文本。

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

  1. pip install nlpaug

使用样例

  1. import nlpaug.augmenter.word as naw
  2. # main parameters to adjust
  3. ACTION = 'substitute' # or use 'insert'
  4. TOP_K = 15 # randomly draw from top 15 suggested words
  5. AUG_P = 0.40 # augment 40% of words within text
  6. aug_bert = naw.ContextualWordEmbsAug(
  7. model_path='bert-base-uncased',
  8. action=ACTION,
  9. top_k=TOP_K,
  10. aug_p=AUG_P
  11. )
  12. text = """
  13. Come into town with me today to buy food!
  14. """
  15. augmented_text = aug_bert.augment(text, n=3) # n: num. of outputs
  16. print(augmented_text)

结果

  1. ORIGINAL:
  2. Come into town with me today to buy food!
  3. OUTPUTS:
  4. • drove into denver with me today to purchase groceries!
  5. • head off town with dad today to buy coffee!
  6. • come up shop with mom today to buy lunch!

假设你正在使用一个具有 15k 条正面评论和仅 4k 条负面评论的数据集上训练监督分类模型。 严重不平衡的数据集会在训练期间产生对多数类(正面评价)的模型偏差。

简单地复制少数类的示例(负面评论)不会向模型添加任何新信息。 相反,利用 NLPAug 的高级文本增强功能来增加多样性的少数类。 该技术已被证明可以提高 AUC 和 F1-Score。

结论

作为数据科学家、Kaggle 参与者或一般程序员,重要的是我们需要找到更多的工具来简化我们的工作流程。这样可以利用这些库来解决问题,增强我们的数据集,并花更多时间思考解决方案而不是编写代码。

原文链接:https://www.toutiao.com/a7046197563833319975/

延伸 · 阅读

精彩推荐