Skip to content

Instantly share code, notes, and snippets.

@tekihei2317
Last active May 1, 2025 08:18
Show Gist options
  • Save tekihei2317/f92c58d42a4d46501a9fdac28779a7f0 to your computer and use it in GitHub Desktop.
Save tekihei2317/f92c58d42a4d46501a9fdac28779a7f0 to your computer and use it in GitHub Desktop.
WTの公式ワードファイルから、特定の文字数のフレーズのみを抽出するスクリプト
require 'rexml/document'
include REXML
# --- 設定 ---
TARGET_LENGTH = 5
SOURCE_FILE = "word2jp.xml"
OUTPUT_FILE = "word2jp_len#{TARGET_LENGTH}.xml"
SIGNATURE = "word2-len#{TARGET_LENGTH}"
# ------------
# XML読み込み
file = File.open(SOURCE_FILE)
doc = Document.new(file)
# 出力
File.open(OUTPUT_FILE, "w:utf-8") do |out|
out.write("\uFEFF") # BOM
out.puts '<?xml version="1.0" encoding="utf-8" standalone="yes"?>'
out.puts '<Words Version="2">'
out.puts " <Name>公式ワード2(#{TARGET_LENGTH}文字)</Name>"
out.puts " <Author>Generated</Author>"
out.puts " <Language>#{doc.elements['Words/Language']&.text}</Language>"
out.puts " <Memo>#{TARGET_LENGTH}文字のフレーズのみ</Memo>"
out.puts " <Signature>#{SIGNATURE}</Signature>"
doc.elements.each("Words/Part") do |original_part|
out.puts " <Part>"
original_part.elements.each("Word") do |word|
chars = word.elements["Characters"]&.text&.strip
display = word.elements["Display"]&.text&.strip
next unless chars && chars.length == TARGET_LENGTH
out.puts " <Word>"
out.puts " <Display>#{display}</Display>"
out.puts " <Characters>#{chars}</Characters>"
out.puts " </Word>"
end
out.puts " </Part>"
end
out.puts "</Words>"
end
puts "✅ 完了: #{TARGET_LENGTH}文字のワードだけを含むXMLを '#{OUTPUT_FILE}' に出力しました。"
@tekihei2317
Copy link
Author

ワードファイルの場所はWeather Typing - よくある質問の「ワードを自作したい」に書かれています。Macの場合は/Users/<use>/Library/Containers/com.denasu.Weather-Typing-Mac/Data/WeatherTyping/Wordでした。

@tekihei2317
Copy link
Author

公式ワード2について、文字数別の出現頻度は以下の通りです。

=== 前半フレーズ ===
Length 2: 2 word(s)
Length 3: 8 word(s)
Length 4: 35 word(s)
Length 5: 60 word(s)
Length 6: 52 word(s)
Length 7: 78 word(s)
Length 8: 54 word(s)
Length 9: 23 word(s)
Length 10: 17 word(s)
Length 11: 13 word(s)
Length 12: 7 word(s)
Length 13: 8 word(s)
Length 14: 1 word(s)
Length 15: 1 word(s)
Length 19: 1 word(s)
Total: 360 word(s)

=== 後半フレーズ ===
Length 2: 2 word(s)
Length 3: 34 word(s)
Length 4: 50 word(s)
Length 5: 59 word(s)
Length 6: 39 word(s)
Length 7: 37 word(s)
Length 8: 17 word(s)
Length 9: 12 word(s)
Length 10: 6 word(s)
Length 11: 6 word(s)
Length 13: 2 word(s)
Total: 264 word(s)

私は5文字、6文字、7文字、8文字、9文字以上の5つのファイルを作りました。それぞれに含まれるフレーズの数は

60+59=119
52+39=91
73+37=110
54+17=71
71+26=97

個です。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment