Example usage for org.apache.commons.lang3.tuple Triple getRight

List of usage examples for org.apache.commons.lang3.tuple Triple getRight

Introduction

In this page you can find the example usage for org.apache.commons.lang3.tuple Triple getRight.

Prototype

public abstract R getRight();

Source Link

Document

Gets the right element from this triple.

Usage

From source file:alluxio.master.file.PermissionCheckTest.java

private LockedInodePath getLockedInodePath(ArrayList<Triple<String, String, Mode>> permissions)
        throws Exception {
    List<Inode<?>> inodes = new ArrayList<>();
    inodes.add(getRootInode());//w w  w.  j a  v a  2  s.co m
    if (permissions.size() == 0) {
        return new MutableLockedInodePath(new AlluxioURI("/"), inodes, null);
    }
    String uri = "";
    for (int i = 0; i < permissions.size(); i++) {
        Triple<String, String, Mode> permission = permissions.get(i);
        String owner = permission.getLeft();
        String group = permission.getMiddle();
        Mode mode = permission.getRight();
        uri += "/" + (i + 1);
        if (i == permissions.size() - 1) {
            Inode<?> inode = InodeFile.create(i + 1, i, (i + 1) + "", CommonUtils.getCurrentMs(),
                    CreateFileOptions.defaults().setBlockSizeBytes(Constants.KB).setOwner(owner).setGroup(group)
                            .setMode(mode));
            inodes.add(inode);
        } else {
            Inode<?> inode = InodeDirectory.create(i + 1, i, (i + 1) + "",
                    CreateDirectoryOptions.defaults().setOwner(owner).setGroup(group).setMode(mode));
            inodes.add(inode);
        }
    }
    return new MutableLockedInodePath(new AlluxioURI(uri), inodes, null);
}

From source file:com.microsoft.azure.management.datalake.store.uploader.StringExtensionsTests.java

@Test
public void StringExtensions_FindNewLine_UTF8() {
    for (Triple<String, Integer, Integer> t : TestDataUTF8) {
        byte[] exactBuffer = t.getLeft().getBytes(StandardCharsets.UTF_8);
        byte[] largerBuffer = new byte[exactBuffer.length + 100];
        System.arraycopy(exactBuffer, 0, largerBuffer, 0, exactBuffer.length);

        int forwardInExactBuffer = StringExtensions.findNewline(exactBuffer, 0, exactBuffer.length, false,
                StandardCharsets.UTF_8, null);
        Assert.assertEquals(t.getMiddle().intValue(), forwardInExactBuffer);

        int forwardInLargeBuffer = StringExtensions.findNewline(largerBuffer, 0, exactBuffer.length, false,
                StandardCharsets.UTF_8, null);
        Assert.assertEquals(t.getMiddle().intValue(), forwardInLargeBuffer);

        int reverseInExactBuffer = StringExtensions.findNewline(exactBuffer,
                Math.max(0, exactBuffer.length - 1), exactBuffer.length, true, StandardCharsets.UTF_8, null);
        Assert.assertEquals(t.getRight().intValue(), reverseInExactBuffer);

        int reverseInLargeBuffer = StringExtensions.findNewline(largerBuffer,
                Math.max(0, exactBuffer.length - 1), exactBuffer.length, true, StandardCharsets.UTF_8, null);
        Assert.assertEquals(t.getRight().intValue(), reverseInLargeBuffer);
    }/*  ww w  .  j a v  a2  s  .  c  o m*/

    for (Triple<String, Integer, Integer> t : TestDataUTF8CustomDelim) {
        byte[] exactBuffer = t.getLeft().getBytes(StandardCharsets.UTF_8);
        byte[] largerBuffer = new byte[exactBuffer.length + 100];
        System.arraycopy(exactBuffer, 0, largerBuffer, 0, exactBuffer.length);

        int forwardInExactBuffer = StringExtensions.findNewline(exactBuffer, 0, exactBuffer.length, false,
                StandardCharsets.UTF_8, customDelim);
        Assert.assertEquals(t.getMiddle().intValue(), forwardInExactBuffer);

        int forwardInLargeBuffer = StringExtensions.findNewline(largerBuffer, 0, exactBuffer.length, false,
                StandardCharsets.UTF_8, customDelim);
        Assert.assertEquals(t.getMiddle().intValue(), forwardInLargeBuffer);

        int reverseInExactBuffer = StringExtensions.findNewline(exactBuffer,
                Math.max(0, exactBuffer.length - 1), exactBuffer.length, true, StandardCharsets.UTF_8,
                customDelim);
        Assert.assertEquals(t.getRight().intValue(), reverseInExactBuffer);

        int reverseInLargeBuffer = StringExtensions.findNewline(largerBuffer,
                Math.max(0, exactBuffer.length - 1), exactBuffer.length, true, StandardCharsets.UTF_8,
                customDelim);
        Assert.assertEquals(t.getRight().intValue(), reverseInLargeBuffer);
    }
}

From source file:blusunrize.immersiveengineering.common.items.ItemRevolver.java

