Reading corpora
read_text_corpus
read_text_corpus(
corpus_files: Iterator[PathType],
) -> pl.DataFrame
Read tagged text files into a DataFrame, one row per token.
Each line of a file is one sentence, and each whitespace-separated token on
it is a word and its tag joined by "/", as in quick/JJ. The split is at
the last "/", so and/or/CC reads as the word and/or with the tag CC.
Blank lines are skipped.
Parameters:
-
corpus_files(iterable of str or Path) –Paths of the files to read, e.g.
Path("corpus").glob("*.txt"). This is an iterable of paths, not a single path.
Returns:
-
DataFrame–One row per token, with columns
token,pos,sentence_tagandfile_id. The files come in the order given, and the tokens of a file in the order they appear in it.sentence_tagis "B" on the first token of a sentence and "I" on the rest.file_idholds the path the token was read from, andsearchstops matches from crossing from one file to the next. A corpus with no tokens comes back as an empty frame with no columns at all.
Raises:
-
FileNotFoundError–If one of
corpus_filesdoes not exist. -
ValueError–If a token has no "/" in it.
Notes
Files are decoded with Python's default text encoding.
See Also
scan_text_corpus : Read the same files a batch at a time, as a LazyFrame.
chunk_id : Number the sentences that sentence_tag marks out.
Examples:
>>> from pathlib import Path
>>> import polars_corpus as plc
>>> corpus = plc.read_text_corpus(Path("corpus").glob("*.txt"))
>>> # Sort the paths, so the file ids run in a predictable order:
>>> corpus = plc.read_text_corpus(sorted(Path("corpus").glob("*.txt")))
>>> plc.search(corpus, "the _JJ _NN")
Source code in python/polars_corpus/corpus_io.py
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 | |
scan_text_corpus
scan_text_corpus(
corpus_files: Iterator[PathType],
) -> pl.LazyFrame
Scan tagged text files as a LazyFrame, a batch of tokens at a time.
The file format is the one read_text_corpus reads. Nothing is read until
the frame is collected, and only a batch of rows is held at once. The
columns a query keeps and the filters it applies run as each batch is
produced, so reading a small part of a large corpus never holds the whole
of it.
Parameters:
-
corpus_files(iterable of str or Path) –Paths of the files to read, e.g.
Path("corpus").glob("*.txt"). This is an iterable of paths, not a single path. The paths are listed when the frame is built, so a generator may be passed.
Returns:
-
LazyFrame–Columns
token,pos,sentence_tagandfile_id, all strings, holding whatread_text_corpusproduces. The schema is fixed, so it is these four columns even when the files hold no tokens.
Raises:
-
ComputeError–On collecting, if one of
corpus_filesdoes not exist or a token has no "/" in it. TheFileNotFoundErrororValueErrorbehind it is named in the message.
Notes
The files are read again every time the frame is collected. Collect once and keep the DataFrame if the corpus is small enough to hold.
See Also
read_text_corpus : Read the same files into a DataFrame.
Examples:
>>> from pathlib import Path
>>> import polars as pl
>>> import polars_corpus as plc
>>> corpus = plc.scan_text_corpus(sorted(Path("corpus").glob("*.txt")))
>>> corpus.select(pl.len()).collect() # Count tokens without holding them
>>> # search() reads a lazy corpus a chunk of files at a time:
>>> plc.search(corpus, "the _JJ _NN")
Source code in python/polars_corpus/corpus_io.py
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 | |
from_nltk
from_nltk(corpus: CorpusReader) -> pl.DataFrame
Read an NLTK corpus into a Polars DataFrame, one row per token.
Works with any corpus NLTK can read: Brown, the Gutenberg texts, anything
under nltk.corpus, or a reader pointed at a directory of your own.
The columns depend on what the reader offers. A tagged corpus gets a pos
column, a corpus read as sentences gets a sentence_tag column, and a
categorized corpus gets a category column.
Parameters:
-
corpus(CorpusReader) –Any NLTK corpus reader, e.g.
nltk.corpus.brownor aPlaintextCorpusReaderover a directory of your own.
Returns:
-
DataFrame–One row per token, in corpus order, with as many of these columns as the reader can supply:
token: the word itselfpos: its part-of-speech tag, if the corpus is taggedsentence_tag: "B" on the first token of each sentence and "I" on the rest, if the reader reads sentencesfile_id: the file the token came fromcategory: the file's category, if the corpus is categorized
Raises:
-
AttributeError–If the reader exposes none of
sents(),tagged_words()orwords(), so its tokens cannot be read.
Examples:
>>> import nltk
>>> import polars_corpus as plc
>>> brown = plc.from_nltk(nltk.corpus.brown)
>>> # `category` is there because Brown is categorized:
>>> brown.group_by("category").len()
Source code in python/polars_corpus/convert.py
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 | |