创新实训 (九)CodeForces 数据和微调数据处理

Codeforces 数据获取

Codeforces的题目中存在一些数学公式,所以处理的时候需要比较小心的对其进行处理。

首先是题面数据,在 CF 当中标识一道题目的方式是 problemSet 与 problemId。其中 problemSet 是一个数字,而 problemId 是一个字母。

另外需要注意的是 CF 题面中存在许多数学公式,所以需要对这些公式进行处理,所以需要比较小心进行处理。将 三个美元符号$$$ 替换为 $。

urllib.request和BeautifulSoup4库对题面信息进行爬取,题面数据获取:

之后比较麻烦的是如何获取正确的代码,在 CF 当中,无法直接对应一个题目查看代码,只能根据比赛去进行查看。但是比赛的编号即为 problemSet。

对于一个比赛,其提交信息列表如下:

这里为了获取正确的代码,我们主要关注 提交编号、提交状态以及题目这三列,这样,我们可以使用爬虫获得这个页面,之后根据这三列的信息,来对数据进行获取。

加载比赛提交页面,从中获取 AC 代码的编号:

再去根据编号,获取 AC 的代码。

微调数据处理

训练数据为jsonl格式,每一行的数据格式如下,其中chat_rounds字段是必需的。

{
    "id":0,
    "data_name":"code-helper",
    "chat_rounds":[
        {
            "role": "system",
            "content": "你是一个智能代码助手,可以回复用户与代码相关的问题"
        },
        {
            "role": "human",
            "content": "写一个快速排序"
        },
        {
            "role": "bot",
            "content": "以下是一个快速排序算法xxxxxx"
        },
        {
            "role": "human",
            "content": "解释一下这段代码"
        },
        {
            "role": "bot",
            "content": "好的,这段代码xxx"
        }
    ]
}

所以需要将数据处理成为上面的形式,而爬虫获取的数据的形式为:question 与 solution 的词对形式,所以我们需要先将数据处理成上面的形式。

import os
import json

jsonl_file_path = os.path.join(os.getcwd(), 'output.jsonl')
tot = 0

prompt = { "role": "system","content": "You are an intelligent code assistant who can respond to users' questions related to code"}

prompt1 = {"role":"human","content":"1"}
prompt2 = {"role":"bot","content":"2"}

ls = []
with open(jsonl_file_path, 'r') as f:
  for line in f:
    da = json.loads(line)
    if len(da['solutions']) == 0:
      continue
    result = {
      "id": tot,
      "data_name": "code-helper",
      "chat_rounds": []
    }
    result["chat_rounds"].append(json.dumps(prompt))

    prompt1["content"] = da['question']
    prompt2["content"] = da['solutions'][0]

    result["chat_rounds"].append(json.dumps(prompt1))
    result["chat_rounds"].append(json.dumps(prompt2))

    ls.append(result)
    
with open("data.jsonl", "a+") as f:
  for data in ls:
    json.dump(data, f)
    f.write("\n")

整理后数据为:

{'id': 0,
 'data_name': 'code-helper',
 'chat_rounds': [{'role': 'system',
   'content': "You are an intelligent code assistant who can respond to users' questions related to code"},
  {'role': 'human',
   'content': 'Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.You may assume that each input would have exactly one solution, and you may not use the same element twice.You can return the answer in any order.'},
  {'role': 'bot',
   'content': "INF = 10000000000.0\nmax_n = 50\nmax_k = 2000\n\ndef main():\n\t(n, s, k) = map(int, input().split())\n\ts -= 1\n\tbuf = [''] * (max_n + 1)\n\tdp = [[0 for i in range(max_n + 1)] for j in range(max_k + 1)]\n\tr = list(map(int, input().split()))\n\tc = input()\n\tanswer = INF\n\tfor i in range(len(c)):\n\t\tbuf[i] = c[i]\n\tfor i in range(k, -1, -1):\n\t\tfor j in range(n):\n\t\t\tdp[i][j] = INF\n\tfor j in range(n):\n\t\tvalue = abs(j - s)\n\t\tif k - r[j] <= 0:\n\t\t\tanswer = min(answer, value)\n\t\telse:\n\t\t\tdp[k - r[j]][j] = value\n\tfor i in range(k, 0, -1):\n\t\tfor j in range(n):\n\t\t\tif dp[i][j] < INF:\n\t\t\t\tfor l in range(n):\n\t\t\t\t\tif buf[j] != buf[l] and r[j] < r[l]:\n\t\t\t\t\t\tvalue = dp[i][j] + abs(j - l)\n\t\t\t\t\t\tif i - r[l] <= 0:\n\t\t\t\t\t\t\tanswer = min(answer, value)\n\t\t\t\t\t\telse:\n\t\t\t\t\t\t\tdp[i - r[l]][l] = min(dp[i - r[l]][l], value)\n\tif answer == INF:\n\t\tprint(-1)\n\t\treturn\n\tprint(answer)\n\ndef __starting_point():\n\tmain()\n__starting_point()\n"}]}
posted @ 2024-06-23 23:50  asuldb  阅读(18)  评论(0编辑  收藏  举报