Пожалуйста, помогите мне сделать функцию для анализа следующего массива и возврата массива, содержащего наиболее часто встречающиеся символы без учета регистра и исключая специальные символы, count Input
$sentences = [
0 => 'The tiger is the national animal of India',
1 => 'The tiger is a large carnivorous mammal that roams the forests',
2 => 'The tiger feeds on animals that also live in the forest',
3 => 'The tiger does have a coat of orange with black stripes',
4 => 'Tigers, regardless of their subspecies, are carnivorous animals',
5 => 'The tiger is a protected species',
];
The code should output the following result:
Array
(
[0] => Array
(
[sentence] => The tiger is the national animal of India
[character] => i
[occurrences] => 6
)
[1] => Array
(
[sentence] => The tiger is a large carnivorous mammal that roams the forests
[character] => a
[occurrences] => 7
)
[2] => Array
(
[sentence] => The tiger feeds on animals that also live in the forest
[character] => e
[occurrences] => 7
)
[3] => Array
(
[sentence] => The tiger does have a coat of orange with black stripes
[character] => e
[occurrences] => 6
)
[4] => Array
(
[sentence] => Tigers, regardless of their subspecies, are carnivorous animals
[character] => s
[occurrences] => 8
)
[5] => Array
(
[sentence] => The tiger is a protected species
[character] => e
[occurrences] => 6
)
)
Я старался
foreach($sentences as $sentence) {
$value = array_count_values($sentence);
}
Пожалуйста, помогите мне сделать функцию для вышеуказанной цели
Вы могли бы использовать array_map()
применить функцию к каждому пункту. В этой функции вы можете преобразовать строку в нижний регистр, разделить символы в массиве (и array_filter()
удалить пробелы), чтобы использовать array_count_values()
. Затем вы можете отсортировать массив, используя arsort()
сохранить связь ключей и получить наиболее часто используемый символ в верхней части. Наконец, вы можете использовать array_keys()
а также reset()
чтобы получить первый ключ и первое значение массива:
$sentences = [
0 => 'The tiger is the national animal of India',
1 => 'The tiger is a large carnivorous mammal that roams the forests',
2 => 'The tiger feeds on animals that also live in the forest',
3 => 'The tiger does have a coat of orange with black stripes',
4 => 'Tigers, regardless of their subspecies, are carnivorous animals',
5 => 'The tiger is a protected species',
];
$out = array_map(function($value) {
$chars = array_filter(str_split(strtolower($value)),'trim');
$vals = array_count_values($chars);
arsort($vals);
$keys = array_keys($vals);
return [
'sentence' => $value,
'character' => reset($keys),
'occurrences' => reset($vals),
];
}, $sentences);
print_r($out) ;
Выходы:
Array (
[0] => Array (
[sentence] => The tiger is the national animal of India
[character] => i
[occurrences] => 6
)
[1] => Array (
[sentence] => The tiger is a large carnivorous mammal that roams the forests
[character] => a
[occurrences] => 7
)
[2] => Array (
[sentence] => The tiger feeds on animals that also live in the forest
[character] => e
[occurrences] => 7
)
[3] => Array (
[sentence] => The tiger does have a coat of orange with black stripes
[character] => e
[occurrences] => 6
)
[4] => Array (
[sentence] => Tigers, regardless of their subspecies, are carnivorous animals
[character] => s
[occurrences] => 8
)
[5] => Array (
[sentence] => The tiger is a protected species
[character] => e
[occurrences] => 6
)
)
Чтобы удалить специальные символы:
$chars = array_filter(str_split(strtolower($value)),function($val){
return trim(preg_replace('~\W+~', '', $val));
});
Я бы использовал preg_match_all()
с шаблоном, который соответствует одной букве или цифре за раз, чтобы извлечь массив символов, а затем найти количество вхождений с помощью array_count_values()
отсортируйте массив по вхождению в порядке убывания, затем извлеките первый ключ и первое значение (которые представляют символ и число самой высокой встречающейся буквы).
Код: (Демо: https://3v4l.org/7OZ5d )
$sentences = [
0 => 'The tiger is the national animal of India',
1 => 'The tiger is a large carnivorous mammal that roams the forests',
2 => 'The tiger feeds on animals that also live in the forest',
3 => 'The tiger does have a coat of orange with black stripes',
4 => 'Tigers, regardless of their subspecies, are carnivorous animals',
5 => 'The tiger is a protected species',
];
foreach ($sentences as $sentence) {
$alphanums = preg_match_all ('~[a-z\d]~', strtolower($sentence), $out) ? $out[0] : [];
// or: $alphanums = preg_split('~[^a-z\d]*~', strtolower($sentence), null, PREG_SPLIT_NO_EMPTY);
$occurrences = array_count_values($alphanums);
arsort($occurrences);
$result[] = [
"sentence" => $sentence,
"character" => key($occurrences),
"occurrences" => current($occurrences)
];
}
var_export($result);
Использовать count_chars
$sentences = [
0 => 'The tiger is the national animal of India',
1 => 'The tiger is a large carnivorous mammal that roams the forests',
2 => 'The tiger feeds on animals that also live in the forest',
3 => 'The tiger does have a coat of orange with **a** black stripe',
4 => 'Tigers, regardless of their subspecies, are carnivorous animals',
5 => 'The tiger is a protected species',
];
Я фиксирую вхождения, используя count_chars () в $ counts, и получаю максимальное количество вхождений, используя max (). Удалите коды ASCII из массива, если число экземпляров меньше максимального (поскольку может быть несколько символов, имеющих одинаковые вхождения).
Преобразуйте оставшийся код (ы) ASII обратно в символы, используя команду chr ().
<?php
foreach ($sentences as $sentence){
$string = strtolower(preg_replace('/\s+/', '', $sentence));
$counts = count_chars($string, 1);
$max = max($counts);
foreach ($counts as $key => $count) {
if ($count < $max) {
unset($counts[$key]);
}
}
$characters = array_map(function($item){
return chr($item);
}, array_keys($counts));
$result[] = [
'sentence' => $sentence,
'character' => implode(',', $characters),
'occurrences' => $max
];
}
echo '<pre>', print_r($result) ,'<pre>';
ВЫХОД:
Array
(
[0] => Array
(
[sentence] => The tiger is the national animal of India
[character] => i
[occurrences] => 6
)
[1] => Array
(
[sentence] => The tiger is a large carnivorous mammal that roams the forests
[character] => a
[occurrences] => 7
)
[2] => Array
(
[sentence] => The tiger feeds on animals that also live in the forest
[character] => e
[occurrences] => 7
)
[3] => Array
(
[sentence] => The tiger does have a coat of orange with a black stripe
[character] => a,e **<== multiple characters**
[occurrences] => 6
)
[4] => Array
(
[sentence] => Tigers, regardless of their subspecies, are carnivorous animals
[character] => s
[occurrences] => 8
)
[5] => Array
(
[sentence] => The tiger is a protected species
[character] => e
[occurrences] => 6
)
)
Чтобы считать только алфавиты:
$string = strtolower(preg_replace('/[^A-Za-z]/', '', $sentence));
DEMO: http://sandbox.onlinephpfunctions.com/code/e2fb793031cd172507d8a464b1096b1b2bb73046