从文本字符串创建关键字 - 西里尔字母的编码问题
Create keywords from text string - encoding issue for cyrillic
我正在寻找可以从文本字符串自动创建关键字的代码。下面的代码适用于拉丁字符,但如果我尝试将其与俄语(西里尔字母)一起使用 - 结果只是中间有空格的逗号。
我不确定是什么问题,但我相信它与编码有关。我试图用这个
编码字符串
$string = preg_replace('/\s\s+/i', '', mb_strtolower(utf8_encode($string)));
但运气不好。结果是一样的
此外,我在 $stopwords var
中只包含俄语单词
$stopWords = array('у','ни','на','да','и','нет','были','уже','лет','в','что','их','до','сих','пор','из','но','бы','чтобы','вы','была','было','мы','к','от','с','так','как','не','есть','ее','нам','для','вас','о','них','без');
这里是原码
function extractCommonWords($string){
$stopWords = array('i','a','about','an','and','are','as','at','be','by','com','de','en','for','from','how','in','is','it','la','of','on','or','that','the','this','to','was','what','when','where','who','will','with','und','the','www');
$string = preg_replace('/\s\s+/i', '', $string); // replace whitespace
$string = trim($string); // trim the string
$string = preg_replace('/[^a-zA-Z0-9 -]/', '', $string); // only take alphanumerical characters, but keep the spaces and dashes too…
$string = strtolower($string); // make it lowercase
preg_match_all('/\b.*?\b/i', $string, $matchWords);
$matchWords = $matchWords[0];
foreach ( $matchWords as $key=>$item ) {
if ( $item == '' || in_array(strtolower($item), $stopWords) || strlen($item) <= 3 ) {
unset($matchWords[$key]);
}
}
$wordCountArr = array();
if ( is_array($matchWords) ) {
foreach ( $matchWords as $key => $val ) {
$val = strtolower($val);
if ( isset($wordCountArr[$val]) ) {
$wordCountArr[$val]++;
} else {
$wordCountArr[$val] = 1;
}
}
}
arsort($wordCountArr);
$wordCountArr = array_slice($wordCountArr, 0, 10);
return $wordCountArr;}
如有任何帮助,我们将不胜感激。
西里尔字符与以下正则表达式匹配并从字符串中删除:
$string = preg_replace('/[^a-zA-Z0-9 -]/', '', $string);
为避免这种情况,请将 word characters with \w
. But you need to use the u (PCRE_UTF8)
modifier 与 \w
和朋友匹配,以匹配来自其他文字(包括西里尔字母)的字母。
$string = preg_replace('/[^\w -]+/u', '', $string);
还有一件事,正则表达式 /\b.*?\b/i
有几个错误的原因。但是仔细想想,同一个边界可以匹配两次。
无论如何,我相信您不需要从字符串中删除不需要的字符。你只是想提取单词。您可以简单地使用 1 个正则表达式来匹配它们。
用于匹配单词的正则表达式(使用 Unicode 脚本,包括西里尔字母)
preg_match_all('/\w+/u', $string, $matchWords);
代码
function extractCommonWords($string){
$stopWords = array('i','a','about','an','and','are','as','at','be','by','com','de','en','for','from','how','in','is','it','la','of','on','or','that','the','this','to','was','what','when','where','who','will','with','und','the','www');
$string = strtolower($string);
preg_match_all('/\w+/u', $string, $matchWords);
$matchWords = $matchWords[0];
foreach ( $matchWords as $key=>$item ) {
if ( $item == '' || in_array(strtolower($item), $stopWords) || strlen($item) <= 3 ) {
unset($matchWords[$key]);
}
}
$wordCountArr = array();
if ( is_array($matchWords) ) {
foreach ( $matchWords as $key => $val ) {
$val = mb_strtolower($val);
if ( isset($wordCountArr[$val]) ) {
$wordCountArr[$val]++;
} else {
$wordCountArr[$val] = 1;
}
}
}
arsort($wordCountArr);
$wordCountArr = array_slice($wordCountArr, 0, 10);
return $wordCountArr;
}
//for testing purposes
$sc = 'Hello there нам для!';
$result = extractCommonWords($sc);
print_r($result);
*免责声明:我什至没有阅读其余代码。我只是指出问题所在
我正在寻找可以从文本字符串自动创建关键字的代码。下面的代码适用于拉丁字符,但如果我尝试将其与俄语(西里尔字母)一起使用 - 结果只是中间有空格的逗号。
我不确定是什么问题,但我相信它与编码有关。我试图用这个
编码字符串$string = preg_replace('/\s\s+/i', '', mb_strtolower(utf8_encode($string)));
但运气不好。结果是一样的
此外,我在 $stopwords var
中只包含俄语单词$stopWords = array('у','ни','на','да','и','нет','были','уже','лет','в','что','их','до','сих','пор','из','но','бы','чтобы','вы','была','было','мы','к','от','с','так','как','не','есть','ее','нам','для','вас','о','них','без');
这里是原码
function extractCommonWords($string){
$stopWords = array('i','a','about','an','and','are','as','at','be','by','com','de','en','for','from','how','in','is','it','la','of','on','or','that','the','this','to','was','what','when','where','who','will','with','und','the','www');
$string = preg_replace('/\s\s+/i', '', $string); // replace whitespace
$string = trim($string); // trim the string
$string = preg_replace('/[^a-zA-Z0-9 -]/', '', $string); // only take alphanumerical characters, but keep the spaces and dashes too…
$string = strtolower($string); // make it lowercase
preg_match_all('/\b.*?\b/i', $string, $matchWords);
$matchWords = $matchWords[0];
foreach ( $matchWords as $key=>$item ) {
if ( $item == '' || in_array(strtolower($item), $stopWords) || strlen($item) <= 3 ) {
unset($matchWords[$key]);
}
}
$wordCountArr = array();
if ( is_array($matchWords) ) {
foreach ( $matchWords as $key => $val ) {
$val = strtolower($val);
if ( isset($wordCountArr[$val]) ) {
$wordCountArr[$val]++;
} else {
$wordCountArr[$val] = 1;
}
}
}
arsort($wordCountArr);
$wordCountArr = array_slice($wordCountArr, 0, 10);
return $wordCountArr;}
如有任何帮助,我们将不胜感激。
西里尔字符与以下正则表达式匹配并从字符串中删除:
$string = preg_replace('/[^a-zA-Z0-9 -]/', '', $string);
为避免这种情况,请将 word characters with \w
. But you need to use the u (PCRE_UTF8)
modifier 与 \w
和朋友匹配,以匹配来自其他文字(包括西里尔字母)的字母。
$string = preg_replace('/[^\w -]+/u', '', $string);
还有一件事,正则表达式 /\b.*?\b/i
有几个错误的原因。但是仔细想想,同一个边界可以匹配两次。
无论如何,我相信您不需要从字符串中删除不需要的字符。你只是想提取单词。您可以简单地使用 1 个正则表达式来匹配它们。
用于匹配单词的正则表达式(使用 Unicode 脚本,包括西里尔字母)
preg_match_all('/\w+/u', $string, $matchWords);
代码
function extractCommonWords($string){
$stopWords = array('i','a','about','an','and','are','as','at','be','by','com','de','en','for','from','how','in','is','it','la','of','on','or','that','the','this','to','was','what','when','where','who','will','with','und','the','www');
$string = strtolower($string);
preg_match_all('/\w+/u', $string, $matchWords);
$matchWords = $matchWords[0];
foreach ( $matchWords as $key=>$item ) {
if ( $item == '' || in_array(strtolower($item), $stopWords) || strlen($item) <= 3 ) {
unset($matchWords[$key]);
}
}
$wordCountArr = array();
if ( is_array($matchWords) ) {
foreach ( $matchWords as $key => $val ) {
$val = mb_strtolower($val);
if ( isset($wordCountArr[$val]) ) {
$wordCountArr[$val]++;
} else {
$wordCountArr[$val] = 1;
}
}
}
arsort($wordCountArr);
$wordCountArr = array_slice($wordCountArr, 0, 10);
return $wordCountArr;
}
//for testing purposes
$sc = 'Hello there нам для!';
$result = extractCommonWords($sc);
print_r($result);
*免责声明:我什至没有阅读其余代码。我只是指出问题所在