statement position

Hi.
Here I have a code snippet. It's curious:
1
2
3
4
5
6
7
8
 cout << musicMap.size() << endl;  //0 is printed         
    while( word_music[i++] )
    {
       word_music[i]= strtok(0, " .,;!?");
       cout << word_music[i] << " ";
       musicMap[ word_music[i-1] ]++;
    }    
 cout << musicMap.size();//nothing prints 

Why does it behave so?

Thanks in advance
¿which is the type of `musicMap' ?


> nothing prints
¿did your program terminated normally? In that case PICNIC
What does PICNIC mean?
Yes, it terminated with no error that is obvious to me.

the definition for musicMap is:
map<string, int> fishMap, musicMap;
closed account (z05DSL3A)
What does PICNIC mean?
Problem In Chair Not In Computer
Now that it terminated normally, why does it behave so?
But ne55, I do have a test case, TWA data set.
closed account (z05DSL3A)
hooshdar3,

Your code snippet makes no sense (at least to me).

Can you post something that can compile and shows that problem you are having?
This is my whole code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include <iostream>
#include <io.h>
#include <string>
#include <cstring>
#include <map>
#include <fcntl.h>

using namespace std;

#define FILENAME "G:\\MS_shirazu\\TextMining\\HWs\\TWA\\TWA\\Bass.Test"

int main()
{
    char buffer[120000] = {0}, fishBuffer[120000] = {0}, audioBuffer[120000] = {0};
    
    int fh = _open(FILENAME, O_RDONLY);
    _read(fh, buffer, 120000 );
    
        map<string, int> fishMap, musicMap;
        
    //cout << buffer << endl;
    int i=0;
    char *pc0=&buffer[0]; //points to  the first element
    char *pc1;
    char *pc2;
    char *ps1, *ps2;
    
    int senseNo = 1;
 
    
    ps1 = strstr(buffer, "%");  //searching for the first occurrence of %
      pc1 = strstr(buffer, "<context>");  //searching for the first occurrence of <context>
      pc2 = strstr(pc1, "</context>");
    ps2 = strstr(ps1, "/>");
      switch( *(ps1+1) )
      {
          case 'm':
               strncat(audioBuffer, pc1+10, pc2-10-pc1);
               break;
          case 'f':

               strncat(fishBuffer, pc1+10, pc2-10-pc1);
               break;
      }//end switch

    ps1=ps2;
    
    while(ps1 &&senseNo<107)
    {    
      ps1 = strstr(ps1, "%");
      ++senseNo ;
      ps2 = strstr(ps1, "\"/>");    
      pc1 = strstr(pc1, "<context>");  //searching for the first occurrence of <context>
      pc2 = strstr(pc1, "</context>");

      switch( *(ps1+1) )
      {
          case 'm':
               strncat(audioBuffer, pc1+10, pc2-10-pc1);
               break;
          case 'f':
               strncat(fishBuffer, pc1+10, pc2-10-pc1);
               break;
      }//end switch

      ps1=ps2;
      pc1=pc2;
    }
  i=0;

    
    char **word_fish  = new char *[strlen(fishBuffer)];
    for(int j=0;j<strlen(fishBuffer); ++j)
       word_fish[j] = new char[strlen(fishBuffer)];
        word_fish[i] = strtok(fishBuffer, " .,;!?");
cout <<strlen(audioBuffer);
    char **word_music  = new char *[strlen(audioBuffer)];
    for(int k=0;k<strlen(audioBuffer); ++k)
      word_music[k] = new char[100];
      
      word_music[50][50] = 'c';
      cout << word_music[50][50];
        

//cout << word_music[0] << endl;
    while( word_fish[i++] )
    {
       word_fish[i] = strtok(0, " .,;!?");
       fishMap[ word_fish[i-1] ]++;
    }
    
    i=0;
    int f;
// cout << strlen( audioBuffer );
      word_music[i] = strtok(audioBuffer, " .,;");
    while( word_music[i++] )
    {
       
       word_music[i]= strtok(0, " .,;");
       //cout << word_music[i] << " ";
       i=f;
       musicMap[ word_music[i-1] ]++;

    }    
 cout << i;//doesn't print

cin >> f;
////////////////////////////////////
string *ds = new string[senseNo];

 

    
    return 0;
}
closed account (z05DSL3A)
Line 93 you declare f and line 101 you assign f to i but I don't see any initialisation of f so it is going to be junk.


_____________________
Side Note:

You have a type mismatch in you for loops; as strlen returns size_t, j should be size_t.

for (size_t j = 0; j < strlen( fishBuffer ); ++j)
Last edited on
Oops!
I changed line 93 to
f=i;
now it works
But when I changed line 79 to word_music[k] = new char[strlen(audioBuffer)];
It again fails to perform as expected
It's like an exception/overflow. What do I do?
Last edited on
closed account (z05DSL3A)
I'm still trying to work out what you are trying to achieve. You seem to be trying to process a file that has something to do with fish and music (I guess the link is bass). The file looks structured, something like XML but I'm loosing what you are trying to do in all the assigning of memory that you are doing.

I'm also at work, so am only giving it quick looks at the moment.
Grey Wolf (4051)
I'm still trying to work out what you are trying to achieve. You seem to be trying to process a file that has something to do with fish and music (I guess the link is bass). The file looks structured, something like XML but I'm loosing what you are trying to do in all the assigning of memory that you are doing.

I'm also at work, so am only giving it quick looks at the moment.


Yes, you are right.
I am trying to process a file containing some contexts about 'bass', and as bass has two senses I am trying to separate the contexts into two buffers.

I am trying to tokenize the buffers so that I can find the most frequent word in them, that is why I am using maps.
Last edited on
closed account (z05DSL3A)
Just a quick thought. I'm assuming that up to line 70 does what you want it to. To do a word count on the fishBuffer you could do something like this:
1
2
3
4
5
6
7
8
9
10
11
12
    typedef std::map<std::string, std::size_t> WordCountMapType;

    std::stringstream ss(fishBuffer);

    WordCountMapType fishWordCounts;

    for (std::string word; ss >> word;) 
    {
        ++fishWordCounts[word];
    }

    std::cout << fishWordCounts.size() << " words found.\n";


You could do some punctuation clean-up in the for loop before adding it to the map.
Used https://github.com/qiuwei/snlp/blob/master/snlp-07/TWA.sensetagged/bass.test as input file
(which you should have provided)

> I changed line 93 to
> f=i;
> now it works
it would depend on the value of `f', but you probably were in an infinite loop. So your program did not terminate.


By the way
1
2
3
for(int j=0;j<strlen(fishBuffer); ++j)
    word_fish[j] = new char[strlen(fishBuffer)];
word_fish[i] = strtok(fishBuffer, " .,;!?"); //memory leak 
Why memory leak?
Let's simplify the snip
1
2
3
4
5
char buffer[100];

char *ptr = NULL;
ptr = new char[42]; //modifies the pointer
ptr = buffer; //modifies the pointer 
You ask for some memory, and the next thing you do is to loose it.
You don't release that memory, you don't have a delete[] operation. And you couldn't have, ¿what would you delete[]?
Last edited on
Topic archived. No new replies allowed.