Commit b450ff1d by Hut

added stream style parser

parent bfb28c5a
...@@ -25,7 +25,7 @@ public class Data { ...@@ -25,7 +25,7 @@ public class Data {
} }
public Lookup info(String... s) { public Lookup info(String... s) {
return data.get(new Prefix(Arrays.stream(s).map(t -> new Token(t)) return data.get(new Prefix(Arrays.stream(s).map(t -> new Token(t, Glyph.Type.word))
.collect(Collectors.toList()))); .collect(Collectors.toList())));
} }
......
...@@ -2,9 +2,12 @@ package markov; ...@@ -2,9 +2,12 @@ package markov;
public class Glyph { public class Glyph {
public enum Type { public enum Type {
word, interpunction, control, whitespace word, interpunction, control, whitespace, empty
} }
public static final Glyph Start = new Glyph(Type.control, "START");
public static final Glyph End = new Glyph(Type.control, "END");
private Type type; private Type type;
private String content; private String content;
...@@ -29,7 +32,11 @@ public class Glyph { ...@@ -29,7 +32,11 @@ public class Glyph {
Glyph glyph = (Glyph) o; Glyph glyph = (Glyph) o;
if (type != glyph.type) return false; if (type != glyph.type) {
return false;
}else if(type == Type.control){
return this == o;
}
return content != null ? content.equals(glyph.content) : glyph.content == null; return content != null ? content.equals(glyph.content) : glyph.content == null;
} }
......
package markov; package markov;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class ParserStreamStyle { public class ParserStreamStyle {
public Stream<Token> collect(Stream<Glyph> glyphs) {
Data data = new Data();
final Container[] previous = new Container[]{null};
return glyphs.map(g -> {
Container o = new Container(g);
o.p = previous[0];
previous[0] = o;
return o;
}).flatMap(go -> {
if (go.p != null && go.p.self != null && !go.self.getType().equals(go.p.self.getType())) {
List<Glyph> containers = new ArrayList<>();
Container c = go;
do {
c = c.p;
containers.add(0, c.self);
}
while (c != null && c.p != null && c.p.self != null &&
c.self.getType().equals(c.p.self.getType()));
return Stream.<List<Glyph>>builder().add(containers).build();
} else {
return Stream.empty();
}
}).map(l -> {
String content = l.stream().map(glyph -> glyph.getContent()).collect(Collectors.joining());
Glyph.Type type = l.get(0).getType();
return new Token(content, type);
});
}
private static class Container {
Container(Glyph self) {
this.self = self;
}
final Glyph self;
Container p;
}
;
} }
...@@ -32,7 +32,7 @@ public class Parser_first_version { ...@@ -32,7 +32,7 @@ public class Parser_first_version {
tokens[i] = Token.START; tokens[i] = Token.START;
} }
for (; i < strings.length + prefix_length; i++) { for (; i < strings.length + prefix_length; i++) {
tokens[i] = new Token(strings[i - prefix_length]); tokens[i] = new Token(strings[i - prefix_length], Glyph.Type.word);
} }
tokens[i] = Token.END; tokens[i] = Token.END;
return tokens; return tokens;
......
...@@ -7,11 +7,15 @@ public class Token { ...@@ -7,11 +7,15 @@ public class Token {
protected static final Token EMPTY = new SpecialToken(); protected static final Token EMPTY = new SpecialToken();
public Token(String content) { public Token(String content) {
this(content, Glyph.Type.word);
}
public Token(String content, Glyph.Type type) {
super(); super();
this.content = content; this.content = content;
} }
String content; private String content;
private Glyph.Type type;
@Override @Override
public String toString() { public String toString() {
...@@ -19,7 +23,7 @@ public class Token { ...@@ -19,7 +23,7 @@ public class Token {
return "Token <END>"; return "Token <END>";
} }
if (this == START) { if (this == START) {
return "TOKEN <START>"; return "TOKEN <Start>";
} }
if (this == EMPTY) if (this == EMPTY)
return "TOKEN <empty>"; return "TOKEN <empty>";
...@@ -27,35 +31,21 @@ public class Token { ...@@ -27,35 +31,21 @@ public class Token {
} }
@Override @Override
public int hashCode() { public boolean equals(Object o) {
final int prime = 31; if (this == o) return true;
int result = 1; if (o == null || getClass() != o.getClass()) return false;
result = prime * result + ((content == null) ? 0 : content.hashCode());
return result; Token token = (Token) o;
if (content != null ? !content.equals(token.content) : token.content != null) return false;
return type == token.type;
} }
@Override @Override
public boolean equals(Object obj) { public int hashCode() {
if (this == obj) { int result = content != null ? content.hashCode() : 0;
return true; result = 31 * result + (type != null ? type.hashCode() : 0);
} return result;
if (obj == null)
return false;
if (this == START)
return false;
if (this == END)
return false;
if (this == EMPTY)
return false;
if (getClass() != obj.getClass())
return false;
Token other = (Token) obj;
if (content == null) {
if (other.content != null)
return false;
} else if (!content.equals(other.content))
return false;
return true;
} }
public String render(String prefix) { public String render(String prefix) {
...@@ -65,7 +55,7 @@ public class Token { ...@@ -65,7 +55,7 @@ public class Token {
private static class SpecialToken extends Token { private static class SpecialToken extends Token {
public SpecialToken() { public SpecialToken() {
super(""); super("", Glyph.Type.control);
} }
@Override @Override
......
package markov;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
class GlyphTest {
@Test
public void testEquals() {
Glyph a = new Glyph(Glyph.Type.word, "a");
Glyph a2 = new Glyph(Glyph.Type.word, "a");
Glyph w = new Glyph(Glyph.Type.whitespace, " ");
Glyph c = new Glyph(Glyph.Type.control, "");
assertEquals(a, a2);
assertNotEquals(a, w);
assertNotEquals(a, c);
assertNotEquals(Glyph.End, c);
assertNotEquals(Glyph.Start, c);
assertNotEquals(Glyph.Start, Glyph.End);
assertEquals(Glyph.Start, Glyph.Start);
assertEquals(Glyph.End, Glyph.End);
}
}
\ No newline at end of file
package markov;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import java.util.Arrays;
import java.util.stream.Collectors;
import java.util.stream.Stream;
class ParserStreamStyleTest {
@Test
void tests() {
ParserStreamStyle parserStreamStyle = new ParserStreamStyle();
Assertions.assertIterableEquals(
Arrays.asList(
new Token(Glyph.Start.getContent(), Glyph.Type.control),
new Token("ab"),
new Token(" ", Glyph.Type.whitespace),
new Token("c"),
new Token(Glyph.End.getContent(), Glyph.Type.control)),
parserStreamStyle.collect(Stream.of(Glyph.Start,
new Glyph(Glyph.Type.word, "a"),
new Glyph(Glyph.Type.word, "b"),
new Glyph(Glyph.Type.whitespace, " "),
new Glyph(Glyph.Type.word, "c"),
Glyph.End, new Glyph(Glyph.Type.empty, "EMPTY")))
.collect(Collectors.toList()));
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment