All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.dennisit.vplus.data.plugin.ask.AskHook Maven / Gradle / Ivy

There is a newer version: 2.0.8
Show newest version
/*--------------------------------------------------------------------------
 *  Copyright (c) 2010-2020, Elon.su All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the elon developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: Elon.su, you can also mail [email protected]
 *--------------------------------------------------------------------------
 */
package com.github.dennisit.vplus.data.plugin.ask;

import com.alibaba.fastjson.JSON;
import com.github.dennisit.vplus.data.plugin.ask.elect.ElectsAction;
import com.github.dennisit.vplus.data.plugin.ask.elect.ElectsQuery;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.util.List;
import java.util.Optional;

/**
 * Created by Elon.su on 2018/1/18.
 */
@Slf4j
public class AskHook {

    private static ElectsAction electsAction = new ElectsQuery();

    public static int electsIndex(AskMixup mixup, ElectsQuery.QueryParser... queryParsers) {
        return electsAction.elect(mixup, queryParsers);
    }

    public static int electsIndex(AskMixup mixup) {
        return electsIndex(mixup, new ElectsQuery.BaiduQueryParser(), new ElectsQuery.GoogleQueryParser(), new ElectsQuery.So360QueryParser());
    }

    public static String electsText(AskMixup mixup, ElectsQuery.QueryParser... queryParsers) {
        log.info(JSON.toJSONString(mixup));
        int index = electsIndex(mixup, queryParsers);
        System.out.println(index);
        return electsText(mixup, index);
    }

    public static String electsText(AskMixup mixup) {
        int index = electsIndex(mixup);
        return electsText(mixup, index);
    }

    private static String electsText(AskMixup mixup, int index) {
        List options = Optional.ofNullable(mixup).map(x -> x.getOptions()).orElse(Lists.newArrayList());
        if (index < 0 || index > options.size() - 1) {
            return StringUtils.EMPTY;
        }
        return options.get(index);
    }

    public static void main(String[] args) {
        AskMixup mixup1 = new AskMixup("“垂死病中惊坐起” 是谁写给谁的?", Lists.newArrayList("杜甫写给李白的", "王维写给孟浩然的", "元稹写给白居易的"));
        AskMixup mixup2 = new AskMixup("美国首都是哪里", Lists.newArrayList("白宫", "伦敦", "华盛顿"));

        AskMixup mixup3 = new AskMixup("改革开放的窗口", Lists.newArrayList("北京", "上海", "西安", "深圳"));

        //System.out.println(electsText(mixup1, new ElectsQuery.So360QueryParser()));
        //System.out.println(electsText(mixup2, new ElectsQuery.So360QueryParser()));
        System.out.println(electsText(mixup3, new ElectsQuery.So360QueryParser()));

    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy