السلام عليكم لدي الكود التالي ولا اعرف باي لغة تمت كتابته
class HMM(object):
...
def train(s, observations, iterations=100):
s2update = list(s._states)
for state in [s.INITIAL, s.FINAL]:
if state in s2update: s2update.remove(state)
for _ in range(iterations):
# run the forward and backward algorithms and get the
# probability of the observations sequence
forword_P = s._forward(observations)
backward_P = s._backward(observations)
obs_prob = forword_P[len(observations)][s.FINAL]
# calculate probabilities of being at a given state and
# emitting observation i
emission_probs = ddict(lambda: {})
for i, observation in enumerate(observations):
for state in s2update:
emission_probs[state] = (
forword_P[state] * backward_P[state] /obs_prob)
# calculate probabilities of taking the transition
# between a pair of states for observations i and i + 1
transition_probs = ddict(lambda: ddict(lambda: {}))
transition_indices = range(len(observations) - 1)
for i in transition_indices:
next_obs = observations[i + 1]
for state1 in s2update:
for state2 in s2update:
transition_probs[state1][state2] = (
forword_P[state1] *
s._transitions[state1][state2] *
s._emissions[state2][next_obs] *
backward_P[i + 1][state2] /
obs_prob)
# update transition probabilities by summing the
# probabilities of each state-state transition
for state1 in s2update:
total = 0
for state2 in s2update:
count = s._transitions[state1][state2] = sum(
transition_probs[state1][state2]
for i in transition_indices)
total += count
# normalize counts into probabilities
if total:
for state2 in s2update:
s._transitions[state1][state2] /= total
# find which observations occurred at which indices
observation_indices = ddict(lambda: [])
for i, observation in enumerate(observations):
observation_indices[observation].append(i)
# update emission probabilities by summing the
# probabilities for each state-observation pair
for state in s2update:
total = 0
for obs, indices in observation_indices.items():
count = s._emissions[state][obs] = sum(
emission_probs[state] for i in indices)
total += count
# normalize counts into probabilities
if total:
for obs in observation_indices:
s._emissions[state][obs] /= total
def _backward(s, observations):
# initialize the trellis
probs = ddict(lambda: ddict(lambda: 0.0))
# all states have equal probability of the final state
for state in s._states:
probs[len(observations) - 1][state] = 1.0
# update the trellis for each observation
for i in xrange(len(observations) - 2, -1, -1):
for state in s._states:
# sum the probabilities of transitioning to
# the current state and emitting the current
# observation from any of the previous states
probs[state] = sum(
probs[i + 1][next_state] *
s._transitions[state][next_state] *
s._emissions[next_state][observations[i + 1]]
for next_state in s._states)
# sum the probabilities of transitioning from the start
# state to any of the paths in the trellis
probs[0][s.INITIAL] = sum(
probs[0][state] *
s._transitions[s.INITIAL][state] *
s._emissions[state][observations[0]]
for state in s._states)
return probs
الكتاب الذي اتعامل معه يتكلم عن لغة البيرل على ما اظن
لكن لا اعلم هل هذا الكود مكتوب بهذه اللغه ام لا؟
اذا كان الجواب ب، لا هل من الممكن تفيدونني لاي قسم اتوجه علما بان المادة مادة معالجة لغات طبيعيه
ولكم كل الشكر