Skip to content

Commit f5a3f10

Browse files
committed
[WIP] voice matching on QuestionsScreen
1 parent d61aaca commit f5a3f10

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+3297
-17
lines changed

android/build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
apply plugin: 'com.android.application'
23

34
apply plugin: 'kotlin-android'
@@ -34,6 +35,7 @@ android {
3435
tasks.lint.enabled = false
3536
}
3637

38+
3739
dependencies {
3840
implementation fileTree(dir: 'libs', include: ['*.jar'])
3941
implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlinVersion"

android/src/main/java/com/willowtreeapps/namegame/store/QuestionFragment.kt

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,62 @@ import java.util.*
2626

2727

2828
class QuestionFragment : BaseNameGameViewFragment<QuestionPresenter>(), QuestionView, MainActivity.IOnBackPressed {
29+
private val speechRecognizer by lazy { SpeechRecognizer.createSpeechRecognizer(activity!!) }
30+
private val speechRecognizerIntent by lazy {
31+
val speechRecIntent = Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH)
32+
speechRecIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
33+
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM)
34+
speechRecIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE,
35+
Locale.getDefault())
36+
speechRecIntent
37+
}
38+
39+
override fun openMic() {
40+
41+
speechRecognizer.setRecognitionListener(object : RecognitionListener {
42+
override fun onReadyForSpeech(params: Bundle?) {
43+
}
44+
45+
override fun onRmsChanged(rmsdB: Float) {
46+
}
47+
48+
override fun onBufferReceived(buffer: ByteArray?) {
49+
}
50+
51+
override fun onPartialResults(partialResults: Bundle) {
52+
val matches = partialResults
53+
.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION)
54+
55+
//displaying the first match
56+
if (matches != null)
57+
presenter.namePicked(matches[0])
58+
}
59+
60+
override fun onEvent(eventType: Int, params: Bundle?) {
61+
}
62+
63+
override fun onBeginningOfSpeech() {
64+
}
65+
66+
override fun onEndOfSpeech() {
67+
}
68+
69+
override fun onError(error: Int) {
70+
}
71+
72+
override fun onResults(results: Bundle) {
73+
val matches = results
74+
.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION)
75+
76+
//displaying the first match
77+
if (matches != null)
78+
presenter.namePicked(matches[0])
79+
}
80+
81+
})
82+
speechRecognizer.startListening(speechRecognizerIntent)
83+
84+
}
2985

