Example usage for java.util LinkedList size

List of usage examples for java.util LinkedList size

Introduction

In this page you can find the example usage for java.util LinkedList size.

Prototype

int size

To view the source code for java.util LinkedList size.

Click Source Link

Usage

From source file:net.sourceforge.msscodefactory.cfbam.v2_7.CFBamJavaFX.CFBamJavaFXEnumDefListPane.java

public CFHBox getPanelHBoxMenu() {
    if (hboxMenu == null) {
        hboxMenu = new CFHBox(10);
        LinkedList<CFButton> list = new LinkedList<CFButton>();

        vboxMenuAdd = new CFVBox(10);
        buttonAddEnumType = new CFButton();
        buttonAddEnumType.setMinWidth(200);
        buttonAddEnumType.setText("Add EnumType");
        buttonAddEnumType.setOnAction(new EventHandler<ActionEvent>() {
            @Override//from   w ww .  ja  v a  2s . c  o  m
            public void handle(ActionEvent e) {
                final String S_ProcName = "handle";
                try {
                    ICFBamSchemaObj schemaObj = (ICFBamSchemaObj) javafxSchema.getSchema();
                    ICFBamEnumTypeObj obj = (ICFBamEnumTypeObj) schemaObj.getEnumTypeTableObj().newInstance();
                    CFBorderPane frame = javafxSchema.getEnumTypeFactory().newAddForm(cfFormManager, obj,
                            getViewEditClosedCallback(), true);
                    ICFBamEnumTypeEditObj edit = (ICFBamEnumTypeEditObj) (obj.beginEdit());
                    if (edit == null) {
                        throw CFLib.getDefaultExceptionFactory().newNullArgumentException(getClass(),
                                S_ProcName, 0, "edit");
                    }
                    ICFSecurityTenantObj secTenant = schemaObj.getSecTenant();
                    edit.setRequiredOwnerTenant(secTenant);
                    ICFBamSchemaDefObj container = (ICFBamSchemaDefObj) (getJavaFXContainer());
                    if (container == null) {
                        throw CFLib.getDefaultExceptionFactory().newNullArgumentException(getClass(),
                                S_ProcName, 0, "JavaFXContainer");
                    }
                    edit.setRequiredContainerSchemaDef(container);
                    ICFBamJavaFXEnumTypePaneCommon jpanelCommon = (ICFBamJavaFXEnumTypePaneCommon) frame;
                    jpanelCommon.setJavaFXFocus(obj);
                    jpanelCommon.setPaneMode(CFPane.PaneMode.Add);
                    cfFormManager.pushForm(frame);
                    setLeft(null);
                    adjustListButtons();
                } catch (Throwable t) {
                    CFConsole.formException(S_FormName, ((CFButton) e.getSource()).getText(), t);
                }
            }
        });
        if (javafxContainer instanceof ICFBamSchemaDefObj) {
            list.add(buttonAddEnumType);
        }

        int len = list.size();
        CFButton arr[] = new CFButton[len];
        Iterator<CFButton> iter = list.iterator();
        int idx = 0;
        while (iter.hasNext()) {
            arr[idx++] = iter.next();
        }
        Arrays.sort(arr, new CompareCFButtonByText());
        for (idx = 0; idx < len; idx++) {
            vboxMenuAdd.getChildren().add(arr[idx]);
        }

        buttonCancelAdd = new CFButton();
        buttonCancelAdd.setMinWidth(200);
        buttonCancelAdd.setText("Cancel Add...");
        buttonCancelAdd.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent e) {
                final String S_ProcName = "handle";
                try {
                    setLeft(null);
                    adjustListButtons();
                } catch (Throwable t) {
                    CFConsole.formException(S_FormName, ((CFButton) e.getSource()).getText(), t);
                }
            }
        });
        vboxMenuAdd.getChildren().add(buttonCancelAdd);

        scrollMenuAdd = new ScrollPane();
        scrollMenuAdd.setMinWidth(240);
        scrollMenuAdd.setMaxWidth(240);
        scrollMenuAdd.setFitToWidth(true);
        scrollMenuAdd.setHbarPolicy(ScrollBarPolicy.NEVER);
        scrollMenuAdd.setVbarPolicy(ScrollBarPolicy.AS_NEEDED);
        scrollMenuAdd.setContent(vboxMenuAdd);

        buttonAdd = new CFButton();
        buttonAdd.setMinWidth(200);
        buttonAdd.setText("Add...");
        buttonAdd.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent e) {
                final String S_ProcName = "handle";
                try {
                    setLeft(scrollMenuAdd);
                    adjustListButtons();
                } catch (Throwable t) {
                    CFConsole.formException(S_FormName, ((CFButton) e.getSource()).getText(), t);
                }
            }
        });
        hboxMenu.getChildren().add(buttonAdd);

        buttonViewSelected = new CFButton();
        buttonViewSelected.setMinWidth(200);
        buttonViewSelected.setText("View Selected");
        buttonViewSelected.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent e) {
                final String S_ProcName = "handle";
                try {
                    ICFBamSchemaObj schemaObj = (ICFBamSchemaObj) javafxSchema.getSchema();
                    if (schemaObj == null) {
                        throw CFLib.getDefaultExceptionFactory().newNullArgumentException(getClass(),
                                S_ProcName, 0, "schemaObj");
                    }
                    ICFBamEnumDefObj selectedInstance = getJavaFXFocusAsEnumDef();
                    if (selectedInstance != null) {
                        String classCode = selectedInstance.getClassCode();
                        if ("ENMD".equals(classCode)) {
                            CFBorderPane frame = javafxSchema.getEnumDefFactory().newViewEditForm(cfFormManager,
                                    selectedInstance, getViewEditClosedCallback(), false);
                            ((ICFBamJavaFXEnumDefPaneCommon) frame).setPaneMode(CFPane.PaneMode.View);
                            cfFormManager.pushForm(frame);
                        } else if ("ENMT".equals(classCode)) {
                            ICFBamEnumTypeObj obj = (ICFBamEnumTypeObj) selectedInstance;
                            CFBorderPane frame = javafxSchema.getEnumTypeFactory()
                                    .newViewEditForm(cfFormManager, obj, getViewEditClosedCallback(), false);
                            ((ICFBamJavaFXEnumTypePaneCommon) frame).setPaneMode(CFPane.PaneMode.View);
                            cfFormManager.pushForm(frame);
                        } else {
                            throw CFLib.getDefaultExceptionFactory().newUnsupportedClassException(getClass(),
                                    S_ProcName, "selectedInstance", selectedInstance,
                                    "ICFBamEnumDefObj, ICFBamEnumTypeObj");
                        }
                    }
                } catch (Throwable t) {
                    CFConsole.formException(S_FormName, ((CFButton) e.getSource()).getText(), t);
                }
            }
        });
        hboxMenu.getChildren().add(buttonViewSelected);

        buttonEditSelected = new CFButton();
        buttonEditSelected.setMinWidth(200);
        buttonEditSelected.setText("Edit Selected");
        buttonEditSelected.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent e) {
                final String S_ProcName = "handle";
                try {
                    ICFBamSchemaObj schemaObj = (ICFBamSchemaObj) javafxSchema.getSchema();
                    if (schemaObj == null) {
                        throw CFLib.getDefaultExceptionFactory().newNullArgumentException(getClass(),
                                S_ProcName, 0, "schemaObj");
                    }
                    ICFBamEnumDefObj selectedInstance = getJavaFXFocusAsEnumDef();
                    if (selectedInstance != null) {
                        String classCode = selectedInstance.getClassCode();
                        if ("ENMD".equals(classCode)) {
                            CFBorderPane frame = javafxSchema.getEnumDefFactory().newViewEditForm(cfFormManager,
                                    selectedInstance, getViewEditClosedCallback(), false);
                            ((ICFBamJavaFXEnumDefPaneCommon) frame).setPaneMode(CFPane.PaneMode.Edit);
                            cfFormManager.pushForm(frame);
                        } else if ("ENMT".equals(classCode)) {
                            ICFBamEnumTypeObj obj = (ICFBamEnumTypeObj) selectedInstance;
                            CFBorderPane frame = javafxSchema.getEnumTypeFactory()
                                    .newViewEditForm(cfFormManager, obj, getViewEditClosedCallback(), false);
                            ((ICFBamJavaFXEnumTypePaneCommon) frame).setPaneMode(CFPane.PaneMode.Edit);
                            cfFormManager.pushForm(frame);
                        } else {
                            throw CFLib.getDefaultExceptionFactory().newUnsupportedClassException(getClass(),
                                    S_ProcName, "selectedInstance", selectedInstance,
                                    "ICFBamEnumDefObj, ICFBamEnumTypeObj");
                        }
                    }
                } catch (Throwable t) {
                    CFConsole.formException(S_FormName, ((CFButton) e.getSource()).getText(), t);
                }
            }
        });
        hboxMenu.getChildren().add(buttonEditSelected);

        buttonDeleteSelected = new CFButton();
        buttonDeleteSelected.setMinWidth(200);
        buttonDeleteSelected.setText("Delete Selected");
        buttonDeleteSelected.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent e) {
                final String S_ProcName = "handle";
                try {
                    ICFBamSchemaObj schemaObj = (ICFBamSchemaObj) javafxSchema.getSchema();
                    if (schemaObj == null) {
                        throw CFLib.getDefaultExceptionFactory().newNullArgumentException(getClass(),
                                S_ProcName, 0, "schemaObj");
                    }
                    ICFBamEnumDefObj selectedInstance = getJavaFXFocusAsEnumDef();
                    if (selectedInstance != null) {
                        String classCode = selectedInstance.getClassCode();
                        if ("ENMD".equals(classCode)) {
                            CFBorderPane frame = javafxSchema.getEnumDefFactory()
                                    .newAskDeleteForm(cfFormManager, selectedInstance, getDeleteCallback());
                            ((ICFBamJavaFXEnumDefPaneCommon) frame).setPaneMode(CFPane.PaneMode.View);
                            cfFormManager.pushForm(frame);
                        } else if ("ENMT".equals(classCode)) {
                            ICFBamEnumTypeObj obj = (ICFBamEnumTypeObj) selectedInstance;
                            CFBorderPane frame = javafxSchema.getEnumTypeFactory()
                                    .newAskDeleteForm(cfFormManager, obj, getDeleteCallback());
                            ((ICFBamJavaFXEnumTypePaneCommon) frame).setPaneMode(CFPane.PaneMode.View);
                            cfFormManager.pushForm(frame);
                        } else {
                            throw CFLib.getDefaultExceptionFactory().newUnsupportedClassException(getClass(),
                                    S_ProcName, "selectedInstance", selectedInstance,
                                    "ICFBamEnumDefObj, ICFBamEnumTypeObj");
                        }
                    }
                } catch (Throwable t) {
                    CFConsole.formException(S_FormName, ((CFButton) e.getSource()).getText(), t);
                }
            }
        });
        hboxMenu.getChildren().add(buttonDeleteSelected);

    }
    return (hboxMenu);
}

From source file:com.rackspacecloud.client.cloudfiles.FilesClient.java

/**
 * /*from   ww  w  .  j  av a 2s . co m*/
 * 
 * @param container
 *             
 * @param startsWith
 *             
 * @param path
 *             
 * @param limit
 *             
 * @param marker
 *             markerlimit
 * @param delimter
 *             
 * 
 * @return A list of FilesObjects starting with the given string
 * @throws IOException
 *              IO
 * @throws HttpException
 *              Http
 * @throws FilesExcepiton
 *              
 * @throws FilesAuthorizationException
 *              
 */
public List<FilesObject> listObjectsStartingWith(String container, String startsWith, String path, int limit,
        String marker, Character delimiter) throws IOException, FilesException {
    if (!this.isLoggedin()) {
        throw new FilesAuthorizationException("You must be logged in", null, null);
    }
    if (!isValidContainerName(container)) {
        throw new FilesInvalidNameException(container);
    }
    HttpGet method = null;
    try {
        LinkedList<NameValuePair> parameters = new LinkedList<NameValuePair>();
        parameters.add(new BasicNameValuePair("format", "xml"));
        if (startsWith != null) {
            parameters.add(new BasicNameValuePair(FilesConstants.LIST_CONTAINER_NAME_QUERY, startsWith));
        }
        if (path != null) {
            parameters.add(new BasicNameValuePair("path", path));
        }
        if (limit > 0) {
            parameters.add(new BasicNameValuePair("limit", String.valueOf(limit)));
        }
        if (marker != null) {
            parameters.add(new BasicNameValuePair("marker", marker));
        }
        if (delimiter != null) {
            parameters.add(new BasicNameValuePair("delimiter", delimiter.toString()));
        }

        String uri = parameters.size() > 0 ? makeURI(storageURL + "/" + sanitizeForURI(container), parameters)
                : storageURL;
        method = new HttpGet(uri);
        method.getParams().setIntParameter("http.socket.timeout", connectionTimeOut);
        method.setHeader(FilesConstants.X_AUTH_TOKEN, authToken);
        FilesResponse response = new FilesResponse(client.execute(method));

        if (response.getStatusCode() == HttpStatus.SC_UNAUTHORIZED) {
            method.removeHeaders(FilesConstants.X_AUTH_TOKEN);
            if (login()) {
                method = new HttpGet(uri);
                method.getParams().setIntParameter("http.socket.timeout", connectionTimeOut);
                method.setHeader(FilesConstants.X_AUTH_TOKEN, authToken);
                response = new FilesResponse(client.execute(method));
            } else {
                throw new FilesAuthorizationException("Re-login failed", response.getResponseHeaders(),
                        response.getStatusLine());
            }
        }

        if (response.getStatusCode() == HttpStatus.SC_OK) {
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();
            Document document = builder.parse(response.getResponseBodyAsStream());

            NodeList nodes = document.getChildNodes();
            Node containerList = nodes.item(0);
            if (!"container".equals(containerList.getNodeName())) {
                logger.error("Got unexpected type of XML");
                return null;
            }
            ArrayList<FilesObject> objectList = new ArrayList<FilesObject>();
            NodeList objectNodes = containerList.getChildNodes();
            for (int i = 0; i < objectNodes.getLength(); ++i) {
                Node objectNode = objectNodes.item(i);
                String nodeName = objectNode.getNodeName();
                if (!("object".equals(nodeName) || "subdir".equals(nodeName)))
                    continue;
                String name = null;
                String eTag = null;
                long size = -1;
                String mimeType = null;
                String lastModified = null;
                NodeList objectData = objectNode.getChildNodes();
                if ("subdir".equals(nodeName)) {
                    size = 0;
                    mimeType = "application/directory";
                    name = objectNode.getAttributes().getNamedItem("name").getNodeValue();
                }
                for (int j = 0; j < objectData.getLength(); ++j) {
                    Node data = objectData.item(j);
                    if ("name".equals(data.getNodeName())) {
                        name = data.getTextContent();
                    } else if ("content_type".equals(data.getNodeName())) {
                        mimeType = data.getTextContent();
                    } else if ("hash".equals(data.getNodeName())) {
                        eTag = data.getTextContent();
                    } else if ("bytes".equals(data.getNodeName())) {
                        size = Long.parseLong(data.getTextContent());
                    } else if ("last_modified".equals(data.getNodeName())) {
                        lastModified = data.getTextContent();
                    } else {
                        logger.warn("Unexpected tag:" + data.getNodeName());
                    }
                }
                if (name != null) {
                    FilesObject obj = new FilesObject(name, container, this);
                    if (eTag != null)
                        obj.setMd5sum(eTag);
                    if (mimeType != null)
                        obj.setMimeType(mimeType);
                    if (size >= 0)
                        obj.setSize(size);
                    if (lastModified != null)
                        obj.setLastModified(lastModified);
                    objectList.add(obj);
                }
            }
            return objectList;
        } else if (response.getStatusCode() == HttpStatus.SC_NO_CONTENT) {
            logger.debug("Container " + container + " has no Objects");
            return new ArrayList<FilesObject>();
        } else if (response.getStatusCode() == HttpStatus.SC_NOT_FOUND) {
            throw new FilesNotFoundException("Container was not found", response.getResponseHeaders(),
                    response.getStatusLine());
        } else {
            throw new FilesException("Unexpected Server Result", response.getResponseHeaders(),
                    response.getStatusLine());
        }
    } catch (FilesNotFoundException fnfe) {
        throw fnfe;
    } catch (Exception ex) {
        logger.error("Error parsing xml", ex);
        throw new FilesException("Error parsing server resposne", ex);
    } finally {
        if (method != null)
            method.abort();
    }
}

From source file:com.ichi2.libanki.Sched.java

/**
 * Sibling spacing//from  w  w w .j  a  v  a2  s  . c o m
 * ********************
 */

private void _burySiblings(Card card) {
    LinkedList<Long> toBury = new LinkedList<Long>();
    JSONObject nconf = _newConf(card);
    boolean buryNew = nconf.optBoolean("bury", true);
    JSONObject rconf = _revConf(card);
    boolean buryRev = rconf.optBoolean("bury", true);
    // loop through and remove from queues
    Cursor cur = null;
    try {
        cur = mCol.getDb().getDatabase()
                .rawQuery(String.format(Locale.US,
                        "select id, queue from cards where nid=%d and id!=%d "
                                + "and (queue=0 or (queue=2 and due<=%d))",
                        new Object[] { card.getNid(), card.getId(), mToday }), null);
        while (cur.moveToNext()) {
            long cid = cur.getLong(0);
            int queue = cur.getInt(1);
            if (queue == 2) {
                if (buryRev) {
                    toBury.add(cid);
                }
                // if bury disabled, we still discard to give same-day spacing
                mRevQueue.remove(cid);
            } else {
                // if bury is disabled, we still discard to give same-day spacing
                if (buryNew) {
                    toBury.add(cid);
                }
                mNewQueue.remove(cid);
            }
        }
    } finally {
        if (cur != null && !cur.isClosed()) {
            cur.close();
        }
    }
    // then bury
    if (toBury.size() > 0) {
        mCol.getDb().execute("update cards set queue=-2,mod=?,usn=? where id in " + Utils.ids2str(toBury),
                new Object[] { Utils.now(), mCol.usn() });
        mCol.log(toBury);
    }
}

From source file:com.redsqirl.workflow.server.Workflow.java

/**
 * Do sort of the workflow./*from  w w w  .  jav  a 2s .c  om*/
 * 
 * If the sort is successful, it is a DAG
 * 
 * @return null if OK, or a description of the error.
 * @throws RemoteException
 */
public String topoligicalSort() throws RemoteException {
    String error = null;
    LinkedList<DataFlowElement> newList = new LinkedList<DataFlowElement>();

    LinkedList<DataFlowElement> queueList = new LinkedList<DataFlowElement>();
    Iterator<DataFlowElement> iconIt = element.iterator();
    while (iconIt.hasNext()) {
        DataFlowElement cur = iconIt.next();
        if (cur.getInputComponent().values().size() == 0) {
            queueList.add(cur);
        }
    }

    while (!queueList.isEmpty()) {
        newList.add(queueList.removeFirst());
        iconIt = element.iterator();
        while (iconIt.hasNext()) {
            DataFlowElement cur = iconIt.next();
            if (!newList.contains(cur) && !queueList.contains(cur)) {
                Iterator<List<DataFlowElement>> it = cur.getInputComponent().values().iterator();
                boolean allThere = true;
                while (it.hasNext() && allThere) {
                    allThere = newList.containsAll(it.next());
                }

                if (allThere) {
                    queueList.add(cur);
                }
            }
        }
    }
    if (newList.size() < element.size()) {
        error = LanguageManagerWF.getText("workflow.topologicalSort");
    } else {
        element = newList;
    }

    return error;
}

