0

我使用 php 类从文章中制作标签云,但我想删除只有 3 个字符或更少的单词,也删除数字单词。

示例标签: 1111 猴 鹿 猫 猪 水牛

我想要结果:猴鹿水牛

该类的 PHP 代码(此处为完整代码)

    function keywords_extract($text)
{
    $text = strtolower($text);
    $text = strip_tags($text);

    /* 
     * Handle common words first because they have punctuation and we need to remove them
     * before removing punctuation.
     */
    $commonWords = "'tis,'twas,a,able,about,across,after,ain't,all,almost,also,am,among,an,and,any,are,aren't," .
        "as,at,be,because,been,but,by,can,can't,cannot,could,could've,couldn't,dear,did,didn't,do,does,doesn't," .
        "don't,either,else,ever,every,for,from,get,got,had,has,hasn't,have,he,he'd,he'll,he's,her,hers,him,his," .
        "how,how'd,how'll,how's,however,i,i'd,i'll,i'm,i've,if,in,into,is,isn't,it,it's,its,just,least,let,like," .
        "likely,may,me,might,might've,mightn't,most,must,must've,mustn't,my,neither,no,nor,not,o'clock,of,off," .
        "often,on,only,or,other,our,own,rather,said,say,says,shan't,she,she'd,she'll,she's,should,should've," .
        "shouldn't,since,so,some,than,that,that'll,that's,the,their,them,then,there,there's,these,they,they'd," .
        "they'll,they're,they've,this,tis,to,too,twas,us,wants,was,wasn't,we,we'd,we'll,we're,were,weren't,what," .
        "what'd,what's,when,when,when'd,when'll,when's,where,where'd,where'll,where's,which,while,who,who'd," .
        "who'll,who's,whom,why,why'd,why'll,why's,will,with,won't,would,would've,wouldn't,yet,you,you'd,you'll," .

    $commonWords = strtolower($commonWords);
    $commonWords = explode(",", $commonWords);
    foreach($commonWords as $commonWord) 
    {
        $text = $this->str_replace_word($commonWord, "", $text);  
    }

    /* remove punctuation and newlines */
    /*
     * Changed to handle international characters
     */
    if ($this->m_bUTF8)
        $text = preg_replace('/[^\p{L}0-9\s]|\n|\r/u',' ',$text);
    else
        $text = preg_replace('/[^a-zA-Z0-9\s]|\n|\r/',' ',$text);

    /* remove extra spaces created */
    $text = preg_replace('/ +/',' ',$text);
    $text = trim($text);
    $words = explode(" ", $text);
    foreach ($words as $value) 
    {
        $temp = trim($value);
        if (is_numeric($temp))
            continue;
        $keywords[] = trim($temp);
    }
    return $keywords;
}

我尝试了各种方法,例如 use if (strlen($words)<3 && is_numeric($words)==true),但没​​有奏效。

请帮我

4

5 回答 5

1

您应该将&&to ||:
from:更改
if (strlen($words)<3 && is_numeric($words)==true)
为:
if (strlen($words)<3 || is_numeric($words)==true)

如果你想删除3 个字符或更少的单词,
那么你应该使用<=而不是<
if (strlen($words) <= 3 || is_numeric($words)==true)

于 2012-07-08T04:37:09.020 回答
1

你可以用正则表达式来做

改变:

/* remove extra spaces created */
$text = preg_replace('/ +/',' ',$text);
$text = trim($text);
$words = explode(" ", $text);

至:

/* remove extra spaces created */
$words = preg_replace('/\b\w{1,3}\s|[0-9]/gi','',$text);
return $words;

并删除以下 foreach 部分,包括返回;

以下是正则表达式模式的解释:

\b = Match a word boundary position (whitespace or the beginning/end of the string).
\w = Match any word character (alphanumeric & underscore).
{1,3} = Matches 1 to 3 of the preceeding token.
\s = Match any whitespace character (spaces, tabs, line breaks).
| = or.
[0-9] = Match any numeric character.

