Jump to content

Pull info from more than one subcategory


Recommended Posts

I think this will be an easily rectifiable bug, but I haven't been able to sort it out myself. I need to change the line 

 

foreach( $translations->term0->PrincipalTranslations as $translation ):

 

to also somehow incorporate the source


 

 $translations->term0->Entries

 

 

 

since sometimes the only available info is under "Entries" rather than "PrincipalTranslations". I tried doubling up the foreach loops (repeating the relevant part of the script twice), but despite the fact that both loops work on their own, when I put one after the other it just stalls for awhile and then fails.

 

I also tried inserting an if statement with the following approach:

 

foreach( $translations->term0 as $general ):

if ($general == "PrincipalTranslations"):

but there I think I'm making a syntax error that I don't know how to overcome.

 

 

For reference, here's the whole script:

 

require_once('workflows.php');

$w = new Workflows('wordreference');
$id = $w->get('api','settings.plist');
if(!$id){
$w->result(
'wordreference-noapi',
'You must provide a WordReference API to use the workflow',
"Get an API using 'getapi', then set it with 'setapi'",
'icon.png',
'yes'
);
echo $w->toxml();
return;
}
$query = urlencode( $argv[1] );
$translations = $w->request( $url );
$translations = json_decode( $translations );
$i = "";
foreach( $translations->term0->PrincipalTranslations as $translation ):
foreach( $translation as $trkey => $tr ):
$icon='icon.png';
if ($trkey == "OriginalTerm"):
$icon = 'france.png';
endif;
if ($trkey !== "Note"):
$w->result( 
$i, 
$query, 
$tr->term,
$tr->POS . " " . $tr->sense,  
$icon, 
'yes' );
endif;
endforeach;
endforeach;
echo $w->toxml();

 

 

Thanks so much for all your help.

Link to comment

I'll try to help but rather than describing errors and showing a bunch of code, could you tell me what it is that you are hoping to do and what ISN'T happening that you expect to? I'm not sure I understand what it is that you are trying to do right now.

Link to comment

Sorry, I lack the correct terminology to explain the problem quickly. Basically, I have Alfred returning inline dictionary results, from a subheading (?) that's labeled "PrincipalTranslation" - which in most instances is where the relevant results are listed. However, I recently noticed that results that exist on the website I'm pulling everything from are sometimes not returned in Alfred at all; this is occurring in instances where there is no "PrincipalTranslation" category, but rather one labelled "Entries". 

 

In case this is unclear, here is the typical way results are presented (which returns inline results without any issues): http://api.wordreference.com/0.8/80186/json/fren/chien. And here is the alternative way: http://api.wordreference.com/0.8/80186/json/fren/fouir

 

So what I want to do is alter my script to have Alfred list inline results first for the "PrincipalTranslation" category and then for the "Entries" category, so that it won't miss out on dictionary entries in instances where translations exist but aren't labeled "PrincipalTranslation". That's what led me to take the approaches I mentioned above. 

 

Does that make sense? Just let me know if you need any further clarifications.

Link to comment

Sorry, I lack the correct terminology to explain the problem quickly. Basically, I have Alfred returning inline dictionary results, from a subheading (?) that's labeled "PrincipalTranslation" - which in most instances is where the relevant results are listed. However, I recently noticed that results that exist on the website I'm pulling everything from are sometimes not returned in Alfred at all; this is occurring in instances where there is no "PrincipalTranslation" category, but rather one labelled "Entries". 

 

In case this is unclear, here is the typical way results are presented (which returns inline results without any issues): http://api.wordreference.com/0.8/80186/json/fren/chien. And here is the alternative way: http://api.wordreference.com/0.8/80186/json/fren/fouir

 

So what I want to do is alter my script to have Alfred list inline results first for the "PrincipalTranslation" category and then for the "Entries" category, so that it won't miss out on dictionary entries in instances where translations exist but aren't labeled "PrincipalTranslation". That's what led me to take the approaches I mentioned above. 

 

Does that make sense? Just let me know if you need any further clarifications.

Ah gotcha. Well what you want to do then it check for the existence of both. If they exist, check and see how many results are in it. You could make it loop through all results in both sections if available. Check out using the php isset and count functions. Those will have you check if the pieces exist and then determine if you should read results based on the number of items in it. Let me know if you need more information than that.

Link to comment

Ah gotcha. Well what you want to do then it check for the existence of both. If they exist, check and see how many results are in it. You could make it loop through all results in both sections if available. Check out using the php isset and count functions. Those will have you check if the pieces exist and then determine if you should read results based on the number of items in it. Let me know if you need more information than that.

 

Thanks for the response. I'm not certain how to go about designing it exactly as you said, but I did incorporate isset into the script using an if statement. Unfortunately the whole thing works only if all of the pieces exist; as soon as one is missing, it fails. I'm guessing it's because the isset if statement is listed within a foreach loop, so I tried included an "else, break" style statement, but with no success - it continues to work if all pieces exist, otherwise it fails.

 

In case that's unclear, here's one of the loops:

foreach( $translations->term0->Entries as $translation ):

if (isset($translation)){
foreach( $translation as $trkey => $tr ):
$icon='icon.png';
if ($trkey == "OriginalTerm"):
$icon = 'france.png';
endif;
if ($trkey !== "Note"):
$w->result( 
$i, 
$query, 
$tr->term,
$tr->POS . " " . $tr->sense,  
$icon, 
'yes' );
endif;
endforeach;
} else {
break;
endforeach;
 
I tried putting the final "endforeach" before the else statement as well, but that just causes the whole thing to fail regardless of whether the different pieces all exist. 
 
I'm not sure if I'm making a minor syntax error or if the structure must be changed if I want to use isset in this way. I also tried constructing the relevant if statement with reference to a variable created using the count function, which had the same partial failures as the isset approach. 
 
Thanks again for the help.
Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...