forked from isnowfy/snownlp
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
127 additions
and
15 deletions.
There are no files selected for viewing
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,6 +24,12 @@ | |
” | ||
《 | ||
》 | ||
( | ||
) | ||
【 | ||
】 | ||
[ | ||
] | ||
! | ||
, | ||
: | ||
|
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
Binary file not shown.
Binary file not shown.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
# -*- coding: utf-8 -*- | ||
from __future__ import unicode_literals | ||
|
||
import sys | ||
import marshal | ||
from math import log | ||
|
||
from ..utils import frequency | ||
|
||
|
||
class CharacterBasedGenerativeModel(object): | ||
|
||
def __init__(self): | ||
self.l1 = 0.0 | ||
self.l2 = 0.0 | ||
self.l3 = 0.0 | ||
self.status = ('b', 'm', 'e', 's') | ||
self.uni = frequency.NormalProb() | ||
self.bi = frequency.NormalProb() | ||
self.tri = frequency.NormalProb() | ||
|
||
def save(self, fname): | ||
d = {} | ||
for k, v in self.__dict__.items(): | ||
if hasattr(v, '__dict__'): | ||
d[k] = v.__dict__ | ||
else: | ||
d[k] = v | ||
if sys.version_info.major == 3: | ||
fname = fname + '.3' | ||
marshal.dump(d, open(fname, 'wb')) | ||
|
||
def load(self, fname): | ||
if sys.version_info.major == 3: | ||
fname = fname + '.3' | ||
d = marshal.load(open(fname, 'rb')) | ||
for k, v in d.items(): | ||
if hasattr(self.__dict__[k], '__dict__'): | ||
self.__dict__[k].__dict__ = v | ||
else: | ||
self.__dict__[k] = v | ||
|
||
def div(self, v1, v2): | ||
if v2 == 0: | ||
return 0 | ||
return float(v1)/v2 | ||
|
||
def train(self, data): | ||
now = [('', 'BOS'), ('', 'BOS')] | ||
for sentence in data: | ||
self.bi.add((('', 'BOS'), ('', 'BOS')), 1) | ||
self.uni.add(('', 'BOS'), 2) | ||
for word, tag in sentence: | ||
now.append((word, tag)) | ||
self.uni.add((word, tag), 1) | ||
self.bi.add(tuple(now[1:]), 1) | ||
self.tri.add(tuple(now), 1) | ||
now.pop(0) | ||
tl1 = 0.0 | ||
tl2 = 0.0 | ||
tl3 = 0.0 | ||
for now in self.tri.samples(): | ||
c3 = self.div(self.tri.get(now)[1]-1, self.bi.get(now[:2])[1]-1) | ||
c2 = self.div(self.bi.get(now[1:])[1]-1, self.uni.get(now[1])[1]-1) | ||
c1 = self.div(self.uni.get(now[2])[1]-1, self.uni.getsum()-1) | ||
if c3 >= c1 and c3 >= c2: | ||
tl3 += self.tri.get(now)[1] | ||
elif c2 >= c1 and c2 >= c3: | ||
tl2 += self.tri.get(now)[1] | ||
elif c1 >= c2 and c1 >= c3: | ||
tl1 += self.tri.get(now)[1] | ||
self.l1 = self.div(tl1, tl1+tl2+tl3) | ||
self.l2 = self.div(tl2, tl1+tl2+tl3) | ||
self.l3 = self.div(tl3, tl1+tl2+tl3) | ||
|
||
def log_prob(self, s1, s2, s3): | ||
uni = self.l1*self.uni.freq(s3) | ||
bi = self.div(self.l2*self.bi.get((s2, s3))[1], self.uni.get(s2)[1]) | ||
tri = self.div(self.l3*self.tri.get((s1, s2, s3))[1], | ||
self.bi.get((s1, s2))[1]) | ||
if uni+bi+tri == 0: | ||
return float('-inf') | ||
return log(uni+bi+tri) | ||
|
||
def tag(self, data): | ||
now = [((('', 'BOS'), ('', 'BOS')), 0.0, [])] | ||
for w in data: | ||
stage = {} | ||
for s in self.status: | ||
for pre in now: | ||
p = pre[1]+self.log_prob(pre[0][0], pre[0][1], (w, s)) | ||
if (not (pre[0][1], | ||
(w, s)) in stage) or p > stage[(pre[0][1], | ||
(w, s))][0]: | ||
stage[(pre[0][1], (w, s))] = (p, pre[2]+[s]) | ||
now = list(map(lambda x: (x[0], x[1][0], x[1][1]), stage.items())) | ||
return zip(data, max(now, key=lambda x: x[1])[2]) |
Binary file not shown.
Binary file not shown.
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