Added new logic (temporary logic for getting files by search) and converting from files to txt

This commit is contained in:
Kikimanox
2022-08-29 22:53:49 +02:00
parent dd42b56c28
commit c8586513a9
6 changed files with 131 additions and 45 deletions
@@ -1,7 +1,13 @@
import connexion
import six
# import tika
from swagger_server import util
import pandas as pd
import docx
import codecs
import xml.etree.ElementTree as ET
from PyPDF2 import PdfReader
def datoteka_v_besedilo_post(file=None): # noqa: E501
@@ -14,7 +20,27 @@ def datoteka_v_besedilo_post(file=None): # noqa: E501
:rtype: str
"""
return 'do some magic!'
if file is None:
return "No file provided", 400
elif "openxmlformats-officedocument.wordprocessingml.document" in file.content_type:
content = [p.text for p in docx.Document(file).paragraphs]
elif "text/plain" in file.content_type:
content = file.read().decode('utf-8')
elif "application/pdf" in file.content_type:
reader = PdfReader(file)
content = '\n'.join([p.extract_text() for p in reader.pages])
content = "ZACASNO UPORABLJEN DRUGI BRALEC KOT TIKA, TA BO SE DODANA KASNEJE...\n\n" + content
elif "text/xml" in file.content_type:
root = ET.parse(file).getroot()
plainText = root.findall('PlainText')
if len(plainText) == 0:
return "Didn't find anything in PlainText", 400
content = '\n'.join([pt.text for pt in plainText])
else:
return "Currently supporing only docx, txt/plain, pdf, xml*... more will be added later", 501
return content, 200
def get_text_ocr(file=None): # noqa: E501
@@ -27,4 +53,4 @@ def get_text_ocr(file=None): # noqa: E501
:rtype: str
"""
return 'do some magic!'
return 'Not yet implemented'
@@ -18,4 +18,4 @@ def get_candidates(body): # noqa: E501
"""
if connexion.request.is_json:
body = IzlusciBody.from_dict(connexion.request.get_json()) # noqa: E501
return 'do some magic!'
return 'do some magic!3'
+57 -36
View File
@@ -8,22 +8,6 @@ from flask import send_file
from swagger_server.db_utils import Ngrams_Manager
def get_conllu(file_id): # noqa: E501
"""Vrne CoNNL-U po id-ju datoteke
# noqa: E501
:param file_id:
:type file_id: int
:rtype: str
"""
try:
return send_file(util.get_conllu_path_by_id(file_id), attachment_filename=f'{file_id}.conllu')
except FileNotFoundError as e:
return "The conllu with this ID doesn't exist.", 404
def get_conllus(leta, vrste, kljucnebesede, cerifpodrocja): # noqa: E501
"""Vrne seznam CoNNL-U-jev glede na iskalne pogoje
@@ -40,7 +24,12 @@ def get_conllus(leta, vrste, kljucnebesede, cerifpodrocja): # noqa: E501
:rtype: List[str]
"""
return 'do some magic!'
if not kljucnebesede:
return "Manjkajo kljucne besede", 400
files = util.get_files_by_keywords(kljucnebesede)
if not files:
return 'Nobena datoteka ne ustreza iskalnemu pogoju', 404
return ' '.join(files), 200
def get_extracted_words(leta, vrste, kljucnebesede, cerifpodrocja): # noqa: E501
@@ -59,20 +48,7 @@ def get_extracted_words(leta, vrste, kljucnebesede, cerifpodrocja): # noqa: E50
:rtype: List[TerminoloskiKandidat]
"""
return 'do some magic!'
def get_file(file_id): # noqa: E501
"""Vrne binarni zapis v originalnem formatu po id-ju datoteke
# noqa: E501
:param file_id:
:type file_id: int
:rtype: List[bytearray]
"""
return 'do some magic!'
return 'do some magic!5'
def get_files(leta, vrste, kljucnebesede, cerifpodrocja): # noqa: E501
@@ -91,7 +67,12 @@ def get_files(leta, vrste, kljucnebesede, cerifpodrocja): # noqa: E501
:rtype: List[List[bytearray]]
"""
return 'do some magic!'
if not kljucnebesede:
return "Manjkajo kljucne besede", 400
files = util.get_files_by_keywords(kljucnebesede)
if not files:
return 'Nobena datoteka ne ustreza iskalnemu pogoju', 404
return ' '.join(files), 200
def get_number_texts(leta, vrste, kljucnebesede, cerifpodrocja): # noqa: E501
@@ -110,7 +91,10 @@ def get_number_texts(leta, vrste, kljucnebesede, cerifpodrocja): # noqa: E501
:rtype: int
"""
return 'do some magic!'
if not kljucnebesede:
return "Manjkajo kljucne besede", 400
files = util.get_files_by_keywords(kljucnebesede)
return len(files), 200
def get_texts(leta, vrste, kljucnebesede, cerifpodrocja): # noqa: E501
@@ -129,7 +113,44 @@ def get_texts(leta, vrste, kljucnebesede, cerifpodrocja): # noqa: E501
:rtype: List[str]
"""
return 'do some magic!'
if not kljucnebesede:
return "Manjkajo kljucne besede", 400
files = util.get_files_by_keywords(kljucnebesede)
if not files:
return 'Nobena datoteka ne ustreza iskalnemu pogoju', 404
return ' '.join(files), 200
def get_conllu(file_id): # noqa: E501
"""Vrne CoNNL-U po id-ju datoteke
# noqa: E501
:param file_id:
:type file_id: int
:rtype: str
"""
try:
return send_file(util.get_conllu_file_path_by_id(file_id), attachment_filename=f'{file_id}.conllu')
except FileNotFoundError as e:
return "The conllu with this ID doesn't exist.", 404
def get_file(file_id): # noqa: E501
"""Vrne binarni zapis v originalnem formatu po id-ju datoteke
# noqa: E501
:param file_id:
:type file_id: int
:rtype: List[bytearray]
"""
try:
return send_file(util.get_original_file_path_by_id(file_id), attachment_filename=f'{file_id}.xml')
except FileNotFoundError as e:
return "The file with this ID doesn't exist.", 404
def oss_besedilo_po_id_get(file_id): # noqa: E501
@@ -143,6 +164,6 @@ def oss_besedilo_po_id_get(file_id): # noqa: E501
:rtype: str
"""
try:
return send_file(util.get_original_file_by_id(file_id), attachment_filename=f'{file_id}.conllu')
return send_file(util.get_original_file_path_by_id(file_id), attachment_filename=f'{file_id}.xml')
except FileNotFoundError as e:
return "The conllu with this ID doesn't exist.", 404
return "The file with this ID doesn't exist.", 404