@Override
public ActionResult<ItemStack> onItemRightClick(World world, EntityPlayer player, @Nonnull EnumHand hand) {
    ItemStack revolver = player.getHeldItem(hand);
    if (!world.isRemote) {
        if (player.isSneaking()) {
            CommonProxy.openGuiForItem(player,
                    hand == EnumHand.MAIN_HAND ? EntityEquipmentSlot.MAINHAND : EntityEquipmentSlot.OFFHAND);
            return new ActionResult(EnumActionResult.SUCCESS, revolver);
        } else if (player.getCooledAttackStrength(1) >= 1) {
            if (this.getUpgrades(revolver).getBoolean("nerf"))
                world.playSound(null, player.posX, player.posY, player.posZ, SoundEvents.ENTITY_ITEM_PICKUP,
                        SoundCategory.PLAYERS, 1f, 0.6f);
            else {
                if (getShootCooldown(revolver) > 0 || ItemNBTHelper.hasKey(revolver, "reload"))
                    return new ActionResult(EnumActionResult.PASS, revolver);

                NonNullList<ItemStack> bullets = getBullets(revolver);

                if (isEmpty(revolver, false))
                    for (int i = 0; i < player.inventory.getSizeInventory(); i++) {
                        ItemStack stack = player.inventory.getStackInSlot(i);
                        if (stack.getItem() instanceof ItemSpeedloader
                                && !((ItemSpeedloader) stack.getItem()).isEmpty(stack)) {
                            for (ItemStack b : bullets)
                                if (!b.isEmpty())
                                    world.spawnEntity(
                                            new EntityItem(world, player.posX, player.posY, player.posZ, b));
                            setBullets(revolver, ((ItemSpeedloader) stack.getItem()).getContainedItems(stack));
                            ((ItemSpeedloader) stack.getItem()).setContainedItems(stack,
                                    NonNullList.withSize(8, ItemStack.EMPTY));
                            player.inventory.markDirty();
                            if (player instanceof EntityPlayerMP)
                                ImmersiveEngineering.packetHandler.sendTo(new MessageSpeedloaderSync(i, hand),
                                        (EntityPlayerMP) player);

                            ItemNBTHelper.setInt(revolver, "reload", 60);
                            return new ActionResult(EnumActionResult.SUCCESS, revolver);
                        }//  w  w w .  j  a  v a  2s  .c om
                    }

                if (!ItemNBTHelper.hasKey(revolver, "reload")) {
                    if (!bullets.get(0).isEmpty() && bullets.get(0).getItem() instanceof ItemBullet
                            && ItemNBTHelper.hasKey(bullets.get(0), "bullet")) {
                        String key = ItemNBTHelper.getString(bullets.get(0), "bullet");
                        IBullet bullet = BulletHandler.getBullet(key);
                        if (bullet != null) {
                            Vec3d vec = player.getLookVec();
                            boolean electro = getUpgrades(revolver).getBoolean("electro");
                            int count = bullet.getProjectileCount(player);
                            if (count == 1) {
                                Entity entBullet = getBullet(player, vec, vec, key, bullets.get(0), electro);
                                player.world.spawnEntity(
                                        bullet.getProjectile(player, bullets.get(0), entBullet, electro));
                            } else
                                for (int i = 0; i < count; i++) {
                                    Vec3d vecDir = vec.add(player.getRNG().nextGaussian() * .1,
                                            player.getRNG().nextGaussian() * .1,
                                            player.getRNG().nextGaussian() * .1);
                                    Entity entBullet = getBullet(player, vec, vecDir, key, bullets.get(0),
                                            electro);
                                    player.world.spawnEntity(
                                            bullet.getProjectile(player, bullets.get(0), entBullet, electro));
                                }
                            bullets.set(0, bullet.getCasing(bullets.get(0)).copy());

                            float noise = 1;
                            Utils.attractEnemies(player, 64 * noise);
                            SoundEvent sound = bullet.getSound();
                            if (sound == null)
                                sound = IESounds.revolverFire;
                            world.playSound(null, player.posX, player.posY, player.posZ, sound,
                                    SoundCategory.PLAYERS, noise, 1f);
                        } else
                            world.playSound(null, player.posX, player.posY, player.posZ,
                                    SoundEvents.BLOCK_NOTE_HAT, SoundCategory.PLAYERS, 1f, 1f);
                    } else
                        world.playSound(null, player.posX, player.posY, player.posZ, SoundEvents.BLOCK_NOTE_HAT,
                                SoundCategory.PLAYERS, 1f, 1f);

                    NonNullList<ItemStack> cycled = NonNullList.withSize(getBulletCount(revolver),
                            ItemStack.EMPTY);
                    for (int i = 1; i < cycled.size(); i++)
                        cycled.set(i - 1, bullets.get(i));
                    cycled.set(cycled.size() - 1, bullets.get(0));
                    setBullets(revolver, cycled);
                    player.inventory.markDirty();
                    ItemNBTHelper.setInt(revolver, "cooldown", getMaxShootCooldown(revolver));
                    return new ActionResult(EnumActionResult.SUCCESS, revolver);
                }
            }
        }
    } else if (!player.isSneaking() && revolver.getItemDamage() == 0) {
        if (getShootCooldown(revolver) > 0 || ItemNBTHelper.hasKey(revolver, "reload"))
            return new ActionResult(EnumActionResult.PASS, revolver);
        NonNullList<ItemStack> bullets = getBullets(revolver);
        if (!bullets.get(0).isEmpty() && bullets.get(0).getItem() instanceof ItemBullet
                && ItemNBTHelper.hasKey(bullets.get(0), "bullet")) {
            Triple<ItemStack, ShaderRegistryEntry, ShaderCase> shader = ShaderRegistry
                    .getStoredShaderAndCase(revolver);
            if (shader != null) {
                Vec3d pos = Utils.getLivingFrontPos(player, .75, player.height * .75,
                        hand == EnumHand.MAIN_HAND ? player.getPrimaryHand()
                                : player.getPrimaryHand().opposite(),
                        false, 1);
                shader.getMiddle().getEffectFunction().execute(world, shader.getLeft(), revolver,
                        shader.getRight().getShaderType(), pos, player.getForward(), .125f);
            }
        }
        return new ActionResult(EnumActionResult.SUCCESS, revolver);
    }
    return new ActionResult(EnumActionResult.SUCCESS, revolver);
}

From source file:com.microsoft.azure.management.datalake.store.uploader.StringExtensionsTests.java

@Test
public void StringExtensions_FindNewLine_ASCII() {
    for (Triple<String, Integer, Integer> t : TestDataUTF8) {
        byte[] exactBuffer = t.getLeft().getBytes(StandardCharsets.US_ASCII);
        byte[] largerBuffer = new byte[exactBuffer.length + 100];
        System.arraycopy(exactBuffer, 0, largerBuffer, 0, exactBuffer.length);

        int forwardInExactBuffer = StringExtensions.findNewline(exactBuffer, 0, exactBuffer.length, false,
                StandardCharsets.US_ASCII, null);
        Assert.assertEquals(t.getMiddle().intValue(), forwardInExactBuffer);

        int forwardInLargeBuffer = StringExtensions.findNewline(largerBuffer, 0, exactBuffer.length, false,
                StandardCharsets.US_ASCII, null);
        Assert.assertEquals(t.getMiddle().intValue(), forwardInLargeBuffer);

        int reverseInExactBuffer = StringExtensions.findNewline(exactBuffer,
                Math.max(0, exactBuffer.length - 1), exactBuffer.length, true, StandardCharsets.US_ASCII, null);
        Assert.assertEquals(t.getRight().intValue(), reverseInExactBuffer);

        int reverseInLargeBuffer = StringExtensions.findNewline(largerBuffer,
                Math.max(0, exactBuffer.length - 1), exactBuffer.length, true, StandardCharsets.US_ASCII, null);
        Assert.assertEquals(t.getRight().intValue(), reverseInLargeBuffer);
    }/*from w  ww .  ja v  a  2  s  .  co  m*/

    for (Triple<String, Integer, Integer> t : TestDataUTF8CustomDelim) {
        byte[] exactBuffer = t.getLeft().getBytes(StandardCharsets.US_ASCII);
        byte[] largerBuffer = new byte[exactBuffer.length + 100];
        System.arraycopy(exactBuffer, 0, largerBuffer, 0, exactBuffer.length);

        int forwardInExactBuffer = StringExtensions.findNewline(exactBuffer, 0, exactBuffer.length, false,
                StandardCharsets.US_ASCII, customDelim);
        Assert.assertEquals(t.getMiddle().intValue(), forwardInExactBuffer);

        int forwardInLargeBuffer = StringExtensions.findNewline(largerBuffer, 0, exactBuffer.length, false,
                StandardCharsets.US_ASCII, customDelim);
        Assert.assertEquals(t.getMiddle().intValue(), forwardInLargeBuffer);

        int reverseInExactBuffer = StringExtensions.findNewline(exactBuffer,
                Math.max(0, exactBuffer.length - 1), exactBuffer.length, true, StandardCharsets.US_ASCII,
                customDelim);
        Assert.assertEquals(t.getRight().intValue(), reverseInExactBuffer);

        int reverseInLargeBuffer = StringExtensions.findNewline(largerBuffer,
                Math.max(0, exactBuffer.length - 1), exactBuffer.length, true, StandardCharsets.US_ASCII,
                customDelim);
        Assert.assertEquals(t.getRight().intValue(), reverseInLargeBuffer);
    }
}

From source file:com.microsoft.azure.management.datalake.store.uploader.StringExtensionsTests.java

@Test
public void StringExtensions_FindNewLine_UTF16() {
    for (Triple<String, Integer, Integer> t : TestDataUTF16) {
        byte[] exactBuffer = t.getLeft().getBytes(StandardCharsets.UTF_16LE);
        byte[] largerBuffer = new byte[exactBuffer.length + 100];
        System.arraycopy(exactBuffer, 0, largerBuffer, 0, exactBuffer.length);

        int forwardInExactBuffer = StringExtensions.findNewline(exactBuffer, 0, exactBuffer.length, false,
                StandardCharsets.UTF_16LE, null);
        Assert.assertEquals(t.getMiddle().intValue(), forwardInExactBuffer);

        int forwardInLargeBuffer = StringExtensions.findNewline(largerBuffer, 0, exactBuffer.length, false,
                StandardCharsets.UTF_16LE, null);
        Assert.assertEquals(t.getMiddle().intValue(), forwardInLargeBuffer);

        int reverseInExactBuffer = StringExtensions.findNewline(exactBuffer,
                Math.max(0, exactBuffer.length - 1), exactBuffer.length, true, StandardCharsets.UTF_16LE, null);
        Assert.assertEquals(t.getRight().intValue(), reverseInExactBuffer);

        int reverseInLargeBuffer = StringExtensions.findNewline(largerBuffer,
                Math.max(0, exactBuffer.length - 1), exactBuffer.length, true, StandardCharsets.UTF_16LE, null);
        Assert.assertEquals(t.getRight().intValue(), reverseInLargeBuffer);
    }//from w w  w  . j av a  2s.c  om

    for (Triple<String, Integer, Integer> t : TestDataUTF16CustomDelim) {
        byte[] exactBuffer = t.getLeft().getBytes(StandardCharsets.UTF_16LE);
        byte[] largerBuffer = new byte[exactBuffer.length + 100];
        System.arraycopy(exactBuffer, 0, largerBuffer, 0, exactBuffer.length);

        int forwardInExactBuffer = StringExtensions.findNewline(exactBuffer, 0, exactBuffer.length, false,
                StandardCharsets.UTF_16LE, customDelim);
        Assert.assertEquals(t.getMiddle().intValue(), forwardInExactBuffer);

        int forwardInLargeBuffer = StringExtensions.findNewline(largerBuffer, 0, exactBuffer.length, false,
                StandardCharsets.UTF_16LE, customDelim);
        Assert.assertEquals(t.getMiddle().intValue(), forwardInLargeBuffer);

        int reverseInExactBuffer = StringExtensions.findNewline(exactBuffer,
                Math.max(0, exactBuffer.length - 1), exactBuffer.length, true, StandardCharsets.UTF_16LE,
                customDelim);
        Assert.assertEquals(t.getRight().intValue(), reverseInExactBuffer);

        int reverseInLargeBuffer = StringExtensions.findNewline(largerBuffer,
                Math.max(0, exactBuffer.length - 1), exactBuffer.length, true, StandardCharsets.UTF_16LE,
                customDelim);
        Assert.assertEquals(t.getRight().intValue(), reverseInLargeBuffer);
    }
}

From source file:com.microsoft.azure.management.datalake.store.uploader.StringExtensionsTests.java

@Test
public void StringExtensions_FindNewLine_UTF16BigEndian() {
    for (Triple<String, Integer, Integer> t : TestDataUTF16) {
        byte[] exactBuffer = t.getLeft().getBytes(StandardCharsets.UTF_16BE);
        byte[] largerBuffer = new byte[exactBuffer.length + 100];
        System.arraycopy(exactBuffer, 0, largerBuffer, 0, exactBuffer.length);

        int forwardInExactBuffer = StringExtensions.findNewline(exactBuffer, 0, exactBuffer.length, false,
                StandardCharsets.UTF_16BE, null);
        Assert.assertEquals(t.getMiddle().intValue(), forwardInExactBuffer);

        int forwardInLargeBuffer = StringExtensions.findNewline(largerBuffer, 0, exactBuffer.length, false,
                StandardCharsets.UTF_16BE, null);
        Assert.assertEquals(t.getMiddle().intValue(), forwardInLargeBuffer);

        int reverseInExactBuffer = StringExtensions.findNewline(exactBuffer,
                Math.max(0, exactBuffer.length - 1), exactBuffer.length, true, StandardCharsets.UTF_16BE, null);
        Assert.assertEquals(t.getRight().intValue(), reverseInExactBuffer);

        int reverseInLargeBuffer = StringExtensions.findNewline(largerBuffer,
                Math.max(0, exactBuffer.length - 1), exactBuffer.length, true, StandardCharsets.UTF_16BE, null);
        Assert.assertEquals(t.getRight().intValue(), reverseInLargeBuffer);
    }// w  w w.  j a v  a2s  .c  o m

    for (Triple<String, Integer, Integer> t : TestDataUTF16CustomDelim) {
        byte[] exactBuffer = t.getLeft().getBytes(StandardCharsets.UTF_16BE);
        byte[] largerBuffer = new byte[exactBuffer.length + 100];
        System.arraycopy(exactBuffer, 0, largerBuffer, 0, exactBuffer.length);

        int forwardInExactBuffer = StringExtensions.findNewline(exactBuffer, 0, exactBuffer.length, false,
                StandardCharsets.UTF_16BE, customDelim);
        Assert.assertEquals(t.getMiddle().intValue(), forwardInExactBuffer);

        int forwardInLargeBuffer = StringExtensions.findNewline(largerBuffer, 0, exactBuffer.length, false,
                StandardCharsets.UTF_16BE, customDelim);
        Assert.assertEquals(t.getMiddle().intValue(), forwardInLargeBuffer);

        int reverseInExactBuffer = StringExtensions.findNewline(exactBuffer,
                Math.max(0, exactBuffer.length - 1), exactBuffer.length, true, StandardCharsets.UTF_16BE,
                customDelim);
        Assert.assertEquals(t.getRight().intValue(), reverseInExactBuffer);

        int reverseInLargeBuffer = StringExtensions.findNewline(largerBuffer,
                Math.max(0, exactBuffer.length - 1), exactBuffer.length, true, StandardCharsets.UTF_16BE,
                customDelim);
        Assert.assertEquals(t.getRight().intValue(), reverseInLargeBuffer);
    }
}

From source file:com.clust4j.algo.NNHSTests.java

License:asdf

@Test
public void testKD1() {
    final Array2DRowRealMatrix mat = new Array2DRowRealMatrix(a, false);
    KDTree kd = new KDTree(mat);

    QuadTup<double[][], int[], NodeData[], double[][][]> arrays = kd.getArrays();

    assertTrue(MatUtils.equalsExactly(arrays.getFirst(), a));
    assertTrue(VecUtils.equalsExactly(new int[] { 0, 1, 2 }, arrays.getSecond()));

    Triple<Integer, Integer, Integer> stats = kd.getTreeStats();
    assertTrue(stats.getLeft() == 0);/*from  www  .j a  v  a  2 s. c  om*/
    assertTrue(stats.getMiddle() == 0);
    assertTrue(stats.getRight() == 0);

    NodeData data = arrays.getThird()[0];
    assertTrue(data.idx_start == 0);
    assertTrue(data.idx_end == 3);
    assertTrue(data.is_leaf);
    assertTrue(data.radius == 1);
}

From source file:com.clust4j.algo.NNHSTests.java

License:asdf

@Test
public void testBall1() {
    final Array2DRowRealMatrix mat = new Array2DRowRealMatrix(a, false);
    BallTree ball = new BallTree(mat);

    QuadTup<double[][], int[], NodeData[], double[][][]> arrays = ball.getArrays();

    assertTrue(MatUtils.equalsExactly(arrays.getFirst(), a));
    assertTrue(VecUtils.equalsExactly(new int[] { 0, 1, 2 }, arrays.getSecond()));

    Triple<Integer, Integer, Integer> stats = ball.getTreeStats();
    assertTrue(stats.getLeft() == 0);/*ww  w .  j  a  va2  s  .com*/
    assertTrue(stats.getMiddle() == 0);
    assertTrue(stats.getRight() == 0);

    NodeData data = arrays.getThird()[0];
    assertTrue(data.idx_start == 0);
    assertTrue(data.idx_end == 3);
    assertTrue(data.is_leaf);
    assertTrue(data.radius == 6.716480559869961);

    double[][][] trip = arrays.getFourth();
    assertTrue(trip.length == 1);
    assertTrue(trip[0][0][0] == 1.6666666666666667);
    assertTrue(trip[0][0][1] == 2.3333333333333333);
    assertTrue(trip[0][0][2] == 2.6666666666666667);
    assertTrue(trip[0][0][3] == 2.6666666666666667);
}

From source file:it.acubelab.smaph.SmaphAnnotator.java

/**
 * Given a query and its gold standard, generate
 * //from w w  w. j  a  v a2 s  . co  m
 * @param query
 *            a query.
 * @param goldStandard
 *            the entities associated to the query.
 * @param posEFVectors
 *            where to store the positive-example (true positives) feature
 *            vectors.
 * @param negEFVectors
 *            where to store the negative-example (false positives) feature
 *            vectors.
 * @param discardNE
 *            whether to limit the output to named entities, as defined by
 *            ERDDatasetFilter.EntityIsNE.
 * @param wikiToFreeb
 *            a wikipedia to freebase-id mapping.
 * @throws Exception
 *             if something went wrong while annotating the query.
 */
public void generateExamples(String query, HashSet<Tag> goldStandard, Vector<double[]> posEFVectors,
        Vector<double[]> negEFVectors, boolean discardNE, WikipediaToFreebase wikiToFreeb) throws Exception {

    /** Search the query on bing */
    List<Pair<String, Integer>> bingBoldsAndRankNS = null;
    List<String> urls = null;
    List<String> relatedSearchRes = null;
    Triple<Integer, Double, JSONObject> resCountAndWebTotal = null;
    int resultsCount = -1;
    double webTotalNS = Double.NaN;
    List<String> filteredBolds = null;
    HashMap<Integer, Integer> rankToIdNS = null;
    if (includeSourceAnnotator || includeSourceWikiSearch || includeSourceRelatedSearch
            || includeSourceNormalSearch) {
        bingBoldsAndRankNS = new Vector<>();
        urls = new Vector<>();
        relatedSearchRes = new Vector<>();
        resCountAndWebTotal = takeBingData(query, bingBoldsAndRankNS, urls, relatedSearchRes, null,
                Integer.MAX_VALUE, false);
        resultsCount = resCountAndWebTotal.getLeft();
        webTotalNS = resCountAndWebTotal.getMiddle();
        filteredBolds = boldFilter.filterBolds(query, bingBoldsAndRankNS, resultsCount);
        rankToIdNS = urlsToRankID(urls);

        if (debugger != null) {
            debugger.addBoldPositionEditDistance(query, bingBoldsAndRankNS);
            debugger.addBoldFilterOutput(query, filteredBolds);
            debugger.addSource2SearchResult(query, rankToIdNS, urls);
            debugger.addBingResponseNormalSearch(query, resCountAndWebTotal.getRight());

        }
    }

    /** Do the wikipedia-search on bing. */
    List<String> wikiSearchUrls = new Vector<>();
    List<Pair<String, Integer>> bingBoldsAndRankWS = new Vector<>();
    HashMap<String, Pair<Integer, Integer>> annTitlesToIdAndRankWS = null;
    Triple<Integer, Double, JSONObject> resCountAndWebTotalWS = null;
    double webTotalWS = Double.NaN;
    if (includeSourceWikiSearch | includeSourceNormalSearch) {
        resCountAndWebTotalWS = takeBingData(query, bingBoldsAndRankWS, wikiSearchUrls, null, null,
                topKWikiSearch, true);
        webTotalWS = resCountAndWebTotalWS.getMiddle();
        HashMap<Integer, Integer> rankToIdWikiSearch = urlsToRankID(wikiSearchUrls);
        if (debugger != null) {
            debugger.addSource3SearchResult(query, rankToIdWikiSearch, wikiSearchUrls);
            debugger.addBingResponseWikiSearch(query, resCountAndWebTotal.getRight());

        }
        annTitlesToIdAndRankWS = adjustTitles(rankToIdWikiSearch);
    }

    /** Do the RelatedSearch on bing */
    String relatedSearch = null;
    List<String> relatedSearchUrls = null;
    List<Pair<String, Integer>> bingBoldsAndRankRS = null;
    HashMap<Integer, Integer> rankToIdRelatedSearch = null;
    HashMap<String, Pair<Integer, Integer>> annTitlesToIdAndRankRS = null;
    double webTotalRelatedSearch = Double.NaN;
    if (includeSourceRelatedSearch) {
        relatedSearch = getRelatedSearch(relatedSearchRes, query);
        relatedSearchUrls = new Vector<>();
        bingBoldsAndRankRS = new Vector<>();
        Triple<Integer, Double, JSONObject> resCountAndWebTotalRS = takeBingData(query, bingBoldsAndRankRS,
                relatedSearchUrls, null, null, topKRelatedSearch, false);
        webTotalRelatedSearch = resCountAndWebTotalRS.getMiddle();
        rankToIdRelatedSearch = urlsToRankID(relatedSearchUrls);
        annTitlesToIdAndRankRS = adjustTitles(rankToIdRelatedSearch);
    }

    /** Annotate bolds on the annotator */
    Pair<HashMap<String, HashMap<String, Double>>, HashMap<String, Annotation>> infoAndAnnotations = null;
    HashMap<String, Annotation> spotToAnnotation = null;
    HashMap<String, HashMap<String, Double>> additionalInfo = null;
    Pair<String, HashSet<Mention>> annInput = null;
    if (includeSourceAnnotator) {
        annInput = concatenateBolds(filteredBolds);
        infoAndAnnotations = disambiguateBolds(annInput.first, annInput.second);
        spotToAnnotation = infoAndAnnotations.second;
        additionalInfo = infoAndAnnotations.first;

        if (debugger != null)
            debugger.addReturnedAnnotation(query, spotToAnnotation);
    }

    List<Pair<Tag, HashMap<String, Double>>> widToEFFtrVect = new Vector<>();
    // Filter and add annotations found by the disambiguator
    if (includeSourceAnnotator) {
        for (String bold : filteredBolds) {
            if (spotToAnnotation.containsKey(bold)) {
                Annotation ann = spotToAnnotation.get(bold);
                HashMap<String, Double> ESFeatures = generateEntitySelectionFeaturesAnnotator(query,
                        resultsCount, ann, annInput, bingBoldsAndRankNS, additionalInfo);
                Tag tag = new Tag(ann.getConcept());
                widToEFFtrVect.add(new Pair<Tag, HashMap<String, Double>>(tag, ESFeatures));
            }
        }
    }

    // Filter and add entities found in the normal search
    if (includeSourceNormalSearch) {
        for (int rank : rankToIdNS.keySet()) {
            int wid = rankToIdNS.get(rank);
            HashMap<String, Double> ESFeatures = generateEntitySelectionFeaturesSearch(query, wid, rank,
                    webTotalNS, webTotalWS, bingBoldsAndRankNS, 2);
            Tag tag = new Tag(wid);
            widToEFFtrVect.add(new Pair<Tag, HashMap<String, Double>>(tag, ESFeatures));
        }
    }

    // Filter and add entities found in the WikipediaSearch
    if (includeSourceWikiSearch) {
        for (String annotatedTitleWS : annTitlesToIdAndRankWS.keySet()) {
            int wid = annTitlesToIdAndRankWS.get(annotatedTitleWS).first;
            int rank = annTitlesToIdAndRankWS.get(annotatedTitleWS).second;
            HashMap<String, Double> ESFeatures = generateEntitySelectionFeaturesSearch(query, wid, rank,
                    webTotalNS, webTotalWS, bingBoldsAndRankWS, 3);

            Tag tag = new Tag(wid);
            widToEFFtrVect.add(new Pair<Tag, HashMap<String, Double>>(tag, ESFeatures));
        }
    }

    // Filter and add entities found in the RelatedSearch
    if (includeSourceRelatedSearch) {
        for (String annotatedTitleRS : annTitlesToIdAndRankRS.keySet()) {
            int wid = annTitlesToIdAndRankRS.get(annotatedTitleRS).first;
            int rank = annTitlesToIdAndRankRS.get(annotatedTitleRS).second;
            HashMap<String, Double> ESFeatures = generateEntitySelectionFeaturesSearch(relatedSearch, wid, rank,
                    webTotalNS, webTotalRelatedSearch, bingBoldsAndRankRS, 5);

            Tag tag = new Tag(wid);
            widToEFFtrVect.add(new Pair<Tag, HashMap<String, Double>>(tag, ESFeatures));
        }
    }

    for (Pair<Tag, HashMap<String, Double>> tagAndFtrs : widToEFFtrVect) {
        Tag tag = tagAndFtrs.first;
        HashMap<String, Double> ftrs = tagAndFtrs.second;
        if (discardNE && !ERDDatasetFilter.EntityIsNE(wikiApi, wikiToFreeb, tag.getConcept()))
            continue;

        if (goldStandard.contains(tag))
            posEFVectors.add(LibSvmEntityFilter.featuresToFtrVectStatic(ftrs));
        else
            negEFVectors.add(LibSvmEntityFilter.featuresToFtrVectStatic(ftrs));
        System.out.printf("%d in query [%s] is a %s example.%n", tag.getConcept(), query,
                goldStandard.contains(tag) ? "positive" : "negative");
    }
}

