tags:

views:

172

answers:

2

How to do n0gram algorithm program in php

A: 

some time ago I had to make something like this and I based the project on this class. at least some parts of it

Gabriel Sosa
+1  A: 

I'm afraid I'm not familiar with n-grams, but you might want to try:

preg_match('/[0-9]+[^0-9]+[0-9]+\s/',$string,$matches);
return $matches[0];

The code searches the first instance of 1 or more numbers followed by a series of non-numbers followed by 1 or more numbers followed by a space, stores the sequence in $matches[0], and returns $matches[0].

This wouldn't assign any values, but it would return the address you're looking for, according to your rules.

Steven Xu
More than being unnecessary, the "\s" keeps this from meeting the requirements. Add grouping to capture the substrings: `/(\d+)(\D+)(\d+)/` (note: `\d` matches a digit and `\D` matches a non-digit).
outis
\d and \D are acceptable. I like [0-9] for readability. I added the \s because of this third requirement for the END string to end with a number label. I read this requirement that mean that "100a" should not be accepted as the end of an address string. Consider the sample string "I live on 31 West 21st St. 90210 with my dad". /(\d+)(\D+)(\d+)/ would match "31 West 21". /[0-9]+[^0-9]+[0-9]+\s/ would match "31 West 21st St. 90210".
Steven Xu