Skip to content

Commit

Permalink
feature: 增加 Pinyin::polyphonesAsArray. fixed #195
Browse files Browse the repository at this point in the history
  • Loading branch information
overtrue committed Jun 12, 2023
1 parent 50d3ce9 commit 768d947
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 10 deletions.
21 changes: 19 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ Pinyin::passportName('律师'); // ['lyu', 'shi']

### 多音字

多音字的返回值为关联数组的集合:
多音字的返回值为关联数组的集合,默认返回去重后的所有读音

```php
$pinyin = Pinyin::polyphones('重庆');
Expand All @@ -139,12 +139,29 @@ $pinyin->toArray();
// ]
```

如果不想要去重,可以数组形式返回:

```php
$pinyin = Pinyin::polyphones('重庆重庆', Converter::TONE_STYLE_SYMBOL, true);

// or
$pinyin = Pinyin::polyphonesAsArray('重庆重庆', Converter::TONE_STYLE_SYMBOL);

$pinyin->toArray();
// [
// ["重" => ["zhòng", "chóng", "tóng"]],
// ["庆" => ["qìng"]],
// ["重" => ["zhòng", "chóng", "tóng"]],
// ["庆" => ["qìng"]]
// ]
```

### 单字转拼音

和多音字类似,单字的返回值为字符串,多音字将根据该字字频调整得到常用音:

```php
$pinyin = Pinyin::polyphones('重庆');
$pinyin = Pinyin::chars('重庆');

echo $pinyin['重']; // "zhòng"
echo $pinyin['庆']; // "qìng"
Expand Down
13 changes: 11 additions & 2 deletions src/Converter.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ class Converter

protected bool $polyphonic = false;

protected bool $polyphonicAsList = false;

protected bool $asSurname = false;

protected bool $noWords = false;
Expand Down Expand Up @@ -53,9 +55,10 @@ public static function make(): static
return new static();
}

public function polyphonic(): static
public function polyphonic(bool $asList = false): static
{
$this->polyphonic = true;
$this->polyphonicAsList = $asList;

return $this;
}
Expand Down Expand Up @@ -196,7 +199,13 @@ public function convertAsChars(string $string, bool $polyphonic = false): Collec
foreach ($chars as $char) {
if (isset($map[$char])) {
if ($polyphonic) {
$items[$char] = \array_map(fn ($pinyin) => $this->formatTone($pinyin, $this->toneStyle), $map[$char]);
$pinyin = \array_map(fn ($pinyin) => $this->formatTone($pinyin, $this->toneStyle), $map[$char]);
if ($this->polyphonicAsList) {
$items[] = [$char => $pinyin];
} else {
$items[$char] = $pinyin;
}

} else {
$items[$char] = $this->formatTone($map[$char][0], $this->toneStyle);
}
Expand Down
11 changes: 8 additions & 3 deletions src/Pinyin.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use InvalidArgumentException;

/**
* @method static Converter polyphonic()
* @method static Converter surname()
* @method static Converter noWords()
* @method static Converter onlyHans()
Expand All @@ -16,6 +15,7 @@
* @method static Converter useNumberTone()
* @method static Converter yuToV()
* @method static Converter yuToU()
* @method static Converter polyphonic(bool $asList = false)
* @method static Converter withToneStyle(string $toneStyle = 'symbol')
* @method static Collection convert(string $string, callable $beforeSplit = null)
*/
Expand All @@ -41,9 +41,14 @@ public static function sentence(string $string, string $toneStyle = Converter::T
return self::withToneStyle($toneStyle)->convert($string);
}

public static function polyphones(string $string, string $toneStyle = Converter::TONE_STYLE_SYMBOL): Collection
public static function polyphones(string $string, string $toneStyle = Converter::TONE_STYLE_SYMBOL, bool $asList = false): Collection
{
return self::polyphonic()->withToneStyle($toneStyle)->convert($string);
return self::polyphonic($asList)->withToneStyle($toneStyle)->convert($string);
}

public static function polyphonesAsArray(string $string, string $toneStyle = Converter::TONE_STYLE_SYMBOL): Collection
{
return self::polyphones($string, $toneStyle, true);
}

public static function chars(string $string, string $toneStyle = Converter::TONE_STYLE_SYMBOL): Collection
Expand Down
6 changes: 6 additions & 0 deletions tests/ConverterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ public function test_polyphonic()
'' => ['zhòng', 'chóng', 'tóng'],
'' => ['qìng'],
], Converter::make()->polyphonic()->convert('重庆'));

$this->assertPinyin([
['' => ['zhòng', 'chóng', 'tóng']],
['' => ['qìng']],
['' => ['qìng']],
], Converter::make()->polyphonic(true)->convert('重庆庆'));
}

public function test_surname()
Expand Down
38 changes: 35 additions & 3 deletions tests/PinyinTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,18 @@ public function test_polyphones()
], Pinyin::polyphones('重庆', Converter::TONE_STYLE_NONE));

$this->assertPinyin([
'' => ['zhong4', 'chong2', 'tong2'],
'' => ['qing4'],
], Pinyin::polyphones('重庆', Converter::TONE_STYLE_NUMBER));
['' => ['zhong4', 'chong2', 'tong2']],
['' => ['qing4']],
['' => ['zhong4', 'chong2', 'tong2']],
['' => ['qing4']],
], Pinyin::polyphones('重庆重庆', Converter::TONE_STYLE_NUMBER, true));

$this->assertPinyin([
['' => ['zhong4', 'chong2', 'tong2']],
['' => ['qing4']],
['' => ['zhong4', 'chong2', 'tong2']],
['' => ['qing4']],
], Pinyin::polyphonesAsArray('重庆重庆', Converter::TONE_STYLE_NUMBER));
}

public function test_polyphones_chars()
Expand Down Expand Up @@ -364,5 +373,28 @@ public function test_issues()
//119
$this->assertPinyin(['e', 'e', 'e'], Pinyin::sentence('呃呃呃', 'none'));
$this->assertPinyin(['wu', 'la', 'gui'], Pinyin::sentence('乌拉圭', 'none'));

//195
$this->assertSame([
'' => ['ni3'],
'' => ['dian4'],
'' => ['nao3'],
'' => ['shen2', 'shi2'],
'' => ['me', 'yao1', 'mo2', 'ma'],
'' => ['de', 'di1', 'di2', 'di4'],
], Pinyin::polyphones('你电脑电脑电脑什么的', Converter::TONE_STYLE_NUMBER)->toArray());

$this->assertSame([
['' => ['ni3']],
['' => ['dian4']],
['' => ['nao3']],
['' => ['dian4']],
['' => ['nao3']],
['' => ['dian4']],
['' => ['nao3']],
['' => ['shen2', 'shi2']],
['' => ['me', 'yao1', 'mo2', 'ma']],
['' => ['de', 'di1', 'di2', 'di4']],
], Pinyin::polyphonesAsArray('你电脑电脑电脑什么的', Converter::TONE_STYLE_NUMBER)->toArray());
}
}

0 comments on commit 768d947

Please sign in to comment.