From source file:it.acubelab.smaph.SmaphAnnotator.java

@Override
public HashSet<ScoredAnnotation> solveSa2W(String query) throws AnnotationException {
    if (debugger != null)
        debugger.addProcessedQuery(query);

    HashSet<ScoredAnnotation> annotations = new HashSet<>();
    try {/*from  w  ww .  j  a v a  2  s  .  co m*/

        /** Search the query on bing */
        List<Pair<String, Integer>> bingBoldsAndRankNS = null;
        List<String> urls = null;
        List<String> relatedSearchRes = null;
        Triple<Integer, Double, JSONObject> resCountAndWebTotalNS = null;
        int resultsCount = -1;
        double webTotalNS = Double.NaN;
        List<String> filteredBolds = null;
        HashMap<Integer, Integer> rankToIdNS = null;
        HashMap<Integer, HashSet<String>> rankToBoldsNS = null;
        List<Pair<String, Vector<Pair<Integer, Integer>>>> snippetsToBolds = null;
        if (includeSourceAnnotator || includeSourceWikiSearch || includeSourceRelatedSearch
                || includeSourceNormalSearch) {
            bingBoldsAndRankNS = new Vector<>();
            urls = new Vector<>();
            relatedSearchRes = new Vector<>();
            snippetsToBolds = new Vector<>();
            resCountAndWebTotalNS = takeBingData(query, bingBoldsAndRankNS, urls, relatedSearchRes,
                    snippetsToBolds, Integer.MAX_VALUE, false);
            resultsCount = resCountAndWebTotalNS.getLeft();
            webTotalNS = resCountAndWebTotalNS.getMiddle();
            filteredBolds = boldFilter.filterBolds(query, bingBoldsAndRankNS, resultsCount);
            rankToIdNS = urlsToRankID(urls);
            rankToBoldsNS = new HashMap<>();
            SmaphUtils.mapRankToBoldsLC(bingBoldsAndRankNS, rankToBoldsNS, null);

            if (debugger != null) {
                debugger.addBoldPositionEditDistance(query, bingBoldsAndRankNS);
                debugger.addSnippets(query, snippetsToBolds);
                debugger.addBoldFilterOutput(query, filteredBolds);
                debugger.addSource2SearchResult(query, rankToIdNS, urls);
                debugger.addBingResponseNormalSearch(query, resCountAndWebTotalNS.getRight());
            }
        }

        /** Do the WikipediaSearch on bing. */
        List<String> wikiSearchUrls = new Vector<>();
        List<Pair<String, Integer>> bingBoldsAndRankWS = new Vector<>();
        HashMap<String, Pair<Integer, Integer>> annTitlesToIdAndRankWS = null;
        Triple<Integer, Double, JSONObject> resCountAndWebTotalWS = null;
        HashMap<Integer, HashSet<String>> rankToBoldsWS = null;
        double webTotalWS = Double.NaN;
        if (includeSourceWikiSearch | includeSourceNormalSearch) {
            resCountAndWebTotalWS = takeBingData(query, bingBoldsAndRankWS, wikiSearchUrls, null, null,
                    topKWikiSearch, true);
            webTotalWS = resCountAndWebTotalWS.getMiddle();
            HashMap<Integer, Integer> rankToIdWikiSearch = urlsToRankID(wikiSearchUrls);
            rankToBoldsWS = new HashMap<>();
            SmaphUtils.mapRankToBoldsLC(bingBoldsAndRankWS, rankToBoldsWS, null);
            if (debugger != null) {
                debugger.addSource3SearchResult(query, rankToIdWikiSearch, wikiSearchUrls);
                debugger.addBingResponseWikiSearch(query, resCountAndWebTotalWS.getRight());

            }
            annTitlesToIdAndRankWS = adjustTitles(rankToIdWikiSearch);
        }

        /** Do the RelatedSearch on bing */
        String relatedSearch = null;
        List<String> relatedSearchUrls = null;
        List<Pair<String, Integer>> bingBoldsAndRankRS = null;
        HashMap<Integer, Integer> rankToIdRelatedSearch = null;
        HashMap<String, Pair<Integer, Integer>> annTitlesToIdAndRankRS = null;
        double webTotalRelatedSearch = Double.NaN;
        HashMap<Integer, HashSet<String>> rankToBoldsRS = null;
        if (includeSourceRelatedSearch) {
            relatedSearch = getRelatedSearch(relatedSearchRes, query);
            relatedSearchUrls = new Vector<>();
            bingBoldsAndRankRS = new Vector<>();
            Triple<Integer, Double, JSONObject> resCountAndWebTotalRS = takeBingData(query, bingBoldsAndRankRS,
                    relatedSearchUrls, null, null, topKRelatedSearch, false);
            webTotalRelatedSearch = resCountAndWebTotalRS.getMiddle();
            rankToIdRelatedSearch = urlsToRankID(relatedSearchUrls);
            annTitlesToIdAndRankRS = adjustTitles(rankToIdRelatedSearch);
            rankToBoldsRS = new HashMap<>();
            SmaphUtils.mapRankToBoldsLC(bingBoldsAndRankRS, rankToBoldsRS, null);

        }

        /** Annotate bolds on the annotator */
        Pair<HashMap<String, HashMap<String, Double>>, HashMap<String, Annotation>> infoAndAnnotations = null;
        HashMap<String, Annotation> spotToAnnotation = null;
        HashMap<String, HashMap<String, Double>> additionalInfo = null;
        Pair<String, HashSet<Mention>> annInput = null;
        if (includeSourceAnnotator) {
            annInput = concatenateBolds(filteredBolds);
            infoAndAnnotations = disambiguateBolds(annInput.first, annInput.second);
            spotToAnnotation = infoAndAnnotations.second;
            additionalInfo = infoAndAnnotations.first;

            if (debugger != null)
                debugger.addReturnedAnnotation(query, spotToAnnotation);
        }

        HashMap<String[], Tag> boldsToAcceptedEntity = new HashMap<>();

        // Filter and add annotations found by the disambiguator
        if (includeSourceAnnotator) {
            for (String bold : filteredBolds) {
                if (spotToAnnotation.containsKey(bold)) {
                    Annotation ann = spotToAnnotation.get(bold);
                    HashMap<String, Double> ESFeatures = generateEntitySelectionFeaturesAnnotator(query,
                            resultsCount, ann, annInput, bingBoldsAndRankNS, additionalInfo);
                    boolean accept = entityFilter.filterEntity(ESFeatures);
                    if (accept)
                        boldsToAcceptedEntity.put(new String[] { bold }, new Tag(ann.getConcept()));
                    if (debugger != null) {
                        HashSet<String> bolds = new HashSet<>();
                        bolds.add(bold);
                        debugger.addQueryCandidateBolds(query, "Source 1", ann.getConcept(), bolds);
                        debugger.addEntityFeaturesS1(query, bold, ann.getConcept(), ESFeatures, accept);
                        if (accept)
                            debugger.addResult(query, ann.getConcept());
                    }
                }
            }
        }

        // Filter and add entities found in the normal search
        if (includeSourceNormalSearch) {
            for (int rank : rankToIdNS.keySet()) {
                int wid = rankToIdNS.get(rank);
                HashMap<String, Double> ESFeatures = generateEntitySelectionFeaturesSearch(query, wid, rank,
                        webTotalNS, webTotalWS, bingBoldsAndRankNS, 2);
                HashSet<String> bolds = rankToBoldsNS.get(rank);
                boolean accept = entityFilter.filterEntity(ESFeatures);
                if (accept)
                    boldsToAcceptedEntity.put(bolds.toArray(new String[] {}), new Tag(wid));
                if (debugger != null) {
                    debugger.addQueryCandidateBolds(query, "Source 2", wid, bolds);
                    debugger.addEntityFeaturesS2(query, wid, ESFeatures, accept);
                    if (accept)
                        debugger.addResult(query, wid);
                }
            }
        }

        // Filter and add entities found in the WikipediaSearch
        if (includeSourceWikiSearch) {
            for (String annotatedTitleWS : annTitlesToIdAndRankWS.keySet()) {
                int wid = annTitlesToIdAndRankWS.get(annotatedTitleWS).first;
                int rank = annTitlesToIdAndRankWS.get(annotatedTitleWS).second;
                HashMap<String, Double> ESFeatures = generateEntitySelectionFeaturesSearch(query, wid, rank,
                        webTotalNS, webTotalWS, bingBoldsAndRankWS, 3);

                HashSet<String> bolds = rankToBoldsWS.get(rank);
                boolean accept = entityFilter.filterEntity(ESFeatures);
                if (accept)
                    boldsToAcceptedEntity.put(bolds.toArray(new String[] {}), new Tag(wid));
                if (debugger != null) {
                    debugger.addQueryCandidateBolds(query, "Source 3", wid, bolds);
                    debugger.addEntityFeaturesS3(query, wid, ESFeatures, accept);
                    if (accept)
                        debugger.addResult(query, wid);

                }
            }
        }

        // Filter and add entities found in the RelatedSearch
        if (includeSourceRelatedSearch) {
            for (String annotatedTitleRS : annTitlesToIdAndRankRS.keySet()) {
                int wid = annTitlesToIdAndRankRS.get(annotatedTitleRS).first;
                int rank = annTitlesToIdAndRankRS.get(annotatedTitleRS).second;
                HashMap<String, Double> ESFeatures = generateEntitySelectionFeaturesSearch(relatedSearch, wid,
                        rank, webTotalNS, webTotalRelatedSearch, bingBoldsAndRankRS, 5);

                HashSet<String> bolds = rankToBoldsRS.get(rank);
                boolean accept = entityFilter.filterEntity(ESFeatures);
                if (accept)
                    boldsToAcceptedEntity.put(bolds.toArray(new String[] {}), new Tag(wid));
            }
        }

        /** Link entities back to query mentions */

        annotations = linkBack.linkBack(query, boldsToAcceptedEntity);

    } catch (Exception e) {
        e.printStackTrace();
        throw new RuntimeException(e);
    }
    SmaphAnnotatorDebugger.out.printf("*** END :%s ***%n", query);

    return annotations;

}