From source file:elh.eus.absa.Features.java

/**
 * Check if the given word/lemma/ngram exists both in the ngram list and in the general or domain polarity
 * lexicons, and if yes updates the corresponding attributes in the feature vector
 * //  w w w . j  ava 2 s .co m
 * @param ngrams
 * @param fVector
 * @param prefix
 * @param toknumNgram
 * @param toknumPol
 * @param empty
 * @param ngram
 */
private void checkNgramsAndPolarLexicons(LinkedList<String> ngrams, double[] fVector, String prefix,
        int toknumNgram, int toknumPol, boolean empty, boolean ngram) {
    //System.err.println(Arrays.asList(ngrams).toString());
    // if empty is active means that we are checking the end of the sentence and 
    // the ngram list must be emptied 
    if (empty) {
        // add ngrams to the feature vector
        while (!ngrams.isEmpty()) {
            String ng = featureFromArray(ngrams, prefix);
            //if the current lemma is in the ngram list activate the feature in the vector
            if (params.containsKey("lemmaNgrams")
                    && (!params.getProperty("lemmaNgrams").equalsIgnoreCase("0"))) {
                // add occurrence to feature vector (the functions checks if the given ngram feature exists).
                addNumericToFeatureVector(ng, fVector, toknumNgram); //tokNum
            }

            ng = featureFromArray(ngrams, "");
            if (params.containsKey("polarLexiconGeneral") || params.containsKey("polarLexiconDomain")) {
                checkPolarityLexicons(ng, fVector, toknumPol, ngram);
            } //end polarity ngram checker

            ngrams.removeFirst();

        } //end ngram checking
    }
    // if empty is false search for all ngrams in the window
    else {
        // add ngrams to the feature vector
        for (int i = 0; i < ngrams.size(); i++) {
            String ng = featureFromArray(ngrams.subList(0, i + 1), prefix);
            //if the current lemma is in the ngram list activate the feature in the vector
            if (params.containsKey("lemmaNgrams")
                    && (!params.getProperty("lemmaNgrams").equalsIgnoreCase("0"))) {
                // add occurrence to feature vector (the functions checks if the given ngram feature exists).
                addNumericToFeatureVector(ng, fVector, toknumNgram); //tokNum                                    
            }

            ng = featureFromArray(ngrams.subList(0, i + 1), "");
            if (params.containsKey("polarLexiconGeneral") || params.containsKey("polarLexiconDomain")) {
                checkPolarityLexicons(ng, fVector, toknumPol, ngram);
            } //end polarity ngram checker
        } //end ngram checking                          
    }
}

From source file:de.innovationgate.wgpublisher.webtml.utils.TMLContext.java

public static TMLContext getThreadMainContext() {

    LinkedList<TMLContext> contexts = _threadMainContexts.get();
    if (contexts != null && contexts.size() > 0) {
        return contexts.getLast();
    } else {//from ww w.  j  a  v a  2 s.c o  m
        return null;
    }

}

From source file:elh.eus.absa.Features.java

/**
 *  Extract n-grams up to a certain length from an Conll tabulated format corpus.
 * /*w ww.j  a v  a 2 s.c o m*/
 * @param int length : which 'n' use for 'n-grams' 
 * @param string type (wf|lemma|pos): what type of ngrams we want to extract.
 * @param boolean save : safe ngrams to file or not. 
 * @return TreeSet<String> return word form ngrams of length length
 */
