I am creating a Font Mapping program where I want to map 2 fonts of different languages.
For eg A in Arial Font(English) will map with अ in Kruti Dev Font(Hindi).
I have a created this conversion in a database this way:
Native_Font | Foreign_Language_Font
-------------------------------------------
A | अ
B | बी
If conversion was restricted only for characters it was OK. Read each character of native font and find its matching character in Foreign Language Font. (I have done it)
But now I have to do it for strings also and that’s where the real problem starts.
If a string is provided in database, then convert it fully. But if its conversion does not exist find conversion for one character less.
An example would be
In Database
Native_Font | Foreign_Language_Font
-------------------------------------------
Cha | चा
r | र
t | ट
and word Chart is given to translate.
- It would first try to convert Full word
Chart. If its mapping is found give at once. - But if it does not find direct conversion for
Chartthen Go forChar. If its mapping is found give at once, then find respective character oft
and so on
Chart
Char t
Cha rt
Cha r t
Ch art
Ch ar t
Ch a r t
C hart
C har t
C ha r t
C h a r t
Moreover if a mapping is not found, it should be replaced by native font character. How to do it? I am sure recursion should be used. But How?
I’d approach it with a greedy algorithm. Something like this:
This starts from the front, looking for the largest possible match. Depending on your data, other approaches might be better – say, going through the mapping in length order to find the longest match and splitting from there:
Finally, you might play with combining these methods: use RecurTranslate2 until you get to some length threshold and then switch to RecurTranslate.
Responding to comment: See new else branch for failed lookup