<commit_before>"""Main API file for backend CLTK webapp."""

import os

from flask import Flask, request
from flask_restful import Resource, Api

app = Flask(__name__)
api = Api(app)

# example
class HelloWorld(Resource):
    def get(self):
        return {'hello': 'world'}

# example
todos = {}
class TodoSimple(Resource):
    def get(self, todo_id):
        return {'example with token': todo_id}


class Authors(Resource):
    def get(self, lang):
        #assert lang in ['greek', 'latin']
        text_path = os.path.expanduser('~/cltk_data/' + lang + '/text/' + lang + '_text_perseus/')
        dir_contents = os.listdir(text_path)
        remove_files = ['README.md', '.git', 'LICENSE.md', 'perseus_compiler.py', '.DS_Store']
        dir_contents = [f for f in dir_contents if f not in remove_files]
        return {'authors': sorted(dir_contents)}


class Texts(Resource):
    def get(self, lang, author_name):
        text_path = os.path.expanduser('~/cltk_data/' + lang + '/text/' + lang + '_text_perseus/' + author_name.casefold() + '/opensource')  # casefold() prob not nec
        dir_contents = os.listdir(text_path)
        if lang == 'greek':
            ending = '_gk.xml.json'
        elif lang == 'latin':
            ending = '_lat.xml.json'
        dir_contents = [f for f in dir_contents if f.endswith(ending)]
        dir_contents = [f.casefold() for f in dir_contents]  # this probably isn't nec
        return {'texts': sorted(dir_contents)}

class Text(Resource):
    def get(self, lang, author_name, fname):
        text_path = os.path.expanduser('~/cltk_data/' + lang + '/text/' + lang + '_text_perseus/' + author_name.casefold() + '/opensource/' + fname )  # casefold() prob not nec
        dir_contents = os.listdir(text_path)
        if lang == 'greek':
            ending = '_gk.xml.json'
        elif lang == 'latin':
            ending = '_lat.xml.json'

        text_path += ending

        print( '********', text_path )
        with open( text_path, "r" ) as f:

            file_string = f.read()

        return { fname : file_string }
<commit_msg>add single text route<commit_after>"""Main API file for backend CLTK webapp."""

import os
import pdb

from flask import Flask, request
from flask_restful import Resource, Api

app = Flask(__name__)
api = Api(app)

# example
class HelloWorld(Resource):
    def get(self):
        return {'hello': 'world'}

# example
todos = {}
class TodoSimple(Resource):
    def get(self, todo_id):
        return {'example with token': todo_id}


class Authors(Resource):
    def get(self, lang):
        #assert lang in ['greek', 'latin']
        text_path = os.path.expanduser('~/cltk_data/' + lang + '/text/' + lang + '_text_perseus/')
        dir_contents = os.listdir(text_path)
        remove_files = ['README.md', '.git', 'LICENSE.md', 'perseus_compiler.py', '.DS_Store']
        dir_contents = [f for f in dir_contents if f not in remove_files]
        return {'authors': sorted(dir_contents)}


class Texts(Resource):
    def get(self, lang, author_name):
        text_path = os.path.expanduser('~/cltk_data/' + lang + '/text/' + lang + '_text_perseus/' + author_name.casefold() + '/opensource')  # casefold() prob not nec
        dir_contents = os.listdir(text_path)
        if lang == 'greek':
            ending = '_gk.xml.json'
        elif lang == 'latin':
            ending = '_lat.xml.json'
        dir_contents = [f for f in dir_contents if f.endswith(ending)]
        dir_contents = [f.casefold() for f in dir_contents]  # this probably isn't nec
        return {'texts': sorted(dir_contents)}

class Text(Resource):
    def get(self, lang, author_name, fname):
        print(True)
        text_path = os.path.expanduser('~') + '/cltk_data/' + lang + '/text/' + lang + '_text_perseus/' + author_name + '/opensource/' + fname
        if lang == 'greek':
            ending = '_gk.xml.json'
        elif lang == 'latin':
            ending = '_lat.xml.json'

        text_path += ending

        print( '********', text_path )
        with open( text_path, "r" ) as f:

            file_string = f.read()

        return { fname : file_string }

{'fraction_non_alphanumeric': 0.1438588007736944, 'fraction_numerical': 0.0, 'mean_word_length': 2.7103139013452915, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 8}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1096439', 'n_tokens_mistral': 1351, 'n_tokens_neox': 1306, 'n_words': 359}