|
9 | 9 |
|
10 | 10 | namespace Flow.Launcher.Infrastructure |
11 | 11 | { |
12 | | - public class TranslationMapping |
13 | | - { |
14 | | - private bool constructed; |
15 | | - |
16 | | - private List<int> originalIndexs = new List<int>(); |
17 | | - private List<int> translatedIndexs = new List<int>(); |
18 | | - private int translatedLength = 0; |
19 | | - |
20 | | - public string key { get; private set; } |
21 | | - |
22 | | - public void setKey(string key) |
23 | | - { |
24 | | - this.key = key; |
25 | | - } |
26 | | - |
27 | | - public void AddNewIndex(int originalIndex, int translatedIndex, int length) |
28 | | - { |
29 | | - if (constructed) |
30 | | - throw new InvalidOperationException("Mapping shouldn't be changed after constructed"); |
31 | | - |
32 | | - originalIndexs.Add(originalIndex); |
33 | | - translatedIndexs.Add(translatedIndex); |
34 | | - translatedIndexs.Add(translatedIndex + length); |
35 | | - translatedLength += length - 1; |
36 | | - } |
37 | | - |
38 | | - public int MapToOriginalIndex(int translatedIndex) |
39 | | - { |
40 | | - if (translatedIndex > translatedIndexs.Last()) |
41 | | - return translatedIndex - translatedLength - 1; |
42 | | - |
43 | | - int lowerBound = 0; |
44 | | - int upperBound = originalIndexs.Count - 1; |
45 | | - |
46 | | - int count = 0; |
47 | | - |
48 | | - // Corner case handle |
49 | | - if (translatedIndex < translatedIndexs[0]) |
50 | | - return translatedIndex; |
51 | | - if (translatedIndex > translatedIndexs.Last()) |
52 | | - { |
53 | | - int indexDef = 0; |
54 | | - for (int k = 0; k < originalIndexs.Count; k++) |
55 | | - { |
56 | | - indexDef += translatedIndexs[k * 2 + 1] - translatedIndexs[k * 2]; |
57 | | - } |
58 | | - |
59 | | - return translatedIndex - indexDef - 1; |
60 | | - } |
61 | | - |
62 | | - // Binary Search with Range |
63 | | - for (int i = originalIndexs.Count / 2;; count++) |
64 | | - { |
65 | | - if (translatedIndex < translatedIndexs[i * 2]) |
66 | | - { |
67 | | - // move to lower middle |
68 | | - upperBound = i; |
69 | | - i = (i + lowerBound) / 2; |
70 | | - } |
71 | | - else if (translatedIndex > translatedIndexs[i * 2 + 1] - 1) |
72 | | - { |
73 | | - lowerBound = i; |
74 | | - // move to upper middle |
75 | | - // due to floor of integer division, move one up on corner case |
76 | | - i = (i + upperBound + 1) / 2; |
77 | | - } |
78 | | - else |
79 | | - return originalIndexs[i]; |
80 | | - |
81 | | - if (upperBound - lowerBound <= 1 && |
82 | | - translatedIndex > translatedIndexs[lowerBound * 2 + 1] && |
83 | | - translatedIndex < translatedIndexs[upperBound * 2]) |
84 | | - { |
85 | | - int indexDef = 0; |
86 | | - |
87 | | - for (int j = 0; j < upperBound; j++) |
88 | | - { |
89 | | - indexDef += translatedIndexs[j * 2 + 1] - translatedIndexs[j * 2]; |
90 | | - } |
91 | | - |
92 | | - return translatedIndex - indexDef - 1; |
93 | | - } |
94 | | - } |
95 | | - } |
96 | | - |
97 | | - public void endConstruct() |
98 | | - { |
99 | | - if (constructed) |
100 | | - throw new InvalidOperationException("Mapping has already been constructed"); |
101 | | - constructed = true; |
102 | | - } |
103 | | - } |
104 | | - |
105 | | - /// <summary> |
106 | | - /// Translate a language to English letters using a given rule. |
107 | | - /// </summary> |
108 | | - public interface IAlphabet |
109 | | - { |
110 | | - /// <summary> |
111 | | - /// Translate a string to English letters, using a given rule. |
112 | | - /// </summary> |
113 | | - /// <param name="stringToTranslate">String to translate.</param> |
114 | | - /// <returns></returns> |
115 | | - public (string translation, TranslationMapping map) Translate(string stringToTranslate); |
116 | | - |
117 | | - /// <summary> |
118 | | - /// Determine if a string can be translated to English letter with this Alphabet. |
119 | | - /// </summary> |
120 | | - /// <param name="stringToTranslate">String to translate.</param> |
121 | | - /// <returns></returns> |
122 | | - public bool ShouldTranslate(string stringToTranslate); |
123 | | - } |
124 | | - |
125 | 12 | public class PinyinAlphabet : IAlphabet |
126 | 13 | { |
127 | 14 | private ConcurrentDictionary<string, (string translation, TranslationMapping map)> _pinyinCache = |
|
0 commit comments