gpt4 book ai didi

php - 确定不同类型文本中的章节编号

转载 作者:IT王子 更新时间:2023-10-28 23:52:34 25 4
gpt4 key购买 nike

我正在从与小说相关的帖子中提取标题。目的是通过使用正则表达式来确定帖子是关于哪一章的。每个站点使用不同的方式来识别章节。以下是最常见的情况:

$title = 'text chapter 25.6 text'; // c25.6
$title = 'text chapters 23, 24, 25 text'; // c23-25
$title = 'text chapters 23+24+25 text'; // c23-25
$title = 'text chapter 23, 25 text'; // c23 & 25
$title = 'text chapter 23 & 24 & 25 text'; // c23-25
$title = 'text c25.5-30 text'; // c25.5-30
$title = 'text c99-c102 text'; // c99-102
$title = 'text chapter 99 - chapter 102 text'; // c99-102
$title = 'text chapter 1 - 3 text'; // c1-3
$title = '33 text chapter 1, 2 text 3'; // c1-2
$title = 'text v2c5-10 text'; // c5-10
$title = 'text chapters 23, 24, 25, 29, 31, 32 text'; // c23-25 & 29 & 31-32

章节编号总是列在标题中,只是在上面显示的不同变体中。

到目前为止我所拥有的

到目前为止,我有一个正则表达式来确定章节的单个案例,例如:

$title = '9 text chapter 25.6 text'; // c25.6

使用此代码(尝试 ideone ):

function get_chapter($text, $terms) {

if (empty($text)) return;
if (empty($terms) || !is_array($terms)) return;

$values = false;

$terms_quoted = array();
foreach ($terms as $term)
$terms_quoted[] = preg_quote($term, '/');

// search for matches in $text
// matches with lowercase, and ignores white spaces...
if (preg_match('/('.implode('|', $terms_quoted).')\s*(\d+(\.\d+)?)/i', $text, $matches)) {
if (!empty($matches[2]) && is_numeric($matches[2])) {
$values = array(
'term' => $matches[1],
'value' => $matches[2]
);
}
}

return $values;
}

$text = '9 text chapter 25.6 text'; // c25.6
$terms = array('chapter', 'chapters');
$chapter = get_chapter($text, $terms);

print_r($chapter);

if ($chapter) {
echo 'Chapter is: c'. $chapter['value'];
}

如何使用上面列出的其他示例进行此操作?鉴于这个问题的复杂性,如果符合条件,我将奖励 200 分。

最佳答案

逻辑

我建议采用以下结合正则表达式和通用字符串处理逻辑的方法:

  • 使用 preg_match使用适当的正则表达式来匹配以 $terms 中的关键字开头的整个文本 block 的第一次出现数组,直到与术语相关的最后一个数字(+ 可选部分字母)
  • 获得匹配后,创建一个包含输入字符串、匹配值和后处理匹配的数组
  • 可以通过删除连字符数字之间的空格并在数字与 + 连接的情况下重建数字范围来完成后处理。 , &,字符。这需要一个多步骤操作:1)匹配前一个整体匹配中的连字符分隔的子字符串,并修剪掉不必要的零和空格,2)将数字 block 拆分为单独的项目并将它们传递给一个单独的函数,该函数将生成数字范围
  • buildNumChain($arr)函数将创建数字范围,如果一个字母跟在数字后面,会将其转换为 section X后缀。

解决方案

你可以使用

$strs = ['c0', 'c0-3', 'c0+3', 'c0 & 9', 'c0001, 2, 03', 'c01-03', 'c1.0 - 2.0', 'chapter 2A Hello', 'chapter 2AHello', 'chapter 10.4c', 'chapter 2B', 'episode 23.000 & 00024', 'episode 23 & 24', 'e23 & 24', 'text c25.6 text', '001 & 2 & 5 & 8-20 & 100 text chapter 25.6 text 98', 'hello 23 & 24', 'ep 1 - 2', 'chapter 1 - chapter 2', 'text chapter 25.6 text', 'text chapters 23, 24, 25 text','text chapter 23, 25 text', 'text chapter 23 & 24 & 25 text','text c25.5-30 text', 'text c99-c102 text', 'text chapter 1 - 3 text', '33 text chapter 1, 2 text 3','text chapters 23, 24, 25, 29, 31, 32 text', 'c19 & c20', 'chapter 25.6 & chapter 29', 'chapter 25+c26', 'chapter 25 + 26 + 27'];
$terms = ['episode', 'chapter', 'ch', 'ep', 'c', 'e', ''];

usort($terms, function($a, $b) {
return strlen($b) - strlen($a);
});

$chapter_main_rx = "\b(?|" . implode("|", array_map(function ($term) {
return strlen($term) > 0 ? "(" . substr($term, 0, 1) . ")(" . substr($term, 1) . "s?)": "()()" ;},
$terms)) . ")\s*";
$chapter_aux_rx = "\b(?:" . implode("|", array_map(function ($term) {
return strlen($term) > 0 ? substr($term, 0, 1) . "(?:" . substr($term, 1) . "s?)": "" ;},
$terms)) . ")\s*";

$reg = "~$chapter_main_rx((\d+(?:\.\d+)?(?:[A-Z]\b)?)(?:\s*(?:[,&+-]|and)\s*(?:$chapter_aux_rx)?(?4))*)~ui";

