1 """Language-specific functions, including models of languages based on data of
11 from math
import log10
14 unaccent_specials
= ''.maketrans({"’": "'", '“': '"', '”': '"'})
17 """Remove all non-alphabetic characters from a text
18 >>> letters('The Quick')
20 >>> letters('The Quick BROWN fox jumped! over... the (9lazy) DOG')
21 'TheQuickBROWNfoxjumpedoverthelazyDOG'
23 return ''.join([c
for c
in text
if c
in string
.ascii_letters
])
26 """Remove all accents from letters.
27 It does this by converting the unicode string to decomposed compatability
28 form, dropping all the combining accents, then re-encoding the bytes.
41 translated_text
= text
.translate(unaccent_specials
)
42 return unicodedata
.normalize('NFKD', translated_text
).\
43 encode('ascii', 'ignore').\
47 """Remove all non-alphabetic characters and convert the text to lowercase
49 >>> sanitise('The Quick')
51 >>> sanitise('The Quick BROWN fox jumped! over... the (9lazy) DOG')
52 'thequickbrownfoxjumpedoverthelazydog'
56 # sanitised = [c.lower() for c in text if c in string.ascii_letters]
57 # return ''.join(sanitised)
58 return letters(unaccent(text
)).lower()
61 def datafile(name
, sep
='\t'):
62 """Read key,value pairs from file.
64 with
open(os
.path
.join(os
.path
.dirname(os
.path
.realpath(__file__
)), name
), 'r') as f
:
66 splits
= line
.split(sep
)
67 yield [splits
[0], int(splits
[1])]
69 english_counts
= collections
.Counter(dict(datafile('count_1l.txt')))
70 normalised_english_counts
= norms
.normalise(english_counts
)
72 english_bigram_counts
= collections
.Counter(dict(datafile('count_2l.txt')))
73 normalised_english_bigram_counts
= norms
.normalise(english_bigram_counts
)
75 english_trigram_counts
= collections
.Counter(dict(datafile('count_3l.txt')))
76 normalised_english_trigram_counts
= norms
.normalise(english_trigram_counts
)
78 with
open(os
.path
.join(os
.path
.dirname(os
.path
.realpath(__file__
)), 'words.txt'), 'r') as f
:
79 keywords
= [line
.rstrip() for line
in f
]
82 def weighted_choice(d
):
83 """Generate random item from a dictionary of item counts
85 target
= random
.uniform(0, sum(d
.values()))
87 for (l
, p
) in d
.items():
93 def random_english_letter():
94 """Generate a random letter based on English letter counts
96 return weighted_choice(normalised_english_counts
)
100 """Returns all n-grams of a text
102 >>> ngrams(sanitise('the quick brown fox'), 2) # doctest: +NORMALIZE_WHITESPACE
103 ['th', 'he', 'eq', 'qu', 'ui', 'ic', 'ck', 'kb', 'br', 'ro', 'ow', 'wn',
105 >>> ngrams(sanitise('the quick brown fox'), 4) # doctest: +NORMALIZE_WHITESPACE
106 ['theq', 'hequ', 'equi', 'quic', 'uick', 'ickb', 'ckbr', 'kbro', 'brow',
107 'rown', 'ownf', 'wnfo', 'nfox']
109 return [text
[i
:i
+n
] for i
in range(len(text
)-n
+1)]
113 """A probability distribution estimated from counts in datafile.
114 Values are stored and returned as log probabilities.
116 def __init__(self
, data
=[], estimate_of_missing
=None):
117 data1
, data2
= itertools
.tee(data
)
118 self
.total
= sum([d
[1] for d
in data1
])
119 for key
, count
in data2
:
120 self
[key
] = log10(count
/ self
.total
)
121 self
.estimate_of_missing
= estimate_of_missing
or (lambda k
, N
: 1./N
)
122 def __missing__(self
, key
):
123 return self
.estimate_of_missing(key
, self
.total
)
125 def log_probability_of_unknown_word(key
, N
):
126 """Estimate the probability of an unknown word.
128 return -log10(N
* 10**((len(key
) - 2) * 1.4))
130 Pw
= Pdist(datafile('count_1w.txt'), log_probability_of_unknown_word
)
131 Pw_wrong
= Pdist(datafile('count_1w.txt'), lambda _k
, N
: log10(1/N
))
132 Pl
= Pdist(datafile('count_1l.txt'), lambda _k
, _N
: 0)
133 P2l
= Pdist(datafile('count_2l.txt'), lambda _k
, _N
: 0)
134 P3l
= Pdist(datafile('count_3l.txt'), lambda _k
, _N
: 0)
137 """The Naive Bayes log probability of a sequence of words.
139 return sum(Pw
[w
.lower()] for w
in words
)
141 def Pwords_wrong(words
):
142 """The Naive Bayes log probability of a sequence of words.
144 return sum(Pw_wrong
[w
.lower()] for w
in words
)
146 def Pletters(letters
):
147 """The Naive Bayes log probability of a sequence of letters.
149 return sum(Pl
[l
.lower()] for l
in letters
)
151 def Pbigrams(letters
):
152 """The Naive Bayes log probability of the bigrams formed from a sequence
155 return sum(P2l
[p
] for p
in ngrams(letters
, 2))
157 def Ptrigrams(letters
):
158 """The Naive Bayes log probability of the trigrams formed from a sequence
161 return sum(P3l
[p
] for p
in ngrams(letters
, 3))
164 def cosine_similarity_score(text
):
165 """Finds the dissimilarity of a text to English, using the cosine distance
166 of the frequency distribution.
168 >>> cosine_similarity_score('abcabc') # doctest: +ELLIPSIS
171 return norms
.cosine_similarity(english_counts
,
172 collections
.Counter(sanitise(text
)))
175 if __name__
== "__main__":