【问题标题】:How I can extract ordered questions and answers from a block of plain text如何从纯文本块中提取有序的问题和答案
【发布时间】:2019-05-30 14:55:41
【问题描述】:

问题也可用于纯文本。我希望能够使用 PHP,Regex。但是,因为我的Regex信息不够,我尝试用PHP函数来做。

纯文本示例: (从第 12 个问题开始 -> 15 个问题,这里总共 4 个问题)

12.一名 47 岁的男子抱怨上牙部分脱落。患者的病史表明因外伤导致牙齿脱落 持续3个月前。 11和12丢失了。 13、21 和 22 是 被 2/3 摧毁并用填充物恢复。咬合是正颌的。 哪种义齿结构最适合该患者, 考虑到他作为讲师的职业? A.瓷熔金属 牙桥 B. 塑料牙桥 C. 固定式牙桥 (bugel) 带附件的可摘局部义齿 D.可摘局部薄片 上颌假牙 E.Swaged-soldered 金属牙桥 多面中间部分 13.一个 7 岁的男孩被诊断​​出患有外伤 流行性腮腺炎(腮腺炎)。说出最可能的并发症 疾病: A.睾丸炎 B.结肠炎 C.皮炎 D.肺炎 E. 胆囊炎 14. 预防性检查 40 岁男子 呈现以下变化:边缘牙龈增大, 圆环状,发绀,用牙齿接触时有轻微出血 探测;没有痛苦。用卢戈氏碘染色牙龈 溶液导致粘膜呈浅棕色。使 诊断: A.慢性卡他性牙龈炎 B.急性卡他性牙龈炎 C.慢性卡他性牙龈炎加重 D.慢性增生 牙龈炎 E.全身性牙周炎 15.一个 4 岁男孩被 诊断为上颌急性化脓性骨膜炎 起源于64牙。选择最佳治疗策略: A.64 拔牙、骨膜切开术、药物治疗 B.64 拔牙、抗炎药物治疗 C.Endodonological 治疗64颗牙齿,消炎药物治疗 D.64颗牙的牙髓治疗,骨膜切开术 E.骨膜切开术、抗炎药物治疗

  • 每个问题的开头都有一个问题编号。
  • 问题通常有 5 或 4 个选项

我做了什么?

 function gettingQuestionAndOptions($string, $start, $end)
    {
        $string = " " . $string;
        $ini = strpos($string, $start);
        if ($ini == 0) return "";
        $ini += strlen($start);
        $len = strpos($string, $end, $ini) - $ini;

        return substr($string, $ini, $len);
    }

如何使用它?

if (gettingQuestionAndOptions($text, '?', "A. ")) {

    $parsedQuestion = gettingQuestionAndOptions($text, '', "?");
    $parsedA = gettingQuestionAndOptions($text, 'A. ', "B. ");
    $parsedB = gettingQuestionAndOptions($text, 'B. ', "C. ");
    $parsedC = gettingQuestionAndOptions($text, 'C. ', "D. ");
    $endQuestion = "?";

} else if (gettingQuestionAndOptions($text, ':', "A. ")) {

    $parsedQuestion = gettingQuestionAndOptions($text, '', ":");
    $parsedA = gettingQuestionAndOptions($text, 'A. ', "B. ");
    $parsedB = gettingQuestionAndOptions($text, 'B. ', "C. ");
    $parsedC = gettingQuestionAndOptions($text, 'C. ', "D. ");
    $endQuestion = ":";

} else {
}

结果:

我试过了。 4 小时,但我仍然得到不规则的结果。如果问题中有数字或AB,我会得到更糟糕的结果。


  1. 我怎样才能做得更好?还是我的错在哪里?
  2. 我可以使用 Regex 获得更正确的数据吗?

【问题讨论】:

  • 如果您一直在处理全文,您可能会知道您总是从“1”开始,从而提高您的结果。然后下一场比赛将是“2”。等
  • 是的,主要是第一个问题开始。但是我用php得到的结果是不均匀的。例如,它采取中间的问题或选项不出现。 @miken32
  • See this PHP demo at 3v4l.org 有正则表达式的想法或pattern demo at regex101。更好的是,获得更正确的数据取决于您的输入的样子。
  • 如何删除问题内容和选项(数组到字符串)? @bobblebubble

