<commit_before><?php
if (!defined('BGM_API')) exit;

// set_error_handler(function () {
//   ob_end_clean();
//   output_error(500, '处理时发生未知错误');
//   exit;
// });

function base64_file($src, $type) {
  $im = file_get_contents(BGM_ROOT . $src);
  $imdata = base64_encode($im);

  echo 'data:', $type, ';base64,', $imdata;
}

function find_dom($dom, $query, $first = true) {
  $arr = $dom->find($query);
  if ($first) {
    if (!isset($arr[0])) {
      return false;
    }

    return $arr[0];
  } else {
    return $arr;
  }
}

function parse_bgm($url, $dom) {
  $bgm = [];
  $bgm['url'] = $url;

  $titleDom = find_dom($dom, '#headerSubject h1 a');
  if (!$titleDom) {
    throw new Exception('获取信息时失败');
  }

  $bgm['title'] = $titleDom->getPlainText();
  $bgm['subtitle'] = $titleDom->getAttr('title');

  $scoreDom = find_dom($dom, '.global_score .number');
  $bgm['score'] = explode('.', $scoreDom->getPlainText());

  $summary = find_dom($dom, '#subject_summary');
  if ($summary) {
    $bgm['summary'] = SvgText::make(htmlspecialchars($summary->getPlainText()), 10, 14, 380);
    $bgm['offset_tag'] = SvgText::$offset;
  } else {
    $bgm['summary'] = '';
    $bgm['offset_tag'] = 0;
  }

  $tagDom = find_dom($dom, '.subject_tag_section .inner');
  $bgm['tag'] = [];
  if ($tagDom) {
    $tagA = $tagDom->find('a');
    $tagB = $tagDom->find('small');
    $bgm['tagCount'] = min(count($tagA), 8);
    for ($i = 0; $i < $bgm['tagCount']; $i++) {
      $bgm['tag'][$i] = [
        'name' => $tagA[$i]->getPlainText(),
        'count' => $tagB[$i]->getPlainText()
      ];<commit_msg>Fix: extra count in tag name<commit_after><?php
if (!defined('BGM_API')) exit;

// set_error_handler(function () {
//   ob_end_clean();
//   output_error(500, '处理时发生未知错误');
//   exit;
// });

function base64_file($src, $type) {
  $im = file_get_contents(BGM_ROOT . $src);
  $imdata = base64_encode($im);

  echo 'data:', $type, ';base64,', $imdata;
}

function find_dom($dom, $query, $first = true) {
  $arr = $dom->find($query);
  if ($first) {
    if (!isset($arr[0])) {
      return false;
    }

    return $arr[0];
  } else {
    return $arr;
  }
}

function parse_bgm($url, $dom) {
  $bgm = [];
  $bgm['url'] = $url;

  $titleDom = find_dom($dom, '#headerSubject h1 a');
  if (!$titleDom) {
    throw new Exception('获取信息时失败');
  }

  $bgm['title'] = $titleDom->getPlainText();
  $bgm['subtitle'] = $titleDom->getAttr('title');

  $scoreDom = find_dom($dom, '.global_score .number');
  $bgm['score'] = explode('.', $scoreDom->getPlainText());

  $summary = find_dom($dom, '#subject_summary');
  if ($summary) {
    $bgm['summary'] = SvgText::make(htmlspecialchars($summary->getPlainText()), 10, 14, 380);
    $bgm['offset_tag'] = SvgText::$offset;
  } else {
    $bgm['summary'] = '';
    $bgm['offset_tag'] = 0;
  }

  $tagDom = find_dom($dom, '.subject_tag_section .inner');
  $bgm['tag'] = [];
  if ($tagDom) {
    $tagA = $tagDom->find('span');
    $tagB = $tagDom->find('small');
    $bgm['tagCount'] = min(count($tagA), 8);
    for ($i = 0; $i < $bgm['tagCount']; $i++) {
      $bgm['tag'][$i] = [
        'name' => $tagA[$i]->getPlainText(),
        'count' => $tagB[$i]->getPlainText()
      ];
{'fraction_non_alphanumeric': 0.2418648310387985, 'fraction_numerical': 0.01376720901126408, 'mean_word_length': 3.6603498542274053, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1941570', 'n_tokens_mistral': 1315, 'n_tokens_neox': 1292, 'n_words': 221}