Last active
June 5, 2021 22:10
-
-
Save neilmayhew/31cbda1003f7352feb71 to your computer and use it in GitHub Desktop.
Python script to print a counted list of all digraphs in a set of files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import fileinput, re | |
counts = {} | |
for l in fileinput.input(): | |
l = l.lower() | |
for m in re.finditer(r'[a-z](?=[a-z])', l): | |
graph = l[m.start():m.end()+1] | |
counts[graph] = counts.get(graph, 0) + 1 | |
# Output in frequency order, highest first | |
for k, v in sorted(counts.items(), key=lambda (k, v): v, reverse=True): | |
print k, v |
To manually change it to search for trigraphs instead of digraphs,
for m in re.finditer(r'[a-z](?=[a-z])(?=[a-z])', l):
graph = l[m.start():m.end()+2]
@neilmayhew doesn't the [a-z]
portion exclude punctuation marks and capitals?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@HughP: OK, just comment out that line (with #) and you should get what you want.
A full implementation would take command-line options for graph length (di-, tri-, etc.) case and sort order (alphabetic vs frequency).