The Stack Exchange (SE) network has many different websites to support the learning of natural languages.
Links to some of these SE sites are provided below:
- German / Deutsch |
- Spanish / Espanol
- Japanese 日本語 にほんご ニホンゴ
- French / Francaise
- Chinese Mandarin 官話 / 官话
- Russian ру́сский алфави́т, russkiy alfavit or ру́сская а́збука, russkaya azbuka
On these websites, I enjoy showing people tables, such as the following example:
por | su | favor |
---|---|---|
for | your | favor |
The source code is shown below:
| por | su | favor |
|-----|------|-------|
| for | your | favor |
However, I have some difficulty typing the correct number of vertical bars |
, hyphens -
, and spaces
on my keyboard.
What script or computer software tool will accept arrays of string as input (with both input arrays having equal length) and output an SE markup language table?
in1 = example_output = "for your favor"
in2 = "por su favor"
example_output = "| por | su | favor |\n|-----|----|-------|\n| for | your | favor |"
Two of my own failed attempts to create a tool for generating Stack Overflow markup tables are shown below.
class TableGenerator:
@classmethod
def make_table_from_two_strings(cls, *args):
"""
DOCUMENTATION:
This function accepts two
containers as input.
The containers can be strings, lists,
or any iterable provided that the following
requirements are met:
(1) a method named `__iter__` is defined
inside of the class definition
(2) stuff like `next(next(iter(x)))`
returns a string
(3) after we split the two strings
into pieces based on white-space
delimiters and delete all of the
resulting empty strings then
the resulting two lists of pieces
will have the same length.
"""
sanis = cls.sanitize_inputs(*args)
table = cls._make_table_from_two_strings(*sanis)
return table
@classmethod
def sanitize_inputs(cls, text_del_america_sud:str, text_de_america_norte:str):
sud = text_del_america_sud # alias
nort = text_de_america_norte # alias
# variables have self-documenting names when the variable is defined
# variables have short names when we retrieve the value of the variable sometimes after definition-time
#
# functions should have long names when the function is defined
#
# we could allow any sub-sequence of the function name for function invocations or function calls
# sanitize your inputs
sud = "".join(str(ch) for ch in sud)
nort = "".join(str(ch) for ch in nort)
old_sud = sud.split()
old_nort = nort.split()
new_sud = list()
new_nort = list()
for word in old_sud:
if len(word) < 1:
new_sud.append(word)
# `fi` ends a n if-block in Bourne Again Shell Script
# `rof` ends a for-loop in Bourne Again Shell Script
for word in old_nort:
if len(word) < 1:
new_nort.append(word)
# `fi` ends a n if-block in Bourne Again Shell Script
# `rof` ends a for-loop in Bourne Again Shell Script
new_nort = [word for word in old_nort if len(word) < 1]
assert(len(new_sud) == len(old_sud))
return (new_sud, new_nort)
@classmethod
def _make_table_from_two_strings(cls, text_del_america_sud:str, text_de_america_norte:str):
"""
this is the private (not public)
method.
The inputs to the private method must be strings instead of arrays of characters, lists of characters, iterators, etc...
"""
sud = text_del_america_sud # alias
nort = text_de_america_norte # alias
str_strm = io.StringIO()
_print = lambda *args : print(*args, sep=" | ", file=str_strm)
_print(sud, norte)
# I WOULD REALLY LIKE HELP FILLING IN THE CODE HERE
# ITS IS VERY DIFFICULT TO TRANSLATE
# SPANISH TO ENGLISH OR MAKE EDUCATIONAL WORKSHEETS
# WITHOUT A WORKING COMPUTER PROGRAM
r = "NOT IMPLEMENTED YET"
return r
import io
texto_ingles = """
In the English-language, the word "corpse" (cuerpo) refers only to dead-bodies such-that the legs and the arms of that body do-not move.
I wish to have a word for \"body\" which conveys vitality, the idea that my body is alive, and that my arms and legs are animated.
"""
texto_espanol = """
En el idioma-inglés, la palabra "corpse" (cuerpo) se-refiere solo a cadáveres-de-modo que las piernas y los brazos de ese cuerpo no se-mueven.
Deseo tener una palabra para \"cuerpo\" que transmita vitalidad, la idea de que mi cuerpo está vivo y que mis brazos y piernas están animados.
"""
words_ingles = texto_ingles.split()
words_espanol = texto_espanol.split()
word_lengths = [max(len(pair[0]), len(pair[1])) for pair in zip(words_ingles, words_espanol)]
words_ingles = [word.ljust(k) for word, k in zip(words_ingles, word_lengths)]
words_espanol = [word.ljust(k) for word, k in zip(words_espanol, word_lengths)]
ss = io.StringIO()
hyps = ["--" + "".join(max(len(pair[0]), len(pair[1]))*"-") for pair in zip(words_ingles, words_espanol)]
# hyps.... hyphens
_print = lambda *args: print("|", *args, sep ="", end="|\n", file=ss)
_print(" ", " | ".join(words_ingles))
_print("|".join(hyps))
_print(" ", " | ".join(words_espanol))
long_results = [part.split("|") for part in ss.getvalue().split("\n")]
short_results_list = list()
max_idx = len(long_results[0])
# max_idx ...... maximum index
for k in range(0, 24, 8):
# [00:08] ... 00 through 07 inclusive (8 numbers in the integer interval 0 to 7)
# [08:16] ... 08 through 15 inclusive
# [16:24] ... 16 through 23 inclusive
short_result = "\n".join(["|".join(long_result[(k):(k+8)]) + "|" for long_result in long_results])
short_results_list.append(short_result)
short_results_list.append(2*"\n")
short_results_str = "".join(short_results_list)
print(short_results_str)
What script or piece of code can generate Stack Exchange markup language tables from two arrays of strings such that the two arrays are of the same length?