Commit 037cfa57 authored by ROBERT PATRICK CAREY III's avatar ROBERT PATRICK CAREY III 🗿

Improved: lesson_pathauto title matching

parent 3e98e0b0
......@@ -48,16 +48,21 @@ function _lesson_pathauto_extract_lesson_from_title($title){
*/
/* Lesson keywords */
$keywords = '/^(lesson|section|chapter|unit|exercise)/i';
$keywords = '/^(lesson|section|chapter|unit|exercise|example)/i';
/* Title delimiter */
$split = '/-|:/';
$split = '/-|:|\s/';
/* Try to convert non-latin characters to latin (α => a) */
if(class_exists('Normalizer')){
$title = Normalizer::normalize($title);
}
/* if - starts with a lesson keyword */
/* else if - starts with lesson number */
/* else - don't touch */
if(preg_match($keywords, $title)){
$tmp = preg_replace($keywords, "", $title);
$tmp = trim(preg_replace($keywords, "", $title));
$tmp = preg_split($split, $tmp);
$lesson = $tmp[0];
} else if(preg_match('/^\d/', $title)){
......@@ -65,25 +70,22 @@ function _lesson_pathauto_extract_lesson_from_title($title){
/* Replacements for non-alphanumeric and a few extras */
$replacements = '/[^\d\w\.\-\:\s]/i';
/* Fuzzy match numeric groups (inc. spaces) */
$fuzzy_match = '/[\d|\w|\.|\s]+/i';
/* Hard match number groups (exc. spaces) */
$hard_match = '/[\d]+(?![-|:])[\d|\w]*(?=\s)*/i';
$hard_match = '/^(?:\d[\.\w\-])+(?:[\w\.]*)\b/i';
/**
* Steps:
* 1 - remove unsupported characters
* 2 - fuzzy match numeric groupings
* 1 - remove unwanted characters
* 2 - match numeric groupings
* 3 - split on spaces, check results
* - if not a decimal continue
* - else add to string
* 4 - match final group
* 4 - return cleaned up results
*/
$tmp = preg_replace($replacements, "", $title);
// preg_match($fuzzy_match, $tmp, $subset);
preg_match_all($hard_match, $tmp, $subset);
$groups = explode("-", join(".", $subset[0]));
$groups = $subset[0];
/* Check for spacing issues. */
/* if - array keys are out of order ignore matches */
......
......@@ -15,8 +15,13 @@ public static $modules = ['node', 'pathauto', 'lesson_pathauto'];
public function testLessonPathauto(){
$tests = [
"1α",
"1(a).6 - Outline of this Course - What Topics Will Follow?",
"1(a) .6 - Outline of this Course - What Topics Will Follow?",
"Lesson 3 : Linear Regression",
"Lesson 3 - Linear Regression",
"Lesson 3: Linear Regression",
"Lesson 32 Advanced Linear Regression",
"3.1 - Linear Methods",
"1.3 R Code for Two Examples in Lessons 1.1 and 1.2",
"3.1 Reviewing Studies - Getting the Big Picture",
......@@ -24,17 +29,26 @@ public static $modules = ['node', 'pathauto', 'lesson_pathauto'];
"Analysis of Classification Data",
"1.2.3.4a - Test",
"1.2.3.4b",
"1b.6a2: 2-way Test",
"1b.6a2 - 2-way Test",
"1b.6a 2 - 2-way Test",
"1b .6a 2 - Test",
"1b .6a 2: 2-way Test",
"1b .6a 2 - 2-way Test",
"B2 - Example",
"6.2 - Numbers That Can Describe 2×2 Tables",
"7.2.8 - Simultaneous (1 - α) x 100% Confidence Intervals"
"7.2.8 - Simultaneous (1 - α) x 100% Confidence Intervals",
"2.1-1 A Lesson on Numbers"
];
$expected = [
"1a",
"1a/1a.6",
"1a/1a.6",
"3",
"3",
"3",
"32",
"3/3.1",
"1/1.3",
"3/3.1",
......@@ -44,9 +58,14 @@ public static $modules = ['node', 'pathauto', 'lesson_pathauto'];
"1/1.2/1.2.3/1.2.3.4b",
"1b/1b.6a2",
"1b/1b.6a2",
"1b/1b.6a2",
"1b/1b.6a2",
"1b/1b.6a2",
"1b/1b.6a2",
"b2",
"6/6.2",
"7/7.2/7.2.8"
"7/7.2/7.2.8",
"2/2.1-1"
];
foreach ($tests as $key => $test) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment