Playing with spaCy, counting the words in this Sherlock Holmes book. It was mostly a quick and clean little exercise. I ran the code this way first:
import spacy
nlp = spacy.load('en_core_web_sm')
with open("sherlock.txt", encoding='utf8') as sherlock:
sherlock = str(sherlock.readlines())
text = nlp(sherlock)
count = text.count_by(spacy.attrs.POS)
for k, v in count.items():
print(text.vocab[k].text, v)
before deciding that changing the readlines() bit to read() would be cleaner:
import spacy
nlp = spacy.load('en_core_web_sm')
with open("sherlock.txt", encoding='utf8') as sherlock:
sherlock = sherlock.read()
text = nlp(sherlock)
count = text.count_by(spacy.attrs.POS)
for k, v in count.items():
print(text.vocab[k].text, v)
But somehow that switch is getting me way different numbers. readlines():
X 2753
PUNCT 53127
DET 9309
NOUN 18666
ADP 11106
PROPN 8769
NUM 979
SPACE 711
ADJ 6118
PRON 15626
AUX 7491
ADV 5800
VERB 13183
PART 2265
SCONJ 3437
CCONJ 3665
INTJ 486
SYM 31
read():
DET 10211
PROPN 4048
ADP 11774
SPACE 9329
NOUN 16950
PUNCT 21603
PART 2433
PRON 16906
AUX 8133
ADV 6236
VERB 13561
ADJ 6628
CCONJ 3945
SCONJ 3735
NUM 974
INTJ 477
SYM 35
X 10
Some things like punctuation can make sense, figuring readlines() returns a list and read() returns a string. But the two result sets are 500 adjectives off from each other? 1300 pronouns? 2000 nouns? What's causing the difference, and which one do I trust? Thanks.