http://qs1969.pair.com?node_id=11133854


in reply to How to count the vocabulary of an author?

Update 2:

Here is a first simple solution. Anybody needs a starting point:

#!/usr/bin/env perl use strict; use warnings; use feature qw(say); use Data::Dump; use Lingua::Stem qw(stem); undef $/; my $text = <DATA>; say $text; $text = lc $text; $text =~ s/\n+/ /g; say $text; $text =~ s/[:;'!?.,]+//g; say $text; my @words = split / /, $text; dd \@words; Lingua::Stem::set_locale('de'); say Lingua::Stem::get_locale; my $stems = stem(@words); dd $stems; my %vocabulary = map {$_ => 1} @$stems; dd \%vocabulary; say scalar keys %vocabulary; __DATA__ Ich Bin Der Geist, Der Stets Verneint! Und Das Mit Recht; denn alles, was entsteht, Ist wert, daß es zugrunde geht; Drum besser wär's, daß nichts entstünde. So ist denn alles, was ihr Sünde, Zerstörung, kurz, das Böse nennt, Mein eigentliches Element.

It isn't so easy as one might think: Simply counting the words with wc doesn't return the vocabulary. And Lingua::Stem thinks that Ist and ist are different stems for example. And how to filter out the real text from sources which contain a preface, index, bla? And so on.

Some may ask why i waste my time with this issue. It has to do with politics. As this isn't a forum about politics i skip the details.

I was a little bit inspired by what Jill Lepore analogously wrote about facts in her splendid book These Truths: A History of the United States about facts: "Show me yours and i'll show you mine." Basically the same game that we played with our cousins when we were nasty little boys. Discussion later.

«The Crux of the Biscuit is the Apostrophe»

Replies are listed 'Best First'.
Re^2: How to count the vocabulary of an author?
by erix (Prior) on Jun 14, 2021 at 17:27 UTC

    Ok, I'll play 'nasty little boy' too (I remember!)

    Of course I had to try the stemming that is built-in in PostgreSQL's full-text search (FTS). I had'nt used it for a while; so this is just playing with it. Below are results of stemming and the distinction between words and stop-words.

    I think this FTS-stuff uses snowball, and I don't know how recent the vocabulary is. (UPDATE: I see regular snowball-related updates (every few months) in the PostgreSQL git log so I now think its snowball stuff is reasonably up-to-date)

    -- Below are three chunks/resultsets: -- 1. Your text -- 2. Real words: -- select .. from ts_debug('german', '$yourtxt') -- where lexemes > 0 -- 3. Stop-words: -- select .. from ts_debug('german', '$yourtxt') -- where lexemes = 0 txt ---------------------------------------------- Ich Bin Der Geist, Der Stets Verneint! + Und Das Mit Recht; denn alles, was entsteht,+ Ist wert, daß es zugrunde geht; + Drum besser wär's, daß nichts entstünde. + So ist denn alles, was ihr Sünde, + Zerstörung, kurz, das Böse nennt, + Mein eigentliches Element. (1 row) alias | token | dictionary | lexemes -----------+--------------+-------------+------------ asciiword | Geist | german_stem | {geist} asciiword | Stets | german_stem | {stet} asciiword | Verneint | german_stem | {verneint} asciiword | Recht | german_stem | {recht} asciiword | entsteht | german_stem | {entsteht} asciiword | wert | german_stem | {wert} asciiword | zugrunde | german_stem | {zugrund} asciiword | geht | german_stem | {geht} asciiword | Drum | german_stem | {drum} asciiword | besser | german_stem | {bess} word | wär | german_stem | {war} asciiword | s | german_stem | {s} word | entstünde | german_stem | {entstund} word | Sünde | german_stem | {sund} word | Zerstörung | german_stem | {zerstor} asciiword | kurz | german_stem | {kurz} word | Böse | german_stem | {bos} asciiword | nennt | german_stem | {nennt} asciiword | eigentliches | german_stem | {eigent} asciiword | Element | german_stem | {element} (20 rows) alias | token | dictionary | lexemes -----------+--------+-------------+--------- asciiword | Ich | german_stem | {} asciiword | Bin | german_stem | {} asciiword | Der | german_stem | {} asciiword | Der | german_stem | {} asciiword | Und | german_stem | {} asciiword | Das | german_stem | {} asciiword | Mit | german_stem | {} asciiword | denn | german_stem | {} asciiword | alles | german_stem | {} asciiword | was | german_stem | {} asciiword | Ist | german_stem | {} word | daß | german_stem | {} asciiword | es | german_stem | {} word | daß | german_stem | {} asciiword | nichts | german_stem | {} asciiword | So | german_stem | {} asciiword | ist | german_stem | {} asciiword | denn | german_stem | {} asciiword | alles | german_stem | {} asciiword | was | german_stem | {} asciiword | ihr | german_stem | {} asciiword | das | german_stem | {} asciiword | Mein | german_stem | {} (23 rows)

    Not perfect but more useful than I thought it would be without any work.

      Very cool! Thanks!

      «The Crux of the Biscuit is the Apostrophe»