1

I have a huge library file containing a word and it's synonyms, this is some words and their synonyms in the format of my library:

aantarrão|1
igrejeiro|igrejeiro|aantarrão|beato

aãsolar|1
desolar|desolar|aãsolar|afligir|arrasar|arruinar|consternar|despovoar|devastar|magoar

aba|11
amparo|amparo|aba|abrigo|achego|acostamento|adminículo|agasalho|ajuda|anteparo|apadrinhamento|apoio|arrimo|asilo|assistência|auxíjlio|auxílio|baluarte|bordão|broquel|coluna|conchego|defesa|égide|encosto|escora|esteio|favor|fulcro|muro|patrocínio|proteção|proteçâo|resguardo|socorro|sustentáculo|tutela|tutoria
apoio|apoio|aba|adesão|adminículo|amparo|aprovação|arrimo|assentimento|base|bordão|coluna|conchego|descanso|eixo|encosto|escora|espeque|fé|fulcro|proteçâo|proteção|refúgio|socorro|sustentáculo
beira|beira|aba|beirada|borda|bordo|cairel|encosta|extremidade|falda|iminência|margem|orla|ourela|proximidade|rai|riba|sopé|vertente
beirada|beirada|aba|beira|encosta|falda|margem|sopé|vertente
encosta|encosta|aba|beira|beirada|clivo|falda|lomba|sopé|subida|vertente
falda|falda|aba|beira|beirada|encosta|fralda|sopé|vertente
fralda|fralda|aba|falda|raiss|raiz|sopé
prestígio|prestígio|aba|auréola|autoridade|domínio|força|halo|importância|influência|preponderância|valia|valimento|valor
proteção|proteção|aba|abrigo|agasalho|ajuda|amparo|apoio|arrimo|asilo|auspiciar|auxílio|bafejo|capa|custódia|defesa|égide|escora|fautoria|favor|fomento|garantia|paládio|patrocínio|pistolão|quartel|refúgio|socorro|tutela|tutoria
sopé|sopé|aba|base|beira|beirada|encosta|falda|fralda|raiz|vertente
vertente|vertente|aba|beira|beirada|declive|encosta|falda|sopé

see aantarrão is a word and below it are the synonyms, I can't think of a way to get the word and the synonyms on an associative array, this is what I'm trying to do:

<?
$file = file('library.txt');
$array_sinonimos = array();
foreach($file as $k)
{
    $explode = explode($k, "|");
    if(is_int($explode[1]))
    {
        $word = $explode[0];
    } 
}
?>

nothing, lol, what can I do here ? loop lines until I find an empty line then try to get a new word with the explode ?, help !

1
  • You may want to read in the file line by line and tell it to execute logic based on when the line is null. Commented Jul 19, 2011 at 6:05

2 Answers 2

2

Here's some code I cooked up that seems to work.

See the code in action here: http://codepad.org/TVpYgW91

See the code here

UPDATED to read line by line

    <?php 
    $filepointer = fopen("library.txt", "rb");
 $words = array();

 while(!feof($filepointer)) {
        $line = trim(fgets($filepointer));
        $content = explode("|", $line);
        if (count($content) == 0)
        continue;
        if (is_numeric(end($content))) {
        $word = reset($content);
        continue;
        }

        if (isset($words[$word]))
        $words[$word] = array_merge($words[$word], $content);
        else
        $words[$word] = $content;
    }

    print_r($words);

So what's the strategy?

  1. fix up the line endings
  2. run through the file line by line
  3. ignore empty lines (count($content))
  4. split the line up on the pipes, if the line has a numerical value for the last value, then this becomes our word
  5. we only get to the last step if none of the other traps got touched, because of the continue statements, so if it is then just split up the words by the pipe and add them to or create the array element.
Sign up to request clarification or add additional context in comments.

2 Comments

got this fatal error: Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 24 bytes) in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\Dicio nario_sinonimos\formatarLibrary.php on line 8
Oh snap... file size issues? okay.. looks like you have to read it line by line
1

Try this. I can't remember if array_merge() will work with a null, but the basic idea is that $word is the $key to the assoc array.

<?
$file = file('library.txt');
$array_sinonimos = array();
foreach($file as $k)
{
    $explode = explode($k, "|");
    if(is_int($explode[1]))
    {
        $word = $explode[0];
    }
    else if(!empty($explode))
    {
        $array_sinonimos[$word] = array_merge($synonyms[$word], $explode);
    }
}
?>

1 Comment

it doesn't work with null in php 5.2 and up yo... also, is_int isn't a good test because that depends on php's ability to auto set the variable as an integer. If there was a space in there it might become a string. Also, what happens if your explode didn't have 2 elements, so explode[1] doesn't exist. Other than that, good concept. sorry I understand that you copied the code from the question, so +1 for concept

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.