标签: php regex strpos strlen


【解决方案1】:

假设您的文本始终以数字开头,将它们分组应该很简单,只需跟踪您当前的问题,然后搜索下一个问题。这将有助于减少误报。

<?php
$data = "12.A 47-year-old man complains of partial loss of his upper teeth. The patient’s medi- cal history states loss of teeth due to trauma sustained 3 months ago. 11 and 12 are lost. 13, 21, and 22 are destroyed by 2/3 and restored with?llings. Occlusion is orthognathic. What denture construction would be optimal for this patient, considering his occupation as a lecturer? A.Porcelain-fused-to-metal dental bridge B.Plastic dental bridge C.Clasp-retained (bugel) removable partial denture with attachments D.Removable partial laminar denture for the upper jaw E.Swaged-soldered metal dental bridge with faceted intermediate part 13.A 7-year-old boy is diagnosed with epi- demic parotitis (mumps). Name the most li- kely complication of this disease: A.Orchitis B.Colitis C.Dermatitis D.Pneumonia E.Cholecystitis14.During preventive examination a 40-year- old man presents with the following changes: marginal gingiva is enlarged, torus-shaped, cyanotic, slightly bleeding when touched wi- th a dental probe; there is no pain. Staining the gums with Lugol’s iodine solution results in light-brown coloring of mucosa. Make the diagnosis: A.Chronic catarrhal gingivitis B.Acute catarrhal gingivitis C.Exacerbation of chronic catarrhal gingivitis D.Chronic hypertrophic gingivitis E.Generalized periodontitis 15.A 4-year-old boy has been diagnosed wi- th acute purulent periostitis of the upper jaw originating from the 64 tooth. Choose the optimal treatment tactics: A.The 64 tooth extraction, periosteotomy, pharmacotherapy B.The 64 tooth extraction, anti-in?ammatory pharmacotherapy C.Endodontological treatment of the 64 tooth, anti-in?ammatory pharmacotherapy D.Endodontological treatment of the 64 tooth, periosteotomy E.Periosteotomy, anti-in?ammatory pharmacotherapy";
$questions = [];
$question_num = 12;
$previous_question_start = 0;
while (true) {
    $question_start = strpos($data, "$question_num.", $previous_question_start);
    $question_end = strpos($data, (++$question_num) . ".", $question_start);
    if ($question_end === false) {
        // no more matches, this is the last question
        $questions[] = substr($data, $question_start);
        break;
    }
    $questions[] = substr($data, $question_start, $question_end - $question_start);
    $previous_question_start = $question_start;
}

此时,每个问题都包含在一个数组元素中,因此请使用相同的技术在每个问题中搜索您的答案。我们会将结果保存到一个新数组中。

$questions_answers = [];
$answer_choices = ["A", "B", "C", "D", "E"];
foreach ($questions as $q) {
    $qa = [];
    $qa["question"] = substr($q, 0, strpos($q, "$answer_choices[0]."));
    $previous_answer_start = 0;
    foreach ($answer_choices as $i=>$a) {
        $answer_start = strpos($q, $answer_choices[$i]. ".", $previous_answer_start);
        if (empty($answer_choices[$i + 1])) {
            // end of the array, this is the last answer
            $qa["answers"][] = substr($q, $answer_start);
            break;
        }
        $answer_end = strpos($q, $answer_choices[$i + 1] . ".", $answer_start);
        $qa["answers"][] = substr($q, $answer_start, $answer_end - $answer_start);
        $previous_answer_start = $answer_start;
    }
    $questions_answers[] = $qa;
}

print_r($questions_answers);

输出:

