r/AskComputerScience • u/Bright-Historian-216 • 3h ago
How is word counting done in writing systems with no delimiters?
For most languages, the algorithm is simple: count spaces and add 1.
however, let's take this example text in mandarin: "你好。我不说汉语。"
counting it this way will return... 1. the "spaces" after the dots are also included into the dot, so they aren't the "ascii" whitespace.
if you have a lot of time to spare, you can sort the whole dictionary by word length in descending order (so that combined words appear first: 语 is a word itself, but here it's part of 汉语, which is also one word). however, this is O(n*m) (amount of chars in sentence times the amount of words in a language). you could reduce the constant m by using a hashmap to look up the first word (dict[str,list[str]] where the list is sorted by str length in descending order), but it still seems like way, way too much.
is there any way this algorithm can be reduced further, or is it simply impossible?