I’m looking for the conversion from the PHP code to Delphi. Currently, i’m stuck when handling with the Isset() function in PHP code. Are there any way i can convert the below code into Delphi ?
$collection = array(
'doc1' =>'php powerbuilder',
'doc2' =>'php visual');
$dictionary = array();
$docCount = array();
foreach ($collection as $docID => $doc) {
$doc = strtolower($doc);
$terms = explode(' ', $doc);
$docCount[$docID] = count($terms);
foreach ($terms as $term) {
if (!isset($dictionary[$term])) {
$dictionary[$term] = array('df' => 0, 'postings' => array());
}
if (!isset($dictionary[$term]['postings'][$docID])) {
$dictionary[$term]['df']++;
$dictionary[$term]['postings'][$docID] = array('tf' => 0);
}
$dictionary[$term]['postings'][$docID]['tf']++;
}
}
PHP arrays are, according to the documentation, a one-size-fits-all data structure that works as an ordered list or as a hash map (dictionary). Delphi does not have a similar built in data structure, you can only get either vector (ordered list) behavior, or hash map / dictionary behavior. And the dictionary behavior is only easily accessible on Delphi 2009+ because that’s the version that introduced Generics.
The easy to use data structure that’s available on Delphi 2007 and can be used for
insettype of operations is theTStringList, in it’sSorted := Truemode. But this is no true dictionary, it’s just a sorted list of strings where each string can have a value associated with it. You would use it like this:This of course is not a complete answer, but should get you started.