foreach ($strs as $s) {
if (preg_match($reg, $s, $m)) {
$p3 = preg_replace_callback(
"~(\d*(?:\.\d+)?)([A-Z]?)\s*-\s*(?:$chapter_aux_rx)?|(\d+(?:\.\d+)?(?:[A-Z]\b)?)(?:\s*(?:[,&+]|and)\s*(?:$chapter_aux_rx)?(?1))*~ui", function($x) use ($chapter_aux_rx) {
return (isset($x[3]) && strlen($x[3])) ? buildNumChain(preg_split("~\s*(?:[,&+]|and)\s*(?:$chapter_aux_rx)?~ui", $x[0]))
: ((isset($x[1]) && strlen($x[1])) ? ($x[1] + 0) : "") . ((isset($x[2]) && strlen($x[2])) ? ord(strtolower($x[2])) - 96 : "") . "-";
}, $m[3]);
print_r(["original" => $s, "found_match" => trim($m[0]), "converted" => $m[1] . $p3]);
echo "\n";
} else {
echo "No match for '$s'!\n";

}
}

function buildNumChain($arr) {
$ret = "";
$rngnum = "";
for ($i=0; $i < count($arr); $i++) {
$val = $arr[$i];
$part = "";
if (preg_match('~^(\d+(?:\.\d+)?)([A-Z]?)$~i', $val, $ms)) {
$val = $ms[1];
if (!empty($ms[2])) {
$part = ' part ' . (ord(strtolower($ms[2])) - 96);
}
}
$val = $val + 0;
if (($i < count($arr) - 1) && $val == ($arr[$i+1] + 0) - 1) {
if (empty($rngnum)) {
$ret .= ($i == 0 ? "" : " & ") . $val;
}
$rngnum = $val;
} else if (!empty($rngnum) || $i == count($arr)) {
$ret .= '-' . $val;
$rngnum = "";
} else {
$ret .= ($i == 0 ? "" : " & ") . $val . $part;
}
}
return $ret;
}

PHP demo .

要点

  • 匹配 cchapter/chapters后面有数字,只捕获 c和数字
  • 找到匹配项后,处理包含数字序列的组 2
  • 全部 <number>-c?<number>子字符串应该去掉空格和 c之前/之间的数字和
  • 全部 ,/& - 分隔的数字应使用 buildNumChain 进行后处理从连续数字中生成范围(假定为整数)。

如果$terms = ['episode', 'chapter', 'ch', 'ep', 'c', 'e', ''],主正则表达式将如下所示:

'~(?|(e)(pisodes?)|(c)(hapters?)|(c)(hs?)|(e)(ps?)|(c)(s?)|(e)(s?)|()())\s*((\d+(?:\.\d+)?(?:[A-Z]\b)?)(?:\s*(?:[,&+-]|and)\s*(?:(?:e(?:pisodes?)|c(?:hapters?)|c(?:hs?)|e(?:ps?)|c(?:s?)|e(?:s?)|)\s*)?(?4))*)~ui'

regex demo .

模式详情

  • (?|(e)(pisodes?)|(c)(hapters?)|(c)(hs?)|(e)(ps?)|(c)(s?)|(e)(s?)|()()) - 一个分支重置组,它捕获搜索词的第一个字母并将剩余的词捕获到强制组 2。如果有一个空词,()()添加以确保组中的分支包含相同数量的组
  • \s* - 0+ 个空格
  • ((\d+(?:\.\d+)?(?:[A-Z]\b)?)(?:\s*(?:[,&+-]|and)\s*c?(?3))*) - 第 2 组:
    • (\d+(?:\.\d+)?(?:[A-Z]\b)?) - 第 3 组:1+ 位,后跟 . 的可选序列, 1+ 个数字,然后是一个可选的 ASCII 字母,后跟一个非单词字符或字符串结尾(注意不区分大小写的修饰符将使 [A-Z] 也匹配小写 ASCII 字母)
    • (?:\s*(?:[,&+-]|and)\s*(?:(?:e(?:pisodes?)|c(?:hapters?)|c(?:hs?)|e(?:ps?)|c(?:s?)|e(?:s?)|)\s*)?(?4))* - 零个或多个序列
      • \s*(?:[,&+-]|and)\s* - 一个 , , & , + , -and包含可选的 0+ 个空格
      • (?:e(?:pisodes?)|c(?:hapters?)|c(?:hs?)|e(?:ps?)|c(?:s?)|e(?:s?)|) - 添加可选复数结尾的任何术语s
      • (?4) - 第 4 组模式递归/重复

当正则表达式匹配时,组 1 的值为 c ,所以这将是结果的第一部分。那么,

 "~(\d*(?:\.\d+)?)([A-Z]?)\s*-\s*(?:$chapter_aux_rx)?|(\d+(?:\.\d+)?(?:[A-Z]\b)?)(?:\s*(?:[,&+]|and)\s*(?:$chapter_aux_rx)?(?1))*~ui"

preg_replace_callback内部使用删除 - 之间的空格(如果有)和术语(如果有)后跟 0+ 个空白字符,如果第 1 组匹配,则匹配以

"~\s*(?:[,&+]|and)\s*(?:$chapter_aux_rx)?~ui"

正则表达式(它匹配 &,+and 在可选的 0+ 空格后跟 0+ 空格,然后是可选字符串,术语后跟 0+ 空格)并且数组是传递给 buildNumChain构建结果字符串的函数。

关于php - 确定不同类型文本中的章节编号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51362947/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com