Skip to content

Instantly share code, notes, and snippets.

@sgqy
Last active September 22, 2021 17:56
Show Gist options
  • Save sgqy/43e77d3f8bea5d9485de056edaf2c519 to your computer and use it in GitHub Desktop.
Save sgqy/43e77d3f8bea5d9485de056edaf2c519 to your computer and use it in GitHub Desktop.
OmegaT
fn = '/dev/shm/one-line.ogg'
import speech_bot
bot = speech_bot.speech('google-conf.json')
import sys
t = ''.join(sys.argv[1:])
bot.save('ja', t, fn)
#import os
#os.system('mplayer ' + fn)
public static String sysrun(String cmd, String dir) {
def proc = cmd.execute([], new File(dir));
proc.waitFor()
return proc.text
}
public static String conv(String s) {
return sysrun('python3 s2tw.py ' + s, '/home/aya/lab-art/OmegaT');
}
def edt(Closure c) {
if (javax.swing.SwingUtilities.isEventDispatchThread()) {
c.call(this)
} else {
javax.swing.SwingUtilities.invokeAndWait(c)
}
}
def cur = 0;
edt { cur = editor.getCurrentEntry().entryNum() };
for(def e in project.getAllEntries()) {
def src = e.getSrcText();
def idst = project.getTranslationInfo(e) ? project.getTranslationInfo(e).translation : null;
if (idst == null) {
continue;
}
dst = conv(idst)
mainWindow.showProgressMessage("${e.entryNum()}/${project.getAllEntries().size()}");
if (dst == idst) {
continue;
}
edt { editor.gotoEntry(e.entryNum()) };
edt { editor.replaceEditText(dst) };
}
edt { editor.gotoEntry(cur) };
mainWindow.showProgressMessage("${cur}: OpenCC Done.");
#!/usr/bin/env python3
import sys
import opencc
src = ''.join(sys.argv[1:])
conv = opencc.OpenCC('s2tw.json')
dst = conv.convert(src)
print(dst, end='')
public static String sysrun(String cmd, String dir) {
def proc = cmd.execute([], new File(dir));
proc.waitFor()
return proc.text
}
String r = editor.currentEntry.getSrcText();
int i = editor.getCurrentEntryNumber();
mainWindow.showProgressMessage("Google Speech: #${i}...");
cmd = 'python3 one_line_ja.py ' + r
'rm -f /dev/shm/one-line.ogg'.execute();
sysrun(cmd, '/home/aya/lab/google');
mainWindow.showProgressMessage("Google Speech: Done.");
'mplayer /dev/shm/one-line.ogg'.execute();
import os
from google.cloud import texttospeech
class speech:
def __init__(self, conf):
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = conf
self.c = texttospeech.TextToSpeechClient()
self.p_zh = texttospeech.VoiceSelectionParams(language_code="cmn-CN", name='cmn-CN-Wavenet-A')
self.p_ja = texttospeech.VoiceSelectionParams(language_code="ja-JP", name='ja-JP-Wavenet-B')
def conv(self, l, t, s=1.0):
if l == 'zh':
p = self.p_zh
elif l == 'ja':
p = self.p_ja
else:
p = None
print(f'Text length {len(t)}')
syn = texttospeech.SynthesisInput(text=t)
ac = texttospeech.AudioConfig(audio_encoding=texttospeech.AudioEncoding.OGG_OPUS, speaking_rate=s)
r = self.c.synthesize_speech(input=syn, voice=p, audio_config=ac)
return r.audio_content
def save(self, l, t, f, s=1.0):
if(len(t) < 1):
return
with open(f, 'wb') as fd:
fd.write(self.conv(l, t, s))
print(f'Saved to {f}')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment