Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'

dependencies {
// Required -- JUnit 4 framework
Expand All @@ -9,17 +10,21 @@ dependencies {
// Optional -- Hamcrest library
androidTestImplementation 'org.hamcrest:hamcrest-library:1.3'

implementation 'org.jetbrains.kotlin:kotlin-stdlib:1.8.10'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.4'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.4'
implementation 'commons-io:commons-io:2.5'
implementation 'androidx.annotation:annotation:1.6.0'
implementation 'androidx.appcompat:appcompat:1.6.1'
implementation 'com.github.curious-odd-man:rgxgen:1.4'
implementation 'com.jayway.jsonpath:json-path:2.8.0'
}

android {
compileSdkVersion rootProject.compileSdkVersion

defaultConfig {
minSdkVersion 23
minSdkVersion 24
targetSdkVersion 33
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package ee.ioc.phon.android.speechutils.editor

import androidx.test.ext.junit.runners.AndroidJUnit4
import ee.ioc.phon.android.speechutils.utils.HttpUtils
import org.junit.Assert.assertTrue
import org.junit.Test
import org.junit.runner.RunWith

private const val F_SEL = "@sel()"

@RunWith(AndroidJUnit4::class)
class FunctionExpanderTest {
@Test
fun test01() {
val regex = "${F_SEL}|${F_SEL}"
val selContent = "SEL"
val gold = "SEL|SEL"
val regexExpanded1: String = regex.replace(F_SEL, selContent)
val regexExpanded2 = expandFuns(regex, SelEvaluated(selContent))
assertTrue(regexExpanded1 == gold)
assertTrue(regexExpanded2 == gold)
}

@Test
fun test02() {
val text = "@timestamp(G 'text', de)|@timestamp(G 'text', de)"
val gold = "n. Chr. text|n. Chr. text"
val res = expandFuns(text, Timestamp())
assertTrue(res == gold)
}

@Test
fun test03() {
val text = "@urlEncode(1+2)"
val gold = HttpUtils.encode("1+2")
val res = expandFuns(text, UrlEncode())
assertTrue(res == gold)
}

// TODO: set up internet permission
// @Test
fun testXX() {
val text = "@getUrl(https://api.mathjs.org/v4/?expr=@urlEncode(1+2))"
val gold = "3"
val res = expandFuns(text, UrlEncode(), GetUrl())
assertTrue(res == gold)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ public class InputConnectionCommandEditorTest {
list2.add(new Command("replaceSelRe ([^ ]+) .+ ([^ ]+)", "", "replaceSelRe", new String[]{"$1 ([^ ]+) $2", "$1 \\$1 $2"}));
list2.add(new Command("selection_quote", "", "replaceSel", new String[]{"\"@sel()\""}));
list2.add(new Command("selection_double", "", "replaceSel", new String[]{"@sel()@sel()"}));
list2.add(new Command("text_double", "", "replaceSel", new String[]{"@text()"}));
list2.add(new Command("expr", "", "replaceSel", new String[]{"@expr(1+2)"}));
// Hyphenates the current selection to the uttered number, adds brackets around the whole thing,
// and selects the uttered number. Notice the need to escape the closing bracket and the end marking dollar sign.
list2.add(new Command("selection_bracket ([0-9]+)", "", "replaceSel", new String[]{"(@sel()-$1)", "-([0-9]+)\\\\)\\$"}));
Expand Down Expand Up @@ -1244,15 +1246,22 @@ public void test105() {
}

@Test
public void test106() {
public void test106a() {
add("1 2 3 1 2 3 1 2 3");
// 1 2 3 1 2 3 1 [2] 3
runOp(mEditor.selectReBefore("2"));
// 1 [2] 3 1 2 3 1 2 3
add("apply 2");
// 1 2 3 1 [2] 3 1 2 3
add("next_sel");
// 1 2 3 1 *[] 3 1 2 3
add("*");
assertThatTextIs("1 2 3 1 * 3 1 2 3");
// 1 2 3 1 * 3 1 [2] 3
runOp(mEditor.selectReAfter("2", 1));
// 1 [2] 3 1 * 3 1 2 3
add("prev_sel");
// 1 * 3 1 * 3 1 2 3
add("*");
assertThatTextIs("1 * 3 1 * 3 1 2 3");
undo(2);
Expand Down Expand Up @@ -1423,19 +1432,21 @@ public void test211() {
}

/**
* selectReBefore interprets the selection as a plain string (not as a regex
* selectReBefore interprets the selection as a plain string (not as a regex)
*/
@Test
public void test212() {
add(". 2 .");
// Select last dot
runOp(mEditor.selectReBefore("\\."));
// Select first dot, because "\Q.\E" is matched
runOp(mEditor.selectReBefore("@sel()"));
add("1");
assertThatTextIs("1 2 .");
}

/**
* selectReAfter interprets the selection as a plain string (not as a regex
* selectReAfter interprets the selection as a plain string (not as a regex)
*/
@Test
public void test213() {
Expand Down Expand Up @@ -1526,6 +1537,26 @@ public void test221() {
assertThatTextIs("n. Chr. text");
}

@Test
public void test222() {
add("123456");
add("text_double");
assertThatTextIs("123456123456");
}

@Test
public void test223() {
add("expr");
assertThatTextIs("3");
}

@Test
public void test224() {
add("2", "select 2");
runOp(mEditor.replaceSel("@expr(@text() - @sel())"));
assertThatTextIs("0");
}

private String getTextBeforeCursor(int n) {
return mEditor.getInputConnection().getTextBeforeCursor(n, 0).toString();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,29 @@ public interface CommandEditor {
// E.g. url == "http://api.mathjs.org/v4/?expr="; arg == "@sel()+1"
Op getUrl(String url, String arg);

/**
* Replace cursor with the result of the given HTTP-query.
* The query is specified as a string that contains a JSON object.
* The result is also expected to be a JSON object, on which a JSON Path
* query will be applied to extract a string that replaces the current selection.
* {
* "url": "https://api.example.org/v1/edits",
* "method": "POST",
* "body": {
* "model": "text-davinci-edit-001",
* "input": "@sel()",
* "instruction": "Fix the spelling mistakes"
* },
* "header": {
* "Authorization": "Bearer API_KEY",
* "Content-Type": "application/json",
* "User-Agent": "K6nele/httpJson/Edits"
* },
* "jsonpath": "$.choices[0].text"
* }
*/
Op httpJson(String json);

// Commands that are not exposed to the end-user in CommandEditorManager

CommandEditorResult commitFinalResult(String text);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ public class CommandEditorManager {
public static final String ACTIVITY = "activity";
public static final String GET_URL = "getUrl";

public static final String HTTP_JSON = "httpJson";

public static final Map<String, EditorCommand> EDITOR_COMMANDS;

static {
Expand Down Expand Up @@ -175,6 +177,8 @@ public class CommandEditorManager {
return ce.getUrl(urlPrefix, urlArg);
});

aMap.put(HTTP_JSON, (ce, args) -> ce.httpJson(getArgString(args, 0, null)));

EDITOR_COMMANDS = Collections.unmodifiableMap(aMap);
}

Expand Down
Loading