如何使用PHP識別/分離聯合詞?

[英]How to recognize/separate joint words using PHP?


I have tried asking this question before, but apparently provided too little detail and it was closed. Let me try again by being a bit more thorough:

我之前嘗試過問這個問題,但顯然提供的細節太少而且已經關閉了。讓我再試一次:

What I need to do is separate any joint word, specifically in, say, domain names. If there is a domain "domainname.com", I need to have "domain" and "name" returned. If it's "stackoverflow.com", I need "stack" and "overflow" returned.

我需要做的是將任何聯合詞分開,特別是在域名中。如果有域名“domainname.com”,我需要返回“域名”和“名稱”。如果它是“stackoverflow.com”,我需要“stack”和“overflow”返回。

I imagine I need a word list to do this and that's not a problem. Keeping in mind that there are multiple ways some joint words may be separated ("stackoverflow.com" can be returned as "stack" and "overflow" or as "stack", "over" and "flow"), I've come up with this php function, but it needs improving.

我想我需要一個單詞列表來做這個,這不是問題。請記住,有多種方式可以分離一些聯合詞(“stackoverflow.com”可以作為“堆棧”和“溢出”或“堆棧”,“結束”和“流程”返回),我來了這個PHP功能,但它需要改進。

 function make_words_capitalised( $str )
    {
// Full list of words
        $words = array( 'you', 'tube', 'my', 'space' );

// Capitalize separate words    
        foreach( $words as $word )
        {
            $str = str_ireplace( $word, ucfirst($word), $str );
        }

        return $str;
    }

var_dump( make_words_capitalised('youtube.com') );
var_dump( make_words_capitalised('myspace.com') );

Any and all ideas are greatly appreciated.

任何和所有的想法都非常感謝。

2 个解决方案

#1


0  

Here is the best I can do ( and I think it's pretty good )

這是我能做的最好的(我覺得它還不錯)

function make_words_capitalised( $str, array $words)
{
    return str_ireplace($words, array_map('ucfirst', $words), $str);
}

var_dump( make_words_capitalised('youtube.com', ['you', 'tube']));
var_dump( make_words_capitalised('myspace.com', ['my', 'space']));

You can give an array to string replace, for the search and the replace. for the latter, we can use array_map and ucfirst to do the uppercase transform on them.

您可以為字符串替換提供數組,以進行搜索和替換。對於后者,我們可以使用array_map和ucfirst對它們進行大寫轉換。

I also made the words list an argument, just seemed like the thing to do, because it will make it way more portable.

我也把單詞列出了一個參數,看起來就像是要做的事情,因為它會讓它變得更加便攜。

Outputs

string(11) "YouTube.com"
string(11) "MySpace.com"

You can test it here.

你可以在這里測試一下。

http://sandbox.onlinephpfunctions.com/code/d0b2367a5438701735f0c909c62f8289edaa08b4

This is essentially the same Idea you originally had, just cleaned up, and polished a bit.

這基本上和你原來的想法一樣,只是清理干凈,並且稍微打磨一下。

#2


0  

In my opinion, your approach is ok. I would just suggest using strtr() string PHP function about which you could read more in the official documentation here.

在我看來,你的方法還可以。我建議使用strtr()字符串PHP函數,您可以在此處的官方文檔中閱讀更多內容。

Beside that, I would try to give a little more flexibility to this function and I would incapsulate it, together with all needed variables inside a class with static methods.

除此之外,我會嘗試為此函數提供更多的靈活性,並且我將使用靜態方法將其與所有需要的變量一起封裝在一起。

It would look something like this:

它看起來像這樣:

    class Capitalize
    {
  /**
   * @var array
   */
  private static $dictionary = ['you', 'tube', 'my', 'space'];
  /**
   * @var array
   */
  private static $transDictionary = [];

  /**
   * Capitalize all the words in the given text, based on the dictionary
   * @param $sentence
   * @return string
   */
  public static function capitalizeWords($sentence)
  {
    if (count(self::$transDictionary) == 0) {
      self::generateTransDictionary();
    }
    return strtr($sentence, self::$transDictionary);
  }


  /**
   * Generates a private translation dictionary based on the dictionary property
   */
  private static function generateTransDictionary()
  {
    $capWords = array_map('ucfirst', self::$dictionary);
    static::$transDictionary = array_combine(self::$dictionary, $capWords);
  }

  /**
   * Static getter - might be needed in the future
   * @return array
   */
  public static function getDictionary()
  {
    return self::$dictionary;
  }


  /**
   * @param $word
   */
  public static function addToDictionary($word)
  {
    static::$dictionary[] = $word;
    if (count (self::$transDictionary) != 0) self::generateTransDictionary();
  }
}


echo Capitalize::capitalizeWords('youtube.com');
echo Capitalize::capitalizeWords('spacetube.com');
Capitalize::addToDictionary('science');
echo Capitalize::capitalizeWords('sciencetube.com');

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2017/11/03/d506b10f1694ba893ac72b0c86cd3a05.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com