Skip to content
This repository has been archived by the owner on May 14, 2024. It is now read-only.

Commit

Permalink
Merge pull request #276 from i3thuan5/翻譯改過渡
Browse files Browse the repository at this point in the history
翻譯改過渡 fix #272
  • Loading branch information
sih4sing5hong5 committed Jun 11, 2018
2 parents 025307d + c6d1553 commit 7ab8d51
Show file tree
Hide file tree
Showing 17 changed files with 451 additions and 498 deletions.
70 changes: 70 additions & 0 deletions .dockerignore
@@ -0,0 +1,70 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]

# C extensions
*.so

# Distribution / packaging
.Python
env/
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
*.egg-info/
.installed.cfg
*.egg

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*,cover

# Translations
*.mo
*.pot

# Django stuff:
*.log

# Sphinx documentation
docs/_build/

# PyBuilder
target/

/.settings

/venv
MANIFEST
/node_modules/
/.project
/.pydevproject
/.git
/Dockerfile
/docker-compose.yml
/外部程式

43 changes: 10 additions & 33 deletions .travis.yml
Expand Up @@ -6,49 +6,26 @@ python:
env:
- TOX_ENV=tan1uan5
- TOX_ENV=hap8sing5-tsi3giam7
- TOX_ENV=hap8sing5-hun3lian7
# - TOX_ENV=hap8sing5-hun3lian7
- TOX_ENV=huan1ik8
# - TOX_ENV=huan1ik8-gua7bun5
- TOX_ENV=huan1ik8-tsing3kui1
- TOX_ENV=flake8
addons:
apt:
sources:
- ubuntu-toolchain-r-test
packages:
- gcc-4.9
- g++-4.9
- python3-dev
- libc6-dev-i386
- linux-libc-dev
- gcc-multilib
- git
- subversion
- automake
- libtool
- zlib1g-dev
- libbz2-dev
- liblzma-dev
- libboost-all-dev
- libgoogle-perftools-dev
- libxmlrpc-c++.*-dev
- cmake
- csh
- libyaml-dev
- libxslt1-dev
- libav-tools
- libmp3lame0
services:
- docker
- rabbitmq
install:
- sudo apt-get install -y libavcodec-extra-54 libx11-dev libx11-dev:i386
- pip install tox
- pip install python-coveralls
branches:
only:
- master
- "/\\d+\\.\\d+\\.\\d+/"
before_script:
- sudo rabbitmqctl add_user ti1a2 gau5tsa2
- sudo rabbitmqctl add_vhost hok8_bu7
- sudo rabbitmqctl set_permissions -p hok8_bu7 ti1a2 '.*' '.*' '.*'
script:
- tox -e $TOX_ENV
- docker build -t tai5-uan5_gian5-gi2_hok8-bu7_travis --build-arg TOX_ENV=$TOX_ENV .
after_success:
- docker run --rm tai5-uan5_gian5-gi2_hok8-bu7_travis cat .coverage > .coverage
- coverage report
- coveralls
deploy:
Expand Down
11 changes: 11 additions & 0 deletions Dockerfile
@@ -0,0 +1,11 @@
FROM i3thuan5/tai5-uan5_gian5-gi2_kang1-ku7:0.6.40
MAINTAINER i3thuan5

ARG TOX_ENV

RUN mkdir -p /usr/local/tai5-uan5_gian5-gi2_hok8-bu7
WORKDIR /usr/local/tai5-uan5_gian5-gi2_hok8-bu7
RUN pip3 install tox
COPY . .
RUN echo RABBIT_MQ_TSU2_KI1 = \'`/sbin/ip route|awk '/default/ { print $3 }'`\' >> tai5uan5_gian5gi2_hok8bu7/settings.py
RUN LANG=zh_TW.utf8 tox --sitepackages -e ${TOX_ENV}
13 changes: 12 additions & 1 deletion tai5uan5_gian5gi2_hok8bu7/celery.py
@@ -1,12 +1,23 @@
from __future__ import absolute_import, unicode_literals

import os

from celery import Celery
from django.conf import settings


# set the default Django settings module for the 'celery' program.
os.environ.setdefault(
'DJANGO_SETTINGS_MODULE', 'tai5uan5_gian5gi2_hok8bu7.settings')

app = Celery('tai5uan5_gian5gi2_hok8bu7')
try:
rabbitmq = 'amqp://ti1a2:gau5tsa2@{}:5672/hok8_bu7'.format(
settings.RABBIT_MQ_TSU2_KI1
)
except AttributeError:
rabbitmq = None

app = Celery('tai5uan5_gian5gi2_hok8bu7', broker=rabbitmq)

# Using a string here means the worker don't have to serialize
# the configuration object to child processes.
Expand Down
38 changes: 35 additions & 3 deletions tox.ini
Expand Up @@ -11,7 +11,8 @@ commands=
coverage run --source=臺灣言語服務 manage.py test -p test*合成整合試驗.py

