Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


New on LowEndTalk? Please Register and read our Community Rules.

All new Registrations are manually reviewed and approved, so a short delay after registration may occur before your account becomes active.

Rewrite a Python script into PHP (python/php programmer requested)

I have a requirement to use an interesting script written in Python.

I'd like a PHP implementation of it as I don't script with Python and want something I can work with in PHP.

$50 via Paypal sounds good, I imagine it'd take someone who can program in both around 30 minutes.

"InputWordList.txt" is a list of tab delimited [WORD] [WORD FREQUENCIES], here is a datasource:
https://github.com/hermitdave/FrequencyWords/blob/master/content/2016/en/en_full.txt

I'll consider the task complete when it returns the same data as the Python version.

def viterbi_segment(text):
      probs, lasts = [1.0], [0]
      for i in range(1, len(text) + 1):
          prob_k, k = max((probs[j] * word_prob(text[j:i]), j)
              for j in range(max(0, i - max_word_length), i))
          probs.append(prob_k)
          lasts.append(k)
      words = []
      i = len(text)
      while 0 < i:
          words.append(text[lasts[i]:i])
          i = lasts[i]
      words.reverse()
      return words, probs[-1]

def word_prob(word): 
      return dictionary.get(word, 0) / total

def words(text): 
      return re.findall('[a-z]+', text.lower()) 

# CREATE DICTIONARY OF WORDS TO COMPARE TO
dictionary = {}
with open('InputWordList.txt') as input:
      for entry in input:
          w = entry.split()
          dictionary[w[0]] = int(w[1])
max_word_length = max(map(len, dictionary))
total = float(sum(dictionary.values()))

# SPLIT URL
words, prob = viterbi_segment('thisisacombinedurl.com')

Comments

Sign In or Register to comment.