3086
private var restoreX: Float? = null
3187
private var restoreY: Float? = null
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.willowtreeapps.fuzzywuzzy;
2+
3+
import com.willowtreeapps.fuzzywuzzy.diffutils.FuzzySearch;
4+
import com.willowtreeapps.fuzzywuzzy.diffutils.algorithms.WeightedRatio;
5+
import com.willowtreeapps.fuzzywuzzy.diffutils.model.ExtractedResult;
6+
7+
import java.util.ArrayList;
8+
import java.util.List;
9+
10+
public class ContractCheck {
11+
12+
/**
13+
* Test that the public interface contract is met, to ensure no backwards
14+
* compatibility breaking changes. The test only ensures type safety
15+
* at compilation time, the rest of the tests are needed for
16+
* functionality testing.
17+
*/
18+
public void testContract() {
19+
int ratio = FuzzySearch.INSTANCE.ratio("mysmilarstring", "myawfullysimilarstirng");
20+
int i = FuzzySearch.INSTANCE.partialRatio("mysmilarstring", "myawfullysimilarstirng");
21+
22+
ArrayList<String> sample = new ArrayList<>();
23+
24+
int order_words_out_of = FuzzySearch.INSTANCE.tokenSortPartialRatio("order words out of", " words out of order");
25+
int order_words_out_of1 = FuzzySearch.INSTANCE.tokenSortRatio("order words out of", " words out of order");
26+
27+
int i1 = FuzzySearch.INSTANCE.tokenSetRatio("fuzzy was a bear", "fuzzy fuzzy fuzzy bear");
28+
int i2 = FuzzySearch.INSTANCE.tokenSetPartialRatio("fuzzy was a bear", "fuzzy fuzzy fuzzy bear");
29+
int i3 = FuzzySearch.INSTANCE.weightedRatio("The quick brown fox jimps ofver the small lazy dog", "the quick brown fox jumps over the small lazy dog");
30+
31+
WeightedRatio weighted = new WeightedRatio();
32+
weighted.with(new StringProcessor() {
33+
@Override
34+
public String process(String in) {
35+
return in;
36+
}
37+
});
38+
39+
ExtractedResult one = FuzzySearch.INSTANCE.extractOne("cowboys", sample);
40+
41+
List<ExtractedResult> e1 = FuzzySearch.INSTANCE.extractTop("goolge", sample, weighted, 3);
42+
List<ExtractedResult> e2 = FuzzySearch.INSTANCE.extractAll("goolge", sample);
43+
List<ExtractedResult> e3 = FuzzySearch.INSTANCE.extractAll("goolge", sample, 40);
44+
List<ExtractedResult> e4 = FuzzySearch.INSTANCE.extractSorted("goolge", sample);
45+
List<ExtractedResult> e5 = FuzzySearch.INSTANCE.extractSorted("goolge", sample, 3);
46+
}
47+
}
48+
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.willowtreeapps.fuzzywuzzy
2+
3+
import com.willowtreeapps.fuzzywuzzy.diffutils.Extractor
4+
import com.willowtreeapps.fuzzywuzzy.diffutils.algorithms.WeightedRatio
5+
import org.junit.Before
6+
import org.junit.Test
7+
8+
class ExtractorTest {
9+
10+
lateinit var choices: List<String>
11+
private lateinit var extractor: Extractor
12+
13+
@Before
14+
fun setUp() {
15+
choices = listOf("google", "bing", "facebook", "linkedin", "twitter", "googleplus", "bingnews", "plexoogl")
16+
extractor = Extractor()
17+
}
18+
19+
@Test
20+
fun testExtractWithoutOrder() {
21+
22+
val res = extractor.extractWithoutOrder("goolge", choices, WeightedRatio())
23+
24+
assert(res.size == choices.size)
25+
assert(res[0].score > 0)
26+
}
27+
28+
@Test
29+
fun testExtractOne() {
30+
31+
val res = extractor.extractOne("goolge", choices, WeightedRatio())
32+
33+
assert(res.string == "google")
34+
35+
}
36+
37+
@Test
38+
fun testExtractBests() {
39+
40+
val res = extractor.extractTop("goolge", choices, WeightedRatio())
41+
42+
assert(res[0].string == "google" && res[1].string == "googleplus")
43+
44+
}
45+
46+
@Test
47+
fun testExtractBests1() {
48+
49+
val res = extractor.extractTop("goolge", choices, WeightedRatio(), 3)
50+
51+
assert(res.size == 3)
52+
assert(res[0].string == "google" && res.get(1).string == "googleplus" && res.get(2).string == "plexoogl")
53+
54+
}
55+
}
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
package com.willowtreeapps.fuzzywuzzy;
2+
3+
4+
import com.willowtreeapps.fuzzywuzzy.diffutils.FuzzySearch;
5+
import com.willowtreeapps.fuzzywuzzy.diffutils.ratio.SimpleRatio;
6+
import org.junit.Assert.assertEquals
7+
import org.junit.Test
8+
9+
10+
class FuzzyWuzzyTest {
11+
12+
13+
val choices = listOf("google", "bing", "facebook", "linkedin", "twitter", "googleplus", "bingnews", "plexoogl")
14+
val moreChoices = listOf("Atlanta Falcons", "New York Jets", "New York Giants", "Dallas Cowboys")
15+
16+
17+
@Test
18+
fun testRatio() {
19+
assertEquals(76, FuzzySearch.ratio("mysmilarstring", "mymostsimilarstsdsdring"))
20+
assertEquals(72, FuzzySearch.ratio("mysmilarstring", "myawfullysimilarstirng"))
21+
assertEquals(97, FuzzySearch.ratio("mysmilarstring", "mysimilarstring"))
22+
assertEquals(75, FuzzySearch.ratio("csr", "c s r"))
23+
24+
}
25+
26+
@Test
27+
fun testPartialRatio() {
28+
29+
assertEquals(71, FuzzySearch.partialRatio("similar", "somewhresimlrbetweenthisstring"))
30+
assertEquals(43, FuzzySearch.partialRatio("similar", "notinheresim"))
31+
assertEquals(38, FuzzySearch.partialRatio("pros holdings, inc.", "settlement facility dow corning trust"))
32+
assertEquals(33, FuzzySearch.partialRatio("Should be the same", "Opposite ways go alike"))
33+
assertEquals(33, FuzzySearch.partialRatio("Opposite ways go alike", "Should be the same"))
34+
35+
}
36+
37+
@Test
38+
fun testTokenSortPartial() {
39+
40+
assertEquals(67, FuzzySearch.tokenSortPartialRatio("mvn", "wwwwww.mavencentral.comm"))
41+
assertEquals(100, FuzzySearch.tokenSortPartialRatio(" order words out of ", " words out of order"))
42+
assertEquals(44, FuzzySearch.tokenSortPartialRatio("Testing token set ratio token", "Added another test"))
43+
44+
}
45+
46+
@Test
47+
fun testTokenSortRatio() {
48+
assertEquals(84, FuzzySearch.tokenSortRatio("fuzzy was a bear", "fuzzy fuzzy was a bear"))
49+
50+
}
51+
52+
@Test
53+
fun testTokenSetRatio() {
54+
55+
assertEquals(100, FuzzySearch.tokenSetRatio("fuzzy fuzzy fuzzy bear", "fuzzy was a bear"))
56+
assertEquals(39, FuzzySearch.tokenSetRatio("Testing token set ratio token", "Added another test"))
57+
58+
}
59+
60+
@Test
61+
fun testTokenSetPartial() {
62+
63+
assertEquals(11, FuzzySearch.tokenSetPartialRatio("fuzzy was a bear", "blind 100"))
64+
assertEquals(67, FuzzySearch.partialRatio("chicago transit authority", "cta"))
65+
66+
}
67+
68+
@Test
69+
fun testWeightedRatio() {
70+
71+
72+
assertEquals(60, FuzzySearch.weightedRatio("mvn", "wwwwww.mavencentral.comm"))
73+
assertEquals(40, FuzzySearch.weightedRatio("mvn", "www;'l3;4;.4;23.4/23.4/234//////www.mavencentral.comm"))
74+
assertEquals(97, FuzzySearch.weightedRatio("The quick brown fox jimps ofver the small lazy dog",
75+
"the quick brown fox jumps over the small lazy dog"))
76+
77+
}
78+
79+
@Test
80+
fun testExtractTop() {
81+
82+
val res = FuzzySearch.extractTop("goolge", choices, 2)
83+
val res2 = FuzzySearch.extractTop("goolge", choices, SimpleRatio(), 2);
84+
85+
assert(res.size == 2)
86+
assert(res.get(0).string == "google" && res.get(1).string == "googleplus")
87+
88+
assert(res2.size == 2)
89+
assert(res2.get(0).string == "google" && res2.get(1).string == "googleplus")
90+
91+
assert(FuzzySearch.extractTop("goolge", choices, 2, 100).isEmpty())
92+
93+
}
94+
95+
@Test
96+
fun testExtractAll() {
97+
98+
val res = FuzzySearch.extractAll("goolge", choices)
99+
100+
assert(res.size == choices.size)
101+
assert(res.get(0).string == "google")
102+
103+
assert(FuzzySearch.extractAll("goolge", choices, 40).size == 3)
104+
105+
}
106+
107+
@Test
108+
fun testExtractSorted() {
109+
110+
val res = FuzzySearch.extractSorted("goolge", choices)
111+
112+
assert(res.size == choices.size)
113+
assert(res.get(0).string == "google")
114+
assert(res.get(1).string == "googleplus")
115+
116+
assert(FuzzySearch.extractSorted("goolge", choices, 40).size == 3)
117+
118+
}
119+
120+
121+
@Test
122+
fun testExtractOne() {
123+
124+
val res = FuzzySearch.extractOne("twiter", choices, SimpleRatio())
125+
val res2 = FuzzySearch.extractOne("twiter", choices)
126+
val res3 = FuzzySearch.extractOne("cowboys", moreChoices)
127+
128+
assert(res.string == "twitter")
129+
assert(res2.string == "twitter")
130+
assert(res3.string == "Dallas Cowboys" && res3.score == 90)
131+
132+
133+
}
134+
135+
136+
}
137+
138+
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.willowtreeapps.fuzzywuzzy.algorithms
2+
3+
import com.willowtreeapps.fuzzywuzzy.diffutils.algorithms.DefaultStringFunction
4+
import org.junit.Assert.assertEquals
5+
import org.junit.Test
6+
import java.util.regex.Pattern
7+
8+
class DefaultStringProcessorTest {
9+
10+
@Test
11+
fun testProcess() {
12+
val inp = "s.trim μεγιουνικουντ n/o/n a.lph.a n.um"
13+
14+
assertEquals("s trim μεγιουνικουντ n o n a lph a n um", DefaultStringFunction().apply(inp))
15+
Pattern.UNICODE_CHARACTER_CLASS
16+
}
17+
}

