Skip to content

Instantly share code, notes, and snippets.

@simecek
Created August 17, 2021 22:44
Show Gist options
  • Save simecek/5e45763b612c51a0d0600477d737f05d to your computer and use it in GitHub Desktop.
Save simecek/5e45763b612c51a0d0600477d737f05d to your computer and use it in GitHub Desktop.
Gramformer_demo.ipynb
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "Gramformer_demo.ipynb",
"provenance": [],
"authorship_tag": "ABX9TyN8Fm2zKogS/LU1TA3eVF0m",
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/simecek/5e45763b612c51a0d0600477d737f05d/gramformer_demo.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "code",
"metadata": {
"id": "4N5EKN1xRei5"
},
"source": [
"!pip3 install pip==20.1.1 \n",
"# IMPORTANT NOTE: (If install runs endlessly resolving package versions in for instance colab, refer to issue #22 - https://github.com/PrithivirajDamodaran/Gramformer/issues/22)\n",
"!pip3 install -U git+https://github.com/PrithivirajDamodaran/Gramformer.git"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "wHre8nLoRxaE",
"outputId": "3957fe5a-b8c9-4ae0-ec08-ec838b67287f"
},
"source": [
"from gramformer import Gramformer\n",
"import torch\n",
"\n",
"def set_seed(seed):\n",
" torch.manual_seed(seed)\n",
" if torch.cuda.is_available():\n",
" torch.cuda.manual_seed_all(seed)\n",
"\n",
"set_seed(1212)\n",
"\n",
"\n",
"gf = Gramformer(models = 1, use_gpu=False) # 1=corrector, 2=detector"
],
"execution_count": 3,
"outputs": [
{
"output_type": "stream",
"text": [
"[Gramformer] Grammar error correct/highlight model loaded..\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "_8E_luZeR1tM",
"outputId": "4758ade1-2c10-4306-e1a3-47c2a90c8b0d"
},
"source": [
"influent_sentences = [\n",
" \"He are moving here.\",\n",
" \"I am doing fine. How is you?\",\n",
" \"How is they?\",\n",
" \"Matt like fish\",\n",
" \"the collection of letters was original used by the ancient Romans\",\n",
" \"We enjoys horror movies\",\n",
" \"Anna and Mike is going skiing\",\n",
" \"I walk to the store and I bought milk\",\n",
" \" We all eat the fish and then made dessert\",\n",
" \"I will eat fish for dinner and drink milk\",\n",
" \"what be the reason for everyone leave the company\",\n",
"] \n",
"\n",
"for influent_sentence in influent_sentences:\n",
" corrected_sentences = gf.correct(influent_sentence, max_candidates=1)\n",
" print(\"[Input] \", influent_sentence)\n",
" for corrected_sentence in corrected_sentences:\n",
" print(\"[Correction] \",corrected_sentence)\n",
" print(\"-\" *100)"
],
"execution_count": 4,
"outputs": [
{
"output_type": "stream",
"text": [
"[Input] He are moving here.\n",
"[Correction] ('He is moving here.', -31.02849578857422)\n",
"----------------------------------------------------------------------------------------------------\n",
"[Input] I am doing fine. How is you?\n",
"[Correction] ('I am doing fine, how are you?', -37.671016693115234)\n",
"----------------------------------------------------------------------------------------------------\n",
"[Input] How is they?\n",
"[Correction] ('How are they?', -24.648284912109375)\n",
"----------------------------------------------------------------------------------------------------\n",
"[Input] Matt like fish\n",
"[Correction] ('Matt likes fish.', -33.768829345703125)\n",
"----------------------------------------------------------------------------------------------------\n",
"[Input] the collection of letters was original used by the ancient Romans\n",
"[Correction] ('the collection of letters was original used by the ancient Romans', -63.627716064453125)\n",
"----------------------------------------------------------------------------------------------------\n",
"[Input] We enjoys horror movies\n",
"[Correction] ('We enjoy horror movies.', -31.77159881591797)\n",
"----------------------------------------------------------------------------------------------------\n",
"[Input] Anna and Mike is going skiing\n",
"[Correction] ('Anna and Mike are going skiing.', -42.5970458984375)\n",
"----------------------------------------------------------------------------------------------------\n",
"[Input] I walk to the store and I bought milk\n",
"[Correction] ('I ran to the store and I bought milk.', -47.339088439941406)\n",
"----------------------------------------------------------------------------------------------------\n",
"[Input] We all eat the fish and then made dessert\n",
"[Correction] ('We all ate the fish and then made dessert.', -54.92893981933594)\n",
"----------------------------------------------------------------------------------------------------\n",
"[Input] I will eat fish for dinner and drink milk\n",
"[Correction] ('I will eat fish for dinner and drink milk.', -44.48554229736328)\n",
"----------------------------------------------------------------------------------------------------\n",
"[Input] what be the reason for everyone leave the company\n",
"[Correction] ('what is the reason for everyone leaving the company?', -45.09092712402344)\n",
"----------------------------------------------------------------------------------------------------\n"
],
"name": "stdout"
}
]
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment