www.cl.ecei.tohoku.ac.jp
00. 文字列の逆順
<?php
$str = 'stressed';
echo strrev($str);
01. 「パタトクカシーー」
<?php
$str = 'パタトクカシーー';
$result = '';
for ($i = 0; $i < mb_strlen($str); ++$i) {
$result .= ($i % 2 === 0) ? mb_substr($str, $i, 1) : '';
}
echo $result;
02. 「パトカー」+「タクシー」=「パタトクカシーー」
<?php
$s1 = 'パトカー';
$s2 = 'タクシー';
$len = mb_strlen($s1) > mb_strlen($s2) ? mb_strlen($s1) : mb_strlen($s2);
$result = '';
for ($i = 0; $i < $len; ++$i) {
$result .= (mb_substr($s1, $i, 1) . mb_substr($s2, $i, 1));
}
echo $result;
03. 円周率
<?php
$str = 'Now I need a drink, alcoholic of course, after the heavy lectures involving quantum mechanics.';
$arr = explode(' ', str_replace([',', '.'], '', $str));
$result = [];
foreach ($arr as $v) {
$result[] = mb_strlen($v);
}
print_r($result);
<?php
$str = 'Hi He Lied Because Boron Could Not Oxidize Fluorine. New Nations Might Also Sign Peace Security Clause. Arthur King Can.';
$arr = explode(' ', str_replace([',', '.'], '', $str));
$targetIndex = [1,5,6,7,8,9,15,16,19];
$result = [];
foreach ($arr as $k => $v) {
$result[in_array($k + 1, $targetIndex, true) ? mb_substr($v, 0, 1) : mb_substr($v, 0, 2)] = $k + 1;
}
print_r($result);
<?php
function nGram(string $str, int $n, string $type = 'char'): string
{
$result = '';
if ($type === 'char') {
for ($i = 0; $i < mb_strlen($str); ++$i) {
$result .= mb_substr($str, $i, $n) . "\n";
}
return $result;
}
if ($type === 'word') {
$arr = explode(' ', $str);
for ($i = 0; $i < count($arr); ++$i) {
$result .= implode(' ', array_slice($arr, $i, $n)) . "\n";
}
return $result;
}
return "引数には'char'もしくは'word'を指定してください\n";
}
$str = 'I am an NLPer';
echo nGram($str, 2);
echo "\n";
echo nGram($str, 2, 'word');
06. 集合
<?php
function nGram(string $str, int $n = 2): array
{
$result = [];
for ($i = 0; $i < mb_strlen($str); ++$i) {
$result[] = mb_substr($str, $i, $n);
}
return array_values(array_unique($result));
}
function union(array $x, array $y): array
{
return array_values(array_unique(array_merge($x, $y)));
}
function intersection(array $x, array $y): array
{
return array_intersect($x, $y);
}
function difference(array $x, array $y): array
{
return array_diff($x, $y);
}
function inBigram(array $arr, string $str): bool
{
return in_array($str, $arr, true);
}
$x = nGram('paraparaparadise');
$y = nGram('paragraph');
echo "X is \n";
print_r($x);
echo "\n";
echo "Y is \n";
print_r($y);
echo "\n";
echo "Union is \n";
print_r(union($x, $y));
echo "\n";
echo "Intersection is \n";
print_r(intersection($x, $y));
echo "\n";
echo "Difference(x-y) is \n";
print_r(difference($x, $y));
echo "\n";
echo "Difference(y-x) is \n";
print_r(difference($y, $x));
echo "\n";
echo "'se' in X \n";
var_export(inBigram($x, 'se'));
echo "\n";
echo "'se' in Y \n";
var_export(inBigram($y, 'se'));
echo "\n";
07. テンプレートによる文生成
<?php
function template($x, $y, $z): string
{
return printf('%s時の%sは%s', $x, $y, $z);
}
$x = 12;
$y = '気温';
$z = 22.4;
template($x, $y, $z);
08. 暗号文
<?php
function cipher(string $str): string
{
$result = '';
for ($i = 0; $i < mb_strlen($str); ++$i) {
$char = mb_substr($str, $i, 1);
$result .= ctype_alpha($char) && ctype_lower($char) ? chr(219 - ord($char)) : $char;
}
return $result;
}
echo cipher("Hello, Peter. I'm Ken. Nice to meet you.");
09. Typoglycemia
<?php
function typoglycemia(string $sentence): string
{
$result = '';
$wordList = explode(' ', $sentence);
foreach ($wordList as $word) {
if (mb_strlen($word) < 4) {
$result .= $word;
} else {
$result .= mb_substr($word, 0, 1);
$result .= str_shuffle(mb_substr($word, 1, (mb_strlen($word) - 2)));
$result .= mb_substr($word, (mb_strlen($word) - 1), 1);
}
$result .= ' ';
}
return $result;
}
$sentence = "I couldn't believe that I could actually understand what I was reading : the phenomenal power of the human mind .";
echo typoglycemia($sentence);