Page Menu
Home
Phabricator
Search
Configure Global Search
Log In
Files
F4684
hanp.body.php.txt
Public
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Authored By
•
bzimport
Nov 21 2014, 10:04 PM
2014-11-21 22:04:18 (UTC+0)
Size
4 KB
Referenced Files
None
Subscribers
None
hanp.body.php.txt
View Options
Patch for /extensions/Hanp/Hanp.body.php
===================================================================
<?php
class Hanp {
/**
- * {{#hanp:word|particle}}
+ * {{#hanp:word|particle|output}}
*/
- static function hangulParticle( $parser, $word = '', $particle = '') {
+ static function hangulParticle( $parser, $word = '', $particle = '', $output = '') {
+
+ /**
+ * Initialization of $output parameter
+ * This parameter used if $word contains signs not read
+ * For example: {{#hanp:Wikimedia|particle|[[Wikimedia]]}}
+ */
+ if ($output == '') { $output = $word; }
+
switch ($particle) {
case '로': # ro
case '으로': # euro
if ( self::endsInHangulRieul($word) ) {
- return $word . '로';
+ return $output . '로';
} elseif ( self::endsInHangulConsonant($word) ) {
- return $word . '으로';
+ return $output . '으로';
} else {
- return $word . '로'; // Vowel or non-hangul
+ return $output . '로';
}
break;
case '을': # eul
case '이': # i
case '와': # wa
case '은': # eun
case '를': # reul
case '가': # ga
case '과': # gwa
case '는': # neun
if ( self::endsInHangulConsonant($word) ) {
- return $word . self::particleMap( $particle, self::SOFT );
+ return $output . self::particleMap( $particle, self::SOFT );
} else {
- return $word . self::particleMap( $particle, self::HARD );
+ return $output . self::particleMap( $particle, self::HARD );
}
break;
- case '의': # yi
- return $word . $particle;
- break;
-
- default: return $word;
+ default: return $output . $particle;
}
}
// Not real terms, but I do not know how to call these
const SOFT = 1; // without consonant
const HARD = 2; // with consonant
/**
* Returns the correct version of the particle.
* Use Hanp::SOFT to get version that can be attached to words ending in a
* consonant and Hanp::HARD for others.
*/
static function particleMap( $particle, $dir ) {
$map = array(
'을' => '를', # (r)eul
'이' => '가', # i, ga
'과' => '와', # gwa, wa
'은' => '는', # (n)eun
);
if ( $dir === self::SOFT ) $map = array_flip( $map );
if ( isset($map[$particle]) ) {
return $map[$particle];
} else {
return $particle; # We want only valid input, so it is already correct
}
}
static function lastLetterToCodePoint( $string ) {
require_once ( dirname( __FILE__ ) . '/php-utf8/utf8.inc' );
$matches = array();
if ( !preg_match( '/.$/u', $string, $matches ) ) return false;
// I hate php
$returnValue = utf8toUnicode( $matches[0] );
return array_pop( $returnValue );
}
static function endsInHangul( $string ) {
$rangeStart = hexdec( 'AC00' ); # GA
$rangeEnd = hexdec( 'D7A3' ); # HIH
$lastLetter = self::lastLetterToCodePoint( $string );
return $rangeStart < $lastLetter && $lastLetter < $rangeEnd;
}
static function endsInHangulVowel( $string ) {
$lastLetter = self::lastLetterToCodePoint( $string );
$candidate = hexdec( 'AC00' ); # GA
$increment = hexdec( '1C' );
$lastValue = hexdec( 'D788' );
do {
if ( $lastLetter < $candidate ) return false; // Fast out
if ( $lastLetter === $candidate ) return true;
$candidate += $increment;
} while ( $candidate < $lastValue );
return false;
}
static function endsInHangulRieul( $string ) {
$lastLetter = self::lastLetterToCodePoint( $string );
$candidate = hexdec( 'AC08' ); # GA
$increment = hexdec( '1C' );
$lastValue = hexdec( 'D790' );
do {
if ( $lastLetter < $candidate ) return false; // Fast out
if ( $lastLetter === $candidate ) return true;
$candidate += $increment;
} while ( $candidate < $lastValue );
return false;
}
static function endsInHangulConsonant( $string ) {
return self::endsInHangul( $string ) && !self::endsInHangulVowel( $string );
}
}
File Metadata
Details
Attached
Mime Type
text/plain
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
4249
Default Alt Text
hanp.body.php.txt (4 KB)
Attached To
Mode
T15712: Install Josa extension parser function on all Korean language wikis
Attached
Detach File
Event Timeline
Log In to Comment