private int extractNgramsTAB(int length, String type, List<String> discardPos, boolean save) {
    //System.err.println("ngram extraction Tab: _"+length+"_"+type);
    if (length == 0) {
        return 0;
    }

    for (String sent : corpus.getSentences().keySet()) {
        //System.err.println("ngram extraction, corpus sentences: "+corpus.getSentences().get(sent));           
        String[] tokens = corpus.getSentences().get(sent).split("\n");
        LinkedList<String> ngrams = new LinkedList<String>();
        for (String row : tokens) {
            String ngram = "";
            String[] fields = row.split("\t");
            String pos = "";
            switch (type) {
            case "wf":
                ngram = fields[0];
                break;
            case "lemma":
                if (fields.length > 1) {
                    ngram = fields[1];
                }
                if (fields.length > 2) {
                    pos = fields[2];
                }
                break;
            case "pos":
                if (fields.length > 2) {
                    ngram = fields[2];
                    switch (ngram.length()) {
                    case 0:
                        ngram = "-";
                        break;
                    case 1:
                        ngram = ngram.substring(0, 1);
                        break;
                    default:
                        ngram = ngram.substring(0, 2);
                        break;
                    }
                }
            }

            //if the is a blank line we assume sentence has ended and we empty and re-initialize the n-gram list 
            if (ngram.equals("")) {
                //empty n-gram list and add remaining n-grams to the feature list
                while (!ngrams.isEmpty()) {
                    String ng = featureFromArray(ngrams, type);
                    addNgram(type, ng);
                    ngrams.removeFirst();
                }
                continue;
            }

            if (ngrams.size() >= length) {
                ngrams.removeFirst();
            }

            //if no alphanumeric char is present discard the element as invalid ngram. Or if it has a PoS tag that
            //should be discarded
            String lCurrent = ngram;
            if ((!discardPos.contains(pos)) && (!ngram.matches("^[^\\p{L}\\p{M}\\p{Nd}\\p{InEmoticons}]+$"))
                    && (lCurrent.length() > 1)) {
                //standarize numeric values to NUMNUM lemma value
                //ngram.replaceFirst("^[0-9]$", "NUMNUM");
                if (!type.equalsIgnoreCase("pos")) {
                    ngrams.add(normalize(ngram, params.getProperty("normalization", "none")));
                } else {
                    ngrams.add(ngram);
                }
            }
            //certain punctuation marks are allowed as lemmas
            else if ((lCurrent.length() < 2) && (lCurrent.matches("[,;.?!]"))) {
                ngrams.add(lCurrent);
            }

            // add ngrams to the feature list
            for (int i = 0; i < ngrams.size(); i++) {
                String ng = featureFromArray(ngrams.subList(0, i + 1), type);
                addNgram(type, ng);
            }
        }
        //empty ngram list and add remaining ngrams to the feature list
        while (!ngrams.isEmpty()) {
            String ng = featureFromArray(ngrams, type);
            addNgram(type, ng);
            ngrams.removeFirst();
        }
    }
    return 1;
}

From source file:elh.eus.absa.Features.java

/**
 *  Extract n-grams up to a certain length from an Conll tabulated format string.
 * /*from w ww  .j  a va2 s . com*/
 * @param String input : input tagged conll string 
 * @param int length : which 'n' use for 'n-grams' 
 * @param string type (wf|lemma|pos): what type of ngrams we want to extract.
 * @param boolean save : safe ngrams to file or not. 
 * @return int success: return 1 if the process ended correctly
 */
private int extractNgramsTABString(InputStream input, int length, String type, List<String> discardPos,
        boolean save) {
    //System.err.println("ngram extraction Tab: _"+length+"_"+type);
    if (length == 0) {
        return 0;
    }

    //System.err.println("ngram extraction, corpus sentences: "+corpus.getSentences().get(sent));                 
    //String[] tokens = input.split("\n");
    BufferedReader reader = new BufferedReader(new InputStreamReader(input));
    LinkedList<String> ngrams = new LinkedList<String>();
    String line;
    try {
        while ((line = reader.readLine()) != null) {
            String ngram = "";
            String[] fields = line.split("\\s");
            String pos = "";
            switch (type) {
            case "wf":
                ngram = fields[0];
                break;
            case "lemma":
                if (fields.length > 1) {
                    ngram = fields[1];
                }
                if (fields.length > 2) {
                    pos = fields[2];
                }
                break;
            case "pos":
                if (fields.length > 2) {
                    ngram = fields[2];
                    switch (ngram.length()) {
                    case 0:
                        ngram = "-";
                        break;
                    case 1:
                        ngram = ngram.substring(0, 1);
                        break;
                    default:
                        ngram = ngram.substring(0, 2);
                        break;
                    }
                }
            }

            //if the is a blank line we assume sentence has ended and we empty and re-initialize the n-gram list 
            if (ngram.equals("")) {
                //empty n-gram list and add remaining n-grams to the feature list
                while (!ngrams.isEmpty()) {
                    String ng = featureFromArray(ngrams, type);
                    addNgram(type, ng);
                    ngrams.removeFirst();
                }
                continue;
            }

            if (ngrams.size() >= length) {
                ngrams.removeFirst();
            }

            //if no alphanumeric char is present discard the element as invalid ngram. Or if it has a PoS tag that
            //should be discarded
            String lCurrent = ngram;
            if ((!discardPos.contains(pos)) && (!ngram.matches("^[^\\p{L}\\p{M}\\p{Nd}\\p{InEmoticons}]+$"))
                    && (lCurrent.length() > 1)) {
                //standarize numeric values to NUMNUM lemma value
                //ngram.replaceFirst("^[0-9]$", "NUMNUM");
                if (!type.equalsIgnoreCase("pos")) {
                    ngrams.add(normalize(ngram, params.getProperty("normalization", "none")));
                } else {
                    ngrams.add(ngram);
                }
            }
            //certain punctuation marks are allowed as lemmas
            else if ((lCurrent.length() < 2) && (lCurrent.matches("[,;.?!]"))) {
                ngrams.add(lCurrent);
            }

            // add ngrams to the feature list
            for (int i = 0; i < ngrams.size(); i++) {
                String ng = featureFromArray(ngrams.subList(0, i + 1), type);
                addNgram(type, ng);
            }
        }
    } catch (IOException e) {
        System.err.println("EliXa::Features::extractNgramsTABString - WARNING: Error reading tagged file, "
                + "ngram extraction may be only partial\n");
    }

    //empty ngram list and add remaining ngrams to the feature list
    while (!ngrams.isEmpty()) {
        String ng = featureFromArray(ngrams, type);
        addNgram(type, ng);
        ngrams.removeFirst();
    }

    return 1;
}

