-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathextracting_words.py
More file actions
48 lines (36 loc) · 1.84 KB
/
Copy pathextracting_words.py
File metadata and controls
48 lines (36 loc) · 1.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
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
"""Extract words from a corpus, and filter them using anonymous functions."""
from wordkit.corpora import subtlexuk
if __name__ == "__main__":
# Subtlex is a specific corpus reader.
# Each corpus reader is associated with a certain corpus, or type of corpus
# fields denotes which type of information is retrieved from the corpus.
# In this case, we say we want to extract orthography and frequency from
# the corpus.
# By doing this, any words extracted from the corpus will be guaranteed
# to have the following fields.
# NOTE: replace path_to_subtlex with the path to subtlex on your machine.
words = subtlexuk("path_to_subtlex",
fields=("orthography", "frequency"))
# words is a list of dictionaries.
print("{} words in corpus".format(len(words)))
print("First 10 words:")
print(words[:10])
# By passing filters, we can limit the words we get to specific ones.
# The syntax of the filter is as follows:
# field_name=function
# This function is then applied to each item in the corpus.
# Only if all filters evaluate to True will the item be selected.
# This filter only extracts words whose orthographic form starts
# with an 'a'.
# Note the use of the lambda, which is an anonymous function.
start_a = words[words["orthography"].apply(lambda x: x[0] == 'a')]
# First 10 words starting with an a
print("Words left after filtering: {}".format(len(start_a)))
# After filtering, we can filter again.
# Let's filter on frequency by throwing away any words with frequencies
# smaller than 10.
# Note that in this case we filter again.
start_a = start_a[start_a["frequency"].apply(lambda x: x >= 10)]
print("Words left after filtering: {}".format(len(start_a)))
print("First 10 words after filtering:")
print(start_a[:10])