Array
(
    [0] => Array
        (
            [question] => 12.A 47-year-old man complains of partial loss of his upper teeth. The patient’s medi- cal history states loss of teeth due to trauma sustained 3 months ago. 11 and 12 are lost. 13, 21, and 22 are destroyed by 2/3 and restored with?llings. Occlusion is orthognathic. What denture construction would be optimal for this patient, considering his occupation as a lecturer? 
            [answers] => Array
                (
                    [0] => A.Porcelain-fused-to-metal dental bridge 
                    [1] => B.Plastic dental bridge 
                    [2] => C.Clasp-retained (bugel) removable partial denture with attachments 
                    [3] => D.Removable partial laminar denture for the upper jaw 
                    [4] => E.Swaged-soldered metal dental bridge with faceted intermediate part 
                )

        )

    [1] => Array
        (
            [question] => 13.A 7-year-old boy is diagnosed with epi- demic parotitis (mumps). Name the most li- kely complication of this disease: 
            [answers] => Array
                (
                    [0] => A.Orchitis 
                    [1] => B.Colitis 
                    [2] => C.Dermatitis 
                    [3] => D.Pneumonia 
                    [4] => E.Cholecystitis
                )

        )

    [2] => Array
        (
            [question] => 14.During preventive examination a 40-year- old man presents with the following changes: marginal gingiva is enlarged, torus-shaped, cyanotic, slightly bleeding when touched wi- th a dental probe; there is no pain. Staining the gums with Lugol’s iodine solution results in light-brown coloring of mucosa. Make the diagnosis: 
            [answers] => Array
                (
                    [0] => A.Chronic catarrhal gingivitis 
                    [1] => B.Acute catarrhal gingivitis 
                    [2] => C.Exacerbation of chronic catarrhal gingivitis 
                    [3] => D.Chronic hypertrophic gingivitis 
                    [4] => E.Generalized periodontitis 
                )

        )

    [3] => Array
        (
            [question] => 15.A 4-year-old boy has been diagnosed wi- th acute purulent periostitis of the upper jaw originating from the 64 tooth. Choose the optimal treatment tactics: 
            [answers] => Array
                (
                    [0] => A.The 64 tooth extraction, periosteotomy, pharmacotherapy 
                    [1] => B.The 64 tooth extraction, anti-in?ammatory pharmacotherapy 
                    [2] => C.Endodontological treatment of the 64 tooth, anti-in?ammatory pharmacotherapy 
                    [3] => D.Endodontological treatment of the 64 tooth, periosteotomy 
                    [4] => E.Periosteotomy, anti-in?ammatory pharmacotherapy
                )

        )

)

我没有对这段代码进行优化,所以它很容易理解,但是简单的字符串操作是一种非常低成本的计算,所以它应该适合日常使用。

附带说明,我注意到您丢失了诸如“f”(f/l 连字)和“fi”(f/i 连字)之类的字符。如果您从数据库或网页中提取此信息,确保您正确处理 UTF-8 字符。

【讨论】:

  • 是否可以将其打印为常规字符串? :) 我试过; $makeJson= json_encode($questions_answers, JSON_FORCE_OBJECT); $toString = json_decode($makeJson); foreach ($toString as $ts) { echo $ts-&gt;answers[1]; } 但我没有成功。
  • 如何在代码中以纯文本形式打印问题和选项? @miken32
  • 这是一个简单的字符串数组,你可以用它做任何你喜欢的事情。 foreach ($questions_answers as $q) {echo $q["question"];}
  • 是的,但我想我说错了。我要求单独打印问题。我需要这个保存到数据库中。所以;是否可以分别打印问题和问题选项? - 每个foreach 操作要写入数据库@miken32
  • 不可能吗?我想将问题和相同问题的选项保存到数据库中。那么我怎样才能得到它呢? // 能不能只把问题的问题和问题的问题作为字符串给出?谢谢你@miken32
猜你喜欢
  • 2017-12-02
  • 1970-01-01
  • 1970-01-01
  • 2022-11-11
  • 1970-01-01
  • 2016-09-07
  • 2021-01-19
  • 2021-09-23
  • 2015-04-03
相关资源
最近更新 更多