Skip to content

Instantly share code, notes, and snippets.

Avatar

Takao Nakaguchi takawitter

View GitHub Profile
@takawitter
takawitter / ScalaJSR223Test.java
Last active Mar 12, 2021
The sample code to run scala via JSR223 API. I turn usejavacp option true at line 10. Unless that you have to use http://github.com/rjolly/jarlister to list classes in scala-library.jar into MANIFEST.MF of jar that contains this ScalaTest class. I use Scala-2.11.6 to run this code.
View ScalaJSR223Test.java
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import scala.tools.nsc.interpreter.IMain;
import scala.tools.nsc.settings.MutableSettings.BooleanSetting;
public class ScalaTest {
public static void main(String[] args) throws Exception{
ScriptEngine engine = new ScriptEngineManager().getEngineByName("scala");
((BooleanSetting)(((IMain)engine).settings().usejavacp())).value_$eq(true);
@takawitter
takawitter / LZ77.java
Created May 14, 2013
「高速文字列解析の世界」を参考に、LZ77実装してみた。非常に単純な実装で、compress1はマッチする文字列を元配列をなめて求める方式。compress2は文字出現位置をマップで持つ方式。 Wikipedia日本語タイトルをTrieに格納してできたTail配列に対してやってみると、513万文字が435万文字になった。 compress1が31.7秒、compress2が8.9秒。ウィンドウサイズは8192。
View LZ77.java
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
public class LZ77 {
public static void compress1(CharSequence src, Appendable out, int windowSize)
throws IOException{
@takawitter
takawitter / LZSS.java
Created May 14, 2013
ちょっとLZSSに浮気。http://ja.wikipedia.org/wiki/LZSS を参考に、さらに一致文字数が2文字未満の場合は無視するようにした。compress6.6秒、363万文字+282byte(bit列サイズ)になった。
View LZSS.java
import java.io.IOException;
import java.util.BitSet;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
public class LZSS {
public static class LZSSData{
@takawitter
takawitter / TestServiceSOAP.groovy
Last active Mar 9, 2017
SOAP Service and Client using language grid libraries and Groovy.
View TestServiceSOAP.groovy
@Grab('org.eclipse.jetty:jetty-webapp:9.4.0.v20161208')
@Grab('org.langrid:jp.go.nict.langrid.service.language_1_2:1.0.10')
@Grab('org.langrid:jp.go.nict.langrid.servicecontainer.handler.axis:1.0.10')
import jp.go.nict.langrid.service_1_2.translation.TranslationService;
import jp.go.nict.langrid.servicecontainer.handler.annotation.Service;
import jp.go.nict.langrid.servicecontainer.handler.axis.SGAxisServlet;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.webapp.WebAppContext;
public class TestServiceImpl implements TranslationService{
@takawitter
takawitter / CallTranslation.groovy
Last active Feb 23, 2017
The script that call Translation service on Language Grid.
View CallTranslation.groovy
@Grab('org.langrid:jp.go.nict.langrid.client.soap:1.0.8')
@Grab('org.langrid:jp.go.nict.langrid.service.language_1_2:1.0.8')
import jp.go.nict.langrid.client.soap.SoapClientFactory;
import jp.go.nict.langrid.service_1_2.translation.TranslationService;
println new SoapClientFactory().create(
TranslationService.class,
new URL("http://langrid.org/service_manager/invoker/TranslationServiceId"),
"yourid",
"yourpass").translate("en", "ja", "hello");
View CollectionFixture.java
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import java.util.Collection;
import java.util.Iterator;
import java.util.function.Consumer;
public class CollectionFixture<T> {
public CollectionFixture(Collection<T> collection){
@takawitter
takawitter / Test.java
Created Oct 19, 2016
Using Iterable.forEach by tunneling exception that may occur inside Consumer.
View Test.java
public class Test{
@Test
public void test_tunnelingExecute() throws Throwable{
try{
tunnelingExecute(
Arrays.asList(1, 2, 3)::forEach,
v -> {throw new IOException();}
);
Assert.fail();
} catch(IOException e){
View result
Testing LinkedList
com.dzyoba.alex.TestAlloc: [7, 25, 276]
com.dzyoba.alex.TestInsert: [7069, 19437, 24856]
com.dzyoba.alex.TestInsertHead: [0, 1, 2]
com.dzyoba.alex.TestInsertTail: [0, 1, 1]
com.dzyoba.alex.TestSearch: [17372, 53823, 70404]
com.dzyoba.alex.TestDelete: [5, 9, 11]
com.dzyoba.alex.TestAlloc: [6, 36, 98]
com.dzyoba.alex.TestRemove: [6472, 8434, 9857]
@takawitter
takawitter / invoke.html
Last active Sep 7, 2016
New Langrid service invocation API for JavaScript.
View invoke.html
<script src="https://code.jquery.com/jquery-2.2.4.min.js"></script>
<script src="${langrid-address}/js"></script>
<script>
new LangridClientFactory({
apiKey: "${your-api-key}"
})
.create("${service-id}")
.translate("en", "ja", "Hello")
.done(function(res){
console.log(res.result);
@takawitter
takawitter / reflectionInvoker.groovy
Created Apr 21, 2016
sample code to invoke ReflectionMethodInvoker of groovy
View reflectionInvoker.groovy
import org.codehaus.groovy.runtime.ReflectionMethodInvoker
class Base{}
class Derived extends Base{}
class Derived2 extends Derived{}
class Hello{
public void hello(Base b){
println "base"
}
public void hello(Derived d){