From source file:de.tudarmstadt.ukp.wikipedia.parser.mediawiki.ModularParser.java

private Table buildTable(SpanManager sm, ContentElementParsingParameters cepp, LinkedList<Span> lineSpans) {

    Table result = new Table();
    int col = -1;
    int row = 0;/*w  w  w . j ava  2  s  . c  om*/
    int subTables = 0;
    LinkedList<Span> tableDataSpans = new LinkedList<Span>();
    sm.manageList(tableDataSpans);

    if (calculateSrcSpans) {
        result.setSrcSpan(new SrcSpan(sm.getSrcPos(lineSpans.getFirst().getStart()), -1));
    }

    lineSpans.removeFirst();

    while (!lineSpans.isEmpty()) {
        Span s = lineSpans.removeFirst();

        int pos = s.nonWSCharPos(sm);
        char c0 = s.charAt(pos, sm);
        char c1 = s.charAt(pos + 1, sm);

        if (subTables == 0 && (c0 == '!' || c0 == '|')) {
            if (!tableDataSpans.isEmpty()) {
                lineSpans.addFirst(s);

                SrcSpan ei = null;
                if (calculateSrcSpans) {
                    ei = new SrcSpan(sm.getSrcPos(tableDataSpans.getFirst().getStart() - 1) + 1, -1);
                }

                TableElement te = new TableElement(parseSections(sm, cepp, tableDataSpans), row, col);
                te.setSrcSpan(ei);
                result.addTableElement(te);
                lineSpans.removeFirst();
            }

            col++;
            if (c1 == '-') {
                row++;
                col = -1;
                continue;
            } else if (c0 == '|' && c1 == '}') {
                sm.removeManagedList(tableDataSpans);

                if (calculateSrcSpans) {
                    result.getSrcSpan().setEnd(sm.getSrcPos(s.getEnd()));
                }

                return result;
            } else if (c0 == '|' && c1 == '+') {
                result.setTitleElement(
                        parseContentElement(sm, cepp, new Span(s.getStart() + pos + 2, s.getEnd()).trim(sm)));
                continue;
            } else {
                int multipleCols;
                if ((multipleCols = sm.indexOf("||", s.getStart() + pos + 1, s.getEnd())) != -1) {
                    lineSpans.addFirst(new Span(multipleCols + 1, s.getEnd()));
                    s.setEnd(multipleCols);
                }

                int optionTagPos = sm.indexOf("|", s.getStart() + pos + 1, s.getEnd());

                if (optionTagPos != -1) {
                    s.setStart(optionTagPos + 1).trim(sm);
                } else {
                    s.adjustStart(pos + 1).trim(sm);
                }
            }
        } else if (c0 == '|' && c1 == '}') {
            subTables--;
        } else if (c0 == '{' && c1 == '|') {
            subTables++;
        }

        tableDataSpans.addLast(s);
    }

    if (tableDataSpans.size() != 0) {

        SrcSpan ei = null;
        if (calculateSrcSpans) {
            ei = new SrcSpan(sm.getSrcPos(tableDataSpans.getFirst().getStart() - 1) + 1, -1);
        }

        TableElement te = new TableElement(parseSections(sm, cepp, tableDataSpans), row, col);
        te.setSrcSpan(ei);

        result.addTableElement(te);
    }

    sm.removeManagedList(tableDataSpans);

    if (calculateSrcSpans) {
        result.getSrcSpan().setEnd(-1);
    }

    return result;
}

From source file:au.edu.ausstage.networks.LookupManager.java