[testenv:hap8sing5-hun3lian7]
whitelist_externals=bash
whitelist_externals =
bash
deps =
hue7jip8
coverage
Expand All @@ -25,12 +26,43 @@ commands=

[testenv:huan1ik8]
passenv = *
whitelist_externals =
bash
deps =
hue7jip8
coverage
commands=
bash 加匯入設定.sh
python manage.py migrate
python manage.py 教典例句
coverage run --source=臺灣言語服務 manage.py 訓練Moses 台語

[testenv:huan1ik8-gua7bun5]
passenv = *
whitelist_externals =
bash
deps =
hue7jip8
coverage
commands=
bash 加匯入設定.sh
python manage.py migrate
python manage.py 教典例句
coverage run --source=臺灣言語服務 manage.py 訓練Moses翻譯做外文 台語

[testenv:huan1ik8-tsing3kui1]
passenv = *
whitelist_externals =
bash
deps =
hue7jip8
coverage
commands=
bash 加匯入設定.sh
python manage.py migrate
coverage run --source=臺灣言語服務 manage.py 訓練Moses 詔安腔
coverage run --source=臺灣言語服務 -a manage.py test 試驗.翻譯.test詔安腔翻譯整合試驗.詔安腔翻譯整合試驗
python manage.py 教典例句
coverage run --source=臺灣言語服務 manage.py 訓練Moses正規化 台語
coverage run --source=臺灣言語服務 -a manage.py test 試驗.翻譯.test台語正規化整合試驗.台語正規化整合試驗

[testenv:flake8]
deps =
Expand Down
4 changes: 2 additions & 2 deletions 臺灣言語服務/Moses介面.py
Expand Up @@ -27,7 +27,7 @@ def 正規化翻譯(self, request):
if 查詢腔口 not in self.服務.支援腔口():
raise RuntimeError()
except (KeyError, RuntimeError):
查詢腔口 = '閩南語'
查詢腔口 = '台語'
try:
查詢語句 = 連線參數['查詢語句']
except KeyError:
Expand All @@ -50,7 +50,7 @@ def 標漢字音標(self, request):
if 查詢腔口 not in self.服務.支援腔口():
raise RuntimeError()
except (KeyError, RuntimeError):
查詢腔口 = '閩南語'
查詢腔口 = '台語'
try:
查詢語句 = 連線參數['查詢語句']
except KeyError:
Expand Down
109 changes: 92 additions & 17 deletions 臺灣言語服務/Moses模型訓練.py
@@ -1,9 +1,11 @@
# -*- coding: utf-8 -*-
import gzip
from os import makedirs
from os.path import join, basename
from sys import stderr
from tempfile import TemporaryDirectory


from 臺灣言語服務.輸出 import 資料輸出工具
from 臺灣言語工具.翻譯.摩西工具.摩西翻譯模型訓練 import 摩西翻譯模型訓練
from 臺灣言語工具.翻譯.摩西工具.語句編碼器 import 語句編碼器
from 臺灣言語工具.系統整合.程式腳本 import 程式腳本
Expand All @@ -15,35 +17,95 @@
from 臺灣言語工具.語言模型.KenLM語言模型訓練 import KenLM語言模型訓練
from 臺灣言語工具.斷詞.拄好長度辭典揣詞 import 拄好長度辭典揣詞
from 臺灣言語工具.斷詞.語言模型揀集內組 import 語言模型揀集內組
from 臺灣言語服務.資料模型路徑 import 翻譯語料資料夾
from 臺灣言語服務.資料模型路徑 import 翻譯模型資料夾
from 臺灣言語工具.解析整理.解析錯誤 import 解析錯誤
from 臺灣言語服務.models import 訓練過渡格式
from 臺灣言語工具.斷詞.國教院斷詞用戶端 import 國教院斷詞用戶端


class Moses模型訓練(程式腳本):

@classmethod
def 輸出全部語料(cls):
資料輸出工具().輸出翻譯語料()
def 輸出全部語料(cls, 資料夾):
makedirs(資料夾, exist_ok=True)

對齊語句數量 = 0
with cls._照資料夾開壓縮檔(資料夾, '對齊母語語句.txt.gz') as 對齊母語語句:
with cls._照資料夾開壓縮檔(資料夾, '對齊外語語句.txt.gz') as 對齊外語語句:
for 一筆 in 訓練過渡格式.objects.filter(
文本__isnull=False, 外文__isnull=False,
種類='語句'
):
print(一筆.文本, file=對齊母語語句)
print(一筆.外文, file=對齊外語語句)
對齊語句數量 += 1

對齊字詞數量 = 0
with cls._照資料夾開壓縮檔(資料夾, '對齊母語字詞.txt.gz') as 對齊母語字詞:
with cls._照資料夾開壓縮檔(資料夾, '對齊外語字詞.txt.gz') as 對齊外語字詞:
for 一筆 in 訓練過渡格式.objects.filter(
文本__isnull=False, 外文__isnull=False,
種類='字詞'
):
print(一筆.文本, file=對齊母語字詞)
print(一筆.外文, file=對齊外語字詞)
對齊字詞數量 += 1

