首页 > 解决方案 > 获取组织名称并将其缩写,其变体中省略了“of”和“and”,但也包含在其他变体中

问题描述

我需要获取提供给我的组织名称作为 PHP 中的字符串,并使用每个单词的第一个字母以相同的顺序对其进行缩写。如果有像“of”或“and”这样的词,我需要显示包含或不包含这两个词的首字母的缩写。另外,我需要省略重复的缩写。

    <?php

function splort($org){
        $org_arr = explode(' ',$org);
        $abbr1 = '';
        $abbr2 = '';
    $abbr_arr = array();
        foreach($org_arr as $word){
              $abbr1 .= $word[0];
              if(strtolower($word) == 'of')
                    continue;
              $abbr2 .= $word[0];
        }
        if($abbr1 == $abbr2) array_push($abbr_arr,$abbr1);
        else  array_push($abbr_arr,$abbr1,$abbr2);
        return $abbr_arr;
     }

     print_r(splort("State Bank of India"));

上面的代码只管理一个词,即'of'。我也需要注意“和”这个词。上面的代码只产生了两种变体,这是意料之中的。“SBoI”和“SBI”。

如果组织名称是“State Bank of and India”,则可能的缩写如下: SBI
SBoI
SBaI
SBoaI

更新

我还被分配了返回组织名称的前三个字母(如果它只包含单词)的任务。我还应该返回组织中的所有单独单词。

我在回答这个问题之前形成了这个代码,因为它被搁置了。请查看并提出改进建议。

<?php

function split_and_shorten($org){

    $org_t = trim($org);
    $org_arr = explode(" ", $org_t);

    if(count($org_arr) > 1){
                $abbr1 = "";
                $abbr2 = "";
                $abbr3 = "";
                $abbr4 = "";
                foreach($org_arr as $word){
                        $abbr1 .= $word[0];
                        if (strtolower($word) != 'of')
                $abbr2 .= $word[0];
            if (strtolower($word) != 'and')
                $abbr3 .= $word[0];
            if (strtolower($word) == 'of' || strtolower($word) == 'and')
                continue;
            $abbr4 .= $word[0];
                }
        array_push($org_arr, $abbr1, $abbr2, $abbr3, $abbr4);
    }

    else {
        $short = substr($org_arr[0], 0, 3);
        array_push($org_arr, $short);
    }

    return array_unique($org_arr);
}

print_r(split_and_shorten("State Bank of and India"));

输出:

Array
(
    [0] => State
    [1] => Bank
    [2] => of
    [3] => and
    [4] => India
    [5] => SBoaI
    [6] => SBaI
    [7] => SBoI
    [8] => SBI
)

标签: phparraysstringalgorithm

解决方案


这是一个可以满足您需求的功能。它需要一串单词和一个停用词列表。如果一个词不是停用词,它的首字母会被添加到列表中的每个缩写词中。如果它是一个停用词,则重复缩写列表,新的缩写将添加到它们的停用词的首字母。

function splort($org, $stop_words) {
    $words = explode(' ', $org);
    $abbrevs = array('');
    foreach ($words as $word) {
        if (in_array($word, $stop_words)) {
            // create a new set of abbreviations with this word's initial included
            $new_abbrevs = array();
            foreach ($abbrevs as $abbrev) {
                $new_abbrevs[] = $abbrev . $word[0];
            }
            // merge the arrays
            $abbrevs = array_merge($abbrevs, $new_abbrevs);
       }
        else {
            // add the initial to each abbreviation
            foreach ($abbrevs as &$abb) {
                $abb .= $word[0];
            }
        }
    }
    return $abbrevs;
}

$stop_words = array('of', 'and');
print_r(splort('State Bank of and India', $stop_words));

输出:

Array
(
    [0] => SBI
    [1] => SBoI
    [2] => SBaI
    [3] => SBoaI
)

3v4l.org 上的演示


推荐阅读