mirror of
https://github.com/leon-ai/leon.git
synced 2025-01-04 15:55:58 +03:00
refactor(server): lang helper to singleton
This commit is contained in:
parent
f21e43c16a
commit
8d3aee8da7
@ -4,7 +4,7 @@ import { LangAll } from '@nlpjs/lang-all'
|
||||
import dotenv from 'dotenv'
|
||||
|
||||
import { LOG } from '@/helpers/log'
|
||||
import { getShortLanguages } from '@/helpers/lang'
|
||||
import { LANG } from '@/helpers/lang'
|
||||
import trainGlobalResolvers from './train-resolvers-model/train-global-resolvers'
|
||||
import trainSkillsResolvers from './train-resolvers-model/train-skills-resolvers'
|
||||
import trainGlobalEntities from './train-main-model/train-global-entities'
|
||||
@ -84,7 +84,7 @@ export default () =>
|
||||
/**
|
||||
* Training phases
|
||||
*/
|
||||
const shortLangs = getShortLanguages()
|
||||
const shortLangs = LANG.getShortLanguages()
|
||||
for (let h = 0; h < shortLangs.length; h += 1) {
|
||||
const lang = shortLangs[h]
|
||||
|
||||
|
@ -7,7 +7,7 @@ import { HAS_TTS } from '@/constants'
|
||||
import { LOG } from '@/helpers/log'
|
||||
import { findAndMap, randomString } from '@/helpers/string'
|
||||
import Synchronizer from '@/core/synchronizer'
|
||||
import { getLongLanguageCode } from '@/helpers/lang'
|
||||
import { LANG } from '@/helpers/lang'
|
||||
import {
|
||||
getSkillDomainInfo,
|
||||
getSkillInfo,
|
||||
@ -160,7 +160,7 @@ class Brain {
|
||||
// Ask to repeat if Leon is not sure about the request
|
||||
if (
|
||||
obj.classification.confidence <
|
||||
langs[getLongLanguageCode(this._lang)].min_confidence
|
||||
langs[LANG.getLongLanguageCode(this._lang)].min_confidence
|
||||
) {
|
||||
if (!opts.mute) {
|
||||
const speech = `${this.wernicke('random_not_sure')}.`
|
||||
|
@ -19,7 +19,7 @@ import {
|
||||
import Ner from '@/core/ner'
|
||||
import { LOG } from '@/helpers/log'
|
||||
import { ucFirst } from '@/helpers/string'
|
||||
import { getShortLanguages, getLongLanguageCode } from '@/helpers/lang'
|
||||
import { LANG } from '@/helpers/lang'
|
||||
import TcpClient from '@/core/tcp-client'
|
||||
import Conversation from '@/core/conversation'
|
||||
|
||||
@ -539,7 +539,7 @@ class Nlu {
|
||||
}
|
||||
}
|
||||
|
||||
const isSupportedLanguage = getShortLanguages().includes(locale)
|
||||
const isSupportedLanguage = LANG.getShortLanguages().includes(locale)
|
||||
if (!isSupportedLanguage) {
|
||||
this.brain.talk(
|
||||
`${this.brain.wernicke('random_language_not_supported')}.`,
|
||||
@ -558,7 +558,7 @@ class Nlu {
|
||||
|
||||
if (intent === 'None') {
|
||||
const fallback = this.fallback(
|
||||
langs[getLongLanguageCode(locale)].fallbacks
|
||||
langs[LANG.getLongLanguageCode(locale)].fallbacks
|
||||
)
|
||||
|
||||
if (fallback === false) {
|
||||
|
@ -1,51 +1,69 @@
|
||||
import { langs } from '@@/core/langs.json'
|
||||
|
||||
export type Languages = typeof langs
|
||||
|
||||
/**
|
||||
* ISO 639-1 (Language codes) - ISO 3166-1 (Country Codes)
|
||||
* @see https://www.iso.org/iso-639-language-codes.html
|
||||
* @see https://www.iso.org/iso-3166-country-codes.html
|
||||
*/
|
||||
|
||||
type Languages = typeof langs
|
||||
export type LongLanguageCode = keyof Languages
|
||||
|
||||
export type Language = Languages[LongLanguageCode]
|
||||
|
||||
type Language = Languages[LongLanguageCode]
|
||||
export type ShortLanguageCode = Language['short']
|
||||
|
||||
/**
|
||||
* TODO
|
||||
*/
|
||||
export function getShortLanguages(): ShortLanguageCode[] {
|
||||
const longLanguages = Object.keys(langs) as LongLanguageCode[]
|
||||
return longLanguages.map((lang) => {
|
||||
return langs[lang].short
|
||||
})
|
||||
}
|
||||
class Lang {
|
||||
private static instance: Lang
|
||||
|
||||
/**
|
||||
* TODO
|
||||
*/
|
||||
export function getLongLanguageCode(
|
||||
shortLanguage: ShortLanguageCode
|
||||
): LongLanguageCode | null {
|
||||
for (const longLanguage in langs) {
|
||||
const longLanguageType = longLanguage as LongLanguageCode
|
||||
const lang = langs[longLanguageType]
|
||||
|
||||
if (lang.short === shortLanguage) {
|
||||
return longLanguageType
|
||||
}
|
||||
private constructor() {
|
||||
// Singleton
|
||||
}
|
||||
|
||||
return null
|
||||
public static getInstance() {
|
||||
if (Lang.instance == null) {
|
||||
Lang.instance = new Lang()
|
||||
}
|
||||
|
||||
return Lang.instance
|
||||
}
|
||||
|
||||
/**
|
||||
* Get short language codes
|
||||
* @example getShortLanguages() // ["en", "fr"]
|
||||
*/
|
||||
public getShortLanguages(): ShortLanguageCode[] {
|
||||
const longLanguages = Object.keys(langs) as LongLanguageCode[]
|
||||
|
||||
return longLanguages.map((lang) => langs[lang].short)
|
||||
}
|
||||
|
||||
/**
|
||||
* Get long language code of the given short language code
|
||||
* @param shortLanguageCode The short language code of the language
|
||||
* @example getLongLanguageCode('en') // en-US
|
||||
*/
|
||||
public getLongLanguageCode(shortLanguageCode: ShortLanguageCode) {
|
||||
for (const longLanguage in langs) {
|
||||
const longLanguageType = longLanguage as LongLanguageCode
|
||||
const lang = langs[longLanguageType]
|
||||
|
||||
if (lang.short === shortLanguageCode) {
|
||||
return longLanguageType
|
||||
}
|
||||
}
|
||||
|
||||
return null
|
||||
}
|
||||
|
||||
/**
|
||||
* Get short language code of the given long language code
|
||||
* @param longLanguageCode The long language code of the language
|
||||
* @example getShortLanguageCode('en-US') // en
|
||||
*/
|
||||
public getShortLanguageCode(
|
||||
longLanguageCode: LongLanguageCode
|
||||
): ShortLanguageCode {
|
||||
return langs[longLanguageCode].short
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO
|
||||
*/
|
||||
export function getShortLanguageCode(
|
||||
longLanguage: LongLanguageCode
|
||||
): ShortLanguageCode {
|
||||
return langs[longLanguage].short
|
||||
}
|
||||
export const LANG = Lang.getInstance()
|
||||
|
@ -4,17 +4,17 @@ import {
|
||||
TCP_SERVER_HOST,
|
||||
TCP_SERVER_PORT,
|
||||
IS_DEVELOPMENT_ENV,
|
||||
LANG
|
||||
LANG as LEON_LANG
|
||||
} from '@/constants'
|
||||
import { getShortLanguageCode } from '@/helpers/lang'
|
||||
import { LANG } from '@/helpers/lang'
|
||||
import TcpClient from '@/core/tcp-client'
|
||||
import server from '@/core/http-server/server'
|
||||
;(async () => {
|
||||
process.title = 'leon'
|
||||
|
||||
global.tcpServerProcess = spawn(
|
||||
`pipenv run python bridges/python/tcp_server/main.py ${getShortLanguageCode(
|
||||
LANG
|
||||
`pipenv run python bridges/python/tcp_server/main.py ${LANG.getShortLanguageCode(
|
||||
LEON_LANG
|
||||
)}`,
|
||||
{
|
||||
shell: true,
|
||||
|
@ -3,7 +3,7 @@ import fs from 'fs'
|
||||
import path from 'path'
|
||||
|
||||
import { LOG } from '@/helpers/log'
|
||||
import { getLongLanguageCode } from '@/helpers/lang'
|
||||
import { LANG } from '@/helpers/lang'
|
||||
|
||||
class Tts {
|
||||
constructor(socket, provider) {
|
||||
@ -57,7 +57,7 @@ class Tts {
|
||||
|
||||
// Dynamically attribute the synthesizer
|
||||
this.synthesizer = require(`${__dirname}/${this.provider}/synthesizer`) // eslint-disable-line global-require
|
||||
this.synthesizer.default.init(getLongLanguageCode(this.lang))
|
||||
this.synthesizer.default.init(LANG.getLongLanguageCode(this.lang))
|
||||
|
||||
this.onSaved()
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user