Skip to content

Commit b1cb852

Browse files
Extract classes
1 parent 46d49d8 commit b1cb852

File tree

3 files changed

+121
-113
lines changed

3 files changed

+121
-113
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
namespace Flow.Launcher.Infrastructure
2+
{
3+
/// <summary>
4+
/// Translate a language to English letters using a given rule.
5+
/// </summary>
6+
public interface IAlphabet
7+
{
8+
/// <summary>
9+
/// Translate a string to English letters, using a given rule.
10+
/// </summary>
11+
/// <param name="stringToTranslate">String to translate.</param>
12+
/// <returns></returns>
13+
public (string translation, TranslationMapping map) Translate(string stringToTranslate);
14+
15+
/// <summary>
16+
/// Determine if a string can be translated to English letter with this Alphabet.
17+
/// </summary>
18+
/// <param name="stringToTranslate">String to translate.</param>
19+
/// <returns></returns>
20+
public bool ShouldTranslate(string stringToTranslate);
21+
}
22+
}

Flow.Launcher.Infrastructure/PinyinAlphabet.cs

Lines changed: 0 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -9,119 +9,6 @@
99

1010
namespace Flow.Launcher.Infrastructure
1111
{
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-
12512
public class PinyinAlphabet : IAlphabet
12613
{
12714
private ConcurrentDictionary<string, (string translation, TranslationMapping map)> _pinyinCache =
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
5+
namespace Flow.Launcher.Infrastructure
6+
{
7+
public class TranslationMapping
8+
{
9+
private bool constructed;
10+
11+
private List<int> originalIndexs = new List<int>();
12+
private List<int> translatedIndexs = new List<int>();
13+
private int translatedLength = 0;
14+
15+
public string key { get; private set; }
16+
17+
public void setKey(string key)
18+
{
19+
this.key = key;
20+
}
21+
22+
public void AddNewIndex(int originalIndex, int translatedIndex, int length)
23+
{
24+
if (constructed)
25+
throw new InvalidOperationException("Mapping shouldn't be changed after constructed");
26+
27+
originalIndexs.Add(originalIndex);
28+
translatedIndexs.Add(translatedIndex);
29+
translatedIndexs.Add(translatedIndex + length);
30+
translatedLength += length - 1;
31+
}
32+
33+
public int MapToOriginalIndex(int translatedIndex)
34+
{
35+
if (translatedIndex > translatedIndexs.Last())
36+
return translatedIndex - translatedLength - 1;
37+
38+
int lowerBound = 0;
39+
int upperBound = originalIndexs.Count - 1;
40+
41+
int count = 0;
42+
43+
// Corner case handle
44+
if (translatedIndex < translatedIndexs[0])
45+
return translatedIndex;
46+
if (translatedIndex > translatedIndexs.Last())
47+
{
48+
int indexDef = 0;
49+
for (int k = 0; k < originalIndexs.Count; k++)
50+
{
51+
indexDef += translatedIndexs[k * 2 + 1] - translatedIndexs[k * 2];
52+
}
53+
54+
return translatedIndex - indexDef - 1;
55+
}
56+
57+
// Binary Search with Range
58+
for (int i = originalIndexs.Count / 2;; count++)
59+
{
60+
if (translatedIndex < translatedIndexs[i * 2])
61+
{
62+
// move to lower middle
63+
upperBound = i;
64+
i = (i + lowerBound) / 2;
65+
}
66+
else if (translatedIndex > translatedIndexs[i * 2 + 1] - 1)
67+
{
68+
lowerBound = i;
69+
// move to upper middle
70+
// due to floor of integer division, move one up on corner case
71+
i = (i + upperBound + 1) / 2;
72+
}
73+
else
74+
return originalIndexs[i];
75+
76+
if (upperBound - lowerBound <= 1 &&
77+
translatedIndex > translatedIndexs[lowerBound * 2 + 1] &&
78+
translatedIndex < translatedIndexs[upperBound * 2])
79+
{
80+
int indexDef = 0;
81+
82+
for (int j = 0; j < upperBound; j++)
83+
{
84+
indexDef += translatedIndexs[j * 2 + 1] - translatedIndexs[j * 2];
85+
}
86+
87+
return translatedIndex - indexDef - 1;
88+
}
89+
}
90+
}
91+
92+
public void endConstruct()
93+
{
94+
if (constructed)
95+
throw new InvalidOperationException("Mapping has already been constructed");
96+
constructed = true;
97+
}
98+
}
99+
}

0 commit comments

Comments
 (0)