음성 읽기: g·kg·cm·GB·Hz·hPa·㎍/㎥·W·V 등 단위 기호를 한글로 낭독(그램을 '지'로 읽던 문제), 통화·읽기 버튼 공용 speech-units.js
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
3a258c534e
commit
a50866eaac
5 files changed
+78
No files matched your search
@@ -0,0 +1,33 @@
|
||||
/**
|
||||
* speech-units — TTS가 영문 단위 기호를 글자 이름으로 읽는 문제(2026-10-17 "그램을 지라고 읽는다").
|
||||
*/
|
||||
import { test, describe } from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
||||
const { spellUnitsForSpeech: f } = require('../web-ui/js/voice/speech-units.js');
|
||||
|
||||
describe('spellUnitsForSpeech', () => {
|
||||
test('숫자에 붙은 단위를 한글로', () => {
|
||||
assert.equal(f('설탕 50g 넣고'), '설탕 50그램 넣고');
|
||||
assert.equal(f('72.5kg'), '72.5킬로그램');
|
||||
assert.equal(f('15 cm'), '15센티미터');
|
||||
assert.equal(f('256GB'), '256기가바이트');
|
||||
assert.equal(f('3.4GHz'), '3.4기가헤르츠');
|
||||
assert.equal(f('1013hPa'), '1013헥토파스칼');
|
||||
assert.equal(f('35㎍/㎥'), '35마이크로그램 퍼 세제곱미터');
|
||||
});
|
||||
test('복합 단위가 단일 단위보다 먼저', () => {
|
||||
assert.equal(f('3.2m/s'), '3.2미터 매 초');
|
||||
assert.equal(f('40km/h'), '40킬로미터 매 시');
|
||||
assert.equal(f('2.5mm'), '2.5밀리미터');
|
||||
});
|
||||
test('조사가 붙어도 변환, 영단어·숫자 없는 글자는 그대로', () => {
|
||||
assert.equal(f('100m를 달렸다'), '100미터를 달렸다');
|
||||
assert.equal(f('5mins left'), '5mins left');
|
||||
assert.equal(f('The A team and g'), 'The A team and g');
|
||||
});
|
||||
test('섭씨·달러', () => {
|
||||
assert.equal(f('21.5℃'), '섭씨 21.5도');
|
||||
assert.equal(f('$100'), '100달러');
|
||||
});
|
||||
});
|
||||
@@ -2086,6 +2086,7 @@
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<script src="../js/voice/speech-units.js"></script>
|
||||
<script src="../js/voice/voice-call.js"></script>
|
||||
<script src="../js/app/helpers-core.js"></script>
|
||||
<script src="../js/app/markdown.js"></script>
|
||||
|
||||
@@ -287,6 +287,7 @@ function _hourReplacer(m, numStr, suffix) {
|
||||
// stop generating audio entirely instead of just mispronouncing it, and a
|
||||
// bare "°C"/"°F" gets read as "그램"(grams) instead of degrees.
|
||||
function _cleanForSpeech(text) {
|
||||
text = (typeof window !== 'undefined' && window.spellUnitsForSpeech) ? window.spellUnitsForSpeech(text || '') : (text || ''); // g/kg/GB/Hz… 단위 낭독(speech-units.js)
|
||||
return (text || '')
|
||||
.replace(/!\[.*?\]\(.*?\)/g, '')
|
||||
.replace(/\[([^\]]+)\]\([^)]+\)/g, '$1')
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
// speech-units.js — 음성 읽기(TTS)용 단위 기호 → 한글 낭독 변환
|
||||
//
|
||||
// OmniVoice는 숫자 뒤의 영문 단위 기호를 글자 이름으로 읽는다("50g"를 "오십 지"). 2026-10-17 통화에서 "그램을
|
||||
// 지라고 읽는다"는 신고가 있었다 — 기존 목록엔 %, km/h, m/s, mm, km, °C만 있었다. 숫자에 붙은 단위만 바꾸므로
|
||||
// 일반 영단어는 건드리지 않는다. voice-call.js(통화)와 voice.js(읽기 버튼)가 같이 쓴다.
|
||||
(function (root) {
|
||||
// [기호, 읽는 말]. 복합 단위가 단일 단위보다 먼저 잡히도록 길이 내림차순으로 정렬해서 쓴다.
|
||||
var UNITS = [
|
||||
['km/h', '킬로미터 매 시'], ['mm/h', '밀리미터 매 시'], ['m/s', '미터 매 초'],
|
||||
['µg/m³', '마이크로그램 퍼 세제곱미터'], ['μg/m³', '마이크로그램 퍼 세제곱미터'], ['㎍/㎥', '마이크로그램 퍼 세제곱미터'], ['ug/m3', '마이크로그램 퍼 세제곱미터'],
|
||||
['tok/s', '토큰 매 초'], ['GB/s', '기가바이트 매 초'], ['MB/s', '메가바이트 매 초'], ['Gbps', '기가비피에스'], ['Mbps', '메가비피에스'],
|
||||
['mmHg', '밀리미터 수은주'], ['kcal', '킬로칼로리'], ['kWh', '킬로와트시'],
|
||||
['GHz', '기가헤르츠'], ['MHz', '메가헤르츠'], ['kHz', '킬로헤르츠'], ['Hz', '헤르츠'],
|
||||
['TB', '테라바이트'], ['GB', '기가바이트'], ['MB', '메가바이트'], ['KB', '킬로바이트'], ['kB', '킬로바이트'],
|
||||
['hPa', '헥토파스칼'], ['kPa', '킬로파스칼'], ['MPa', '메가파스칼'], ['Pa', '파스칼'],
|
||||
['kW', '킬로와트'], ['mA', '밀리암페어'], ['ppm', '피피엠'], ['rpm', '알피엠'], ['RPM', '알피엠'], ['bpm', '비피엠'],
|
||||
['kg', '킬로그램'], ['mg', '밀리그램'], ['cm', '센티미터'], ['mm', '밀리미터'], ['km', '킬로미터'], ['nm', '나노미터'],
|
||||
['µm', '마이크로미터'], ['μm', '마이크로미터'], ['ml', '밀리리터'], ['mL', '밀리리터'], ['dL', '데시리터'],
|
||||
['m²', '제곱미터'], ['㎡', '제곱미터'], ['m³', '세제곱미터'], ['㎥', '세제곱미터'], ['cal', '칼로리'], ['cc', '시시'],
|
||||
['g', '그램'], ['m', '미터'], ['L', '리터'], ['ℓ', '리터'], ['W', '와트'], ['V', '볼트'], ['A', '암페어'],
|
||||
['%', '퍼센트'], ['℃', '도'],
|
||||
];
|
||||
UNITS.sort(function (a, b) { return b[0].length - a[0].length; });
|
||||
var MAP = {};
|
||||
var alts = UNITS.map(function (u) {
|
||||
MAP[u[0]] = u[1];
|
||||
return u[0].replace(/[.*+?^${}()|[\]\\\/]/g, '\\$&');
|
||||
});
|
||||
// 숫자 + (공백) + 단위. 뒤에 영문자가 더 붙으면(예: "5mins") 단위가 아니므로 제외.
|
||||
var UNIT_RE = new RegExp('(\\d+(?:\\.\\d+)?)\\s*(' + alts.join('|') + ')(?![A-Za-z])', 'g');
|
||||
|
||||
function spellUnitsForSpeech(text) {
|
||||
return String(text == null ? '' : text)
|
||||
// 달러: "$100" → "100달러"
|
||||
.replace(/\$\s?(\d[\d.]*)/g, '$1달러')
|
||||
.replace(/(\d+(?:\.\d+)?)\s*℃/g, '섭씨 $1도')
|
||||
.replace(UNIT_RE, function (m, num, unit) { return MAP[unit] ? num + MAP[unit] : m; });
|
||||
}
|
||||
|
||||
if (typeof module !== 'undefined' && module.exports) module.exports = { spellUnitsForSpeech: spellUnitsForSpeech };
|
||||
if (root) root.spellUnitsForSpeech = spellUnitsForSpeech;
|
||||
})(typeof window !== 'undefined' ? window : (typeof globalThis !== 'undefined' ? globalThis : null));
|
||||
@@ -394,6 +394,7 @@
|
||||
}
|
||||
|
||||
function sanitizeForSpeech(text) {
|
||||
text = window.spellUnitsForSpeech ? window.spellUnitsForSpeech(text) : text; // g/kg/GB/Hz… 단위 낭독(speech-units.js)
|
||||
return text
|
||||
// 천 단위 쉼표(9,500 / 1,234,567)는 숫자 낭독 전에 제거한다. 안 그러면 "9"와 "500"이 따로 읽혀
|
||||
// "9,500원"이 "구, 오백원"이 되어 "천"이 통째로 사라진다(09-26 "단어가 한번씩 빠진다", 표의 가격 열).
|
||||
|
||||
Reference in new issue
Block a user