build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ buildscript {
1313
classpath 'com.android.tools.build:gradle:3.5.0-alpha11'
1414
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion"
1515
classpath "org.jetbrains.kotlin:kotlin-serialization:$kotlinVersion"
16+
classpath 'org.codehaus.groovy:groovy-android-gradle-plugin:2.0.1'
1617
// NOTE: Do not place your application dependencies here; they belong
1718
// in the individual module build.gradle files
1819
}

common/src/commonMain/kotlin/com/willowtreeapps/common/Actions.kt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ import com.willowtreeapps.common.repo.Profile
77
sealed class Actions : Action {
88

99
class FetchingItemsStartedAction
10-
class FetchingItemsSuccessAction(val itemsHolder: ItemsHolder)
11-
class FetchingItemsFailedAction(val message: String)
10+
data class FetchingItemsSuccessAction(val itemsHolder: ItemsHolder)
11+
data class FetchingItemsFailedAction(val message: String)
1212

13-
class NamePickedAction(val name: String)
13+
data class NamePickedAction(val name: String)
1414

1515
class NextQuestionAction
1616

@@ -19,17 +19,17 @@ sealed class Actions : Action {
1919
class StartOverAction
2020
class ResetGameStateAction
2121

22-
class StartQuestionTimerAction(val initialValue: Int)
22+
data class StartQuestionTimerAction(val initialValue: Int)
2323
class DecrementCountDownAction
2424
class TimesUpAction
2525

2626

2727
class SettingsTappedAction
2828
class LoadAllSettingsAction
29-
class SettingsLoadedAction(val settings: UserSettings)
30-
class ChangeNumQuestionsSettingsAction(val num: Int)
31-
class ChangeCategorySettingsAction(val categoryId: QuestionCategoryId)
32-
class ChangeMicrophoneModeSettingsAction(val enabled: Boolean)
29+
data class SettingsLoadedAction(val settings: UserSettings)
30+
data class ChangeNumQuestionsSettingsAction(val num: Int)
31+
data class ChangeCategorySettingsAction(val categoryId: QuestionCategoryId)
32+
data class ChangeMicrophoneModeSettingsAction(val enabled: Boolean)
3333

3434
}
3535

0 commit comments

Comments
 (0)