我正在搜索可以自动从文本字符串创建关键字的代码。下面的代码适用于拉丁字符但如果我尝试使用俄语(西里尔文) - 结果只是逗号,中间有空格。
我不确定问题是什么,但我认为它与编码有关。我试图用这个
编码字符串$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;}
任何帮助都将不胜感激。
答案 0 :(得分:1)
西里尔字符由以下正则表达式匹配,并从字符串中删除:
$string = preg_replace('/[^a-zA-Z0-9 -]/', '', $string);
要避免这种情况,请将word characters与\w
匹配。但您需要使用\w
$string = preg_replace('/[^\w -]+/u', '', $string);
和朋友来匹配其他脚本(包括西里尔语)中的字母。
/\b.*?\b/i
还有一件事,正则表达式preg_match_all('/\w+/u', $string, $matchWords);
有几个原因是错误的。但是如果你考虑一下,它可以匹配相同的边界两次。
无论如何,我相信您不需要从字符串中删除不需要的字符。你只是想提取单词。你可以简单地使用1个正则表达式匹配它们。
正则表达式匹配单词(使用Unicode脚本,包括西里尔语)
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);
<强>代码强>
Type type = new TypeToken<Map<String, Object>>(){}.getType();
Map<String, Object> myMap = gson.fromJson(jsonString, type);
*免责声明:我甚至没有读过剩下的代码。我只是指出出了什么问题