Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
pos-tag/src/PosTrainingSet.php
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
51 lines (47 sloc)
1.06 KB
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
<?php | |
use NlpTools\Documents\TrainingSet; | |
use NlpTools\Documents\WordDocument; | |
class PosTrainingSet extends TrainingSet | |
{ | |
/** | |
* Read word-tag pairs from a file and create a TrainingSet. | |
* The file should have the following format | |
* <word> <space> <tag> <new line> | |
* | |
* @param string|Iterator $file The filename that contains the tagged words or an iterator returninthe above formatted lines | |
* @return PosTrainingSet The training set from the file | |
*/ | |
public static function fromFile($file, $context=1) { | |
if (!($file instanceof Iterator)) { | |
$file = new SplFileObject($file); | |
} | |
$lines = array_filter( | |
array_map( | |
function ($line) { | |
return array_map("trim",explode(" ", $line)); | |
}, | |
iterator_to_array($file, false) | |
) | |
); | |
$words = array_map( | |
function ($l) { | |
return $l[0]; | |
}, | |
$lines | |
); | |
$tset = new PosTrainingSet(); | |
foreach ($lines as $idx=>$l) { | |
if (count($l)<2) | |
continue; | |
$tset->addDocument( | |
$l[1], // the tag | |
new WordDocument( | |
$words, | |
$idx, | |
$context | |
) | |
); | |
} | |
return $tset; | |
} | |
} |