Search in sources :

Example 1 with IntIntSink

use of primal.primitive.IntIntSink in project suite by stupidsing.

the class ScrapeHtml method parse.

public HtmlNode parse(String in) {
    var pairs = new ArrayList<IntRange>();
    int pos0, posx = 0;
    nextTag: while (0 <= (pos0 = in.indexOf("<", posx))) if ((posx = pos0 + 1) < in.length() && !Is.whitespace(in.charAt(posx)))
        if (0 <= (posx = in.indexOf(">", posx))) {
            pairs.add(IntRange.of(pos0, ++posx));
            if (in.startsWith("<![CDATA[", pos0)) {
                posx = in.indexOf("]]>", pos0 + 9);
                continue nextTag;
            }
            for (var rawTextTag : List.of("script", "style", "textarea", "title")) if (in.startsWith(rawTextTag, pos0 + 1)) {
                posx = in.indexOf("</" + rawTextTag, posx);
                continue nextTag;
            }
        } else
            break;
    Fun<String, IntObjPair<String>> getNameFun = tag -> {
        int p1 = 1, px = tag.length() - 1;
        var first = tag.charAt(p1);
        var last = tag.charAt(px - 1);
        int d;
        if (first == '!')
            return IntObjPair.of(0, null);
        else {
            if (first == '/') {
                p1++;
                d = -1;
            } else if (last == '/') {
                px--;
                d = 0;
            } else
                d = 1;
            var ps = 0;
            while (ps < px && !Is.whitespace(tag.charAt(ps))) ps++;
            var name = tag.substring(p1, ps);
            return IntObjPair.of(d, name);
        }
    };
    var deque = new ArrayDeque<>(List.of(new HtmlNode(null, "", 0, 0)));
    IntIntSink addTextFun = (prevp, p0) -> {
        if (prevp != p0) {
            var s = htmlUtil.decode(in.substring(prevp, p0)).trim();
            if (!s.isEmpty())
                deque.element().children.add(new HtmlNode(null, s, prevp, p0));
        }
    };
    var prevp = 0;
    for (var pair : pairs) {
        var htmlNode = deque.element();
        var p0 = pair.s;
        var px = pair.e;
        addTextFun.sink2(prevp, p0);
        var tag = in.substring(p0, px);
        prevp = getNameFun.apply(tag).map((d, name) -> {
            if (d == -1) {
                // closing tag
                HtmlNode hn;
                while (!deque.isEmpty()) if (Equals.string(getNameFun.apply((hn = deque.pop()).tag).v, name)) {
                    hn.p2 = p0;
                    hn.px = px;
                    break;
                }
            } else {
                // opening tag
                var htmlNode1 = new HtmlNode(name, tag, p0, px);
                htmlNode.children.add(htmlNode1);
                if (d == 1)
                    deque.push(htmlNode1);
            }
            return px;
        });
    }
    addTextFun.sink2(prevp, in.length());
    return deque.pop();
}
Also used : Pair(primal.adt.Pair) PerMap(primal.persistent.PerMap) IntRange(primal.primitive.adt.IntRange) Build(primal.Verbs.Build) IntIntSink(primal.primitive.IntIntSink) Predicate(java.util.function.Predicate) Fun(primal.fp.Funs.Fun) Set(java.util.Set) Assoc(primal.parser.Operator.Assoc) Read(primal.MoreVerbs.Read) Is(primal.Verbs.Is) ArrayList(java.util.ArrayList) Streamlet(primal.streamlet.Streamlet) List(java.util.List) Split(primal.MoreVerbs.Split) Substring(primal.Verbs.Substring) ArrayDeque(java.util.ArrayDeque) Equals(primal.Verbs.Equals) IntObjPair(primal.primitive.adt.pair.IntObjPair) IntIntSink(primal.primitive.IntIntSink) IntObjPair(primal.primitive.adt.pair.IntObjPair) ArrayList(java.util.ArrayList) ArrayDeque(java.util.ArrayDeque)

Example 2 with IntIntSink

use of primal.primitive.IntIntSink in project suite by stupidsing.

the class Trade_ method collectBrokeredTrades.

public static Streamlet<Trade> collectBrokeredTrades(Puller<Trade> puller) {
    var trades0 = puller.toArray(Trade.class);
    var trades1 = new ArrayList<Trade>();
    var length0 = trades0.length;
    var i0 = 0;
    IntIntSink tx = (i0_, i1_) -> {
        if (forInt(i0_, i1_).mapInt(i -> trades0[i].buySell).sum() != 0)
            while (i0_ < i1_) {
                var trade0 = trades0[i0_++];
                if (!Equals.string(trade0.remark, "#"))
                    trades1.add(trade0);
            }
    };
    for (var i = 1; i < length0; i++) {
        var trade0 = trades0[i0];
        var trade1 = trades0[i];
        var isGroup = // 
        true && // 
        Equals.string(trade0.date, trade1.date) && // 
        Equals.string(trade0.symbol, trade1.symbol) && trade0.price == trade1.price;
        if (!isGroup) {
            tx.sink2(i0, i);
            i0 = i;
        }
    }
    tx.sink2(i0, length0);
    return Read.from(trades1);
}
Also used : Union(primal.Verbs.Union) Math.log1p(java.lang.Math.log1p) Fun(primal.fp.Funs.Fun) Math_(suite.math.Math_) HashMap(java.util.HashMap) LngFltPair(primal.primitive.adt.pair.LngFltPair) Math.abs(java.lang.Math.abs) Obj_Flt(primal.primitive.FltPrim.Obj_Flt) Dbl_Dbl(primal.primitive.Dbl_Dbl) Streamlet_.forInt(suite.util.Streamlet_.forInt) ArrayList(java.util.ArrayList) Streamlet(primal.streamlet.Streamlet) Map(java.util.Map) Math.expm1(java.lang.Math.expm1) Valuation(suite.trade.Account.Valuation) Pair(primal.adt.Pair) IntFltPair(primal.primitive.adt.pair.IntFltPair) IntIntSink(primal.primitive.IntIntSink) Source(primal.fp.Funs.Source) Set(java.util.Set) Math.min(java.lang.Math.min) Read(primal.MoreVerbs.Read) LngIntPair(primal.primitive.adt.pair.LngIntPair) List(java.util.List) Eod(suite.trade.data.DataSource.Eod) AsInt(primal.primitive.fp.AsInt) Math.max(java.lang.Math.max) Math.floor(java.lang.Math.floor) Equals(primal.Verbs.Equals) Puller(primal.puller.Puller) IntIntSink(primal.primitive.IntIntSink) ArrayList(java.util.ArrayList)

Aggregations

ArrayList (java.util.ArrayList)2 List (java.util.List)2 Set (java.util.Set)2 Read (primal.MoreVerbs.Read)2 Equals (primal.Verbs.Equals)2 Pair (primal.adt.Pair)2 Fun (primal.fp.Funs.Fun)2 IntIntSink (primal.primitive.IntIntSink)2 Streamlet (primal.streamlet.Streamlet)2 Math.abs (java.lang.Math.abs)1 Math.expm1 (java.lang.Math.expm1)1 Math.floor (java.lang.Math.floor)1 Math.log1p (java.lang.Math.log1p)1 Math.max (java.lang.Math.max)1 Math.min (java.lang.Math.min)1 ArrayDeque (java.util.ArrayDeque)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Predicate (java.util.function.Predicate)1 Split (primal.MoreVerbs.Split)1