id stringlengths 36 36 | text stringlengths 1 1.25M |
|---|---|
37f46f14-451f-439d-abb8-1f0ae9160535 | public TagParser(String testName, String tag) {
super(testName);
this.tag = tag;
} |
1ed31343-b5ae-47e4-860b-b1ed8de39ad1 | protected void runTest() {
Tag [] tags = Tag.create(tag);
assertNotNull(tags);
assertTrue(tags.length > 0);
StringBuffer buf = new StringBuffer();
for (int i = 0; i< tags.length; i++) {
if (i > 0) buf.append("|");
buf.append(tags[i].toString());
}
if (tag.indexOf('_') == -1) {
assertEquals("Tag not deserialized identically from code (" + buf.toString() + ")",
tag, buf.toString());
}
} |
56d51380-efb5-4679-8ba6-19b2ba98b7cf | public static Test suite() {
TestSuite suite = new TestSuite(MorfeuszTagsTest.class.getName());
try {
LineNumberReader reader =
new LineNumberReader(
new InputStreamReader(
MorfeuszTagsTest.class.getResourceAsStream(
"tokens.samples.ipi-wstepny.txt"), "UTF-8"));
try {
Analyzer analyzer = Morfeusz.getInstance().getAnalyzer();
do {
String ipiTag = reader.readLine();
if (ipiTag == null || ipiTag.trim().length() == 0)
break;
String word;
do {
word = reader.readLine();
if (word.trim().length() == 0)
break;
InterpMorf [] analysis = analyzer.analyze(word.getBytes("iso8859-2"));
for (int i = 0; i < analyzer.getTokensNumber(); i++) {
byte [] tagBytes = analysis[i].getTag();
if (tagBytes == null) continue;
String tag = new String(tagBytes, 0, analysis[i].getTagLength());
// System.out.println(ipiTag + ", " + word + ", " + tag);
if (tag.length() > 0) {
suite.addTest(new TagParser("Parsing: '" + tag + "', ipi: '" + ipiTag + "' (" + word + ")", tag));
}
}
} while (true);
} while (true);
} finally {
reader.close();
}
} catch (IOException e) {
throw new RuntimeException("Could not read tags resource.");
}
return suite;
} |
e97a4444-3576-4ded-a2f9-9f96d729d887 | public TagParser(String testName, String tag) {
super(testName);
this.tag = tag;
} |
5c8c72e2-a950-46b6-aec1-5dc64e749fb6 | protected void runTest() {
Tag [] tags = Tag.create(tag);
assertNotNull(tags);
assertTrue(tags.length > 0);
for (int i = 0; i< tags.length; i++) {
assertEquals("Tag not deserialized identically from code (" + tag + " -- " + tags[i].toString() + ")",
tag, tags[i].toString());
}
} |
e6f132ec-acdb-4a25-806b-f6a730fd05bd | public static Test suite() {
TestSuite suite = new TestSuite(IpiWstepnyTagsTest.class.getName());
try {
LineNumberReader reader =
new LineNumberReader(
new InputStreamReader(
IpiWstepnyTagsTest.class.getResourceAsStream("tokens.unique.ipi-wstepny.txt")));
try {
do {
String line = reader.readLine();
if (line == null) break;
suite.addTest(new TagParser("Parsing: '" + line + "'", line));
} while (true);
} finally {
reader.close();
}
} catch (IOException e) {
throw new RuntimeException("Could not read tags resource.");
}
return suite;
} |
d479730c-190c-436c-bc55-4a961ce271ce | public ConcurrentAccessTest(String s) {
super(s);
} |
3e1a8e78-4646-471f-bfb8-52014da2a266 | public void testCrossClassloaderConcurrentAccess()
throws Exception {
// create a 'virtual' class loader that will allow us
// to load two singletons.
String [] sandboxedClasses = new String [] {
"com.dawidweiss.morfeusz."
};
VirtualVmClassLoader loader1 = new VirtualVmClassLoader(
Thread.currentThread().getContextClassLoader(), Arrays.asList(sandboxedClasses));
VirtualVmClassLoader loader2 = new VirtualVmClassLoader(
Thread.currentThread().getContextClassLoader(), Arrays.asList(sandboxedClasses));
Class clazz1 = loader1.loadClass(Morfeusz.class.getName());
Class clazz2 = loader2.loadClass(Morfeusz.class.getName());
assertNotSame(clazz1, clazz2);
// ok, now instantiate Morfeusz class
Method [] methods = clazz1.getMethods();
Method getInstance1 = null;
for (int i=0;i<methods.length;i++) {
if ("getInstance".equals(methods[i].getName()) &&
Modifier.isStatic(methods[i].getModifiers())) {
getInstance1 = methods[i];
break;
}
}
assertNotNull(getInstance1);
clazz1.getDeclaredMethod("getInstance", new Class [] {});
Object morfeusz1 = getInstance1.invoke(null, (Object[]) null);
assertNotNull(morfeusz1);
try {
Method getInstance2 = clazz2.getDeclaredMethod("getInstance", new Class [] {});
getInstance2.invoke(null, (Object[]) null);
fail("Shared object loaded from two class loaders.");
} catch (InvocationTargetException e) {
if (e.getTargetException() instanceof UnsatisfiedLinkError) {
// this is what is expected - the shared object
// has been loaded by another class loader.
} else {
fail("Unexpected exception when loading the second shared object.");
}
}
} |
7ad38fee-2f47-488b-aed3-bd3b83399043 | public LowLevelAnalyzerTest(String s) {
super(s);
} |
14f7a86c-3cce-411b-826e-41477813ca66 | public void testBasicAnalysis()
throws UnsupportedEncodingException, SecurityException,
UnsatisfiedLinkError {
Analyzer analyzer = Morfeusz.getInstance().getAnalyzer();
InterpMorf[] analysis = analyzer.analyze("ja zostałem".getBytes(
"iso8859-2"));
assertTrue("analysis should not be null.", analysis != null);
assertTrue("there should be tokens in the analysis.",
analyzer.getTokensNumber() > 0);
assertTrue("token should be available.", analysis[0].getToken() != null);
assertTrue("lemma should be available.", analysis[0].getLemma() != null);
assertTrue("tag should be available.", analysis[0].getTag() != null);
} |
87bbef61-c0c4-4603-90dc-ede1f3453c1d | public void testAnalysisOfSelectedExpressions()
throws UnsupportedEncodingException, SecurityException,
UnsatisfiedLinkError {
Analyzer analyzer = Morfeusz.getInstance().getAnalyzer();
String [] words = {
"ja zostałem",
"wziąć",
"kominiarz",
"komin",
"kominiarka",
"jak",
"żółtodzioby"
};
for (int i=0;i<words.length;i++) {
InterpMorf[] analysis =
analyzer.analyze(words[i].getBytes("iso8859-2"));
assertTrue("analysis should not be null.", analysis != null);
assertTrue("there should be tokens in the analysis.",
analyzer.getTokensNumber() > 0);
System.out.println("Analysis of: " + words[i]);
for (int j=0;j<analyzer.getTokensNumber();j++) {
String token = new String(analysis[j].getToken(), 0,
analysis[j].getTokenLength(), "iso8859-2");
String lemma = new String(analysis[j].getLemma(), 0,
analysis[j].getLemmaLength(), "iso8859-2");
String tag = new String(analysis[j].getTag(), 0,
analysis[j].getTagLength(), "iso8859-2");
System.out.println("\t"
+ analysis[j].getNodeStart()
+ "-"
+ analysis[j].getNodeEnd()
+ " : "
+ token + " [" + lemma + " : " + tag + "]");
}
}
} |
799c0544-f391-46ad-b82b-c8a5eac3a88a | public MorfeuszTest(String s) {
super(s);
} |
c520a3b4-90ec-483c-a7ac-2b2f1345f9c6 | public void testSingletonAvailability()
throws UnsupportedEncodingException, SecurityException,
UnsatisfiedLinkError {
Morfeusz.getInstance();
} |
99bb4800-d3bb-4ded-aff7-d9288a5a88f1 | public void testAboutInfoMethod()
throws UnsupportedEncodingException, SecurityException,
UnsatisfiedLinkError {
assertTrue("About info string should not be null.",
Morfeusz.getInstance().about() != null);
} |
decf050d-0952-412b-b8ee-f46151445a96 | public TagParserException(String message) {
super(message);
} |
5a5244af-7e9a-4a10-974f-8fbfd85bf410 | private Tag(String tagCode) {
parse(tagCode);
} |
f950e6a0-ef2e-4309-9eee-a9f945babd86 | private void assertTrue(boolean condition, String message) {
if (condition == false) throw new RuntimeException(message);
} |
2e9dd086-8eee-4e5f-a3eb-c62d3bb10fd3 | private long assertValidReturnCode(HashMap allowed, String values) {
long code = 0;
if (values.equals("_")) {
for (Iterator i = allowed.values().iterator(); i.hasNext(); ) {
code |= ((Long) i.next()).longValue();
}
} else {
if (values.indexOf('.') >= 0) {
String [] v = values.split("\\.");
for (int i = 0; i < v.length; i++) {
Long l = (Long) allowed.get(v[i]);
if (l == null) {
throw new RuntimeException("Illegal attribute value: " + v[i]);
}
code |= l.longValue();
}
} else {
Long l = (Long) allowed.get(values);
if (l == null) {
throw new RuntimeException("Illegal attribute value: " + values);
}
code |= l.longValue();
}
}
return code;
} |
d88e4f82-06ca-41e0-9205-09a6d7cea5d9 | private long parseAspect(String [] codes, int position) {
assertTrue(position < codes.length, "Aspect not present in tag data.");
this.aspect = codes[position];
return assertValidReturnCode(ASPECT_VALUES, aspect);
} |
9d8c5b50-ada3-4bd5-885f-fa84ed0480bf | private long parseDegree(String [] codes, int position) {
assertTrue(position < codes.length, "Degree not present in tag data.");
this.degree = codes[position];
return assertValidReturnCode(DEGREE_VALUES, codes[position]);
} |
c1210241-3e48-474b-86f7-c251769949ff | private long parseVocality(String [] codes, int position) {
assertTrue(position < codes.length, "Vocality not present in tag data.");
this.vocalicity = codes[position];
return assertValidReturnCode(VOCALITY_VALUES, codes[position]);
} |
4e577a50-c5df-40e6-9bae-768b6d0d8383 | private long parseCase(String [] codes, int position) {
assertTrue(position < codes.length, "Case not present in tag data.");
this._case = codes[position];
return assertValidReturnCode(CASE_VALUES, codes[position]);
} |
c19590b4-c6ea-47b2-a2ec-c5be41464706 | private long parseNumber(String [] codes, int position) {
assertTrue(position < codes.length, "Number not present in tag data.");
this.number = codes[position];
return assertValidReturnCode(NUMBER_VALUES, codes[position]);
} |
bfa4757c-00aa-4300-8335-5f6329dc1ca4 | private long parseGender(String [] codes, int position) {
assertTrue(position < codes.length, "Gender not present in tag data.");
this.gender = codes[position];
return assertValidReturnCode(GENDER_VALUES, codes[position]);
} |
12814d2d-1b2f-4b4f-a372-e2bf4e4837d9 | private long parseNegation(String [] codes, int position) {
assertTrue(position < codes.length, "Negation not present in tag data.");
this.negation = codes[position];
return assertValidReturnCode(NEGATION_VALUES, codes[position]);
} |
9967ba78-36dd-40df-b433-72083a3076e1 | private long parsePerson(String [] codes, int position) {
assertTrue(position < codes.length, "Person not present in tag data.");
this.person = codes[position];
return assertValidReturnCode(PERSON_VALUES, codes[position]);
} |
8e10439b-3b63-45f2-9775-030f57b680e8 | private long parseAccentability(String [] codes, int position) {
assertTrue(position < codes.length, "Accentability not present in tag data.");
this.accentability = codes[position];
return assertValidReturnCode(ACCENTABILITY_VALUES, codes[position]);
} |
3f215ead-e5d6-4e56-9090-a64bf0c36fba | private long parsePostPrepositionality(String [] codes, int position) {
assertTrue(position < codes.length, "Post-prepositionality not present in tag data.");
this.postPrepositionality = codes[position];
return assertValidReturnCode(POST_PREPOSITIONALITY_VALUES, codes[position]);
} |
5c86e501-be7d-4e61-86c7-5a5b482161cf | private long parseAccommodability(String [] codes, int position) {
assertTrue(position < codes.length, "Accommodability not present in tag data.");
this.accommodability = codes[position];
return assertValidReturnCode(ACCOMMODABILITY_VALUES, codes[position]);
} |
fce42e60-d372-4633-b20b-110ecf78a59a | private long parseAgglutination(String [] codes, int position) {
assertTrue(position < codes.length, "Agglutination not present in tag data.");
this.agglutination = codes[position];
return assertValidReturnCode(AGGLUTINATION_VALUES, codes[position]);
} |
43520e06-c74c-4db6-b65f-c25480f9f8c6 | private void parse(String tagCode) {
String [] codes = tagCode.split(":");
this.partOfSpeech = codes[0];
long tagLongCode = 0;
if ("adja".equals(partOfSpeech)
|| "adjp".equals(partOfSpeech)
|| "conj".equals(partOfSpeech)
|| "interp".equals(partOfSpeech)
|| "pred".equals(partOfSpeech)
|| "xxx".equals(partOfSpeech)
|| "ign".equals(partOfSpeech)) {
assertTrue(codes.length == 1,
"Unknown extra data associated with POS tag: " + partOfSpeech);
} else if ("adv".equals(partOfSpeech)) {
tagLongCode |= parseDegree(codes, 1);
assertTrue(codes.length == 2, "Incorrect extra tag data: " + tagCode);
} else if ("imps".equals(partOfSpeech)
|| "inf".equals(partOfSpeech)
|| "pant".equals(partOfSpeech)
|| "pcon".equals(partOfSpeech)) {
tagLongCode |= parseAspect(codes, 1);
assertTrue(codes.length == 2, "Incorrect extra tag data: " + tagCode);
} else if ("qub".equals(partOfSpeech)) {
if (codes.length > 1) {
tagLongCode |= parseVocality(codes, 1);
assertTrue(codes.length == 2, "Incorrect extra tag data: " + tagCode);
} else {
assertTrue(codes.length == 1, "Incorrect extra tag data: " + tagCode);
}
} else if ("prep".equals(partOfSpeech)) {
tagLongCode |= parseCase(codes, 1);
if (codes.length > 2) {
tagLongCode |= parseVocality(codes, 2);
assertTrue(codes.length == 3, "Incorrect extra tag data: " + tagCode);
} else {
assertTrue(codes.length == 2, "Incorrect extra tag data: " + tagCode);
}
} else if ("siebie".equals(partOfSpeech)) {
tagLongCode |= parseCase(codes, 1);
} else if ("subst".equals(partOfSpeech)
|| "depr".equals(partOfSpeech)
|| "xxs".equals(partOfSpeech)) {
tagLongCode |= parseNumber(codes, 1);
tagLongCode |= parseCase(codes, 2);
tagLongCode |= parseGender(codes, 3);
assertTrue(codes.length == 4, "Incorrect extra tag data: " + tagCode);
} else if ("ger".equals(partOfSpeech)) {
tagLongCode |= parseNumber(codes, 1);
tagLongCode |= parseCase(codes, 2);
tagLongCode |= parseGender(codes, 3);
tagLongCode |= parseAspect(codes, 4);
tagLongCode |= parseNegation(codes, 5);
assertTrue(codes.length == 6, "Incorrect extra tag data: " + tagCode);
} else if ("ppron12".equals(partOfSpeech)) {
tagLongCode |= parseNumber(codes, 1);
tagLongCode |= parseCase(codes, 2);
tagLongCode |= parseGender(codes, 3);
tagLongCode |= parsePerson(codes, 4);
if (codes.length > 5) {
tagLongCode |= parseAccentability(codes, 5);
assertTrue(codes.length == 6, "Incorrect extra tag data: " + tagCode);
} else {
assertTrue(codes.length == 5, "Incorrect extra tag data: " + tagCode);
}
} else if ("ppron3".equals(partOfSpeech)) {
tagLongCode |= parseNumber(codes, 1);
tagLongCode |= parseCase(codes, 2);
tagLongCode |= parseGender(codes, 3);
tagLongCode |= parsePerson(codes, 4);
int i = 5;
if (codes.length > 5) {
if (!codes[i].endsWith("p")) {
tagLongCode |= parseAccentability(codes, i);
i++;
}
if (i < codes.length) {
tagLongCode |= parsePostPrepositionality(codes, i);
i++;
}
}
assertTrue(codes.length == i, "Incorrect extra tag data: " + tagCode);
} else if ("num".equals(partOfSpeech)) {
tagLongCode |= parseNumber(codes, 1);
tagLongCode |= parseCase(codes, 2);
tagLongCode |= parseGender(codes, 3);
if (codes.length > 4) {
tagLongCode |= parseAccommodability(codes, 4);
}
} else if ("adj".equals(partOfSpeech)) {
tagLongCode |= parseNumber(codes, 1);
tagLongCode |= parseCase(codes, 2);
tagLongCode |= parseGender(codes, 3);
tagLongCode |= parseDegree(codes, 4);
assertTrue(codes.length == 5, "Incorrect extra tag data: " + tagCode);
} else if ("pact".equals(partOfSpeech)
|| "ppas".equals(partOfSpeech)) {
tagLongCode |= parseNumber(codes, 1);
tagLongCode |= parseCase(codes, 2);
tagLongCode |= parseGender(codes, 3);
tagLongCode |= parseAspect(codes, 4);
tagLongCode |= parseNegation(codes, 5);
assertTrue(codes.length == 6, "Incorrect extra tag data: " + tagCode);
} else if ("bedzie".equals(partOfSpeech)
|| "fin".equals(partOfSpeech)
|| "impt".equals(partOfSpeech)) {
tagLongCode |= parseNumber(codes, 1);
tagLongCode |= parsePerson(codes, 2);
tagLongCode |= parseAspect(codes, 3);
assertTrue(codes.length == 4, "Incorrect extra tag data: " + tagCode);
} else if ("winien".equals(partOfSpeech)) {
tagLongCode |= parseNumber(codes, 1);
tagLongCode |= parseGender(codes, 2);
tagLongCode |= parseAspect(codes, 3);
assertTrue(codes.length == 4, "Incorrect extra tag data: " + tagCode);
} else if ("praet".equals(partOfSpeech)) {
tagLongCode |= parseNumber(codes, 1);
tagLongCode |= parseGender(codes, 2);
tagLongCode |= parseAspect(codes, 3);
int i = 4;
if (codes.length > 4) {
tagLongCode |= parseAgglutination(codes, i);
i++;
}
assertTrue(codes.length == i, "Incorrect extra tag data: " + tagCode);
} else if ("aglt".equals(partOfSpeech)) {
tagLongCode |= parseNumber(codes, 1);
tagLongCode |= parsePerson(codes, 2);
tagLongCode |= parseAspect(codes, 3);
tagLongCode |= parseVocality(codes, 4);
assertTrue(codes.length == 5, "Incorrect extra tag data: " + tagCode);
} else {
throw new TagParserException("Unknown POS tag: '" + tagCode + "'");
}
Long posCode = (Long) posCodes.get(partOfSpeech);
tagLongCode = tagLongCode | posCode.longValue();
this.tagCode = tagLongCode;
} |
9a9f7460-29b4-4688-8129-543beb05a884 | public long getCode() {
return tagCode;
} |
647ef082-f38d-49ec-926c-2bd174f27f17 | public static String toString(final long code) {
StringBuffer buf = new StringBuffer();
switch ((int) (code & MASK_POS)) {
case (int)NOUN_SUBST : buf.append("subst"); break;
case (int)NOUN_DEPR : buf.append("depr"); break;
case (int)NOUN : buf.append("NOUN"); break;
case (int)ADJ : buf.append("ADJ"); break;
case (int)ADJ_ : buf.append("adj"); break;
case (int)ADJ_A : buf.append("adja"); break;
case (int)ADJ_P : buf.append("adjp"); break;
case (int)ADV : buf.append("adv"); break;
case (int)NUM : buf.append("num"); break;
case (int)PPRON : buf.append("ppron"); break;
case (int)PPRON_12 : buf.append("ppron12"); break;
case (int)PPRON_3 : buf.append("ppron3"); break;
case (int)PPRON_SIEBIE : buf.append("siebie"); break;
case (int)VERB : buf.append("VERB"); break;
case (int)VERB_FIN : buf.append("fin"); break;
case (int)VERB_BEDZIE : buf.append("bedzie"); break;
case (int)VERB_AGLT : buf.append("aglt"); break;
case (int)VERB_PRAET : buf.append("praet"); break;
case (int)VERB_IMPT : buf.append("impt"); break;
case (int)VERB_IMPS : buf.append("imps"); break;
case (int)VERB_INF : buf.append("inf"); break;
case (int)VERB_PCON : buf.append("pcon"); break;
case (int)VERB_PANT : buf.append("pant"); break;
case (int)VERB_GER : buf.append("ger"); break;
case (int)VERB_PACT : buf.append("pact"); break;
case (int)VERB_PPAS : buf.append("ppas"); break;
case (int)WINIEN : buf.append("winien"); break;
case (int)PRED : buf.append("pred"); break;
case (int)PREP : buf.append("prep"); break;
case (int)CONJ : buf.append("conj"); break;
case (int)QUB : buf.append("qub"); break;
case (int)XXS : buf.append("xxs"); break;
case (int)XXX : buf.append("xxx"); break;
case (int)INTERP : buf.append("interp"); break;
case (int)IGN : buf.append("ign"); break;
case (int)UNKNOWN : buf.append("?"); break;
default:
throw new RuntimeException("Unreachable state.");
}
int emitChunks = 0;
switch ((int) (code & MASK_POS)) {
case (int) ADJ_A:
case (int) ADJ_P:
case (int) CONJ:
case (int) INTERP:
case (int) PRED:
case (int) XXX:
case (int) IGN:
break;
case (int) ADV:
emitChunks |= EMIT_DEGREE;
break;
case (int) VERB_IMPS:
case (int) VERB_INF:
case (int) VERB_PANT:
case (int) VERB_PCON:
emitChunks |= EMIT_ASPECT;
break;
case (int) QUB:
emitChunks |= EMIT_VOCALITY;
break;
case (int) PREP:
emitChunks |= EMIT_CASE | EMIT_VOCALITY;
break;
case (int) PPRON:
emitChunks |= EMIT_NUMBER | EMIT_CASE | EMIT_GENDER | EMIT_PERSON | EMIT_ACCENTABILITY | EMIT_POST_PREPOSITIONALITY;
break;
case (int) PPRON_SIEBIE:
emitChunks |= EMIT_CASE;
break;
case (int) NOUN:
case (int) NOUN_SUBST:
case (int) NOUN_DEPR:
case (int) XXS:
emitChunks |= EMIT_NUMBER | EMIT_CASE | EMIT_GENDER;
break;
case (int) VERB_GER:
emitChunks |= EMIT_NUMBER | EMIT_CASE | EMIT_GENDER | EMIT_ASPECT | EMIT_NEGATION;
break;
case (int) PPRON_12:
emitChunks |= EMIT_NUMBER | EMIT_CASE | EMIT_GENDER | EMIT_PERSON | EMIT_ACCENTABILITY;
break;
case (int) PPRON_3:
emitChunks |= EMIT_NUMBER | EMIT_CASE | EMIT_GENDER | EMIT_PERSON | EMIT_ACCENTABILITY | EMIT_POST_PREPOSITIONALITY;
break;
case (int) NUM:
emitChunks |= EMIT_NUMBER | EMIT_CASE | EMIT_GENDER | EMIT_ACCOMMODABILITY;
break;
case (int) ADJ:
case (int) ADJ_:
emitChunks |= EMIT_NUMBER | EMIT_CASE | EMIT_GENDER | EMIT_DEGREE;
break;
case (int) VERB:
case (int) VERB_PACT:
case (int) VERB_PPAS:
emitChunks |= EMIT_NUMBER | EMIT_CASE | EMIT_GENDER | EMIT_ASPECT | EMIT_NEGATION;
break;
case (int) VERB_BEDZIE:
case (int) VERB_FIN:
case (int) VERB_IMPT:
emitChunks |= EMIT_NUMBER | EMIT_PERSON | EMIT_ASPECT;
break;
case (int) WINIEN:
emitChunks |= EMIT_NUMBER | EMIT_GENDER | EMIT_ASPECT;
break;
case (int) VERB_PRAET:
emitChunks |= EMIT_NUMBER | EMIT_GENDER | EMIT_ASPECT | EMIT_AGGLUTINATION;
break;
case (int) VERB_AGLT:
emitChunks |= EMIT_NUMBER | EMIT_PERSON | EMIT_ASPECT | EMIT_VOCALITY;
break;
default:
throw new RuntimeException("Unreachable state.");
}
if ((emitChunks & EMIT_NUMBER) != 0 && (code & MASK_NUMBER) != 0) {
emit(buf, NUMBER_VALUES_ORDER, NUMBER_VALUES, code);
}
if ((emitChunks & EMIT_CASE) != 0) {
emit(buf, CASE_VALUES_ORDER, CASE_VALUES, code);
}
if ((emitChunks & EMIT_GENDER) != 0 && (code & MASK_GENDER) != 0) {
String [] codesOrder;
if ((code & (GENDER_N1 | GENDER_N2)) == GENDER_N) {
// There is NO specific gender code. Emit 'N' emission then.
// IPI-WSTEPNY
codesOrder = GENDER_VALUES_ORDER_IPI_WSTEPNY;
} else {
codesOrder = GENDER_VALUES_ORDER;
}
emit(buf, codesOrder, GENDER_VALUES, code & MASK_GENDER);
}
if ((emitChunks & EMIT_PERSON) != 0) {
emit(buf, PERSON_VALUES_ORDER, PERSON_VALUES, code);
}
if ((emitChunks & EMIT_DEGREE) != 0) {
emit(buf, DEGREE_VALUES_ORDER, DEGREE_VALUES, code);
}
if ((emitChunks & EMIT_ASPECT) != 0) {
emit(buf, ASPECT_VALUES_ORDER, ASPECT_VALUES, code);
}
if ((emitChunks & EMIT_NEGATION) != 0) {
emit(buf, NEGATION_VALUES_ORDER, NEGATION_VALUES, code);
}
if ((emitChunks & EMIT_ACCENTABILITY) != 0) {
emit(buf, ACCENTABILITY_VALUES_ORDER, ACCENTABILITY_VALUES, code);
}
if ((emitChunks & EMIT_POST_PREPOSITIONALITY) != 0) {
emit(buf, POST_PREPOSITIONALITY_VALUES_ORDER, POST_PREPOSITIONALITY_VALUES, code);
}
if ((emitChunks & EMIT_ACCOMMODABILITY) != 0) {
emit(buf, ACCOMMODABILITY_VALUES_ORDER, ACCOMMODABILITY_VALUES, code);
}
if ((emitChunks & EMIT_AGGLUTINATION) != 0) {
emit(buf, AGGLUTINATION_VALUES_ORDER, AGGLUTINATION_VALUES, code);
}
if ((emitChunks & EMIT_VOCALITY) != 0) {
emit(buf, VOCALITY_VALUES_ORDER, VOCALITY_VALUES, code);
}
return buf.toString();
} |
e6f48a99-3530-4060-8a99-9861e38559d9 | public String toString() {
return toString(this.tagCode);
} |
b3eab15d-9ef5-49ae-b778-95018e6f05e5 | private static void emit(StringBuffer buf, String [] keys, HashMap values, long code) {
buf.append(':');
boolean emitted = false;
long allValues = 0;
for (int i = 0; i< keys.length; i++) {
final String key = keys[i];
long bitmask = ((Long) values.get(key)).longValue();
allValues |= bitmask;
if ((code & bitmask) == bitmask) {
if (emitted) {
buf.append('.');
} else {
emitted = true;
}
buf.append(key);
}
}
if (!emitted) {
buf.deleteCharAt(buf.length()-1);
} else {
if (EMIT_UNDERSCORE && allValues == code) {
buf.delete(buf.lastIndexOf(":") + 1, Integer.MAX_VALUE);
buf.append("_");
}
}
} |
db6aa94f-35cb-4715-84a5-e286c83afd8b | public static Tag[] create(final String tagAlternatives) {
if (tagAlternatives.length() == 0) {
return new Tag[0];
}
final String [] codes = tagAlternatives.split("\\|");
final Tag[] tagList = new Tag[codes.length];
for (int i=0; i<tagList.length; i++) {
tagList[i] = new Tag(codes[i]);
}
return tagList;
} |
df552f24-b7e6-44c7-80b2-e8438ed3d85c | public static boolean contained(final long widerTag, final long narrowTag) {
if ((widerTag & Tag.MASK_POS) == (narrowTag & Tag.MASK_POS)) {
final long masked = narrowTag & widerTag;
if (((widerTag & MASK_ASPECT) != 0) && (masked & MASK_ASPECT) == 0)
return false;
if (((widerTag & MASK_CASE) != 0) && (masked & MASK_CASE) == 0)
return false;
if (((widerTag & MASK_DEGREE) != 0) && (masked & MASK_DEGREE) == 0)
return false;
if (((widerTag & MASK_GENDER) != 0) && (masked & MASK_GENDER) == 0)
return false;
if (((widerTag & MASK_NUMBER) != 0) && (masked & MASK_NUMBER) == 0)
return false;
if (((widerTag & MASK_PERSON) != 0) && (masked & MASK_PERSON) == 0)
return false;
return true;
}
return false;
} |
34d8e4a9-666d-41e8-a9c4-f8300307dff2 | public MorfeuszDemo() {
} |
e484a8be-8d7a-4b9a-998a-72747d2361a3 | public void analyze(Reader input, Writer output) throws IOException {
Analyzer analyzer = Morfeusz.getInstance().getAnalyzer();
int words = 0;
long start = System.currentTimeMillis();
StreamTokenizer tokenizer = new StreamTokenizer(input);
int token;
while (StreamTokenizer.TT_EOF != (token = tokenizer.nextToken())) {
if (token == StreamTokenizer.TT_WORD) {
output.write(tokenizer.sval);
output.write(" ");
words++;
InterpMorf[] analysis =
analyzer.analyze(tokenizer.sval);
if (analysis == null) {
output.write("?");
} else {
for (int j=0; j<analyzer.getTokensNumber(); j++) {
String part = analysis[j].getTokenImage();
String lemma = analysis[j].getLemmaImage();
String tag = analysis[j].getTagImage();
if (j>0) output.write("; ");
output.write(part);
output.write(",");
output.write(lemma);
output.write(",");
output.write(tag);
if (parseTags) {
try {
Tag.create(tag);
} catch (RuntimeException e) {
System.err.println("Could not parse tag for: "
+ tokenizer.sval + " ('" + tag + "'); Error: "
+ e.toString());
}
}
}
}
output.write("\n");
} else {
// ignore other tokens
}
}
input.close();
output.close();
long time = (System.currentTimeMillis() - start);
System.err.println("Analyzed: " + words + " words in " +
time + " milliseconds." );
System.err.println( (int)(words / (time / 1000.0f)) + " words per second.");
} |
baf6f731-0b9a-4a59-85e5-64cc37c96223 | public static void main(String[] args) {
MorfeuszDemo demo = new MorfeuszDemo();
Reader r = null;
Writer w = null;
String encoding = "utf-8";
int i = 0;
try {
for (; i<args.length ; i++) {
if (args[i].equals("-help")) {
System.err.println("Arguments: [-version] [-encoding input_stream_encoding] [-parsetags] [input file] [output file]");
return;
} else if (args[i].equals("-encoding")) {
i++;
encoding = args[i];
} else if (args[i].equals("-version")) {
System.out.println(Morfeusz.getInstance().about());
// exit immediately
return;
} else if (args[i].equals("-parsetags")) {
demo.setParseTags(true);
} else {
if (r == null) {
System.err.println("Using input characters encoding: " + encoding);
r = new InputStreamReader(
new BufferedInputStream( new FileInputStream(args[i])), encoding);
} else {
if (w == null) {
w = new OutputStreamWriter(
new BufferedOutputStream( new FileOutputStream(args[i])), "utf-8");
} else {
System.err.println("Too many parameters.");
return;
}
}
}
}
if (r == null) {
System.err.println("Using input characters encoding: " + encoding);
System.err.println("Reading from standard input.");
r = new InputStreamReader( System.in, encoding );
}
if (w == null) {
w = new OutputStreamWriter( System.out, "utf-8");
}
demo.analyze(r, w);
} catch (ArrayIndexOutOfBoundsException e) {
System.err.println("Error parsing parameters at: "
+ args[i-1]);
System.err.println("Type '-help' for help.");
} catch (IOException e) {
System.err.println("IO Exception: " + e.getMessage());
} finally {
try {
if (r != null) r.close();
if (w != null) w.close();
} catch (IOException e) {
}
}
} |
5c83d52a-e3c0-4cf2-81aa-0992830f60ba | private void setParseTags(boolean value) {
this.parseTags = value;
} |
d3a935c5-4101-4812-befa-0bf03f9b9094 | Analyzer(final String encoding) {
this.encoding = encoding;
morphologicalAnalysis = new InterpMorf[MAX_GRAPH_NODES];
for (int i = 0; i < morphologicalAnalysis.length; i++) {
morphologicalAnalysis[i] = new InterpMorf(encoding);
}
} |
405e477b-46c5-4863-99b1-80253b29b8fa | public InterpMorf[] analyze(String term) {
try {
tokensNumber = morfeusz_analyse(term.getBytes(encoding));
return morphologicalAnalysis;
} catch (UnsupportedEncodingException e) {
// Practically unreachable.
throw new RuntimeException();
}
} |
89eca0b4-da65-4c6b-a4e2-420235f872ce | public InterpMorf[] analyze(byte[] term) {
tokensNumber = morfeusz_analyse(term);
return morphologicalAnalysis;
} |
780607e9-7dc9-430e-8d16-a8337a7627ef | public int getTokensNumber() {
return tokensNumber;
} |
7a99920e-c06e-49fd-9973-19aac5a7d0d0 | private final native int morfeusz_analyse(byte[] term); |
b0070ed0-caeb-407b-bc1a-8f4e1318525b | InterpMorf(String byteToCharEncoding) {
this.morfeuszCharEncoding = byteToCharEncoding;
} |
09936db0-2f25-4a44-adf4-19c3d912de26 | public final byte[] getToken() {
return token;
} |
6aa89140-3e5a-47f9-aa0b-badce3d0b44f | public final byte[] getLemma() {
return lemma;
} |
c8f78816-96d9-47f0-90a1-f1f2916908cb | public final byte[] getTag() {
return tag;
} |
5f215daa-9bdb-43e5-8bc9-417ddf5c6444 | public final int getNodeStart() {
return p;
} |
f21b4c64-bda9-482a-a119-63c41d3765d3 | public final int getNodeEnd() {
return k;
} |
a65cc86a-79f4-49ed-a91b-a792a6b47722 | public final int getLemmaLength() {
return lemmaLength;
} |
35a6d518-d35d-4d50-b329-f9aea6d9ba99 | public final int getTagLength() {
return tagLength;
} |
fd3309d4-ab19-4b2d-b9bb-b445369064e8 | public final int getTokenLength() {
return tokenLength;
} |
941d29e9-c1ee-4d6e-bc71-bbfa788aebbe | public final String getTagImage() {
try {
return new String(getTag(), 0, getTagLength(), morfeuszCharEncoding);
} catch (UnsupportedEncodingException e) {
/* Checked in a static block. */
throw new RuntimeException();
}
} |
52f5a2c5-32f1-4f2f-9ea7-5a4a4dc8f9a4 | public final String getLemmaImage() {
try {
return new String(getLemma(), 0, getLemmaLength(), morfeuszCharEncoding);
} catch (UnsupportedEncodingException e) {
/* Checked in a static block. */
throw new RuntimeException();
}
} |
6c3b67ec-0544-44ae-99ba-5fc9db1d6a90 | public final String getTokenImage() {
try {
return new String(getToken(), 0, getTokenLength(), morfeuszCharEncoding);
} catch (UnsupportedEncodingException e) {
/* Checked in a static block. */
throw new RuntimeException();
}
} |
07969665-888e-4a05-b8da-156fcbfb865e | public String toString() {
return getNodeStart() + "-" + getNodeEnd() + " : " +
getTokenImage() + " : " +
getLemmaImage() + " : " +
getTagImage();
} |
046c1854-b6f8-4636-a9fe-44fb32ce5a71 | private Morfeusz(final int morfeuszEncoding) {
this.encoding = encodingToCodePage(morfeuszEncoding);
if (morfeusz_set_option(MORFOPT_ENCODING, morfeuszEncoding) != 1) {
throw new RuntimeException("Morfeusz option MORFOPT_ENCODING could not be set to the default" +
" encoding: " + instance.encoding);
}
} |
e469a356-6b58-43ad-ba87-0c470fd040a1 | public static Morfeusz getInstance()
throws UnsupportedEncodingException, SecurityException, UnsatisfiedLinkError
{
synchronized (Morfeusz.class) {
if (instance != null) {
return instance;
}
Runtime.getRuntime().loadLibrary("morfeusz-java");
Morfeusz instance = new Morfeusz(MORFEUSZ_DEFAULT_ENCODING);
instance.aboutInfo = new String(instance.aboutJniNative(), ENCODING_ISO8859_2) + "\n\n";
InputStream is = instance.getClass().getResourceAsStream("/res/version.txt");
if (is == null) {
instance.aboutInfo += "Java JNI binding code (c) Dawid Weiss\n(no version info)";
} else {
try {
Properties p = new Properties();
p.load(is);
instance.aboutInfo += p.getProperty("copyclause") + "\n";
instance.aboutInfo += "Version: " + p.getProperty("version") + "\n";
instance.aboutInfo += "SVN-Id: " + p.getProperty("svnid") + "\n";
} catch (IOException e) {
instance.aboutInfo += "Java JNI binding code (c) Dawid Weiss\n(exception reading version info)";
} finally {
try {
is.close();
} catch (IOException e) {
// ignore
}
}
}
Morfeusz.instance = instance;
return instance;
}
} |
c14c2e51-a446-4031-8b4a-790bb022b31c | public String about() {
return aboutInfo;
} |
40417820-e1ff-4b9b-a819-ddadabb7c0f2 | public Analyzer getAnalyzer() {
return new Analyzer(encoding);
} |
3f3c3159-5a65-4f5c-a8d9-124dc2fb2df3 | private native byte[] aboutJniNative(); |
1f04937a-bde6-415a-b3d9-f2c59fc69a2b | public native int morfeusz_set_option(int option, int value); |
843b19b6-0cbc-4380-92f1-739fca33f2fc | private static String encodingToCodePage(final int morfeuszEncoding) {
switch (morfeuszEncoding) {
case MORFEUSZ_CP1250:
return ENCODING_CP1250;
case MORFEUSZ_ISO8859_2:
return ENCODING_ISO8859_2;
case MORFEUSZ_CP852:
return ENCODING_CP852;
case MORFEUSZ_UTF8:
return ENCODING_UTF8;
default:
throw new IllegalArgumentException("Unknown encoding: " + morfeuszEncoding);
}
} |
f6e35f8b-41fd-436e-a12b-e67c7c1f47bf | private KDTree() {} |
df180c71-1bac-4ff2-a825-29e2ac00e013 | public static KDTree build(List<? extends Point> points) {
KDTree tree = new KDTree();
tree.root = build(points, 0);
return tree;
} |
05442958-a42c-4112-8718-43105d2e0ffc | private static KDTreeNode build(List<? extends Point> points, int depth) {
if (points.isEmpty()) return null;
final int axis = depth % 2;
Collections.sort(points, new Comparator<Point>() {
public int compare(Point p1, Point p2) {
double coord1 = p1.getCoords()[axis];
double coord2 = p2.getCoords()[axis];
return Double.compare(coord1, coord2);
}
});
int index = points.size() / 2;
KDTreeNode leftChild = build(points.subList(0, index), depth + 1);
KDTreeNode rightChild = build(points.subList(index + 1, points.size()), depth + 1);
Point point = points.get(index);
return new KDTreeNode(point, axis, leftChild, rightChild);
} |
df4d8f9c-798f-4e30-92f1-6f37637d997d | public int compare(Point p1, Point p2) {
double coord1 = p1.getCoords()[axis];
double coord2 = p2.getCoords()[axis];
return Double.compare(coord1, coord2);
} |
57850ece-0dfb-4020-8669-7d0629f696a6 | @SuppressWarnings({"unchecked"})
public <T extends Point> T findNearest(Point point) {
return (T) findNearest(point, 1).get(0);
} |
b9ba9faf-bf38-42ef-bdff-46ccc8bed6be | public List<? extends Point> findNearest(Point point, int amount) {
return root.findNearest(point, amount);
} |
b1d0f22b-475b-4d8e-bcaa-d0dcf5d550d4 | @SuppressWarnings({"unchecked"})
public <T extends Point> T getRootPoint() {
return (T) root.getPoint();
} |
e6c0d3aa-c7ec-40cd-bdce-385cf3025c49 | public KDTreeNode(Point point, int axis, KDTreeNode leftChild, KDTreeNode rightChild) {
this.point = point;
this.axis = axis;
this.leftChild = leftChild;
this.rightChild = rightChild;
} |
71c78077-95d0-4caf-bfaa-0e281ad86f2d | private KDTreeNode(KDTreeNode node, Double distance) {
this(node.point, node.axis, node.leftChild, node.rightChild);
this.distance = distance;
} |
1e7be6a3-f8db-4ed1-a433-fbccea7ab0c6 | @Override
public String toString() {
return "KDTreeNode{" +
"leftChild=" + leftChild +
", rightChild=" + rightChild +
", point=" + point +
'}';
} |
fb34c12b-9bd6-444a-9e53-ab79a9fce7b7 | public double[] getPointCoords() {
return point.getCoords();
} |
d0b902f8-96c2-4ecc-a9d1-991fdb4aff69 | public List<Point> findNearest(Point point, int amount) {
if (amount <= 0)
throw new IllegalArgumentException("Amount of points to return must not be negative, but (" + amount + ") was provided.");
KDTreeNode[] nodes = findNearestNodes(point, amount);
List<Point> points = new ArrayList<>();
for (KDTreeNode node : nodes) {
if (node != null) points.add(node.point);
}
return points;
} |
63ecfd84-3ab6-4f73-b0e1-921a517e1fcb | private KDTreeNode[] findNearestNodes(Point point, int amount) {
if (liesOnLeftPlane(point)) {
return findNearestNodes(point, amount, leftChild, rightChild);
}
return findNearestNodes(point, amount, rightChild, leftChild);
} |
ed05d171-da42-4555-b1bf-e0d0b393e4aa | private KDTreeNode[] findNearestNodes(Point point, int amount, KDTreeNode currentChild, KDTreeNode otherChild) {
KDTreeNode[] bestNodes = null;
KDTreeNode currentNode = new KDTreeNode(this, distance(point));
if (currentChild != null) {
bestNodes = currentChild.findNearestNodes(point, amount);
insertNode(bestNodes, currentNode);
} else {
bestNodes = new KDTreeNode[amount];
bestNodes[0] = currentNode;
}
if (otherChild != null) {
KDTreeNode[] otherBestNodes = null;
for (KDTreeNode bestNode : bestNodes) {
// Check if on the other plane of the node may be nodes that lie closer
// than current best nodes. If do search the other plane.
if (bestNode != null && pow(getCoordOnAxist() - point.getCoords()[axis], 2) < bestNode.distance) {
otherBestNodes = otherChild.findNearestNodes(point, amount);
break;
}
}
if (otherBestNodes != null) merge(bestNodes, otherBestNodes);
}
return bestNodes;
} |
03c072d9-0069-4088-ac0e-234702795d60 | private void insertNode(KDTreeNode[] nodes, KDTreeNode node) {
if (node != null) {
for (int i = 0; i < nodes.length; i++) {
if (nodes[i] == null || nodes[i].distance > node.distance) {
rshift(nodes, i);
nodes[i] = node;
break;
}
}
}
} |
1dee1340-e610-4bad-9f0a-bb97a3f7848f | private void merge(KDTreeNode[] mergeTo, KDTreeNode[] mergeFrom) {
for (KDTreeNode node : mergeFrom) {
insertNode(mergeTo, node);
}
} |
8c8e4848-6890-4eab-af19-66e1aeb9af5e | private double distance(Point point) {
// intentionally skipped square root
return pow(getPointCoords()[0] - point.getCoords()[0], 2) + pow(getPointCoords()[1] - point.getCoords()[1], 2);
} |
a45b8e6d-4d13-44dc-bad6-f0f74d7ee810 | private boolean liesOnLeftPlane(Point point) {
return point.getCoords()[axis] <= getCoordOnAxist();
} |
585ea1c3-c69b-4907-8505-347e69ac3f68 | private double getCoordOnAxist() {
return point.getCoords()[axis];
} |
171fb1f3-86f1-4602-9dfe-825c2be41d68 | public Point getPoint() {
return point;
} |
3b21f8e2-d99d-4a2d-940b-e045b08fefac | double[] getCoords(); |
b9fe20d9-4afd-431c-af8b-5f8cc961d162 | @DataProvider
public Object[][] itShouldFindNearestPoint() {
return new Object[][] {
{ points, $(4, 4), 1 },
{ points, $(4.1, 6.4), 0.5 },
{ points, $(4.1, 6.4), 0.5 },
{ points, $(9, 8), 1.99999 },
{ points, $(9, 4), 0.99999 },
};
} |
c099184b-96f9-4a6b-85a7-0a205bfa0ec7 | @Test(dataProvider = "itShouldFindNearestPoint")
public void itShouldFindNearestPoint(List<? extends Point> points, Point expectedPoint, double delta) {
// when
KDTree tree = KDTree.build(points);
// then
double[] coords = expectedPoint.getCoords();
assertEquals(tree.findNearest($(coords[0], coords[1] - delta)), expectedPoint);
assertEquals(tree.findNearest($(coords[0] + delta, coords[1] - delta)), expectedPoint);
assertEquals(tree.findNearest($(coords[0] + delta, coords[1])), expectedPoint);
assertEquals(tree.findNearest($(coords[0] + delta, coords[1] + delta)), expectedPoint);
assertEquals(tree.findNearest($(coords[0], coords[1] + delta)), expectedPoint);
assertEquals(tree.findNearest($(coords[0] - delta, coords[1] + delta)), expectedPoint);
assertEquals(tree.findNearest($(coords[0] - delta, coords[1])), expectedPoint);
assertEquals(tree.findNearest($(coords[0] - delta, coords[1] + delta)), expectedPoint);
} |
fc9454eb-23b5-470d-b89b-ebf1e5598453 | @DataProvider
public Object[][] itShouldFindNearestPoints() {
return new Object[][] {
{ points, $(6, 4), Arrays.asList($(4, 4)) },
{ points, $(6, 4), Arrays.asList($(4, 4), $(6, 1)) },
{ points, $(6, 4), Arrays.asList($(4, 4), $(6, 1), $(9, 4)) },
{ points, $(6, 4), Arrays.asList($(4, 4), $(6, 1), $(9, 4), $(4.1, 6.4)) },
};
} |
d9b51314-6a02-4a91-ac62-b49f5122f72a | @Test(dataProvider = "itShouldFindNearestPoints")
public void itShouldFindNearestPoints(List<? extends Point> points, Point point, List<? extends Point> expectedPoints) {
// when
KDTree tree = KDTree.build(points);
List<? extends Point> result = tree.findNearest(point, expectedPoints.size());
// then
assertEquals(result.size(), expectedPoints.size());
for (int i = 0; i < expectedPoints.size(); i++) {
assertEquals(result.get(i), expectedPoints.get(i));
}
} |
ddcc8e61-52ce-44b2-aeda-f31907804fca | private static TestPoint $(double lon, double lat) {
return new TestPoint(lon, lat);
} |
52448f81-2b5d-416d-9b6f-7be9cd076037 | public TestPoint(double lon, double lat) {
this.lon = lon;
this.lat = lat;
} |
3bd600aa-fde6-4286-9c69-df5c06d80a7e | @Override
public double[] getCoords() {
return new double[] { lon, lat };
} |
b33b82d1-5501-4aa1-b510-f7f24cd60163 | @Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
TestPoint testPoint = (TestPoint) o;
if (Double.compare(testPoint.lat, lat) != 0) return false;
if (Double.compare(testPoint.lon, lon) != 0) return false;
return true;
} |
8d9b9ce2-8146-4f14-9037-712f81f671a8 | @Override
public int hashCode() {
int result;
long temp;
temp = lon != +0.0d ? Double.doubleToLongBits(lon) : 0L;
result = (int) (temp ^ (temp >>> 32));
temp = lat != +0.0d ? Double.doubleToLongBits(lat) : 0L;
result = 31 * result + (int) (temp ^ (temp >>> 32));
return result;
} |
ce064ae1-b08f-4f26-858a-91e15840954a | @Override
public String toString() {
return "[" + lon +
", " + lat +
']';
} |
a49ae55d-2cd5-46ef-87be-9e22f46e2ef3 | public BirdWeightLogger() throws FileNotFoundException, IOException {
config = new ConfigParser("config.cfg");
WAIT_FOR_ATT = config.getTimeout() * 1000;
DATA_RATE = config.getDataRate();
initOptions();
} |
2d94e3ed-5583-41a3-b37a-e88788d0605e | private void initOptions() {
System.out.println("Starting Bird Weight Logger...");
bridges = new HashMap<Integer, BridgePhidget>(config.getNumBridges());
rfids = new HashMap<Integer, RFIDPhidget>(config.getNumRfidReaders());
// Init all bridges in configuration file and open them.
for(int i = 0; i < config.getNumBridges(); i++) {
try {
BridgePhidget tempB = new BridgePhidget();
tempB.open(config.getBridgeSerial(i));
tempB.addAttachListener(new AttachListener() {
@Override
public void attached(AttachEvent ae) {
try {
System.out.println("Bridge attached: "
+ ae.getSource().getDeviceLabel() + ", S/N: "
+ ae.getSource().getSerialNumber());
BridgePhidget b = (BridgePhidget)ae.getSource();
b.setDataRate(DATA_RATE);
b.setGain(0, BridgePhidget.PHIDGET_BRIDGE_GAIN_32);
b.setEnabled(0, true);
b.setGain(1, BridgePhidget.PHIDGET_BRIDGE_GAIN_32);
b.setEnabled(1, true);
}
catch (PhidgetException e) {
e.printStackTrace();
}
}
});
tempB.addBridgeDataListener(new BridgeDataListener() {
@Override
public void bridgeData(BridgeDataEvent arg0) {
BridgePhidget b = (BridgePhidget)arg0.getSource();
try {
double data_0;
double data_1;
double k_0 = config.getLoadCellKValue(0, 0);
double k_1 = config.getLoadCellKValue(0, 1);
double offset_0 = config.getLoadCellOffset(0, 0);
double offset_1 = config.getLoadCellOffset(0, 1);
POSSIBLE_TARE_0 = k_0 * (b.getBridgeValue(0) + offset_0);
data_0 = k_0 * (b.getBridgeValue(0) + offset_0) - TARE_0;
POSSIBLE_TARE_1 = k_1 * (b.getBridgeValue(1) + offset_1);
data_1 = k_1 * (b.getBridgeValue(1) + offset_1) - TARE_1;
int i_data_0 = (int)(data_0 * 10);
int i_data_1 = (int)(data_1 * 10);
data_0 = i_data_0 / 10.0;
data_1 = i_data_1 / 10.0;
String rfid0 = rfids.get(config.getRfidSerials()[1]).getDeviceLabel();
String rfid1 = rfids.get(config.getRfidSerials()[0]).getDeviceLabel();
System.out.print(" \r");
System.out.print(rfid0 + ": " + data_0 + "\t" + rfid1 + ":" + data_1 + "\r");
//Thread.sleep(500);
} catch (PhidgetException e) {
e.printStackTrace();
}
}
});
tempB.waitForAttachment(WAIT_FOR_ATT);
bridges.put(tempB.getSerialNumber(), tempB);
} catch (PhidgetException e) {
System.out.println("Bridge " + i + " connection timed out.");
e.printStackTrace();
}
}
// Init all RFID reader in configuration file and open them.
for(int i = 0; i < config.getNumRfidReaders(); i++) {
try {
RFIDPhidget tempR = new RFIDPhidget();
Boolean tempFlag = new Boolean(false);
tempR.open(config.getRfidSerial(i));
tempR.addTagGainListener(new RfidTagGainerListener(tempR, tempFlag));
tempR.addDetachListener(new DetachListener() {
@Override
public void detached(DetachEvent arg0) {
RFIDPhidget r = (RFIDPhidget)arg0.getSource();
try {
System.out.println("Detached: " + r.getDeviceLabel());
} catch (PhidgetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
tempR.addAttachListener(new AttachListener() {
@Override
public void attached(AttachEvent ae) {
try {
RFIDPhidget r = (RFIDPhidget)ae.getSource();
System.out.println("RFID attached: "
+ ae.getSource().getDeviceLabel() + ", S/N: "
+ ae.getSource().getSerialNumber());
r.setAntennaOn(true);
r.setLEDOn(true);
} catch (PhidgetException e) {
e.printStackTrace();
}
}
});
tempR.waitForAttachment(WAIT_FOR_ATT);
rfids.put(tempR.getSerialNumber(), tempR);
} catch (PhidgetException e) {
System.out.println("RFID " + i + " connection timed out.");
e.printStackTrace();
}
}
// Main thread loop
Scanner s = new Scanner(System.in);
while(true) {
char option = s.next().charAt(0);
switch(option)
{
case 'q':
System.exit(0);
break;
case '0':
TARE_0 = POSSIBLE_TARE_0;
break;
case '1':
TARE_1 = POSSIBLE_TARE_1;
break;
default:
System.out.println("Invalid command.");
break;
}
}
} |
0ddecf9c-30d6-4173-a858-e535ebb49ce8 | @Override
public void attached(AttachEvent ae) {
try {
System.out.println("Bridge attached: "
+ ae.getSource().getDeviceLabel() + ", S/N: "
+ ae.getSource().getSerialNumber());
BridgePhidget b = (BridgePhidget)ae.getSource();
b.setDataRate(DATA_RATE);
b.setGain(0, BridgePhidget.PHIDGET_BRIDGE_GAIN_32);
b.setEnabled(0, true);
b.setGain(1, BridgePhidget.PHIDGET_BRIDGE_GAIN_32);
b.setEnabled(1, true);
}
catch (PhidgetException e) {
e.printStackTrace();
}
} |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.