/**
 * A method to lookup the key collaborators for a contributor
 *
 * @param id         the unique id of the contributor
 * @param formatType the required format of the data
 * @param sortType   the required way in which the data is to be sorted
 *
 * @return           the results of the lookup
 *///from ww w.  j a v a 2  s .co  m
public String getKeyCollaborators(String id, String formatType, String sortType) {

    // check on the parameters
    if (InputUtils.isValidInt(id) == false || InputUtils.isValid(formatType) == false
            || InputUtils.isValid(sortType) == false) {
        throw new IllegalArgumentException("All parameters to this method are required");
    }

    // define a Tree Set to store the results
    java.util.LinkedList<Collaborator> collaborators = new java.util.LinkedList<Collaborator>();

    // define other helper variables
    QuerySolution row = null;
    Collaborator collaborator = null;

    // define the base sparql query
    String sparqlQuery = "PREFIX foaf:       <" + FOAF.NS + ">" + "PREFIX ausestage:  <" + AuseStage.NS + "> "
            + "SELECT ?collaborator ?collabGivenName ?collabFamilyName ?function ?firstDate ?lastDate ?collabCount "
            + "WHERE {  " + "       @ a foaf:Person ; "
            + "                      ausestage:hasCollaboration ?collaboration. "
            + "       ?collaboration ausestage:collaborator ?collaborator; "
            + "                      ausestage:collaborationFirstDate ?firstDate; "
            + "                      ausestage:collaborationLastDate ?lastDate; "
            + "                      ausestage:collaborationCount ?collabCount. "
            + "       ?collaborator  foaf:givenName ?collabGivenName; "
            + "                      foaf:familyName ?collabFamilyName; "
            + "                      ausestage:function ?function. " + "       FILTER (?collaborator != @) "
            + "}";

    // do we need to sort by name?
    if (sortType.equals("count") == true) {
        sparqlQuery += " ORDER BY DESC(?collabCount)";
    } else if (sortType.equals("name") == true) {
        sparqlQuery += " ORDER BY ?collabFamilyName ?collabGivenName";
    }

    // build a URI from the id
    id = AusStageURI.getContributorURI(id);

    // add the contributor URI to the query
    sparqlQuery = sparqlQuery.replaceAll("@", "<" + id + ">");

    // execute the query
    ResultSet results = rdf.executeSparqlQuery(sparqlQuery);

    // build the dataset
    // use a numeric sort order
    while (results.hasNext()) {
        // loop though the resulset
        // get a new row of data
        row = results.nextSolution();

        // instantiate a collaborator object
        collaborator = new Collaborator(AusStageURI.getId(row.get("collaborator").toString()));

        // check to see if the list contains this collaborator
        if (collaborators.indexOf(collaborator) != -1) {
            // collaborator is already in the list
            collaborator = collaborators.get(collaborators.indexOf(collaborator));

            // update the function
            collaborator.setFunction(row.get("function").toString());

        } else {
            // collaborator is not on the list

            // get the name
            collaborator.setGivenName(row.get("collabGivenName").toString());
            collaborator.setFamilyName(row.get("collabFamilyName").toString(), true);

            // get the dates
            collaborator.setFirstDate(row.get("firstDate").toString());
            collaborator.setLastDate(row.get("lastDate").toString());

            // get the collaboration count
            collaborator.setCollaborations(Integer.toString(row.get("collabCount").asLiteral().getInt()));

            // add the url
            collaborator.setUrl(AusStageURI.getURL(row.get("collaborator").toString()));

            // add the function
            collaborator.setFunction(row.get("function").toString());

            collaborators.add(collaborator);
        }
    }

    // play nice and tidy up
    rdf.tidyUp();

    // sort by the id
    if (sortType.equals("id") == true) {
        TreeMap<Integer, Collaborator> collaboratorsToSort = new TreeMap<Integer, Collaborator>();

        for (int i = 0; i < collaborators.size(); i++) {
            collaborator = collaborator = collaborators.get(i);

            collaboratorsToSort.put(Integer.parseInt(collaborator.getId()), collaborator);
        }

        // empty the list
        collaborators.clear();

        // add the collaborators back to the list
        Collection values = collaboratorsToSort.values();
        Iterator iterator = values.iterator();

        while (iterator.hasNext()) {
            // get the collaborator
            collaborator = (Collaborator) iterator.next();

            collaborators.add(collaborator);
        }

        collaboratorsToSort = null;
    }

    // define a variable to store the data
    String dataString = null;

    if (formatType.equals("html") == true) {
        dataString = createHTMLOutput(collaborators);
    } else if (formatType.equals("xml") == true) {
        dataString = createXMLOutput(collaborators);
    } else if (formatType.equals("json") == true) {
        dataString = createJSONOutput(collaborators);
    }

    // return the data
    return dataString;
}