{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "ba8f64d9-3fda-488c-93a4-ae7f1e0b9e97",
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "4a03c1cb-feb5-4a33-92d2-6c6ed92d0645",
   "metadata": {},
   "outputs": [],
   "source": [
    "def draw_histogram(filename, bin_range, bin_width, dataset_name):\n",
    "    \"\"\"\n",
    "    绘制频率分布直方图\n",
    "\n",
    "    Args:\n",
    "        filename: 数据文件路径\n",
    "        bin_range: 区间范围，一个长度为2的列表，表示最小值和最大值\n",
    "        bin_width: 区间间隔\n",
    "\n",
    "    Returns:\n",
    "        None\n",
    "    \"\"\"\n",
    "\n",
    "    # 读取数据\n",
    "    data = np.loadtxt(filename)\n",
    "\n",
    "    # 计算bin的个数\n",
    "    num_bins = int((bin_range[1] - bin_range[0]) / bin_width)\n",
    "\n",
    "    # 绘制直方图\n",
    "    plt.hist(data, bins=num_bins, range=bin_range)\n",
    "\n",
    "    # 设置标题和坐标轴标签\n",
    "    plt.title('Quality Distribution of ' + dataset_name)\n",
    "    plt.xlabel('Quality Score (cos-sim of embeddings for each pair)')\n",
    "    plt.ylabel('Frequency')\n",
    "\n",
    "    # 显示图形\n",
    "    plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "55ffc3e1-0807-403a-ae1f-c7520ec9f46f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 28576 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# 示例用法\n",
    "filename = './ccmatrix/CCMatrix.en-zh.sample.sim'  # 替换为你的文件名\n",
    "bin_range = [0.2, 1]  # 设置区间范围\n",
    "bin_width = 0.025  # 设置区间间隔\n",
    "\n",
    "draw_histogram(filename, bin_range, bin_width, \"CCMatrix (ZH-EN)\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "e6385cff-4b14-4177-9aa1-7e01b3af2b8f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 30728 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# 示例用法\n",
    "filename = './translation2019/result.sim'  # 替换为你的文件名\n",
    "bin_range = [0.2, 1]  # 设置区间范围\n",
    "bin_width = 0.025  # 设置区间间隔\n",
    "\n",
    "draw_histogram(filename, bin_range, bin_width, \"nlp_chinese_corpus/translation2019\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "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.10.14"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
