idx int64 0 165k | question stringlengths 73 4.15k | target stringlengths 5 918 | len_question int64 21 890 | len_target int64 3 255 |
|---|---|---|---|---|
5,300 | public void onlineTrain ( final double [ ] x , final int labelIndex ) { Map < Integer , Double > result = predict ( x ) ; Map . Entry < Integer , Double > maxResult = result . entrySet ( ) . stream ( ) . max ( ( e1 , e2 ) -> e1 . getValue ( ) . compareTo ( e2 . getValue ( ) ) ) . orElse ( null ) ; if ( maxResult . getKey ( ) != labelIndex ) { double e_correction_d = 1 ; model . weights [ labelIndex ] = reweight ( x , model . weights [ labelIndex ] , e_correction_d ) ; model . bias [ labelIndex ] = e_correction_d ; double w_correction_d = - 1 ; model . weights [ maxResult . getKey ( ) ] = reweight ( x , model . weights [ maxResult . getKey ( ) ] , w_correction_d ) ; model . bias [ maxResult . getKey ( ) ] = w_correction_d ; } if ( LOG . isDebugEnabled ( ) ) { LOG . debug ( "New bias: " + Arrays . toString ( model . bias ) ) ; LOG . debug ( "New weight: " + Arrays . stream ( model . weights ) . map ( Arrays :: toString ) . reduce ( ( wi , wii ) -> wi + ", " + wii ) . get ( ) ) ; } } | public use for doing one training sample . | 315 | 8 |
5,301 | @ Override public Map < String , Double > predict ( Tuple predict ) { Map < Integer , Double > indexResult = predict ( predict . vector . getVector ( ) ) ; return indexResult . entrySet ( ) . stream ( ) . map ( e -> new ImmutablePair <> ( model . labelIndexer . getLabel ( e . getKey ( ) ) , VectorUtils . sigmoid . apply ( e . getValue ( ) ) ) ) // Only do sigmoid here! . collect ( Collectors . toMap ( ImmutablePair :: getLeft , ImmutablePair :: getRight ) ) ; } | Do a prediction . | 135 | 4 |
5,302 | private boolean isTrimEnabled ( ) { String contentType = response . getContentType ( ) ; // If the contentType is the same string (by identity), return the previously determined value. // This assumes the same string instance is returned by the response when content type not changed between calls. if ( contentType != isTrimEnabledCacheContentType ) { isTrimEnabledCacheResult = contentType == null || contentType . equals ( "application/xhtml+xml" ) || contentType . startsWith ( "application/xhtml+xml;" ) || contentType . equals ( "text/html" ) || contentType . startsWith ( "text/html;" ) || contentType . equals ( "application/xml" ) || contentType . startsWith ( "application/xml;" ) || contentType . equals ( "text/xml" ) || contentType . startsWith ( "text/xml;" ) ; isTrimEnabledCacheContentType = contentType ; } return isTrimEnabledCacheResult ; } | Determines if trimming is enabled based on the output content type . | 213 | 15 |
5,303 | private boolean processChar ( char c ) { if ( inTextArea ) { if ( c == TrimFilterWriter . textarea_close [ readCharMatchCount ] || c == TrimFilterWriter . TEXTAREA_CLOSE [ readCharMatchCount ] ) { readCharMatchCount ++ ; if ( readCharMatchCount >= TrimFilterWriter . textarea_close . length ) { inTextArea = false ; readCharMatchCount = 0 ; } } else { readCharMatchCount = 0 ; } return true ; } else if ( inPre ) { if ( c == TrimFilterWriter . pre_close [ preReadCharMatchCount ] || c == TrimFilterWriter . PRE_CLOSE [ preReadCharMatchCount ] ) { preReadCharMatchCount ++ ; if ( preReadCharMatchCount >= TrimFilterWriter . pre_close . length ) { inPre = false ; preReadCharMatchCount = 0 ; } } else { preReadCharMatchCount = 0 ; } return true ; } else { if ( c == ' ' ) { readCharMatchCount = 0 ; preReadCharMatchCount = 0 ; // Carriage return only output when no longer at the beginning of the line return ! atLineStart ; } else if ( c == ' ' ) { readCharMatchCount = 0 ; preReadCharMatchCount = 0 ; // Newline only output when no longer at the beginning of the line if ( ! atLineStart ) { atLineStart = true ; return true ; } else { return false ; } } else if ( c == ' ' || c == ' ' ) { readCharMatchCount = 0 ; preReadCharMatchCount = 0 ; // Space and tab only output when no longer at the beginning of the line return ! atLineStart ; } else { atLineStart = false ; if ( c == TrimFilterWriter . textarea [ readCharMatchCount ] || c == TrimFilterWriter . TEXTAREA [ readCharMatchCount ] ) { readCharMatchCount ++ ; if ( readCharMatchCount >= TrimFilterWriter . textarea . length ) { inTextArea = true ; readCharMatchCount = 0 ; } } else { readCharMatchCount = 0 ; } if ( c == TrimFilterWriter . pre [ preReadCharMatchCount ] || c == TrimFilterWriter . PRE [ preReadCharMatchCount ] ) { preReadCharMatchCount ++ ; if ( preReadCharMatchCount >= TrimFilterWriter . pre . length ) { inPre = true ; preReadCharMatchCount = 0 ; } } else { preReadCharMatchCount = 0 ; } return true ; } } } | Processes one character and returns true if the character should be outputted . | 560 | 15 |
5,304 | public int set ( final int flags ) { for ( ; ; ) { int current = _flags . get ( ) ; int newValue = current | flags ; if ( _flags . compareAndSet ( current , newValue ) ) { return current ; } } } | Atomically add the given flags to the current set | 55 | 11 |
5,305 | public int unset ( final int flags ) { for ( ; ; ) { int current = _flags . get ( ) ; int newValue = current & ~ flags ; if ( _flags . compareAndSet ( current , newValue ) ) { return current ; } } } | Atomically remove the given flags from the current set | 57 | 11 |
5,306 | public int change ( final int add , final int remove ) { for ( ; ; ) { int current = _flags . get ( ) ; int newValue = ( current | add ) & ~ remove ; if ( _flags . compareAndSet ( current , newValue ) ) { return current ; } } } | Atomically add and remove the given flags from the current set | 64 | 13 |
5,307 | public static < E > String message ( Response < E > response ) { return Optional . ofNullable ( response ) . map ( Response :: getMessage ) . orElse ( StringUtils . EMPTY ) ; } | get response message | 45 | 3 |
5,308 | public static DTree convertTreeBankToCoNLLX ( final String constituentTree ) { Tree tree = Tree . valueOf ( constituentTree ) ; SemanticHeadFinder headFinder = new SemanticHeadFinder ( false ) ; // keep copula verbs as head Collection < TypedDependency > dependencies = new EnglishGrammaticalStructure ( tree , string -> true , headFinder ) . typedDependencies ( ) ; List < CoreLabel > tokens = tree . taggedLabeledYield ( ) ; StanfordParser . tagLemma ( tokens ) ; return StanfordTreeBuilder . generate ( tokens , dependencies , null ) ; } | Parser for tag Lemma | 136 | 5 |
5,309 | private String addLocale ( Locale locale , String url , String encodedParamName , String encoding ) { // Split the anchor int poundPos = url . lastIndexOf ( ' ' ) ; String beforeAnchor ; String anchor ; if ( poundPos == - 1 ) { beforeAnchor = url ; anchor = null ; } else { anchor = url . substring ( poundPos ) ; beforeAnchor = url . substring ( 0 , poundPos ) ; } // Only add for non-excluded file types if ( isLocalizedPath ( beforeAnchor ) ) { int questionPos = beforeAnchor . lastIndexOf ( ' ' ) ; // Only rewrite a URL that does not already contain a paramName parameter. if ( questionPos == - 1 || ( ! beforeAnchor . startsWith ( "?" + encodedParamName + "=" , questionPos ) && beforeAnchor . indexOf ( "&" + encodedParamName + "=" , questionPos + 1 ) == - 1 ) ) { try { beforeAnchor += ( questionPos == - 1 ? ' ' : ' ' ) + encodedParamName + ' ' + URLEncoder . encode ( toLocaleString ( locale ) , encoding ) ; } catch ( UnsupportedEncodingException e ) { // Should never happen with standard supported encoding throw new WrappedException ( e ) ; } } return ( anchor != null ) ? ( beforeAnchor + anchor ) : beforeAnchor ; } else { // Unmodified return url ; } } | Adds the current locale as a parameter to the URL . | 326 | 11 |
5,310 | public static Map < String , Locale > getEnabledLocales ( ServletRequest request ) { @ SuppressWarnings ( "unchecked" ) Map < String , Locale > enabledLocales = ( Map < String , Locale > ) request . getAttribute ( ENABLED_LOCALES_REQUEST_ATTRIBUTE_KEY ) ; if ( enabledLocales == null ) throw new IllegalStateException ( "Not in request filtered by LocaleFilter, unable to get enabled locales." ) ; return enabledLocales ; } | Gets the set of enabled locales for the provided request . This must be called from a request that has already been filtered through LocaleFilter . When container s default locale is used will return an empty map . | 116 | 43 |
5,311 | protected boolean isLocalizedPath ( String url ) { int questionPos = url . lastIndexOf ( ' ' ) ; String lowerPath = ( questionPos == - 1 ? url : url . substring ( 0 , questionPos ) ) . toLowerCase ( Locale . ROOT ) ; return // Matches SessionResponseWrapper // Matches NoSessionFilter ! lowerPath . endsWith ( ".bmp" ) && ! lowerPath . endsWith ( ".css" ) && ! lowerPath . endsWith ( ".exe" ) && ! lowerPath . endsWith ( ".gif" ) && ! lowerPath . endsWith ( ".ico" ) && ! lowerPath . endsWith ( ".jpeg" ) && ! lowerPath . endsWith ( ".jpg" ) && ! lowerPath . endsWith ( ".js" ) && ! lowerPath . endsWith ( ".png" ) && ! lowerPath . endsWith ( ".svg" ) && ! lowerPath . endsWith ( ".txt" ) && ! lowerPath . endsWith ( ".zip" ) ; } | Checks if the locale parameter should be added to the given URL . | 223 | 14 |
5,312 | protected String toLocaleString ( Locale locale ) { String language = locale . getLanguage ( ) ; if ( language . isEmpty ( ) ) return "" ; String country = locale . getCountry ( ) ; if ( country . isEmpty ( ) ) return language ; String variant = locale . getVariant ( ) ; if ( variant . isEmpty ( ) ) { return language + ' ' + country ; } else { return language + ' ' + country + ' ' + variant ; } } | Gets a string representation of the given locale . This default implementation only supports language country and variant . Country will only be added when language present . Variant will only be added when both language and country are present . | 103 | 42 |
5,313 | @ Override public void loadModel ( InputStream modelIs ) { ByteArrayOutputStream baos = new ByteArrayOutputStream ( ) ; try { IOUtils . copy ( modelIs , baos ) ; } catch ( IOException e ) { LOG . error ( "Load model err." , e ) ; } InputStream isForSVMLoad = new ByteArrayInputStream ( baos . toByteArray ( ) ) ; try ( ZipInputStream zipInputStream = new ZipInputStream ( isForSVMLoad ) ) { ZipEntry entry ; while ( ( entry = zipInputStream . getNextEntry ( ) ) != null ) { if ( entry . getName ( ) . endsWith ( ".model" ) ) { BufferedReader br = new BufferedReader ( new InputStreamReader ( zipInputStream , Charset . defaultCharset ( ) ) ) ; this . model = svm . svm_load_model ( br ) ; } } } catch ( IOException e ) { // Do Nothing. } modelIs = new ByteArrayInputStream ( baos . toByteArray ( ) ) ; try ( ZipInputStream zipInputStream = new ZipInputStream ( modelIs ) ) { ZipEntry entry ; while ( ( entry = zipInputStream . getNextEntry ( ) ) != null ) { if ( entry . getName ( ) . endsWith ( ".lbindexer" ) ) { String lbIndexer = IOUtils . toString ( zipInputStream , Charset . defaultCharset ( ) ) ; this . labelIndexer = new LabelIndexer ( new ArrayList <> ( ) ) ; this . labelIndexer . readFromSerializedString ( lbIndexer ) ; } } } catch ( IOException e ) { LOG . error ( "Err in load LabelIndexer" , e ) ; } } | We load twice because svm . svm_load_model will close the stream after load . so the next guy will have Steam closed exception . | 396 | 30 |
5,314 | static String getJavaScriptUnicodeEscapeString ( char ch ) { int chInt = ( int ) ch ; if ( chInt >= ENCODE_RANGE_1_START && chInt < ENCODE_RANGE_1_END ) { return javaScriptUnicodeEscapeStrings1 [ chInt - ENCODE_RANGE_1_START ] ; } if ( chInt >= ENCODE_RANGE_2_START && chInt < ENCODE_RANGE_2_END ) { return javaScriptUnicodeEscapeStrings2 [ chInt - ENCODE_RANGE_2_START ] ; } if ( chInt >= ENCODE_RANGE_3_START && chInt < ENCODE_RANGE_3_END ) { return javaScriptUnicodeEscapeStrings3 [ chInt - ENCODE_RANGE_3_START ] ; } // No encoding needed return null ; } | Gets the unicode escape for a JavaScript character or null if may be passed - through without escape . | 216 | 21 |
5,315 | @ Override public Map < String , Double > predict ( Tuple predict ) { Map < Integer , Double > labelProb = new HashMap <> ( ) ; for ( Integer labelIndex : model . labelIndexer . getIndexSet ( ) ) { double likelihood = 1.0D ; for ( int i = 0 ; i < predict . vector . getVector ( ) . length ; i ++ ) { double fi = predict . vector . getVector ( ) [ i ] ; likelihood = likelihood * VectorUtils . gaussianPDF ( model . meanVectors [ labelIndex ] [ i ] , model . varianceVectors [ labelIndex ] [ i ] , fi ) ; } double posterior = model . labelPrior . get ( labelIndex ) * likelihood ; // prior*likelihood, This is numerator of posterior labelProb . put ( labelIndex , posterior ) ; } double evidence = labelProb . values ( ) . stream ( ) . reduce ( ( e1 , e2 ) -> e1 + e2 ) . orElse ( - 1D ) ; if ( evidence == - 1 ) { LOG . error ( "Evidence is Empty!" ) ; return new HashMap <> ( ) ; } labelProb . entrySet ( ) . forEach ( entry -> { double prob = entry . getValue ( ) / evidence ; labelProb . put ( entry . getKey ( ) , prob ) ; } ) ; // This is denominator of posterior Map < String , Double > result = model . labelIndexer . convertMapKey ( labelProb ) ; if ( predict . label == null || predict . label . isEmpty ( ) ) { // Just for write to predict tuple. predict . label = result . entrySet ( ) . stream ( ) . max ( ( e1 , e2 ) -> e1 . getValue ( ) . compareTo ( e2 . getValue ( ) ) ) . map ( Entry :: getKey ) . orElse ( StringUtils . EMPTY ) ; } return result ; } | Integer is label s Index from labelIndexer | 427 | 9 |
5,316 | public static void splitData ( final String originalTrainingDataFile ) { List < Tuple > trainingData = NaiveBayesClassifier . readTrainingData ( originalTrainingDataFile , "\\s" ) ; List < Tuple > wrongData = new ArrayList <> ( ) ; int lastTrainingDataSize ; int iterCount = 0 ; do { System . out . println ( "Iteration:\t" + ( ++ iterCount ) ) ; lastTrainingDataSize = trainingData . size ( ) ; NaiveBayesClassifier nbc = new NaiveBayesClassifier ( ) ; nbc . train ( trainingData ) ; Iterator < Tuple > trainingDataIter = trainingData . iterator ( ) ; while ( trainingDataIter . hasNext ( ) ) { Tuple t = trainingDataIter . next ( ) ; String actual = nbc . predictLabel ( t ) ; if ( ! t . label . equals ( actual ) && ! t . label . equals ( "1" ) ) { // preserve 1 since too few. wrongData . add ( t ) ; trainingDataIter . remove ( ) ; } } Iterator < Tuple > wrongDataIter = wrongData . iterator ( ) ; while ( wrongDataIter . hasNext ( ) ) { Tuple t = wrongDataIter . next ( ) ; String actual = nbc . predictLabel ( t ) ; if ( t . label . equals ( actual ) ) { trainingData . add ( t ) ; wrongDataIter . remove ( ) ; } } } while ( trainingData . size ( ) != lastTrainingDataSize ) ; writeToFile ( trainingData , originalTrainingDataFile + ".aligned" ) ; writeToFile ( wrongData , originalTrainingDataFile + ".wrong" ) ; } | Split the data between trainable and wrong . | 371 | 9 |
5,317 | public void addFilePart ( final String fieldName , final InputStream stream , final String contentType ) throws IOException { addFilePart ( fieldName , stream , null , contentType ) ; } | Adds a upload file section to the request by stream | 41 | 10 |
5,318 | public void addFilePart ( final String fieldName , final URL urlToUploadFile ) throws IOException { // // Maybe try and extract a filename from the last part of the url? // Or have the user pass it in? // Or just leave it blank as I have already done? // addFilePart ( fieldName , urlToUploadFile . openStream ( ) , null , URLConnection . guessContentTypeFromName ( urlToUploadFile . toString ( ) ) ) ; } | Adds a upload file section to the request by url stream | 101 | 11 |
5,319 | public void addHeaderField ( final String name , final String value ) { writer . append ( name + ": " + value ) . append ( LINE_FEED ) ; writer . flush ( ) ; } | Adds a header field to the request . | 43 | 8 |
5,320 | public HttpResponse finish ( ) throws IOException { writer . append ( "--" + boundary + "--" ) . append ( LINE_FEED ) ; writer . flush ( ) ; try { return doFinish ( ) ; } finally { writer . close ( ) ; } } | Completes the request and receives response from the server . | 59 | 12 |
5,321 | public double update ( final double units ) { final double speed ; lock . lock ( ) ; try { final long currentTime = System . nanoTime ( ) ; final long timeDifference = ( currentTime - lastUpdateTime ) / C1 ; // nanoseconds to micros if ( timeDifference >= averagingPeriod ) { speed = units / averagingPeriod ; cachedSpeed = speed ; lastUpdateTime = currentTime ; elapsedTime = ZERO_TIME ; quantity = ZERO_UNITS ; } else { if ( timeDifference > ZERO_TIME ) { lastUpdateTime = currentTime ; elapsedTime += timeDifference ; } if ( units != ZERO_UNITS ) { quantity += units ; } if ( elapsedTime >= averagingPeriod ) { speed = quantity / elapsedTime ; cachedSpeed = speed ; elapsedTime = ZERO_TIME ; quantity = ZERO_UNITS ; } else { speed = ( cachedSpeed * ( averagingPeriod - elapsedTime ) + quantity ) / averagingPeriod ; } } } finally { lock . unlock ( ) ; } return speed * C0 ; // units per micro to units per second } | Updates speed value . | 242 | 5 |
5,322 | public static < T > Optional < T > toBean ( String json , Class < T > clazz ) { if ( StringUtils . isBlank ( json ) ) { log . warn ( "json is blank. " ) ; return Optional . empty ( ) ; } try { OBJECT_MAPPER . configure ( DeserializationFeature . FAIL_ON_UNKNOWN_PROPERTIES , false ) ; return Optional . of ( OBJECT_MAPPER . readValue ( json , clazz ) ) ; } catch ( Exception e ) { log . error ( e . getMessage ( ) , e ) ; return Optional . empty ( ) ; } } | json to object | 140 | 3 |
5,323 | public static < T > String toJson ( T t ) { if ( Objects . isNull ( t ) ) { log . warn ( "t is blank. " ) ; return "" ; } try { return OBJECT_MAPPER . writeValueAsString ( t ) ; } catch ( Exception e ) { log . error ( e . getMessage ( ) , e ) ; return "" ; } } | object to json | 84 | 3 |
5,324 | @ RequestMapping ( produces = MediaType . APPLICATION_JSON_VALUE , value = "/parse" , method = RequestMethod . GET ) public String parse ( @ RequestParam ( "sentence" ) String sentence , HttpServletRequest request ) { if ( sentence == null || sentence . trim ( ) . isEmpty ( ) ) { return StringUtils . EMPTY ; } sentence = sentence . trim ( ) ; LOGGER . info ( "Parse [" + sentence + "]" ) ; DTree tree = PARSER . parse ( sentence ) ; DTreeEntity parseTreeEntity = new DTreeEntity ( tree , "SAMPLE_AUTHOR" ) ; dNodeEntityRepository . save ( parseTreeEntity . dNodeEntities ) ; dTreeEntityRepository . save ( parseTreeEntity ) ; return "[" + toJSON ( tree . get ( 0 ) ) + "]" ; } | Parse sentence . | 192 | 4 |
5,325 | public static < T > T resolveValue ( ValueExpression expression , Class < T > type , ELContext elContext ) { if ( expression == null ) { return null ; } else { return type . cast ( expression . getValue ( elContext ) ) ; } } | Evaluates an expression then casts to the provided type . | 56 | 12 |
5,326 | public static < T > T resolveValue ( Object value , Class < T > type , ELContext elContext ) { if ( value == null ) { return null ; } else if ( value instanceof ValueExpression ) { return resolveValue ( ( ValueExpression ) value , type , elContext ) ; } else { return type . cast ( value ) ; } } | Casts or evaluates an expression then casts to the provided type . | 76 | 13 |
5,327 | public static MediaValidator getMediaValidator ( MediaType contentType , Writer out ) throws MediaException { // If the existing out is already validating for this type, use it. // This occurs when one validation validates to a set of characters that are a subset of the requested validator. // For example, a URL is always valid TEXT. if ( out instanceof MediaValidator ) { MediaValidator inputValidator = ( MediaValidator ) out ; if ( inputValidator . isValidatingMediaInputType ( contentType ) ) return inputValidator ; } // Add filter if needed for the given type switch ( contentType ) { case JAVASCRIPT : case JSON : case LD_JSON : return new JavaScriptValidator ( out , contentType ) ; case SH : return new ShValidator ( out ) ; case MYSQL : return new MysqlValidator ( out ) ; case PSQL : return new PsqlValidator ( out ) ; case TEXT : return new TextValidator ( out ) ; case URL : return new UrlValidator ( out ) ; case XHTML : return new XhtmlValidator ( out ) ; case XHTML_ATTRIBUTE : return new XhtmlAttributeValidator ( out ) ; default : throw new MediaException ( ApplicationResources . accessor . getMessage ( "MediaValidator.unableToFindValidator" , contentType . getContentType ( ) ) ) ; } } | Gets the media validator for the given type . If the given writer is already validator for the requested type will return the provided writer . | 304 | 29 |
5,328 | @ Override public boolean getAllowRobots ( ServletContext servletContext , HttpServletRequest request , HttpServletResponse response , Page page ) { return false ; } | No robots for transient Git status . | 39 | 7 |
5,329 | public Map < String , Double > convertMapKey ( Map < Integer , Double > probs ) { Map < String , Double > stringKeyProb = new HashMap <> ( ) ; probs . entrySet ( ) . forEach ( e -> stringKeyProb . put ( getLabel ( e . getKey ( ) ) , e . getValue ( ) ) ) ; return stringKeyProb ; } | Convert Index to actual string . | 87 | 7 |
5,330 | public static List < ValidationMessage > validateMediaType ( TagData data , List < ValidationMessage > messages ) { Object o = data . getAttribute ( "type" ) ; if ( o != null && o != TagData . REQUEST_TIME_VALUE && ! ( o instanceof MediaType ) ) { String type = Coercion . toString ( o ) ; try { // First allow shortcuts (matching enum names) MediaType mediaType = MediaType . getMediaTypeByName ( type ) ; if ( mediaType == null ) { mediaType = MediaType . getMediaTypeForContentType ( type ) ; } // Value is OK } catch ( MediaException err ) { messages = MinimalList . add ( messages , new ValidationMessage ( data . getId ( ) , err . getMessage ( ) ) ) ; } } return messages ; } | Checks that a type is a valid MediaType . | 182 | 11 |
5,331 | public static List < ValidationMessage > validateScope ( TagData data , List < ValidationMessage > messages ) { Object o = data . getAttribute ( "scope" ) ; if ( o != null && o != TagData . REQUEST_TIME_VALUE ) { String scope = Coercion . toString ( o ) ; try { Scope . getScopeId ( scope ) ; // Value is OK } catch ( JspTagException err ) { messages = MinimalList . add ( messages , new ValidationMessage ( data . getId ( ) , err . getMessage ( ) ) ) ; } } return messages ; } | Checks that a scope is a valid . | 131 | 9 |
5,332 | public static double distanceKms ( BigDecimal lat1 , BigDecimal lng1 , BigDecimal lat2 , BigDecimal lng2 ) { return new GeoCoordinate ( lat1 , lng1 ) . distanceTo ( new GeoCoordinate ( lat2 , lng2 ) ) ; } | Equitorial Radius of Earth . | 66 | 7 |
5,333 | public static < K , V > MapBuilder < K , V > map ( Map < K , V > instance ) { return new MapBuilder <> ( instance ) ; } | Creates a MapBuilder around the passed instance | 36 | 9 |
5,334 | public void run ( final List < Tuple > data ) { List < Tuple > dataCopy = new ArrayList <> ( data ) ; this . labels = data . parallelStream ( ) . map ( x -> x . label ) . collect ( Collectors . toSet ( ) ) ; if ( shuffleData ) { Collections . shuffle ( dataCopy ) ; } int chunkSize = data . size ( ) / nfold ; int reminder = data . size ( ) % chunkSize ; for ( int i = data . size ( ) - 1 ; i > data . size ( ) - 1 - reminder ; i -- ) { LOG . info ( "Dropping the tail id: " + data . get ( i ) . id ) ; } for ( int i = 0 ; i < nfold ; i ++ ) { System . err . println ( "Cross validation round " + ( i + 1 ) + "/" + nfold ) ; List < Tuple > testing = new ArrayList <> ( data . subList ( i , i + chunkSize ) ) ; List < Tuple > training = new ArrayList <> ( data . subList ( 0 , i ) ) ; training . addAll ( data . subList ( i + chunkSize , data . size ( ) ) ) ; eval ( training , testing , i ) ; } } | Cross validation . | 280 | 3 |
5,335 | private void eval ( List < Tuple > training , List < Tuple > testing , int nfold ) { classifier . train ( training ) ; for ( Tuple tuple : testing ) { String actual = classifier . predict ( tuple ) . entrySet ( ) . stream ( ) . max ( ( e1 , e2 ) -> e1 . getValue ( ) . compareTo ( e2 . getValue ( ) ) ) . map ( Map . Entry :: getKey ) . orElse ( StringUtils . EMPTY ) ; updateScore ( tuple , actual , nfold ) ; } } | This is for one fold . | 125 | 6 |
5,336 | private static String filter ( String s ) { int len = s . length ( ) ; StringBuilder filtered = new StringBuilder ( len ) ; int pos = 0 ; while ( pos < len ) { char ch1 = s . charAt ( pos ++ ) ; if ( Character . isHighSurrogate ( ch1 ) ) { // Handle surrogates if ( pos < len ) { char ch2 = s . charAt ( pos ++ ) ; if ( Character . isLowSurrogate ( ch2 ) ) { if ( isValidCharacter ( Character . toCodePoint ( ch1 , ch2 ) ) ) { filtered . append ( ch1 ) . append ( ch2 ) ; } } else { // High surrogate not followed by low surrogate, invalid } } else { // High surrogate at end of string, invalid } } else { // Not surrogates if ( isValidCharacter ( ch1 ) ) { filtered . append ( ch1 ) ; } } } assert filtered . length ( ) <= len ; return filtered . length ( ) != len ? filtered . toString ( ) : s ; } | Filters invalid XML characters . | 228 | 6 |
5,337 | protected void doTag ( Writer out ) throws JspException , IOException { JspFragment body = getJspBody ( ) ; if ( body != null ) { // Check for JspWriter to avoid a JspWriter wrapping a JspWriter body . invoke ( ( out instanceof JspWriter ) ? null : out ) ; } } | Once the out JspWriter has been replaced to output the proper content type this version of invoke is called . | 73 | 22 |
5,338 | private static BinRelation extractPolar ( DTree tree ) { // TODO: HERE. DNode rootVerb = tree . getRoots ( ) . get ( 0 ) ; // rootVerb.getChildren(). BinRelation binRelation = new BinRelation ( ) ; return binRelation ; } | POLAR doesnt have wildcard . | 67 | 7 |
5,339 | private Event createEvent ( String obs ) { int lastSpace = obs . lastIndexOf ( StringUtils . SPACE ) ; Event event = null ; if ( lastSpace != - 1 ) { String label = obs . substring ( lastSpace + 1 ) ; String [ ] contexts = obs . substring ( 0 , lastSpace ) . split ( "\\s+" ) ; // Split name and value float [ ] values = RealValueFileEventStream . parseContexts ( contexts ) ; // Label, feature name, feature value event = new Event ( label , contexts , values ) ; } return event ; } | away pdiff = 9 . 6875 ptwins = 0 . 5 lose | 127 | 16 |
5,340 | public static List < CoreLabel > stanfordTokenize ( String str ) { TokenizerFactory < ? extends HasWord > tf = PTBTokenizer . coreLabelFactory ( ) ; // ptb3Escaping=false -> '(' not converted as '-LRB-', Dont use it, it will cause Dependency resolution err. Tokenizer < ? extends HasWord > originalWordTokenizer = tf . getTokenizer ( new StringReader ( str ) , "ptb3Escaping=false" ) ; Tokenizer < ? extends HasWord > tokenizer = tf . getTokenizer ( new StringReader ( str ) ) ; List < ? extends HasWord > originalTokens = originalWordTokenizer . tokenize ( ) ; List < ? extends HasWord > tokens = tokenizer . tokenize ( ) ; // Curse you Stanford! List < CoreLabel > coreLabels = new ArrayList <> ( tokens . size ( ) ) ; for ( int i = 0 ; i < tokens . size ( ) ; i ++ ) { CoreLabel coreLabel = new CoreLabel ( ) ; coreLabel . setWord ( tokens . get ( i ) . word ( ) ) ; coreLabel . setOriginalText ( originalTokens . get ( i ) . word ( ) ) ; coreLabel . setValue ( tokens . get ( i ) . word ( ) ) ; coreLabel . setBeginPosition ( ( ( CoreLabel ) tokens . get ( i ) ) . beginPosition ( ) ) ; coreLabel . setEndPosition ( ( ( CoreLabel ) tokens . get ( i ) ) . endPosition ( ) ) ; coreLabels . add ( coreLabel ) ; } return coreLabels ; } | 1 . Tokenize | 353 | 4 |
5,341 | public void tagPOS ( List < CoreLabel > tokens ) { if ( posTagger == null ) { if ( POS_TAGGER_MODEL_PATH == null ) { LOG . warn ( "Default POS Tagger model" ) ; POS_TAGGER_MODEL_PATH = StanfordConst . STANFORD_DEFAULT_POS_EN_MODEL ; } posTagger = new MaxentTagger ( POS_TAGGER_MODEL_PATH ) ; } List < TaggedWord > posList = posTagger . tagSentence ( tokens ) ; for ( int i = 0 ; i < tokens . size ( ) ; i ++ ) { String pos = posList . get ( i ) . tag ( ) ; tokens . get ( i ) . setTag ( pos ) ; } } | 2 . POS Tagger | 170 | 5 |
5,342 | public static void tagLemma ( List < CoreLabel > tokens ) { // Not sure if this can be static. Morphology morpha = new Morphology ( ) ; for ( CoreLabel token : tokens ) { String lemma ; String pos = token . tag ( ) ; if ( pos . equals ( LangLib . POS_NNPS ) ) { pos = LangLib . POS_NNS ; } if ( pos . length ( ) > 0 ) { String phrasalVerb = phrasalVerb ( morpha , token . word ( ) , pos ) ; if ( phrasalVerb == null ) { lemma = morpha . lemma ( token . word ( ) , pos ) ; } else { lemma = phrasalVerb ; } } else { lemma = morpha . stem ( token . word ( ) ) ; } // LGLibEn.convertUnI only accept cap I. if ( lemma . equals ( "i" ) ) { lemma = "I" ; } token . setLemma ( lemma ) ; } } | 3 . Lemma Tagger | 231 | 6 |
5,343 | private Tuple < String , String > getOverrideEntry ( final String key ) { for ( String prefix : _overrides ) { String override = prefix + "." + key ; String value = getPropertyValue ( override ) ; if ( value != null ) { return new Tuple < String , String > ( override , value ) ; } } return null ; } | Looks for each instance of key in all of the overrides in order . Does not look for a non - overridden version . | 76 | 26 |
5,344 | private Tuple < String , String > getEntry ( final String key ) { Tuple < String , String > override = getOverrideEntry ( key ) ; if ( override == null ) { String value = getPropertyValue ( key ) ; if ( value != null ) { return new Tuple < String , String > ( key , value ) ; } } return override ; } | Searches for a property of key using all overrides | 77 | 12 |
5,345 | private Tuple < String , String > getEntry ( final String key , final Collection < String > prefixes ) { if ( CollectionUtils . isEmpty ( prefixes ) ) { return getEntry ( key ) ; } for ( String prefix : prefixes ) { String prefixedKey = prefix + "." + key ; Tuple < String , String > override = getOverrideEntry ( prefixedKey ) ; if ( override != null ) { return override ; } // // Above we were checking overrides of the override. Here, // just check for the first override. If that doesn't work, // then we need to just pass it on and ignore the specified override. // String value = getPropertyValue ( prefixedKey ) ; if ( value != null ) { return new Tuple < String , String > ( prefixedKey , value ) ; } } // // No prefixed overrides were found, so drop back to using // the standard, non-prefixed version // return getEntry ( key ) ; } | Searches for a property of key with all overrides using the specified prefixes | 211 | 17 |
5,346 | @ Bean public LocalContainerEntityManagerFactoryBean entityManagerFactory ( ) { HibernateJpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter ( ) ; jpaVendorAdapter . setDatabase ( Database . H2 ) ; jpaVendorAdapter . setGenerateDdl ( true ) ; LocalContainerEntityManagerFactoryBean entityManagerFactory = new LocalContainerEntityManagerFactoryBean ( ) ; entityManagerFactory . setPackagesToScan ( this . getClass ( ) . getPackage ( ) . getName ( ) ) ; entityManagerFactory . setJpaVendorAdapter ( jpaVendorAdapter ) ; entityManagerFactory . setDataSource ( dataSource ( ) ) ; return entityManagerFactory ; } | Entity manager factory . | 162 | 4 |
5,347 | @ Bean public ServletRegistrationBean h2servletRegistration ( ) { ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean ( new WebServlet ( ) ) ; servletRegistrationBean . addUrlMappings ( "/h2/*" ) ; return servletRegistrationBean ; } | H2 console . | 68 | 4 |
5,348 | public static void setAttribute ( PageContext pageContext , String scope , String name , Object value ) throws JspTagException { pageContext . setAttribute ( name , value , Scope . getScopeId ( scope ) ) ; } | Sets an attribute in the provided textual scope . | 47 | 10 |
5,349 | public static Object findObject ( PageContext pageContext , String scope , String name , String property , boolean beanRequired , boolean valueRequired ) throws JspTagException { try { // Check the name if ( name == null ) throw new AttributeRequiredException ( "name" ) ; // Find the bean Object bean ; if ( scope == null ) bean = pageContext . findAttribute ( name ) ; else bean = pageContext . getAttribute ( name , Scope . getScopeId ( scope ) ) ; // Check required if ( bean == null ) { if ( beanRequired ) { // null and required if ( scope == null ) throw new LocalizedJspTagException ( ApplicationResources . accessor , "PropertyUtils.bean.required.nullScope" , name ) ; else throw new LocalizedJspTagException ( ApplicationResources . accessor , "PropertyUtils.bean.required.scope" , name , scope ) ; } else { // null and not required return null ; } } else { if ( property == null ) { // No property lookup, use the bean directly return bean ; } else { // Find the property Object value = org . apache . commons . beanutils . PropertyUtils . getProperty ( bean , property ) ; if ( valueRequired && value == null ) { // null and required if ( scope == null ) throw new LocalizedJspTagException ( ApplicationResources . accessor , "PropertyUtils.value.required.nullScope" , property , name ) ; else throw new LocalizedJspTagException ( ApplicationResources . accessor , "PropertyUtils.value.required.scope" , property , name , scope ) ; } return value ; } } } catch ( IllegalAccessException | InvocationTargetException | NoSuchMethodException err ) { throw new JspTagException ( err ) ; } } | Gets the object given its scope name and optional property . | 385 | 12 |
5,350 | public static int getScopeId ( String scope ) throws JspTagException { if ( scope == null || PAGE . equals ( scope ) ) return PageContext . PAGE_SCOPE ; else if ( REQUEST . equals ( scope ) ) return PageContext . REQUEST_SCOPE ; else if ( SESSION . equals ( scope ) ) return PageContext . SESSION_SCOPE ; else if ( APPLICATION . equals ( scope ) ) return PageContext . APPLICATION_SCOPE ; else throw new LocalizedJspTagException ( ApplicationResources . accessor , "Scope.scope.invalid" , scope ) ; } | Gets the PageContext scope value for the textual scope name . | 130 | 13 |
5,351 | public static Map < String , String > bundleToStringMap ( final ResourceBundle bundle , final String suffix ) { if ( bundle == null ) { return Collections . < String , String > emptyMap ( ) ; } String theSuffix ; if ( StringUtils . isEmpty ( suffix ) ) { theSuffix = "" ; } else { theSuffix = suffix + "." ; } Map < String , String > map = new LinkedHashMap < String , String > ( ) ; Enumeration < String > keys = bundle . getKeys ( ) ; while ( keys . hasMoreElements ( ) ) { String key = keys . nextElement ( ) ; Object value = bundle . getObject ( key ) ; String strValue = ( value != null ) ? value . toString ( ) : null ; map . put ( theSuffix + key , strValue ) ; } return map ; } | Converts a resource bundle to a map after prepending suffix + . to each property | 193 | 17 |
5,352 | @ Override public Map < String , Double > predict ( Tuple predict ) { KNNEngine engine = new KNNEngine ( predict , trainingData , k ) ; if ( mode == 1 ) { engine . getDistance ( engine . chebyshevDistance ) ; } else if ( mode == 2 ) { engine . getDistance ( engine . manhattanDistance ) ; } else { engine . getDistance ( engine . euclideanDistance ) ; } predict . label = engine . getResult ( ) ; Map < String , Double > outputMap = new ConcurrentHashMap <> ( ) ; trainingData . parallelStream ( ) . forEach ( x -> outputMap . put ( String . valueOf ( x . id ) , ( Double ) x . getExtra ( ) . get ( DISTANCE ) ) ) ; return outputMap ; } | Return the predict to every other train vector s distance . | 177 | 11 |
5,353 | public void purge ( ) { WeakElement < ? > element ; while ( ( element = ( WeakElement < ? > ) _queue . poll ( ) ) != null ) { _set . remove ( element ) ; } } | Removes all garbage - collected elements from this set | 46 | 10 |
5,354 | @ Override public ISeqClassifier train ( List < SequenceTuple > trainingData ) { if ( trainingData == null || trainingData . size ( ) == 0 ) { LOG . warn ( "Training data is empty." ) ; return this ; } if ( modelPath == null ) { try { modelPath = Files . createTempDirectory ( "crfsuite" ) . toAbsolutePath ( ) . toString ( ) ; } catch ( IOException e ) { LOG . error ( "Create temp directory failed." , e ) ; e . printStackTrace ( ) ; } } Pair < List < ItemSequence > , List < StringList > > crfCompatibleTrainingData = loadTrainingData ( trainingData ) ; Trainer trainer = new Trainer ( ) ; String algorithm = ( String ) props . getOrDefault ( "algorithm" , DEFAULT_ALGORITHM ) ; props . remove ( "algorithm" ) ; String graphicalModelType = ( String ) props . getOrDefault ( "graphicalModelType" , DEFAULT_GRAPHICAL_MODEL_TYPE ) ; props . remove ( "graphicalModelType" ) ; trainer . select ( algorithm , graphicalModelType ) ; // Set parameters props . entrySet ( ) . forEach ( pair -> trainer . set ( ( String ) pair . getKey ( ) , ( String ) pair . getValue ( ) ) ) ; // Add training data into the trainer for ( int i = 0 ; i < trainingData . size ( ) ; i ++ ) { // Use group id = 0 but the API doesn't say what it is used for :( trainer . append ( crfCompatibleTrainingData . getLeft ( ) . get ( i ) , crfCompatibleTrainingData . getRight ( ) . get ( i ) , 0 ) ; } // Start training without hold-outs. trainer.message() // will be called to report the training process trainer . train ( modelPath , - 1 ) ; return this ; } | Train CRF Suite with annotated item sequences . | 421 | 10 |
5,355 | public static String getSignatureBaseString ( String requestMethod , String requestUrl , Map < String , String > protocolParameters ) throws AuthException { StringBuilder sb = new StringBuilder ( ) ; sb . append ( requestMethod . toUpperCase ( ) ) . append ( "&" ) . append ( AuthUtils . percentEncode ( normalizeUrl ( requestUrl ) ) ) . append ( "&" ) . append ( AuthUtils . percentEncode ( normalizeParameters ( requestUrl , protocolParameters ) ) ) ; return sb . toString ( ) ; } | Returns a signature base string . The signature base string is a consistent reproducible concatenation of several of the HTTP request elements into a single string . | 124 | 30 |
5,356 | public static List < Tuple > createBalancedTrainingData ( final List < Tuple > trainingData ) { Map < String , Long > tagCount = trainingData . parallelStream ( ) . map ( x -> new AbstractMap . SimpleImmutableEntry <> ( x . label , 1 ) ) . collect ( Collectors . groupingBy ( Map . Entry :: getKey , Collectors . counting ( ) ) ) ; Map . Entry < String , Long > minCountEntry = tagCount . entrySet ( ) . stream ( ) . min ( Comparator . comparing ( Map . Entry :: getValue ) ) . orElse ( null ) ; tagCount . clear ( ) ; List < Tuple > newData = new ArrayList <> ( ) ; for ( Tuple t : trainingData ) { String label = t . label ; if ( ! tagCount . containsKey ( label ) ) { tagCount . put ( t . label , 0L ) ; } if ( tagCount . get ( label ) < minCountEntry . getValue ( ) ) { tagCount . put ( label , tagCount . get ( label ) + 1 ) ; newData . add ( t ) ; } } return newData ; } | Regardless of the label just consider isPosExample and !isPosExample | 253 | 14 |
5,357 | public static Pair < List < Tuple > , List < Tuple > > splitData ( final List < Tuple > trainingData , double proportion ) { if ( proportion < 0 || proportion > 1 ) { throw new RuntimeException ( "Proportion should between 0.0 - 1.0" ) ; } if ( proportion > 0.5 ) { proportion = 1 - proportion ; } List < Tuple > smallList = new ArrayList <> ( ) ; List < Tuple > largeList = new ArrayList <> ( ) ; int smallListSize = ( int ) Math . floor ( proportion * trainingData . size ( ) ) ; int ct = 0 ; Set < Integer > indices = new HashSet <> ( ) ; while ( ct < smallListSize && trainingData . size ( ) > indices . size ( ) ) { int index = ( int ) ( Math . random ( ) * ( trainingData . size ( ) - 1 ) ) ; while ( indices . contains ( index ) ) { index = ( int ) ( Math . random ( ) * ( trainingData . size ( ) - 1 ) ) ; } indices . add ( index ) ; ct ++ ; } smallList . addAll ( indices . stream ( ) . map ( trainingData :: get ) . collect ( Collectors . toList ( ) ) ) ; IntStream . range ( 0 , trainingData . size ( ) ) . filter ( x -> ! indices . contains ( x ) ) . forEach ( i -> largeList . add ( trainingData . get ( i ) ) ) ; return new ImmutablePair <> ( smallList , largeList ) ; } | Shuffle the data and split by proportion | 346 | 8 |
5,358 | public void calculateLabelPrior ( ) { double prior = 1D / model . labelIndexer . getLabelSize ( ) ; model . labelIndexer . getIndexSet ( ) . forEach ( labelIndex -> model . labelPrior . put ( labelIndex , prior ) ) ; } | Assume labels have equal probability . Not depends on the training data size . | 59 | 15 |
5,359 | public static void writeWithMarkup ( Object value , MarkupType markupType , MediaEncoder encoder , Writer out ) throws IOException { if ( encoder == null ) { writeWithMarkup ( value , markupType , out ) ; } else { if ( value != null ) { if ( value instanceof Writable && ! ( ( Writable ) value ) . isFastToString ( ) ) { // Avoid intermediate String from Writable Coercion . write ( value , encoder , out ) ; } else { String str = Coercion . toString ( value ) ; BundleLookupMarkup lookupMarkup ; BundleLookupThreadContext threadContext = BundleLookupThreadContext . getThreadContext ( false ) ; if ( threadContext != null ) { lookupMarkup = threadContext . getLookupMarkup ( str ) ; } else { lookupMarkup = null ; } if ( lookupMarkup != null ) lookupMarkup . appendPrefixTo ( markupType , encoder , out ) ; encoder . write ( str , out ) ; if ( lookupMarkup != null ) lookupMarkup . appendSuffixTo ( markupType , encoder , out ) ; } } } } | Writes a value with markup enabled using the provided encoder . | 254 | 13 |
5,360 | public void buildModel ( String wordFileName ) throws IOException { BufferedReader br = new BufferedReader ( new FileReader ( new File ( wordFileName ) ) ) ; String str ; while ( ( str = br . readLine ( ) ) != null ) { List < String > tokens = StanfordParser . stanfordTokenize ( str ) . stream ( ) . map ( CoreLabel :: originalText ) . collect ( Collectors . toList ( ) ) ; for ( String word : tokens ) { double count = model . wordProbability . containsKey ( word ) ? model . wordProbability . get ( word ) : 0 ; count ++ ; model . wordProbability . put ( word , count ) ; } } br . close ( ) ; // Remove Empty prob. model . wordProbability . remove ( StringUtils . EMPTY ) ; model . normalizeWordProbability ( ) ; } | Parser here is just for tokenize . | 196 | 8 |
5,361 | public static int ipv4ToInt ( final Inet4Address addr ) { int value = 0 ; for ( byte chunk : addr . getAddress ( ) ) { value <<= 8 ; value |= chunk & 0xff ; } return value ; } | Turns an Inet4Address into a 32 - bit integer representation | 53 | 14 |
5,362 | public static < E > List < E > empty ( List < E > list ) { return Optional . ofNullable ( list ) . orElse ( newArrayList ( ) ) ; } | null to empty list | 39 | 4 |
5,363 | public static < E > List < E > clean ( List < E > list ) { if ( iterable ( list ) ) { return list . stream ( ) . filter ( e -> { if ( Objects . isNull ( e ) ) { return false ; } if ( e instanceof Nullable ) { return ! ( ( Nullable ) e ) . isNull ( ) ; } return true ; } ) . collect ( Collectors . toList ( ) ) ; } return list ; } | clean null element | 101 | 3 |
5,364 | public ChainWriter encodeJavaScriptStringInXmlAttribute ( Object value ) throws IOException { // Two stage encoding: // 1) Text -> JavaScript (with quotes added) // 2) JavaScript -> XML Attribute if ( value instanceof Writable && ! ( ( Writable ) value ) . isFastToString ( ) ) { // Avoid unnecessary toString calls textInJavaScriptEncoder . writePrefixTo ( javaScriptInXhtmlAttributeWriter ) ; Coercion . write ( value , textInJavaScriptEncoder , javaScriptInXhtmlAttributeWriter ) ; textInJavaScriptEncoder . writeSuffixTo ( javaScriptInXhtmlAttributeWriter ) ; } else { String str = Coercion . toString ( value ) ; BundleLookupMarkup lookupMarkup ; BundleLookupThreadContext threadContext = BundleLookupThreadContext . getThreadContext ( false ) ; if ( threadContext != null ) { lookupMarkup = threadContext . getLookupMarkup ( str ) ; } else { lookupMarkup = null ; } if ( lookupMarkup != null ) lookupMarkup . appendPrefixTo ( MarkupType . JAVASCRIPT , javaScriptInXhtmlAttributeWriter ) ; textInJavaScriptEncoder . writePrefixTo ( javaScriptInXhtmlAttributeWriter ) ; textInJavaScriptEncoder . write ( str , javaScriptInXhtmlAttributeWriter ) ; textInJavaScriptEncoder . writeSuffixTo ( javaScriptInXhtmlAttributeWriter ) ; if ( lookupMarkup != null ) lookupMarkup . appendSuffixTo ( MarkupType . JAVASCRIPT , javaScriptInXhtmlAttributeWriter ) ; } return this ; } | Encodes a javascript string for use in an XML attribute context . Quotes are added around the string . Also if the string is translated comments will be added giving the translation lookup id to aid in translation of server - translated values in JavaScript . | 362 | 48 |
5,365 | @ Deprecated public ChainWriter printEU ( String value ) { int len = value . length ( ) ; for ( int c = 0 ; c < len ; c ++ ) { char ch = value . charAt ( c ) ; if ( ch == ' ' ) out . print ( ' ' ) ; else { if ( ( ch >= ' ' && ch <= ' ' ) || ( ch >= ' ' && ch <= ' ' ) || ( ch >= ' ' && ch <= ' ' ) ) out . print ( ch ) ; else { out . print ( ' ' ) ; out . print ( getHex ( ch >>> 4 ) ) ; out . print ( getHex ( ch ) ) ; } } } return this ; } | Prints a value that may be placed in a URL . | 153 | 12 |
5,366 | public static void writeHtmlImagePreloadJavaScript ( String url , Appendable out ) throws IOException { out . append ( "<script type='text/javascript'>\n" + " var img=new Image();\n" + " img.src=\"" ) ; // Escape for javascript StringBuilder javascript = new StringBuilder ( url . length ( ) ) ; encodeTextInJavaScript ( url , javascript ) ; // Encode for XML attribute encodeJavaScriptInXhtmlAttribute ( javascript , out ) ; out . append ( "\";\n" + "</script>" ) ; } | Prints a JavaScript script that will preload the image at the provided URL . | 126 | 16 |
5,367 | public Map < String , String > split ( final CharSequence source ) { java . util . Objects . requireNonNull ( source , "source" ) ; Map < String , String > parameters = new HashMap <> ( ) ; Iterator < String > i = new StringIterator ( source , pairSeparator ) ; while ( i . hasNext ( ) ) { String keyValue = i . next ( ) ; int keyValueSeparatorPosition = keyValueSeparatorStart ( keyValue ) ; if ( keyValueSeparatorPosition == 0 || keyValue . length ( ) == 0 ) { continue ; } if ( keyValueSeparatorPosition < 0 ) { parameters . put ( keyValue , null ) ; continue ; } int keyStart = 0 ; int keyEnd = keyValueSeparatorPosition ; while ( keyStart < keyEnd && keyTrimMatcher . matches ( keyValue . charAt ( keyStart ) ) ) { keyStart ++ ; } while ( keyStart < keyEnd && keyTrimMatcher . matches ( keyValue . charAt ( keyEnd - 1 ) ) ) { keyEnd -- ; } int valueStart = keyValueSeparatorPosition + keyValueSeparator . length ( ) ; int valueEnd = keyValue . length ( ) ; while ( valueStart < valueEnd && valueTrimMatcher . matches ( keyValue . charAt ( valueStart ) ) ) { valueStart ++ ; } while ( valueStart < valueEnd && valueTrimMatcher . matches ( keyValue . charAt ( valueEnd - 1 ) ) ) { valueEnd -- ; } String key = keyValue . substring ( keyStart , keyEnd ) ; String value = keyValue . substring ( valueStart , valueEnd ) ; parameters . put ( key , value ) ; } return parameters ; } | Splits the CharSequence passed in parameter . | 387 | 10 |
5,368 | public Splitter trim ( char c ) { Matcher matcher = new CharMatcher ( c ) ; return new Splitter ( pairSeparator , keyValueSeparator , matcher , matcher ) ; } | Returns a splitter that removes all leading or trailing characters matching the given character from each returned key and value . | 46 | 22 |
5,369 | public Splitter trim ( char [ ] chars ) { Matcher matcher = new CharsMatcher ( chars ) ; return new Splitter ( pairSeparator , keyValueSeparator , matcher , matcher ) ; } | Returns a splitter that removes all leading or trailing characters matching the given characters from each returned key and value . | 49 | 22 |
5,370 | public void close ( ) { super . clear ( ) ; if ( indexMap != null ) { indexMap . clear ( ) ; indexMap = null ; } if ( this . indexStore != null ) { getIndexStore ( ) . close ( ) ; this . indexStore = null ; } if ( this . cacheStore != null ) { getCacheStore ( ) . close ( ) ; this . cacheStore = null ; } } | Ensure closing without opening should notbe an issue . | 90 | 11 |
5,371 | private double gaussianUpdate ( int predicate , int oid , double correctionConstant ) { double param = params [ predicate ] . getParameters ( ) [ oid ] ; double x0 = 0.0 ; double modelValue = modelExpects [ 0 ] [ predicate ] . getParameters ( ) [ oid ] ; double observedValue = observedExpects [ predicate ] . getParameters ( ) [ oid ] ; for ( int i = 0 ; i < 50 ; i ++ ) { double tmp = modelValue * Math . exp ( correctionConstant * x0 ) ; double f = tmp + ( param + x0 ) / sigma - observedValue ; double fp = tmp * correctionConstant + 1 / sigma ; if ( fp == 0 ) { break ; } double x = x0 - f / fp ; if ( Math . abs ( x - x0 ) < 0.000001 ) { x0 = x ; break ; } x0 = x ; } return x0 ; } | modeled on implementation in Zhang Le s maxent kit | 212 | 11 |
5,372 | @ Override final public void writeSuffixTo ( Appendable out ) throws IOException { writeSuffix ( buffer , out ) ; buffer . setLength ( 0 ) ; } | Writes the suffix and clears the buffer for reuse . | 40 | 11 |
5,373 | public static < T > T nullIfEmpty ( T value ) throws IOException { return isEmpty ( value ) ? null : value ; } | Returns the provided value or null if the value is empty . | 29 | 12 |
5,374 | public static String toCapCase ( final String string ) { if ( string == null ) { return null ; } if ( string . length ( ) == 1 ) { return string . toUpperCase ( ) ; } return Character . toUpperCase ( string . charAt ( 0 ) ) + string . substring ( 1 ) . toLowerCase ( ) ; } | Takes the first letter and capitalizes it . | 77 | 10 |
5,375 | public static void appendToBuffer ( final StringBuffer buffer , final String string , final String delimiter ) { if ( string == null ) { return ; } // // Only append the delimiter in front if the buffer isn't empty. // if ( buffer . length ( ) == 0 || delimiter == null ) { buffer . append ( string ) ; } else { buffer . append ( delimiter ) . append ( string ) ; } } | Appends a string to a buffer prepending with a delimiter string . For instance this may be used to create a string of comma - delimited values . This does not ignore empty strings . | 90 | 39 |
5,376 | @ SafeVarargs public static < T > T coalesce ( final T ... things ) { if ( things == null || things . length == 0 ) { return null ; } for ( T thing : things ) { if ( thing != null ) { return thing ; } } return null ; } | Similar to SQL coalesce returns the first non - null argument | 60 | 12 |
5,377 | @ SafeVarargs public static < T > T coalesceNonEmpty ( final T ... things ) { if ( things == null || things . length == 0 ) { return null ; } for ( T thing : things ) { if ( thing instanceof CharSequence ) { if ( ! StringUtils . isBlank ( ( CharSequence ) thing ) ) { return thing ; } } else if ( thing != null ) { return thing ; } } return null ; } | Similar to SQL coalesce returns the first non - EMPTY argument | 98 | 13 |
5,378 | @ Override public BeanInfo [ ] getAdditionalBeanInfo ( ) { try { return new BeanInfo [ ] { Introspector . getBeanInfo ( InputTag . class . getSuperclass ( ) ) } ; } catch ( IntrospectionException err ) { throw new AssertionError ( err ) ; } } | Include base class . | 68 | 5 |
5,379 | public static String fileNameFromString ( final String text ) { String value = text . replace ( ' ' , ' ' ) ; if ( value . length ( ) < 48 ) { return value ; } return value . substring ( 0 , 47 ) ; } | This method takes a string and converts it into a filename by replacing any spaces with underscores and then also truncating it if it is over a certain length . At the time of the writing of this comment that was 48 characters which was chosen by pulling a number out of my brain . | 54 | 56 |
5,380 | public static int executeToFile ( final String [ ] command , final File file ) throws IOException , InterruptedException { return executeToFile ( command , file , System . err ) ; } | Executes the given command redirecting stdout to the given file and stderr to actual stderr | 40 | 22 |
5,381 | public static int executeToFile ( final String [ ] command , final File file , final OutputStream stderr ) throws IOException , InterruptedException { return executeToStreams ( command , new FileOutputStream ( file ) , stderr ) ; } | Executes the given command redirecting stdout to the given file and stderr to the given stream | 54 | 21 |
5,382 | public static void makeSecurityCheck ( final File file , final File base ) { if ( ! file . getAbsolutePath ( ) . startsWith ( base . getAbsolutePath ( ) ) ) { throw new IllegalArgumentException ( "Illegal file path [" + file + "]" ) ; } } | Check to make sure the user has not put .. anywhere in the path to allow overwriting of system files . Throws IllegalArgumentException if it fails the check . | 64 | 35 |
5,383 | public void setLink ( Link link ) { setHref ( link . getHref ( ) ) ; setHrefAbsolute ( link . getHrefAbsolute ( ) ) ; HttpParameters linkParams = link . getParams ( ) ; if ( linkParams != null ) { for ( Map . Entry < String , List < String > > entry : linkParams . getParameterMap ( ) . entrySet ( ) ) { String paramName = entry . getKey ( ) ; for ( String paramValue : entry . getValue ( ) ) { addParam ( paramName , paramValue ) ; } } } this . addLastModified = link . getAddLastModified ( ) ; setHreflang ( link . getHreflang ( ) ) ; setRel ( link . getRel ( ) ) ; setType ( link . getType ( ) ) ; setMedia ( link . getMedia ( ) ) ; setTitle ( link . getTitle ( ) ) ; } | Copies all values from the provided link . | 211 | 9 |
5,384 | protected static void update ( String [ ] ec , Set < String > predicateSet , Map < String , Integer > counter , int cutoff ) { for ( String s : ec ) { Integer val = counter . get ( s ) ; val = val == null ? 1 : val + 1 ; counter . put ( s , val ) ; if ( ! predicateSet . contains ( s ) && counter . get ( s ) >= cutoff ) { predicateSet . add ( s ) ; } } } | Updates the set of predicated and counter with the specified event contexts and cutoff . | 100 | 17 |
5,385 | private GrammaticalStructure tagDependencies ( List < ? extends HasWord > taggedWords ) { GrammaticalStructure gs = nndepParser . predict ( taggedWords ) ; return gs ; } | 4 . Dependency Label | 44 | 5 |
5,386 | public String [ ] getFeatNames ( ) { String [ ] namesArray = new String [ nameIndexMap . size ( ) ] ; for ( Map . Entry < String , Integer > entry : nameIndexMap . entrySet ( ) ) { namesArray [ entry . getValue ( ) ] = entry . getKey ( ) ; } return namesArray ; } | This method should only be used for print or debug purpose . | 75 | 12 |
5,387 | public void tagPOS ( List < CoreLabel > tokens , Tree tree ) { try { List < TaggedWord > posList = tree . getChild ( 0 ) . taggedYield ( ) ; for ( int i = 0 ; i < tokens . size ( ) ; i ++ ) { String pos = posList . get ( i ) . tag ( ) ; tokens . get ( i ) . setTag ( pos ) ; } } catch ( Exception e ) { tagPOS ( tokens ) ; // At least gives you something. LOG . warn ( "POS Failed:\n" + tree . pennString ( ) ) ; } } | This is for the backward compatibility | 130 | 6 |
5,388 | public Pair < CoreMap , GrammaticalStructure > parseForCoref ( String sentence ) { List < CoreLabel > tokens = stanfordTokenize ( sentence ) ; Tree tree = parser . parse ( tokens ) ; GrammaticalStructure gs = tagDependencies ( tree , true ) ; tagPOS ( tokens ) ; tagLemma ( tokens ) ; tagNamedEntity ( tokens ) ; CoreMap result = new ArrayCoreMap ( ) ; result . set ( CoreAnnotations . TokensAnnotation . class , tokens ) ; result . set ( TreeCoreAnnotations . TreeAnnotation . class , tree ) ; GrammaticalStructure . Extras extras = GrammaticalStructure . Extras . NONE ; SemanticGraph deps = SemanticGraphFactory . generateCollapsedDependencies ( gs , extras ) ; SemanticGraph uncollapsedDeps = SemanticGraphFactory . generateUncollapsedDependencies ( gs , extras ) ; SemanticGraph ccDeps = SemanticGraphFactory . generateCCProcessedDependencies ( gs , extras ) ; result . set ( SemanticGraphCoreAnnotations . CollapsedDependenciesAnnotation . class , deps ) ; result . set ( SemanticGraphCoreAnnotations . BasicDependenciesAnnotation . class , uncollapsedDeps ) ; result . set ( SemanticGraphCoreAnnotations . CollapsedCCProcessedDependenciesAnnotation . class , ccDeps ) ; return new ImmutablePair <> ( result , gs ) ; } | This is for coref using . | 324 | 7 |
5,389 | public static boolean causedBy ( final Throwable ex , final Class < ? extends Throwable > exceptionClass ) { Throwable cause = ex ; while ( cause != null && ! exceptionClass . isInstance ( cause ) ) { cause = cause . getCause ( ) ; } return ( cause == null ) ? false : true ; } | Checks to see if this exception or any of its causes is an instance of the given throwable class | 68 | 21 |
5,390 | public static < T extends Throwable > T getCause ( final Throwable ex , final Class < T > exceptionClass ) { Throwable cause = ex ; while ( cause != null && ! exceptionClass . isInstance ( cause ) ) { cause = cause . getCause ( ) ; } return ( cause == null ) ? null : exceptionClass . cast ( cause ) ; } | Checks to see if an exception or any of its causes are of a certain type . Returns the first type in the chain if it exists or null if no causes are of this type . | 77 | 38 |
5,391 | public static String getMessage ( final Throwable ex ) { String message = ex . getMessage ( ) ; // // It *appears* as though the SQLException hasn't been // converted to nest? It seems like it has it's own // method of nesting, not sure why. I don't know // why Sun wouldn't have converted it. Maybe they // did, but just left these getNextException methods // on for compatibility? In my java source code, they // aren't deprecated, though. // if ( ex instanceof SQLException ) { String sqlMessage = getSqlExceptionMessage ( ( SQLException ) ex ) ; if ( ! StringUtils . isBlank ( sqlMessage ) ) { if ( ! StringUtils . isBlank ( message ) ) { message += "\n" + sqlMessage ; } else { message = sqlMessage ; } } } Throwable cause = ex . getCause ( ) ; if ( ex instanceof SamSixException && ( ( SamSixException ) ex ) . isShowThisCauseOnly ( ) ) { return message ; } if ( cause != null ) { String causeMessage = ExceptionUtils . getMessage ( cause ) ; if ( ! StringUtils . isBlank ( causeMessage ) ) { if ( ! StringUtils . isBlank ( message ) ) { // // ALWAYS use "broadest first" when showing error messages. // Otherwise, error messages end up with some non-human readable thing at the top, // confusing users with deeply technical details. This is especially important for user errors. // // broadest/non-broadest should be used for stack traces only. // message = message + "\n" + causeMessage ; } else { message = causeMessage ; } } } if ( ! StringUtils . isBlank ( message ) ) { return message ; } return ex . getClass ( ) . getName ( ) + ": An error has been detected." ; } | Static method to recursively get the message text from the entire stack . Messages are concatenated with the linefeed character . This allows for a more informational message to be displayed to the user . | 412 | 40 |
5,392 | public static String getNonce ( long issueTime ) { long currentTime = new Date ( ) . getTime ( ) ; return TimeUnit . MILLISECONDS . toSeconds ( currentTime - issueTime ) + ":" + Long . toString ( System . nanoTime ( ) ) ; } | Returns a nonce value . The nonce value MUST be unique across all requests with the same MAC key identifier . | 64 | 23 |
5,393 | public static Object invoke ( final Object obj , final String methodName , final Object param ) throws UtilException { // // This time we have a parameter passed in. // // We can therefore work out it's class (type) and pass // that through to our invokeOneArgMethod 'wrapper' method. // return invokeOneArgMethod ( obj , methodName , param , param . getClass ( ) ) ; } | Invoke an arbitrary one argument method on an arbitrary object and let the method work out the parameter s type . The user of this method had better be sure that the param isn t null . If there s a chance it is null then the invoke method with a paramType argument must be used . | 86 | 59 |
5,394 | public static Object invoke ( final Object obj , final String methodName , final Object param , final Class < ? > parameterType ) throws UtilException { // // For this call, we have all the information passed in to // this method for us to use. // // It may turn out to have the wrong contents etc. but the // final method to actually invoke the 'methodName' stated // here on the 'obj'(ect) stated here, will deal with that. // return invokeOneArgMethod ( obj , methodName , param , parameterType ) ; } | Invoke an arbitrary one argument method on an arbitrary object but also include the parameter s type . | 117 | 19 |
5,395 | public static Object invokeStaticMethod ( final Class < ? > objClass , final String methodName , final Object param ) throws UtilException { // // This time we have a parameter passed in. // // We can therefore work out it's class (type) and pass // that through to our invokeOneArgStaticMethod 'wrapper' method. // return invokeOneArgStaticMethod ( objClass , methodName , param , param . getClass ( ) ) ; } | Invoke an arbitrary one argument method on an arbitrary class and let the method work out the parameter s type . The user of this method had better be sure that the param isn t null . If there s a chance it is null then the invoke method with a paramType argument must be used . | 95 | 59 |
5,396 | public int runCommandLine ( ) throws IOException , InterruptedException { logRunnerConfiguration ( ) ; ProcessBuilder processBuilder = new ProcessBuilder ( getCommandLineArguments ( ) ) ; processBuilder . directory ( workingDirectory ) ; processBuilder . environment ( ) . putAll ( environmentVars ) ; Process commandLineProc = processBuilder . start ( ) ; final StreamPumper stdoutPumper = new StreamPumper ( commandLineProc . getInputStream ( ) , outputConsumer ) ; final StreamPumper stderrPumper = new StreamPumper ( commandLineProc . getErrorStream ( ) , errorConsumer ) ; stdoutPumper . start ( ) ; stderrPumper . start ( ) ; if ( standardInputString != null ) { OutputStream outputStream = commandLineProc . getOutputStream ( ) ; outputStream . write ( standardInputString . getBytes ( ) ) ; outputStream . close ( ) ; } int exitCode = commandLineProc . waitFor ( ) ; stdoutPumper . waitUntilDone ( ) ; stderrPumper . waitUntilDone ( ) ; if ( exitCode == 0 ) { LOGGER . fine ( processName + " returned zero exit code" ) ; } else { LOGGER . severe ( processName + " returned non-zero exit code (" + exitCode + ")" ) ; } return exitCode ; } | Execute the configured program | 296 | 5 |
5,397 | private File getDefaultOutputDirectory ( ) { //The default output directory is the configuration name String childOutputDirectory = getConfiguration ( ) ; //However, for platforms others than Win32, the default output directory becomes platform/configuration if ( ! getPlatform ( ) . equals ( "Win32" ) ) { childOutputDirectory = new File ( getPlatform ( ) , childOutputDirectory ) . getPath ( ) ; } //Place the default output directory within the appropriate base directory return new File ( getBaseDirectory ( ) , childOutputDirectory ) ; } | Retrieve the default output directory for the Visual C ++ project . | 114 | 13 |
5,398 | private File getBaseDirectory ( ) { File referenceFile = ( solutionFile != null ? solutionFile : getInputFile ( ) ) ; return referenceFile . getParentFile ( ) . getAbsoluteFile ( ) ; } | Retrieve the base directory for the Visual C ++ project . If this project is part of a Visual Studio solution the base directory is the solution directory ; for standalone projects the base directory is the project directory . | 46 | 41 |
5,399 | public String toSqlString ( Criteria criteria , CriteriaQuery criteriaQuery ) throws HibernateException { String [ ] columns ; try { columns = criteriaQuery . getColumnsUsingProjection ( criteria , propertyName ) ; } catch ( QueryException e ) { columns = new String [ 0 ] ; } // if there are columns that map the given property.. the property exists, so we don't need to add anything to the sql return columns . length > 0 ? "FALSE" : "TRUE" ; } | Render the SQL fragment that corresponds to this criterion . | 110 | 10 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.