mirror of
https://github.com/leon-ai/leon.git
synced 2025-01-07 02:06:56 +03:00
refactor(server): lang helper to TypeScript
This commit is contained in:
parent
dabd657acf
commit
c979a9ea08
@ -4,7 +4,7 @@ import { LangAll } from '@nlpjs/lang-all'
|
||||
import dotenv from 'dotenv'
|
||||
|
||||
import log from '@/helpers/log'
|
||||
import lang from '@/helpers/lang'
|
||||
import { getShortLanguages } 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 = lang.getShortLangs()
|
||||
const shortLangs = getShortLanguages()
|
||||
for (let h = 0; h < shortLangs.length; h += 1) {
|
||||
const lang = shortLangs[h]
|
||||
|
||||
|
@ -1,5 +1,7 @@
|
||||
import dotenv from 'dotenv'
|
||||
|
||||
import type { LongLanguageCode } from '@/helpers/lang'
|
||||
|
||||
dotenv.config()
|
||||
|
||||
const PRODUCTION_ENV = 'production'
|
||||
@ -11,7 +13,7 @@ export const IS_DEVELOPMENT_ENV =
|
||||
process.env['LEON_NODE_ENV'] === DEVELOPMENT_ENV
|
||||
export const IS_TESTING_ENV = process.env['LEON_NODE_ENV'] === TESTING_ENV
|
||||
|
||||
export const LANG = process.env['LEON_LANG']
|
||||
export const LANG = process.env['LEON_LANG'] as LongLanguageCode
|
||||
|
||||
export const HOST = process.env['LEON_HOST']
|
||||
export const PORT = process.env['LEON_PORT']
|
||||
|
@ -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 lang from '@/helpers/lang'
|
||||
import { getLongLanguageCode } from '@/helpers/lang'
|
||||
import { getSkillDomainInfo, getSkillInfo } from '@/helpers/skill-domain'
|
||||
import json from '@/helpers/json'
|
||||
|
||||
@ -157,7 +157,7 @@ class Brain {
|
||||
// Ask to repeat if Leon is not sure about the request
|
||||
if (
|
||||
obj.classification.confidence <
|
||||
langs[lang.getLongCode(this._lang)].min_confidence
|
||||
langs[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 lang from '@/helpers/lang'
|
||||
import { getShortLanguages, getLongLanguageCode } from '@/helpers/lang'
|
||||
import TcpClient from '@/core/tcp-client'
|
||||
import Conversation from '@/core/conversation'
|
||||
|
||||
@ -539,8 +539,8 @@ class Nlu {
|
||||
}
|
||||
}
|
||||
|
||||
// Language isn't supported
|
||||
if (!lang.getShortLangs().includes(locale)) {
|
||||
const isSupportedLanguage = getShortLanguages().includes(locale)
|
||||
if (!isSupportedLanguage) {
|
||||
this.brain.talk(
|
||||
`${this.brain.wernicke('random_language_not_supported')}.`,
|
||||
true
|
||||
@ -558,7 +558,7 @@ class Nlu {
|
||||
|
||||
if (intent === 'None') {
|
||||
const fallback = this.fallback(
|
||||
langs[lang.getLongCode(locale)].fallbacks
|
||||
langs[getLongLanguageCode(locale)].fallbacks
|
||||
)
|
||||
|
||||
if (fallback === false) {
|
||||
|
@ -1,23 +0,0 @@
|
||||
import { langs } from '@@/core/langs.json'
|
||||
|
||||
const lang = {}
|
||||
|
||||
lang.getShortLangs = () => Object.keys(langs).map((lang) => langs[lang].short)
|
||||
|
||||
lang.getLongCode = (shortLang) => {
|
||||
const langsArr = Object.keys(langs)
|
||||
|
||||
for (let i = 0; i < langsArr.length; i += 1) {
|
||||
const { short } = langs[langsArr[i]]
|
||||
|
||||
if (short === shortLang) {
|
||||
return langsArr[i]
|
||||
}
|
||||
}
|
||||
|
||||
return null
|
||||
}
|
||||
|
||||
lang.getShortCode = (longLang) => langs[longLang].short
|
||||
|
||||
export default lang
|
40
server/src/helpers/lang.ts
Normal file
40
server/src/helpers/lang.ts
Normal file
@ -0,0 +1,40 @@
|
||||
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
|
||||
*/
|
||||
export type LongLanguageCode = keyof Languages
|
||||
|
||||
export type Language = Languages[LongLanguageCode]
|
||||
|
||||
export type ShortLanguageCode = Language['short']
|
||||
|
||||
export function getShortLanguages(): ShortLanguageCode[] {
|
||||
const longLanguages = Object.keys(langs) as LongLanguageCode[]
|
||||
return longLanguages.map((lang) => {
|
||||
return langs[lang].short
|
||||
})
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
export function getShortLanguageCode(
|
||||
longLanguage: LongLanguageCode
|
||||
): ShortLanguageCode {
|
||||
return langs[longLanguage].short
|
||||
}
|
@ -3,17 +3,18 @@ import { spawn } from 'child_process'
|
||||
import {
|
||||
TCP_SERVER_HOST,
|
||||
TCP_SERVER_PORT,
|
||||
IS_DEVELOPMENT_ENV
|
||||
IS_DEVELOPMENT_ENV,
|
||||
LANG
|
||||
} from '@/constants'
|
||||
import lang from '@/helpers/lang'
|
||||
import { getShortLanguageCode } 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 ${lang.getShortCode(
|
||||
process.env['LEON_LANG']
|
||||
`pipenv run python bridges/python/tcp_server/main.py ${getShortLanguageCode(
|
||||
LANG
|
||||
)}`,
|
||||
{
|
||||
shell: true,
|
||||
|
@ -3,7 +3,7 @@ import fs from 'fs'
|
||||
import path from 'path'
|
||||
|
||||
import log from '@/helpers/log'
|
||||
import lang from '@/helpers/lang'
|
||||
import { getLongLanguageCode } 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(lang.getLongCode(this.lang))
|
||||
this.synthesizer.default.init(getLongLanguageCode(this.lang))
|
||||
|
||||
this.onSaved()
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user