以下是对这种模式的人类可以理解的解释:“找到一个词——从起始位置到 1 或 3 个字符的长度和后面的空格具有任何单词字符——或——任何数字字符——并替换它带有一个空字符串。

于 2012-07-08T04:54:07.803 回答
1

我将稍微修改您的流程以使其运行得更快(我相信它应该)。

第1步:我不会将每个常用词替换为空字符串$text(替换过程很昂贵),而是将每个常用词存储到哈希表中以供以后过滤。

$commonWords = explode(",", $commonWords);
foreach($commonWords as $commonWord)
    $hashWord[$commonWord] = $commonWord;

第二步:过滤常用词、数字和同时包含少于4位数字的词。

$words = preg_split("/[\s\n\r]/", $text);
foreach ($words as $value) 
{
    // Skip it is common word
    if (isset($hashWord[$value])) continue;
    // Skip if it is numeric
    if (is_numeric($value)) continue;
    // Skip if word contains less than 4 digits
    if (strlen($value) < 4) continue;

    $keywords[] = preg_replace('/[^a-zA-Z0-9\s].+/', '', $value);
}

下面是这个函数的完整源代码(如果你想复制和粘贴)

function keywords_extract($text) {
    $text = strtolower($text);
    $text = strip_tags($text);

    $commonWords = "'tis,'twas,a,able,about,across,after,ain't,all,almost,also,am,among,an,and,any,are,aren't," .
        "as,at,be,because,been,but,by,can,can't,cannot,could,could've,couldn't,dear,did,didn't,do,does,doesn't," .
        "don't,either,else,ever,every,for,from,get,got,had,has,hasn't,have,he,he'd,he'll,he's,her,hers,him,his," .
        "how,how'd,how'll,how's,however,i,i'd,i'll,i'm,i've,if,in,into,is,isn't,it,it's,its,just,least,let,like," .
        "likely,may,me,might,might've,mightn't,most,must,must've,mustn't,my,neither,no,nor,not,o'clock,of,off," .
        "often,on,only,or,other,our,own,rather,said,say,says,shan't,she,she'd,she'll,she's,should,should've," .
        "shouldn't,since,so,some,than,that,that'll,that's,the,their,them,then,there,there's,these,they,they'd," .
        "they'll,they're,they've,this,tis,to,too,twas,us,wants,was,wasn't,we,we'd,we'll,we're,were,weren't,what," .
        "what'd,what's,when,when,when'd,when'll,when's,where,where'd,where'll,where's,which,while,who,who'd," .
        "who'll,who's,whom,why,why'd,why'll,why's,will,with,won't,would,would've,wouldn't,yet,you,you'd,you'll,";

    $commonWords = explode(",", $commonWords);
    foreach($commonWords as $commonWord)
        $hashWord[$commonWord] = $commonWord;

    $words = preg_split("/[\s\n\r]/", $text);
    foreach ($words as $value) 
    {
        // Skip it is common word
        if (isset($hashWord[$value])) continue;
        // Skip if it is numeric
        if (is_numeric($value)) continue;
        // Skip if word contains less than 4 digits
        if (strlen($value) < 4) continue;

        $keywords[] = preg_replace('/[^a-zA-Z0-9\s].+/', '', $value);
    }
    return $keywords;
}

演示:ideone.com/obG6n

于 2012-07-08T05:17:01.743 回答
0

现在我添加$text = preg_replace('!\\b\\w{1,3}\\b!', ' ', $text);

    $text = preg_replace('/ +/',' ',$text);
    $text = trim($text);
    $words = explode(" ", $text);

没有错误:)

资源

如果你想使用这个 php 类,你可以在这里获取代码

感谢你所做的一切 :)

于 2012-07-08T15:15:41.063 回答
0
If((strlen($word) <=  3) && is_numeric($words)){
     //Don't add in the list
}
于 2012-07-08T04:22:25.847 回答