cdb = Reader::open( $target ); } catch ( Exception $e ) { // Do nothing } } /** * @since 0.1 * * @return bool */ public function isAvailable() { return $this->cdb !== null; } /** * @since 0.1 * * @param string $language * * @return string */ public static function getLocation() { return str_replace( [ '\\', '/' ], DIRECTORY_SEPARATOR, __DIR__ . '/data/' ); } /** * @since 0.1 * * @param string $language * * @return string */ public static function getTargetByLanguage( $language ) { return self::getLocation() . 'cdb/' . strtolower( $language ?? '' ) . '.cdb'; } /** * @since 0.1 * * @param string $word * * @return bool */ public function isStopWord( $word ) { if ( $this->cdb !== null && $this->cdb->get( $word ) !== false ) { return true; } return false; } /** * @since 0.1 * * @param string $location * @param string $language * * @return bool */ public static function createCdbByLanguage( $location, $language ) { $language = strtolower( $language ?? '' ); $source = $location . $language . '.json'; if ( !file_exists( $source ) ) { throw new RuntimeException( "{$source} is not available." ); } $contents = json_decode( file_get_contents( $source ), true ); if ( !isset( $contents['list'] ) ) { throw new RuntimeException( "JSON is missing the `list` index." ); } $writer = Writer::open( self::getTargetByLanguage( $language ) ); foreach ( $contents['list'] as $words ) { $writer->set( trim( $words ), true ); } $writer->close(); return true; } }