Skip to content

Commit

Permalink
<add> klang controller and service
Browse files Browse the repository at this point in the history
  • Loading branch information
kirianguiller committed Nov 10, 2020
1 parent 07a5bc7 commit 7ab88eb
Show file tree
Hide file tree
Showing 10 changed files with 87 additions and 2,989 deletions.
6 changes: 3 additions & 3 deletions app/klang/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
BASE_ROUTE = "klang"


# def register_routes(api, app, root="api"):
# from .controller import api as project_api
def register_routes(api, app, root="api"):
from .controller import api as project_api

# api.add_namespace(project_api, path=f"/{root}/{BASE_ROUTE}")
api.add_namespace(project_api, path=f"/{root}/{BASE_ROUTE}")
31 changes: 31 additions & 0 deletions app/klang/controller.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from datetime import datetime
from typing import List

from flask import session
from flask_accepts.decorators.decorators import responds
from flask_restx import Namespace, Resource

from .service import ConllService

api = Namespace("Klang", description="Single namespace, single entity") # noqa

@api.route("/conlls")
class ConllServiceResource(Resource):
"ConllService"

def get(self) :
return ConllService.get_all()


@api.route("/conll/<string:conll_name>")
class ConllNameServiceResource(Resource):
"ConllService"

def get(self, conll_name) :
conll_string = ConllService.get_by_name(conll_name)
sentences_string = ConllService.seperate_conll_sentences(conll_string)
sentences_audio_token = []
for sentence_string in sentences_string:
audio_tokens = ConllService.sentence_to_audio_tokens(sentence_string)
sentences_audio_token.append(audio_tokens)
return sentences_audio_token

This file was deleted.

This file was deleted.

853 changes: 0 additions & 853 deletions app/klang/data_prod/Zaynab_Affes/Zaynab_Affes.intervals.conll

This file was deleted.

810 changes: 0 additions & 810 deletions app/klang/data_prod/Zoe_Sacotte/Zoe_Sacotte.intervals.conll

This file was deleted.

38 changes: 30 additions & 8 deletions app/klang/service.py
Original file line number Diff line number Diff line change
@@ -1,33 +1,55 @@
import os
import re
from typing import List

from app import klang_config

align_begin_and_end_regex = re.compile(
r"^\d+\t(.+?)\t.*AlignBegin=(\d+).*AlignEnd=(\d+)"
)

class ConllService:
@staticmethod
def get_path_data():
path_data = klang_config.path
return path_data

@staticmethod
def get_path_conll(file_name_suffix):
file_name = file_name_suffix + ".intervals.conll"
path_data = ConllService.get_path_data()
path_conll = os.path.join(path_data,file_name_suffix, file_name)
path_conll = os.path.join(path_data, file_name_suffix, file_name)
return path_conll

@staticmethod
def read_conll(path_conll):
with open(path_conll, "r", encoding="utf-8") as infile:
conll = infile.read()
return conll

return conll

@staticmethod
def get_all():
path_data = ConllService.get_path_data()
conlls = os.listdir(path_data)
return conlls

# @staticmethod
# def get_by_name(conll_name):
# with
# return True
@staticmethod
def get_by_name(conll_name):
path_conll = ConllService.get_path_conll(conll_name)
conll_string = ConllService.read_conll(path_conll)
return conll_string

@staticmethod
def seperate_conll_sentences(conll_string: str) -> List[str]:
return list(filter(lambda x: x != "", conll_string.split("\n\n")))

@staticmethod
def sentence_to_audio_tokens(sentence_string: str):
audio_tokens = []
for line in sentence_string.split("\n"):
if line:
if not line.startswith("#"):
m = align_begin_and_end_regex.search(line)
audio_tokens += [(m.group(1), int(m.group(2)), int(m.group(3)))]

return audio_tokens
29 changes: 21 additions & 8 deletions app/klang/service_test.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
from .service import ConllService
from app.test.fixtures import app, db # noqa
import os

from app.test.fixtures import app, db # noqa

from .service import ConllService

folder_name = "data_test"
file_name = "John_Doe"


def test_get_path_data():
path_data = ConllService.get_path_data()
print("KK path_data", path_data)
Expand All @@ -23,9 +26,19 @@ def test_get_all():
conlls = ConllService.get_all()
assert conlls == ["John_Doe"]

# # def test_get_by_name():
# # # conll = ConllService.get_by_name("John_Doe")
# # conll_string = ConllService.get_by_name(file_name)
# # path_conll = ConllService.get_path_conll(folder_name, file_name)
# # conll_string = ConllService.read_conll(path_conll)
# # assert type(conll_string) == str
def test_get_by_name():
path_conll = ConllService.get_path_conll(file_name)
conll_string = ConllService.read_conll(path_conll)
assert conll_string == ConllService.get_by_name(file_name)

def test_seperate_conll_sentences():
conll_string = "# sent_id = test_sentence_1\n1\ttest_token\ntest_lemma\ntest_upos\n\n# sent_id = test_sentence_2\n1\ttest_token\ntest_lemma\ntest_upos\n\n"
sentences = ConllService.seperate_conll_sentences(conll_string)
assert len(sentences) == 2

def test_sentence_to_audio_tokens():
sentence = "# sent_id = test_sentence_1\n1\tdonc\tdonc\t_\t_\t_\t_\t_\t_\tAlignBegin=0|AlignEnd=454"
audio_tokens = ConllService.sentence_to_audio_tokens(sentence)
assert audio_tokens[0][0] == "donc"
assert audio_tokens[0][1] == 0
assert audio_tokens[0][2] == 454
2 changes: 2 additions & 0 deletions app/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ def register_routes(api, app, root="api"):
from app.trees import register_routes as attach_trees
from app.lexicon import register_routes as attach_lexicon
from app.grew import register_routes as attach_grew
from app.klang import register_routes as attach_klang

# Add routes
attach_user(api, app, root)
Expand All @@ -13,3 +14,4 @@ def register_routes(api, app, root="api"):
attach_trees(api, app, root)
attach_lexicon(api, app, root)
attach_grew(api, app, root)
attach_klang(api, app, root)
1 change: 0 additions & 1 deletion wsgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,5 @@
env = os.getenv("FLASK_ENV") or "test"

app = create_app(env)
print("KK app", app)
if __name__ == "__main__":
app.run("0.0.0.0", 8080, debug=True)

0 comments on commit 7ab88eb

Please sign in to comment.