16
Korpuslinguistik für und mit Computerlinguistik Seminar SS 2003 Sitzung 1: UNIX, Perl Gerold Schneider

Korpuslinguistik für und mit Computerlinguistik

  • Upload
    lotta

  • View
    27

  • Download
    0

Embed Size (px)

DESCRIPTION

Korpuslinguistik für und mit Computerlinguistik. Seminar SS 2003 Sitzung 1: UNIX, Perl Gerold Schneider. UNIX: Grundbefehle. cat (catalog) head, tail more, less |, (pipe, in ,out) man (manual pages). ls (list): Verzeichnisinhalt cd (change dir.) pwd (present working dir.) - PowerPoint PPT Presentation

Citation preview

Page 1: Korpuslinguistik für und mit Computerlinguistik

Korpuslinguistik für und mit Computerlinguistik

Seminar SS 2003

Sitzung 1: UNIX, Perl

Gerold Schneider

Page 2: Korpuslinguistik für und mit Computerlinguistik

Gerold Schneider: Korpuslinguistik für Computerlinguistik, I

2

UNIX: Grundbefehle

ls (list): Verzeichnisinhalt cd (change dir.) pwd (present working dir.) cp (copy) mv (move) rm (remove) rmdir (rm dir) mkdir (make dir) pico, nano, emacs (ed.s) chmod (change mod.)

cat (catalog) head, tail more, less |, <, > (pipe, in ,out) man (manual pages)

Page 3: Korpuslinguistik für und mit Computerlinguistik

Gerold Schneider: Korpuslinguistik für Computerlinguistik, I

3

UNIX: Linguists´ toolbox

grep, egrep ((extended) global regular expression) tr (transform) wc (word count) sort uniq cut (cut columns) paste (paste columns)

Page 4: Korpuslinguistik für und mit Computerlinguistik

Gerold Schneider: Korpuslinguistik für Computerlinguistik, I

4

Bsp. 1: Wortliste, Konkordanz

Lexikon eines Textes: Text in 1-Wort-pro-Zeile Format konvertieren, dann sortieren, Duplikate entfernen

cat my.txt | tr -s " " "\n" |sort | uniq Häufigkeit mitzählen

cat my.txt | tr -s " " "\n" |sort | uniq -c Preprocessing

cat my.txt | tr -s " " "\n" | tr -s "[A-Z]" "[a-z]" | tr -d

'[.,;:?\!"\(\)+-&=]' | sort | uniq -c Konkordanz, Anzahl Types, Types per Token ? Zipf interaktiv: http://users.info.unicaen.fr/~giguet/java/zipf.html

Page 5: Korpuslinguistik für und mit Computerlinguistik

Gerold Schneider: Korpuslinguistik für Computerlinguistik, I

5

Bsp. 2: N-Gramme

Preprocessing

cat my.txt | tr -s " " "\n" | tr "[A-Z]" "[a-z]" | tr -d "[.,:;'?\!]" > my.tmp

Selber Text ohne erstes Wort

cat my.tmp | tail +2 > my.tmp2 Bigramme

paste -d" " my.tmp my.tmp2 > bigramme

Liste der häufigsten Bigramme ? Welche Wörter sind am häufigsten vor und nach der Präposition

„mit“ ?

Page 6: Korpuslinguistik für und mit Computerlinguistik

Gerold Schneider: Korpuslinguistik für Computerlinguistik, I

6

Bsp. 3: Kontexte

Kontexte aller Wörter in einer bestimmten Fenstergrösse F Vorgehen:

F-Gramme erstellen, analog zu Bigrammen Herausschneiden der Kontextworte

cut -d" " -f 1,2 mygrams > kontexts

cut -d" " -f 1,3 mygrams >> kontexts

cut -d" " -f 1,4 mygrams >> kontexts

...

cut -d" " -f 1,F mygrams >> kontexts

Lokale, syntaktische und semantische Distanz

Page 7: Korpuslinguistik für und mit Computerlinguistik

Gerold Schneider: Korpuslinguistik für Computerlinguistik, I

7

Perl: Motivation

UNIX ist streng zeilenweise orientiert. Zeilensprünge können z.B. nicht mittels tr entfernt werden.

tr erlaubt nur buchstabenweises Ersetzen, und die Ausgabe darf nicht länger sein als die Eingabe. Dies verunmöglicht z.B. die Erstellung einer Buchstabenkonkordanz.

UNIX ist nicht flexibel genug. Die gesammelten Daten stehen z.B. nicht als Variablen/Listen/Hashes zur Verfügung

Mächtigere Werkzeug sind nötig: z.B. Perl

Page 8: Korpuslinguistik für und mit Computerlinguistik

Gerold Schneider: Korpuslinguistik für Computerlinguistik, I

8

Perl: Zeilenfilterskelett

#!usr/bin/perl ## Pfad zum Perl-Interpreter

while(<>) { ## zeilenweise Daten einlesen

s/ersetzmich/durchdas/;

print; ## Ausgabe des Ergebnisses

}

Aufruf aus der Kommandozeile

cat myin.txt | perl myperlscript.pl > myout.txt

Page 9: Korpuslinguistik für und mit Computerlinguistik

Gerold Schneider: Korpuslinguistik für Computerlinguistik, I

9

Perl: Umordungsschema

#!usr/bin/perl ## Pfad zum Perl-Interpreter

$/="\n"; ## Datensatzbegrenzer

while(<>) { ## Datensatzweise Daten einlesen

($f1,$f2,$flast) = split(/\,/,$_); ## Felder lesen

$flast =~ s/\n//; ## chop Datensatzbegrenzer

$swapwas = $f1.$flast.$f2;

print "$swapwas\n"; ## Ausgabe des Ergebnisses

}

Beispiel:

prolog(wort,NN,12). prolog(wort,12,NN).

Page 10: Korpuslinguistik für und mit Computerlinguistik

Gerold Schneider: Korpuslinguistik für Computerlinguistik, I

10

Perl: Geordnetes Einsammeln mit Hashes: Wortliste

#!/usr/bin/perl$/ = ""; #Datensatzbegrenzer: Paragraph$* = 1; #mehrzeilige Matcheswhile (<>){

s/-\n//g; tr/A-Z/a-z/; #Trennzeichen weg,Kleinbuchstaben@words = split (/\W*\s+\W*/,$_);foreach $word (@words) {

$wordcount{$word}++;}

}foreach $word (sort keys (%wordcount)) {

printf "%20s,%d\n",$word,$wordcount{$word};}

Page 11: Korpuslinguistik für und mit Computerlinguistik

Gerold Schneider: Korpuslinguistik für Computerlinguistik, I

11

Perl: Geordnetes Einsammeln mit Hashes: Bigramme

while(<>) {

s/^\s+//;

@words = split(/\W*\s+\W*/,$_);

for ($count=0;$count<=$#words-1;++$count) { $wordcount{$words[$count]. " " .

$words[$count+1]}++;

}

}

foreach $wordpair (sort keys(%wordcount)) {

printf "%20s,%d\n",$wordpair,$wordcount{$wordpair};

}

Page 12: Korpuslinguistik für und mit Computerlinguistik

Gerold Schneider: Korpuslinguistik für Computerlinguistik, I

12

Perl: Geordnetes Einsammeln mit Hashes: Endungshäufigkeiten (3B.)

while(<>) { s/^\s+//; @words = split(/\s+/,$_); for ($count=0;$count<=$#words;++$count) { @chars = split(//,$words[$count]); # split w/o sep.

if ($#chars > 1) { # at least 3 letters in this word

$ending{ $chars[$#chars-2] . " " .$chars[$#chars-1] . " " .

$chars[$#chars]}++; } }

}

foreach $end (sort keys(%ending)) {

printf "%20s,%d\n",$end,$ending{$end};

}

Page 13: Korpuslinguistik für und mit Computerlinguistik

Gerold Schneider: Korpuslinguistik für Computerlinguistik, I

13

Perl: Evaluation I

Gegeben sei die Ausgabenliste einer jeweils binären, maschinell getroffenen Entscheidung E=[0|1], aligniert mit der entsprechenden richtigen menschlichen Entscheidung („gold standard“) G =[0|1], also eine Matrix {E,M}.

als 0 klassif. als 1 klassifiziert

E=0 E=1

G=0 {0,0} I {1,0} II

G=1 {0,1} III {1,1} IV

Accuracy=

2*Precision=

2*Recall=

IVIIIIII

IVI

IVII

IV

IIII

I

IVIII

IV

III

I

Page 14: Korpuslinguistik für und mit Computerlinguistik

Gerold Schneider: Korpuslinguistik für Computerlinguistik, I

14

Perl: Evaluation II

while(<>) {

if /0\,0/{$caseI ++};

if /1\,0/{$caseII ++};

if /0\,1/{$caseIII++};

if /1\,1/{$caseIV ++};

}

$accuracy = ...

$precision= ...

$recall = ...

Accuracy=

Precision=

Recall=

IVIIIIII

IVI

IVII

IV

IIII

I

IVIII

IV

III

I

Page 15: Korpuslinguistik für und mit Computerlinguistik

Gerold Schneider: Korpuslinguistik für Computerlinguistik, I

15

Perl: Aufgaben 4 + 12 (Microproject in Genf) I

Schreibmaschine mit Stemming-Spellchecker fürs Französische Schritt 1: Einlesen eines grossen Textcorpus, ermitteln der häufisten

Endungen (siehe Endungsprogramm) Schritt 2: Manuell morphologische Endungen ermitteln, Stemmingregeln

schreiben, stemmen:$word =~s/s$//;

$word =~s/ée$//; $word =~s/ées$//; $word =~s/es$//; $word =~s/er$//; $word =~s/ait$//; $word =~s/able$//; $word =~s/é$//; $word =~s/elles$//; $word =~s/elle$//; $word =~s/ant$//;

Page 16: Korpuslinguistik für und mit Computerlinguistik

Gerold Schneider: Korpuslinguistik für Computerlinguistik, I

16

Perl: Aufgaben 4 + 12 (Microproject in Genf) II

Schritt 3: Gestemmten Korpus einlesen in Hash, Schreibmaschinenschleife durchaufen bis „STOP“ eingegeben wird. Dann den Brief ausgeben und die eingegebenen Worte stemmen, ausser sie sind im Ausnahmenlexikon (dans, sans, ...) und mit dem gestemmten Korpus vergleichen.

if ($table{$word}) { print "Le mot $word est accepté.\n";

}

elsif (!$table{$word}) {

print "Le mot $word n'est pas dans l'index, changez-le!\n";