<commit_before># encoding: utf-8
# This file is part of the K5 bot project.
# See files README.md and COPYING for copyright and licensing information.

# Pinyin plugin

require_relative '../../IRCPlugin'
require 'ruby-pinyin'

class Pinyin < IRCPlugin
  Description = "Pinyin translation plugin."
  Commands = { :pinyin => "translates hanzi to pinyin" }

  def on_privmsg(msg)
    case msg.botcommand
    when :pinyin
      pinyin = PinYin.sentence(msg.tail, true) 
      msg.reply pinyin if pinyin
    end
  end

end<commit_msg>Add zhuyin, wadegiles and IPA support to Pinyin<commit_after># encoding: utf-8
# This file is part of the K5 bot project.
# See files README.md and COPYING for copyright and licensing information.

# Pinyin plugin

require_relative '../../IRCPlugin'
require 'ting'
require 'ting/string'
require 'ruby-pinyin'

class Pinyin < IRCPlugin
  Description = "Hanzi conversion plugin."
  Commands = {
    :pinyin => "convert hanzi to pinyin",
    :zhuyin => "convert hanzi to zhuyin (bopomofo)",
    :wadegiles => "convert hanzi to wadegiles",
    :ipa => "convert hanzi to ipa",
  }

  def on_privmsg(msg)
    case msg.botcommand
    when :pinyin
      pinyin = _pinyin(msg.tail).pretty_tones
      msg.reply pinyin if pinyin
    when :zhuyin
      zhuyin = _translation msg.tail, :zhuyin, :marks
      msg.reply zhuyin if zhuyin
    when :wadegiles
      wadegiles = _translation msg.tail, :wadegiles, :supernum
      msg.reply wadegiles if wadegiles
    when :ipa
      ipa = _translation msg.tail, :ipa, :ipa
      msg.reply ipa if ipa
    end
  end
  
  def _pinyin text
    PinYin.sentence text, true
  end
  
  def _reader
    Ting.reader :hanyu, :numbers
  end
  
  def _writer type, tone
    Ting.writer type, tone
  end
  
  def _translation text, type, tone
    pinyin = _pinyin text
    x = _writer type, tone
    x = x << (_reader << pinyin)
  end

end
{'fraction_non_alphanumeric': 0.08748674443266172, 'fraction_numerical': 0.0021208907741251328, 'mean_word_length': 2.9642857142857144, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '941162', 'n_tokens_mistral': 682, 'n_tokens_neox': 672, 'n_words': 210}