from pathlib import Path
from collections import Counter
counter = Counter()
dir = Path(r'C:\Users\user\OneDrive\somedirectory\pvi_textgrid')
out_file = Path('output.txt')
for file in dir.glob('*.textgrid'):
with file.open('r', encoding='utf-8') as f:
for l in f:
counter.update(l.strip().split())
counter.most_common(1)
with out_file.open('w', encoding='utf-8') as f:
f.write(",".join(counter))
print(counter)
output is as such
Counter({'0': 133,
'""': 73,
'"IntervalTier"': 57,
'=': 38,
'1': 38,
'3': 35,
'File': 19,
'type': 19,
'"ooTextFile"': 19,
'Object': 19,
'class': 19,
'"TextGrid"': 19,
'<exists>': 19,
'"Phonation"': 19,
'"Speech"': 19,
'"Comments"': 19,
'3.93': 14,
'3.96': 7,
'5.0200000000000005': 7,
'3.77': 7,
'10.21': 7,
'12.52': 7,
'11.370000000000001': 7,
'3.7800000000000002': 7,
'3.72': 7,
'12.1': 7,
'3.97': 7,
'3.89': 7,
'5.94': 7,
'6.2': 7,
'4.63': 7,
'4.18': 7,
'4.62': 7,
'6.83': 7,
'"no"': 4,
'2': 3,
'0.36100874171044706': 2,
'1.0890132576679536': 2,
'0.7216091094277167': 2,
'1.3713701493710988': 2,
'0.7099782814647281': 2,
'1.4161172126881183': 2,
'0.8348417168127676': 2,
'3.2335119433221124': 2,
'five"': 2,
'1.7299889316602575': 2,
'2.480568841515854': 2,
'2.148692441200811': 2,
'"aa"': 2,
'2.1688157906233343': 2,
'1.2904978880596738': 2,
'0.5533687507723187': 2,
'0.9558305467454027': 2,
'0.4383214942352268': 2,
'0.9760798418291502': 2,
'0.985814582373786': 2,
'10.873283112340998': 2,
'0.7770649427891344': 2,
'1.1413355432567072': 2,
'0.5661048087832754': 2,
'1.0812964186884253': 2,
'0.7924526404870945': 2,
'3.11139917918915': 2,
'0.5212286824463059': 2,
'3.472996059458859': 2,
'1.5274624044665293': 2,
'1.9402683705731323': 2,
'"yes"': 2,
'0.991847317448846': 2,
'1.7480710757789653': 2,
'0.9602805622853893': 2,
'2.863884865551033': 2,
'0.9643968456947999': 2,
'3.140083120204604': 2,
'"cancel"': 1,
'"help"': 1,
'"quit"': 1,
'"three': 1,
'six': 1,
'"thirty': 1,
'"ee"': 1,
'"oo"': 1,
'"you': 1,
'wish': 1,
'to': 1,
'know': 1,
'all': 1,
'about': 1,
'my': 1,
'grandfather"': 1,
'"well': 1,
'he': 1,
'is': 1,
'nearly': 1,
'ninety': 1,
'three': 1,
'years': 1,
'old"': 1,
'"eleven': 1,
'thousand': 1,
'one"': 1,
'"oh': 1,
'zero': 1,
'ten"': 1})
I want to access the "yes" and "no" keys and put their values in to another variable to show the amount of times they are mentioned
CodePudding user response:
You can access the count of words via counter
:
yes_counts = counter['yes']
no_counts = counter['no']