語句數 = 0
with cls._照資料夾開壓縮檔(資料夾, '語句文本.txt.gz') as 語句文本:
for 一筆 in 訓練過渡格式.objects.filter(文本__isnull=False, 種類='語句'):
print(一筆.文本, file=語句文本)
語句數 += 1
字詞數 = 0
with cls._照資料夾開壓縮檔(資料夾, '字詞文本.txt.gz') as 字詞文本:
for 一筆 in 訓練過渡格式.objects.filter(文本__isnull=False, 種類='字詞'):
print(一筆.文本, file=字詞文本)
字詞數 += 1
return 對齊語句數量, 對齊字詞數量, 語句數, 字詞數

@classmethod
def 訓練一个摩西翻譯模型(cls, 語言, 語族):
語料資料夾 = 翻譯語料資料夾(語言)
模型資料夾 = 翻譯模型資料夾(語言)
def _照資料夾開壓縮檔(cls, 資料夾, 檔名):
return gzip.open(join(資料夾, 檔名), 'wt')

@classmethod
def 訓練翻譯做母語模型(cls, 語料資料夾, 模型資料夾):
平行華語, 平行母語, 母語文本 = cls._原始語料(語料資料夾)
模型訓練 = 摩西翻譯模型訓練()
模型訓練.訓練(
平行華語, 平行母語, 母語文本,
模型資料夾,
連紲詞長度=3,
編碼器=語句編碼器(), # 若用著Unicdoe擴充就需要,
使用記憶體量='80%',
刣掉暫存檔=False,
)

@classmethod
def 訓練翻譯做外文模型(cls, 語料資料夾, 模型資料夾):
平行華語, 平行母語, _母語文本 = cls._原始語料(語料資料夾)
with TemporaryDirectory() as 暫存資料夾:
平行斷詞華語 = cls._外文斷詞(平行華語, 暫存資料夾)
模型訓練 = 摩西翻譯模型訓練()
模型訓練.訓練(
平行母語, 平行斷詞華語, 平行斷詞華語,
模型資料夾,
連紲詞長度=3,
編碼器=語句編碼器(), # 若用著Unicdoe擴充就需要,
使用記憶體量='80%',
刣掉暫存檔=False,
)

@classmethod
def 訓練正規化模型(cls, 語料資料夾, 模型資料夾):
makedirs(模型資料夾, exist_ok=True)
if 語族 == '漢語':
平行華語, 平行母語, 母語文本 = cls._漢語語料訓練(語料資料夾, 模型資料夾)
elif 語族 == '南島語':
平行華語, 平行母語, 母語文本 = cls._南島語語料訓練(語料資料夾, 模型資料夾)
else:
raise RuntimeError('{}的語族無設定!!'.format(語族))
平行華語, 平行母語, 母語文本 = cls._漢語語料訓練(語料資料夾, 模型資料夾)

模型訓練 = 摩西翻譯模型訓練()
模型訓練.訓練(
平行華語, 平行母語, 母語文本,
模型資料夾,
連紲詞長度=3,
編碼器=語句編碼器(), # 若用著Unicdoe擴充就需要,
使用記憶體量='80%',
刣掉暫存檔=False,
)

Expand Down Expand Up @@ -117,7 +179,7 @@ def _文本檔轉模型物件(cls, 語言資料夾, 母語辭典檔名):
母語辭典檔名,
join(語言資料夾, '語言模型資料夾'),
連紲詞長度=3,
使用記憶體量='20%',
使用記憶體量='80%',
)

return KenLM語言模型(模型檔)
Expand All @@ -139,5 +201,18 @@ def _原始語料(cls, 語言資料夾):
return 平行華語, 平行母語, 母語文本

@classmethod
def _南島語語料訓練(cls, 語言資料夾, 翻譯模型資料夾):
return cls._漢語語料訓練(語言資料夾, 翻譯模型資料夾)
def _外文斷詞(cls, 語料陣列, 暫存資料夾):
斷詞檔名 = join(暫存資料夾, '外文斷詞.txt.gz')
幾逝 = 0
with gzip.open(斷詞檔名, 'wt') as 寫檔:
for 原本檔案 in 語料陣列:
for 一逝 in cls._讀檔案(原本檔案):
斷詞結果 = []
句物件 = 拆文分析器.分詞句物件(一逝)
for 詞條, 詞性 in 國教院斷詞用戶端.語句斷詞做陣列(句物件.看型(' ', ' ')):
斷詞結果.append("{}-(-{}-)".format(詞條, 詞性))
print(' '.join(斷詞結果), file=寫檔)
幾逝 += 1
if 幾逝 % 100 == 0:
print('外文斷 {} 句矣'.format(幾逝), file=stderr)
return [斷詞檔名]

0 comments on commit 7ab8d51

Please sign in to comment.