Stefan Ram
2025-03-24 10:03:55 UTC
Antworten
PermalinkASCIIMathML soll wohl tatsächlich sogar mathematische
"ad-hoc"-Notation verstehen und
x=(-b +- sqrt(b^2 – 4ac))/(2a)
richtig (so wie vermutlich gemeint) interpretieren!
Da es in "de.alt.folklore.computer" nicht zum Thema paßt:"ad-hoc"-Notation verstehen und
x=(-b +- sqrt(b^2 – 4ac))/(2a)
richtig (so wie vermutlich gemeint) interpretieren!
Newsgroups: de.alt.folklore.computer,de.sci.mathematik
Followup-To: de.sci.mathematik
. ASCIIMathML findet jeweils das längste nächste Symbol. So würde
token_list =[ "sqrt", "+-" ]
input = "+-sqrt(4ac)"
die Symbole
+-
sqrt
(
4
a
c
)
ergeben. Hier ein entsprechendes Python-Skript:
class TokenFinder:
def __init__(self, token_dict):
# Sort the dictionary keys (tokens) by length in descending order
self.sorted_tokens = sorted(token_dict.keys(), key=len, reverse=True)
self.token_dict = token_dict
def next_token(self, s):
# Check for the longest string in the sorted list that is a prefix of s
for token in self.sorted_tokens:
if s.startswith(token):
return token
# If no string in the list is a prefix, return the first character of s (if any)
return s[0] if s else None
def tokens_of(self, s):
while s:
token = self.next_token(s)
# Yield the replacement value if the token is in the dictionary, otherwise yield the token itself
yield self.token_dict.get(token, token)
s = s[len(token):] # Remove the found token from the beginning of s
# Example usage:
token_dict = {"sqrt": "√", "+-": "±"}
finder = TokenFinder(token_dict)
s = "sqrt(x) +- y"
for token in finder.tokens_of(s):
print(token, end='')
print()
# Ausgabe: √(x) ± y
.