{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Request thành công\n", "Response: {'code': 0, 'message': 'OK', 'result': {'suggestions': [{'startIndex': 8, 'endIndex': 11, 'originalText': 'the', 'suggestion': 'thế', 'confidence': 1.0}, {'startIndex': 12, 'endIndex': 14, 'originalText': 'cậ', 'suggestion': 'cậu', 'confidence': 1.0}]}}\n" ] } ], "source": [ "import json\n", "import requests\n", "\n", "# Thay thế các giá trị dưới đây bằng thông tin của API bạn muốn gọi và mã thông báo (token) của bạn\n", "api_url = \"https://viettelgroup.ai/nlp/api/v1/spell-checking\"\n", "token = \"d3dad67f59d38557460aaeae820e6071\"\n", "\n", "# Dữ liệu bạn muốn gửi trong request POST (nếu có)\n", "data = {\n", " \"sentence\": \"Làm sao the cậ?\"\n", "}\n", "\n", "# Tiêu đề Authorization chứa mã thông báo\n", "headers = {'Content-Type': 'application/json', 'token': token}\n", "\n", "# Thực hiện cuộc gọi POST\n", "response = requests.post(api_url, json=data, headers=headers)\n", "\n", "# Kiểm tra phản hồi từ server\n", "if response.status_code == 200:\n", " print('Request thành công')\n", " print('Response:', response.json())\n", "else:\n", " print('Request thất bại')\n", " print('Status code:', response.status_code)\n", " print('Response:', response.text)" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "api_url = \"https://viettelgroup.ai/nlp/api/v1/spell-checking\"\n", "token = \"d3dad67f59d38557460aaeae820e6071\"\n", "\n", "def spell_correcting(sentence, api_url, token):\n", " # Dữ liệu bạn muốn gửi trong request POST (nếu có)\n", " data = {\n", " \"sentence\": sentence\n", " }\n", "\n", " # Tiêu đề Authorization chứa mã thông báo\n", " headers = {'Content-Type': 'application/json', 'token': token}\n", "\n", " # Thực hiện cuộc gọi POST\n", " response = requests.post(api_url, json=data, headers=headers)\n", "\n", " # Kiểm tra phản hồi từ server\n", " if response.status_code == 200:\n", " return response.json()['result']['suggestions']\n", " else:\n", " return []\n", "\n", "def replace_text(input, api_url, token):\n", " suggestion = spell_correcting(input, api_url, token)\n", " for error in suggestion[::-1]:\n", " if error['confidence'] > 0.7:\n", " input = input[:error[\"startIndex\"]] + input[error[\"startIndex\"]:].replace(error['originalText'], error['suggestion'])\n", " return input\n" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
Unnamed: 0contentscorethumbsUpCountApplication
00không làm được11Messenger
11đăng xuất ra vô lại bị lỗi nhìu11Messenger
22cập nhật lại giùm cái52Messenger
331 sao vì chặn mà vẫn gỡ được10Messenger
44sản năm sao luân51Messenger
..................
9199491994cập nhật không được ạ40Instagram
9199591995sao ko vô dc50Instagram
9199691996chụp đẹp nhưng chưa có tính năng giây nha mong...40Instagram
9199791997điện video bị lỗi10Instagram
9199891998chụp ảnh lưu về máy xem lại ko có10Instagram
\n", "

91999 rows × 5 columns

\n", "
" ], "text/plain": [ " Unnamed: 0 content score \\\n", "0 0 không làm được 1 \n", "1 1 đăng xuất ra vô lại bị lỗi nhìu 1 \n", "2 2 cập nhật lại giùm cái 5 \n", "3 3 1 sao vì chặn mà vẫn gỡ được 1 \n", "4 4 sản năm sao luân 5 \n", "... ... ... ... \n", "91994 91994 cập nhật không được ạ 4 \n", "91995 91995 sao ko vô dc 5 \n", "91996 91996 chụp đẹp nhưng chưa có tính năng giây nha mong... 4 \n", "91997 91997 điện video bị lỗi 1 \n", "91998 91998 chụp ảnh lưu về máy xem lại ko có 1 \n", "\n", " thumbsUpCount Application \n", "0 1 Messenger \n", "1 1 Messenger \n", "2 2 Messenger \n", "3 0 Messenger \n", "4 1 Messenger \n", "... ... ... \n", "91994 0 Instagram \n", "91995 0 Instagram \n", "91996 0 Instagram \n", "91997 0 Instagram \n", "91998 0 Instagram \n", "\n", "[91999 rows x 5 columns]" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import pandas as pd\n", "\n", "data = pd.read_csv(\"../data/clean_data_v2.csv\")\n", "data" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "from tqdm import tqdm" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "100%|██████████| 91999/91999 [4:01:07<00:00, 6.36it/s] \n" ] } ], "source": [ "tqdm.pandas()\n", "data['spell_corrected_content'] = data['content'].progress_apply(lambda x: replace_text(x, api_url, token))" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "# data.to_csv(\"../data/spell_corrected_data_v3.csv\")" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
Unnamed: 0contentscorethumbsUpCountApplicationspell_corrected_content
00không làm được11Messengerkhông làm được
11đăng xuất ra vô lại bị lỗi nhìu11Messengerđăng xuất ra vô lại bị lỗi nhiều
22cập nhật lại giùm cái52Messengercập nhật lại giùm cái
331 sao vì chặn mà vẫn gỡ được10Messenger1 sào vì chăn mà vẫn gỡ được
44sản năm sao luân51Messengersạn năm sao luận
.....................
9199491994cập nhật không được ạ40Instagramcập nhật không được ạ
9199591995sao ko vô dc50Instagramsao không vô được
9199691996chụp đẹp nhưng chưa có tính năng giây nha mong...40Instagramchụp đẹp nhưng chưa có tính năng giấy nhà mong...
9199791997điện video bị lỗi10Instagramđiện video bị lỗi
9199891998chụp ảnh lưu về máy xem lại ko có10Instagramchụp ảnh lưu về máy xem lại không có
\n", "

91999 rows × 6 columns

\n", "
" ], "text/plain": [ " Unnamed: 0 content score \\\n", "0 0 không làm được 1 \n", "1 1 đăng xuất ra vô lại bị lỗi nhìu 1 \n", "2 2 cập nhật lại giùm cái 5 \n", "3 3 1 sao vì chặn mà vẫn gỡ được 1 \n", "4 4 sản năm sao luân 5 \n", "... ... ... ... \n", "91994 91994 cập nhật không được ạ 4 \n", "91995 91995 sao ko vô dc 5 \n", "91996 91996 chụp đẹp nhưng chưa có tính năng giây nha mong... 4 \n", "91997 91997 điện video bị lỗi 1 \n", "91998 91998 chụp ảnh lưu về máy xem lại ko có 1 \n", "\n", " thumbsUpCount Application \\\n", "0 1 Messenger \n", "1 1 Messenger \n", "2 2 Messenger \n", "3 0 Messenger \n", "4 1 Messenger \n", "... ... ... \n", "91994 0 Instagram \n", "91995 0 Instagram \n", "91996 0 Instagram \n", "91997 0 Instagram \n", "91998 0 Instagram \n", "\n", " spell_corrected_content \n", "0 không làm được \n", "1 đăng xuất ra vô lại bị lỗi nhiều \n", "2 cập nhật lại giùm cái \n", "3 1 sào vì chăn mà vẫn gỡ được \n", "4 sạn năm sao luận \n", "... ... \n", "91994 cập nhật không được ạ \n", "91995 sao không vô được \n", "91996 chụp đẹp nhưng chưa có tính năng giấy nhà mong... \n", "91997 điện video bị lỗi \n", "91998 chụp ảnh lưu về máy xem lại không có \n", "\n", "[91999 rows x 6 columns]" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data" ] }, { "cell_type": "code", "execution_count": 73, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Success!!!\n" ] }, { "data": { "text/plain": [ "'Làm sao thế cậu'" ] }, "execution_count": 73, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sentence = \"Làm sao thê cậ\"\n", "replace_text(sentence, api_url, token)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "base", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.11.4" }, "orig_nbformat": 4 }, "nbformat": 4, "nbformat_minor": 2 }