Höhere Programmierung in der Computerlinguistik mit C++ - Übung 10

Zurück

C++ SS11 - Übung 10 - Lösung Aufgabenkomplex 9 : Aufgabe 35

#include <iostream>
#include <string>
#include <boost/foreach.hpp>
#include <boost/regex.hpp>

using namespace std;
using namespace boost;

int main() {
                setlocale(LC_ALL,"");     
        wstring text = L" »De muaß außi!« sagt der Mann mit dem Gemsbart, »und überhaupts, wia könna denn Sie de Frau da einaschiab'n? Muaß ma si vielleicht dös g'fallen lassen bei der Tramway? Daß de Bazüllen im Wag'n umanandfliag'n?« Der Schaffner trifft die Entscheidung, daß die Frau sich auf die vordere Plattform stellen muß. Sie verläßt ihren Platz und geht hinaus. »Dös war amal a freche Person!« sagt der Mann mit dem Gemsbart.";
        
        wregex re(L"[A-ZÖÄÜ]\\S+");
        
        regex_token_iterator<wstring::const_iterator > aMatch( text.begin(), text.end(), re) ;
            regex_token_iterator<wstring::const_iterator> noMatch ;
        
        while (aMatch != noMatch ) {
                wcout << * aMatch << endl;
                aMatch++;
        }
}

C++ SS11 - Übung 10 - Lösung Aufgabenkomplex 9 : Aufgabe 34

#include <iostream>
#include <string>
#include <boost/regex.hpp>

using namespace std;
using namespace boost;
int main() {        

        regex telefonnummer("(?:\\+?[\\d ]{0,5})?\\s*(?:\\(0\\))?[\\d -]+");
        
        string input;
        while(getline(cin,input)) {
                if ( regex_match(input,telefonnummer ) ) {
                 cout << input << " ist ein valide Telefonnummer." << endl;
                }
            else {
                     cout << "! " << input << " keine valide Telefonnumer." << endl;
            }
        
        }
}

C++ SS11 - Übung 10 - Lösung Aufgabenkomplex 9 : Aufgabe 33

# 33.1
regex1 = /^\d{3}\s*\/\s*\d{4}$/
regex2 = /^(?:\+?[\d ]{0,5})?\s*(?:\(0\))?[\d -]+$/

numbers = ["089 / 1234", "123/4567", "+598" , "1/23/234" , "+ 49 (0)89 - 1234 1234", "+ 49 089 - 1234 1234", "08912341234"]

for num in numbers do
        if num =~ regex2 then
                puts ":) #{num} matched."
        else
                puts ":< #{num} didn't match'."
        end
end

C++ SS11 - Übung 10 - Lösung Aufgabenkomplex 9 : Aufgabe 32

#include <string>
#include <vector>
#include <iostream>
#include <boost/foreach.hpp>
#include <boost/algorithm/string.hpp>

using namespace std;
using namespace boost;

int main() {
        
        setlocale(LC_ALL,"");
        
        wstring text = L" »De muaß außi!« sagt der Mann mit dem Gemsbart, »und überhaupts, wia könna denn Sie de Frau da einaschiab'n? Muaß ma si vielleicht dös g'fallen lassen bei der Tramway? Daß de Bazüllen im Wag'n umanandfliag'n?« Der Schaffner trifft die Entscheidung, daß die Frau sich auf die vordere Plattform stellen muß. Sie verläßt ihren Platz und geht hinaus. »Dös war amal a freche Person!« sagt der Mann mit dem Gemsbart.";

        vector<wstring> words;
        
        /*split( wohinSpeichern, woSuchen, "an was splitten" )*/
        split( words, text, is_any_of(L" "), token_compress_on );
        
        BOOST_FOREACH( wstring word, words) {
                wcout << word << endl;
        }
}

C++ SS11 - Übung 10 - Lösung Aufgabenkomplex 9 : Aufgabe 31

/*Aufgabe 31 - unordered_map
Ordnen sie den Buchstaben von 'A'-'Z' deren ASCII-Zahlenwert
in einer boost::unordered_map zu. 
*/

#include <boost/unordered_map.hpp>
#include <boost/foreach.hpp>
#include <iostream>
#include <string>

#include <map>

using namespace std;
using namespace boost;

typedef unordered_map<char,int> hash_map;

int main() {

    hash_map myHash;
        char currentChar = 'A';
        while(currentChar <= 'Z' ) {
                myHash[currentChar] = (int) currentChar;
                currentChar++;
        }
    BOOST_FOREACH(hash_map::value_type buchstaben, myHash) {
            cout << buchstaben.first << "->" << buchstaben.second << endl;
    }
       
}
Zurück