long Words::isFloat ( long n, float& f) { char buf[128]; char *p = buf; long offset = 0; while(isPunct(n+offset) && !(m_words[n+offset][0] == '.' || m_words[n+offset][0] == '-')) offset++; while(isPunct(n+offset) && !(m_words[n+offset][0] == '.' || m_words[n+offset][0] == '-')) offset++; memcpy(buf, getWord(n), getWordLen(n)); buf[getWordLen(n)] = '\0'; log(LOG_WARN, "trying to get %s %li", buf, offset); if(isNum(n)) { if(1 + n < m_numWords && isPunct(n+1) && m_words[n+1][0] == '.') { if(2 + n < m_numWords && isNum(n+2)) { memcpy(p, m_words[n], m_wordLens[n]); p += m_wordLens[n]; memcpy(p, ".", 1); p++; memcpy(p, m_words[n+2], m_wordLens[n+2]); f = atof(buf); return 3 + offset; } else { return offset; } } else if(n > 0 && isPunct(n-1) && m_wordLens[n-1] > 0 && (m_words[n-1][m_wordLens[n-1]-1] == '.' || m_words[n-1][m_wordLens[n-1]-1] == '-')) { //hmm, we just skipped the period as punct? sprintf(buf, "0.%s",m_words[n]); f = atof(buf); return 1 + offset; } else { f = atof(m_words[n]); return 1 + offset; } } //does this have a period in front? if(isPunct(n) && (m_words[n][0] == '.' || m_words[n][0] == '-')) { if(1 + n < m_numWords && isNum(n+1)) { memcpy(p, m_words[n], m_wordLens[n]); p += m_wordLens[n]; memcpy(p, m_words[n+1], m_wordLens[n+1]); f = atof(buf); return 2 + offset; } } return offset; }
int main() { int state = LOOKINGFOR; char c, pC; for(; scanf("%c", &c) > 0 && c;) { if(state == PISPUNCT) { if(c == ' ') printf("%c", pC); else printf("%c ", pC); } if(isPunct(c)) { if(state == LOOKINGFOR) state = PISPUNCT; } else { if(state == PISPUNCT) state = LOOKINGFOR; printf("%c", c); } pC = c; } return(0); }
void paragraph::considerHyphenation(flags & flgs) { bool vowel = false; bool nonAlphaFound = false; int cnt = 0; int j = 0; int punkpos = -1; bool locAllLower = true; bool locAllUpper = true; for(j = 0;j != waited && isFlatSpace(circularBuffer[j]);++j) circularBuffer[j] = ' '; for(;j != waited;++j) { ++cnt; int k = circularBuffer[j]; if(isAlpha(k)) { if(!isLower(k)) locAllLower = false; if(!isUpper(k)) locAllUpper = false; } else if(k != '-') { if(punkpos < 0 && isPunct(k)) punkpos = cnt; else if(!isSemiPunct(k)) nonAlphaFound = true; break; } if(isVowel(k)) vowel = true; } if(dropHyphen) { // Require agreement of case if(allLower) { if(!locAllLower) dropHyphen = false; } else { if(!locAllUpper) dropHyphen = false; } } if((!nonAlphaFound && cnt >= 2 && punkpos < 0) || punkpos == cnt) { if(!dropHyphen || !vowel) Segment.Put(file,'-',flgs); for(j = 0;j != waited;++j) Segment.Put(file,circularBuffer[j],flgs); } else hyphenate(flgs); }
/* * "int charType(char c)" * return 0, 1, 2, 3 for the character type * 0 for neither, 1 for numerical, 2 for letter, 3 for punctuation */ int charType(char c){ int result =0; int target = (int)c; if (target > 9) { if ( (result = isDigit( c )) || (result = isPunct( c ) ) || ( result = isAlpha( c ) ) ){ // if any is true, then result will have the corresponding value already // result = 1 for numerical, result = 2 for letter, result = 3 for punctuation return (result); } } // else default, return 0, is neither of the three types return 0; }
void TokenizerEn::splitLine(const wstring& i_sentence, vector<wstring>& o_content, vector<size_t>& o_positions) const { // find last non-punctuator int lastLetter = (int)i_sentence.length()-1; for(; lastLetter >= 0; --lastLetter) { if(!isSpace(i_sentence[lastLetter]) && !isPunct(i_sentence[lastLetter])) { break; } } o_content.clear(); o_positions.clear(); size_t wordBegin = i_sentence.length(); for(size_t pos = 0; pos <= i_sentence.length(); pos++) { if(pos == i_sentence.length() || isSpace(i_sentence[pos]) ) { if(wordBegin < pos) { wstring word = i_sentence.substr(wordBegin, pos - wordBegin); vector<wstring> newContent; vector<size_t> newPos; splitSpecialSymbols(word, newContent, newPos, (pos > lastLetter) ); o_content.insert(o_content.end(), newContent.begin(), newContent.end()); for(auto pos_iter = newPos.begin(); pos_iter != newPos.end(); ++pos_iter) { (*pos_iter)+=wordBegin; } o_positions.insert(o_positions.end(), newPos.begin(), newPos.end()); wordBegin = i_sentence.length(); } } else if(wordBegin > pos) { wordBegin = pos; } } }
void loop( void ) { // Serial.write( '\n' ) ; // send a char // Serial.write( '\r' ) ; // send a char // Serial5.write( '-' ) ; // send a char // adding a constant integer to a string: stringThree = stringOne + 123; Serial.println(stringThree); // prints "stringThree = 123" // adding a constant long interger to a string: stringThree = stringOne + 123456789; Serial.println(stringThree); // prints " You added 123456789" // adding a constant character to a string: stringThree = stringOne + 'A'; Serial.println(stringThree); // prints "You added A" // adding a constant string to a string: stringThree = stringOne + "abc"; Serial.println(stringThree); // prints "You added abc" stringThree = stringOne + stringTwo; Serial.println(stringThree); // prints "You added this string" // adding a variable integer to a string: int sensorValue = analogRead(A0); stringOne = "Sensor value: "; stringThree = stringOne + sensorValue; Serial.println(stringThree); // prints "Sensor Value: 401" or whatever value analogRead(A0) has // adding a variable long integer to a string: long currentTime = millis(); stringOne = "millis() value: "; stringThree = stringOne + millis(); Serial.println(stringThree); // prints "The millis: 345345" or whatever value currentTime has // do nothing while true: while (true); #if 0 // get any incoming bytes: if (Serial.available() > 0) { int thisChar = Serial.read(); //////// int thisChar = 'a'; // say what was sent: Serial.print("You sent me: \'"); Serial.write(thisChar); Serial.print("\' ASCII Value: "); Serial.println(thisChar); // analyze what was sent: if (isAlphaNumeric(thisChar)) { Serial.println("it's alphanumeric"); } if (isAlpha(thisChar)) { Serial.println("it's alphabetic"); } if (isAscii(thisChar)) { Serial.println("it's ASCII"); } if (isWhitespace(thisChar)) { Serial.println("it's whitespace"); } if (isControl(thisChar)) { Serial.println("it's a control character"); } if (isDigit(thisChar)) { Serial.println("it's a numeric digit"); } if (isGraph(thisChar)) { Serial.println("it's a printable character that's not whitespace"); } if (isLowerCase(thisChar)) { Serial.println("it's lower case"); } if (isPrintable(thisChar)) { Serial.println("it's printable"); } if (isPunct(thisChar)) { Serial.println("it's punctuation"); } if (isSpace(thisChar)) { Serial.println("it's a space character"); } if (isUpperCase(thisChar)) { Serial.println("it's upper case"); } if (isHexadecimalDigit(thisChar)) { Serial.println("it's a valid hexadecimaldigit (i.e. 0 - 9, a - F, or A - F)"); } // add some space and ask for another byte: Serial.println(); Serial.println("Give me another byte:"); Serial.println(); } #endif #if 0 sensorValue = analogRead(A0); // apply the calibration to the sensor reading sensorValue = map(sensorValue, sensorMin, sensorMax, 0, 255); Serial.print("sensor = " ); Serial.print(sensorValue); // in case the sensor value is outside the range seen during calibration outputValue = constrain(sensorValue, 0, 255); // print the results to the serial monitor: Serial.print("\t output = "); Serial.println(outputValue); // fade the LED using the calibrated value: analogWrite(9/*ledPin*/, sensorValue); delay(1); #endif #if 0 // read the analog in value: sensorValue = analogRead(A0); // map it to the range of the analog out: outputValue = map(sensorValue, 0, 1023, 0, 255); // change the analog out value: analogWrite(9/*analogOutPin*/, outputValue); // print the results to the serial monitor: Serial.print("sensor = " ); Serial.print(sensorValue); Serial.print("\t output = "); Serial.println(outputValue); // wait 2 milliseconds before the next loop // for the analog-to-digital converter to settle // after the last reading: delay(2); // delay(1000); #endif #if 0 digitalWrite( 0, HIGH ) ; // set the red LED on digitalWrite( 0, LOW ) ; // set the red LED on digitalWrite( 1, HIGH ) ; // set the red LED on digitalWrite( 1, LOW ) ; // set the red LED on digitalWrite( 4, HIGH ) ; // set the red LED on digitalWrite( 4, LOW ) ; // set the red LED on digitalWrite( 5, HIGH ) ; // set the red LED on digitalWrite( 5, LOW ) ; // set the red LED on digitalWrite( 6, HIGH ) ; // set the red LED on digitalWrite( 6, LOW ) ; // set the red LED on digitalWrite( 7, HIGH ) ; // set the red LED on digitalWrite( 7, LOW ) ; // set the red LED on digitalWrite( 8, HIGH ) ; // set the red LED on digitalWrite( 8, LOW ) ; // set the red LED on digitalWrite( 9, HIGH ) ; // set the red LED on digitalWrite( 9, LOW ) ; // set the red LED on digitalWrite( 10, HIGH ) ; // set the red LED on digitalWrite( 10, LOW ) ; // set the red LED on digitalWrite( 11, HIGH ) ; // set the red LED on digitalWrite( 11, LOW ) ; // set the red LED on digitalWrite( 12, HIGH ) ; // set the red LED on digitalWrite( 12, LOW ) ; // set the red LED on digitalWrite( 13, HIGH ) ; // set the red LED on digitalWrite( 13, LOW ) ; // set the red LED on #endif #if 0 // int a = 123; // Serial.print(a, DEC); // for ( uint32_t i = A0 ; i <= A0+NUM_ANALOG_INPUTS ; i++ ) for ( uint32_t i = 0 ; i <= NUM_ANALOG_INPUTS ; i++ ) { int a = analogRead(i); // int a = 123; Serial.print(a, DEC); Serial.print(" "); // Serial.write( ' ' ) ; // send a char // Serial.write( 0x30 + i ) ; // send a char // Serial.write( 0x30 + ((a/1000)%10) ) ; // send a char // Serial.write( 0x30 + ((a/100)%10) ) ; // send a char // Serial.write( 0x30 + ((a/10)%10) ) ; // send a char // Serial.write( 0x30 + (a%10) ) ; // send a char // Serial.write( ' ' ) ; // send a char } Serial.println(); #endif #if 0 volatile int pin_value=0 ; static volatile uint8_t duty_cycle=0 ; static volatile uint16_t dac_value=0 ; // Test digitalWrite led_step1() ; delay( 500 ) ; // wait for a second led_step2() ; delay( 500 ) ; // wait for a second // Test Serial output Serial5.write( '-' ) ; // send a char Serial5.write( "test1\n" ) ; // send a string Serial5.write( "test2" ) ; // send another string // Test digitalRead: connect pin 2 to either GND or 3.3V. !!!! NOT on 5V pin !!!! pin_value=digitalRead( 2 ) ; Serial5.write( "pin 2 value is " ) ; Serial5.write( (pin_value == LOW)?"LOW\n":"HIGH\n" ) ; duty_cycle+=8 ;//=(uint8_t)(millis() & 0xff) ; analogWrite( 13, duty_cycle ) ; analogWrite( 12, duty_cycle ) ; analogWrite( 11, duty_cycle ) ; analogWrite( 10 ,duty_cycle ) ; analogWrite( 9, duty_cycle ) ; analogWrite( 8, duty_cycle ) ; dac_value += 64; analogWrite(A0, dac_value); Serial5.print("\r\nAnalog pins: "); for ( uint32_t i = A0 ; i <= A0+NUM_ANALOG_INPUTS ; i++ ) { int a = analogRead(i); Serial5.print(a, DEC); Serial5.print(" "); } Serial5.println(); Serial5.println("External interrupt pins:"); if ( ul_Interrupt_Pin3 == 1 ) { Serial5.println( "Pin 3 triggered (LOW)" ) ; ul_Interrupt_Pin3 = 0 ; } if ( ul_Interrupt_Pin4 == 1 ) { Serial5.println( "Pin 4 triggered (HIGH)" ) ; ul_Interrupt_Pin4 = 0 ; } if ( ul_Interrupt_Pin5 == 1 ) { Serial5.println( "Pin 5 triggered (FALLING)" ) ; ul_Interrupt_Pin5 = 0 ; } if ( ul_Interrupt_Pin6 == 1 ) { Serial5.println( "Pin 6 triggered (RISING)" ) ; ul_Interrupt_Pin6 = 0 ; } if ( ul_Interrupt_Pin7 == 1 ) { Serial5.println( "Pin 7 triggered (CHANGE)" ) ; ul_Interrupt_Pin7 = 0 ; } #endif }