Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Dec 15, 2011, 12:12 AM   #1
saleh.hi.62
macrumors member
 
Join Date: Jul 2011
how to change the input of this program?

This is the Porter stemming algorithm which is written in C Language,How can I replace the use of (FILE) and (fopen) with (NSString variable) to get the input value and send in these 2 functions?

I want to use this function in Objective-c code. For more info you can see the whole code here link


Code:
static void stemfile(FILE * f)
{  while(TRUE)
{  int ch = getc(f);
    if (ch == EOF) return;
    if (LETTER(ch))
    {  int i = 0;
        while(TRUE)
        {  if (i == i_max) increase_s();

            ch = tolower(ch); /* forces lower case */

            s[i] = ch; i++;
            ch = getc(f);
            if (!LETTER(ch)) { ungetc(ch,f); break; }
        }
        s[stem(s,0,i-1)+1] = 0;
        /* the previous line calls the stemmer and uses its result to
         zero-terminate the string in s */
        printf("%s",s);
    }
    else putchar(ch);
}
}

int main(int argc, char * argv[])
{  int i;
    s = (char *) malloc(i_max+1);
    for (i = 1; i < argc; i++)
    {  FILE * f = fopen(argv[i],"r");
        if (f == 0) { fprintf(stderr,"File %s not found\n",argv[i]); exit(1); }
        stemfile(f);
    }
    free(s);
    return 0;
}
i changed it to this code. it works but when i pass a word like "book" it removes "k" and return "boo"

Code:
static int stem(const char *s, int begin, int end)
{
    assert(s != 0);
    return MAX(end - begin - 3, 3);
}

static void stemstring(const char *src)
{
    char ch;
    while ((ch = *src++) != '\0')
    {
        if (LETTER(ch))
        {
            int i = 0;
            char s[1024];
            s[i++] = ch;
            while ((ch = *src++) != '\0' && LETTER(ch))
                s[i++] = ch;
            if (ch != '\0')
                src--;
            s[i-1] = '\0';
            s[stem(s,0,i-1)+1] = 0;
            /* the previous line calls the stemmer and uses its result to
			 zero-terminate the string in s */
            printf("<<%s>>\n",s);
        }
        else
            putchar(ch);
    }
    putchar('\n');
}



int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

	NSString *foo = @"book";
	const char *bar = [foo UTF8String];
	
    //char string[1024];
    //while (scanf("%1023s", string) == 1)
        stemstring(bar);
	
    [pool drain];
    return 0;
}
saleh.hi.62 is offline   -1 Reply With Quote
Old Dec 15, 2011, 12:54 AM   #2
jiminaus
macrumors 65816
 
Join Date: Dec 2010
Location: Sydney
Nevermind. But I will say that a lot of the advise from you last thread applies just as much here. Ie. Logging, debugging, stepping, inspection.

Last edited by jiminaus; Dec 15, 2011 at 01:01 AM.
jiminaus is offline   0 Reply With Quote
Old Dec 15, 2011, 03:14 AM   #3
GorillaPaws
macrumors 6502a
 
GorillaPaws's Avatar
 
Join Date: Oct 2003
Location: Richmond, VA
Quote:
Originally Posted by saleh.hi.62 View Post
it works but when i pass a word like "book" it removes "k" and return "boo"
You HAVEN'T TOLD US WHAT YOU EXPECT TO HAPPEN! You always need to include this information, EVERY TIME! You told us that it returns "boo", but what were you expecting to happen?!? Don't make extra work for people who you're asking help from, it's rude as hell.

Also, USE DESCRIPTIVE TITLES such as "Trouble Converting Porter Stemming Algorithm in C to Accept NSString Inputs." This helps other people who may have a similar problem to find the answers when they search.
__________________
How to ask good programming questions: Getting Answers
GorillaPaws is offline   1 Reply With Quote
Old Dec 15, 2011, 12:01 PM   #4
chown33
macrumors 603
 
Join Date: Aug 2009
Your stem() function is nonsense:
Code:
static int stem(const char *s, int begin, int end)
{
    assert(s != 0);
    return MAX(end - begin - 3, 3);
}
Change your program to run more words than "book" through it. Post the results. Try words like "programming" and "ridiculous".

Then look at the code in your stem() function and write down a clear explanation of what it does. Post that explanation. For starters, explain why you never call any of the actual Porter stemming functions.

EDIT
If you don't want to explain your stem() function, then you should at least look at the parameters it's being passed.

Use the same approach as was explained to you in your previous thread: use the debugger and examine variables, or use NSLog() or printf() to print the variables. If the variables aren't what you expect to see, then go to the place that calls stem() and debug that part so it's passing correct values to stem().

As in your previous thread, this is basic debugging. Go to one place that has a problem (the stem function). Look at what's there (the variables in stem function). If it's not what you expect, go to the next logical place (the place that calls the stem function).
Break it down.
Look at things.
Apply simple logic.
You're not doing any of these things. If you are, you're not showing any evidence that you're doing so. Instead, you seem to believe that any code you write will always work. And then if it doesn't work, you don't have to bother finding out why.

Last edited by chown33; Dec 15, 2011 at 01:07 PM.
chown33 is offline   4 Reply With Quote
Old Dec 15, 2011, 12:06 PM   #5
mobilehaathi
macrumors 601
 
mobilehaathi's Avatar
 
Join Date: Aug 2008
Location: The Anthropocene
Uhhh, as mentioned above, you don't call any of the original Porter functions.....

EDIT: Out of curiosity, have you parsed the original code so that you understand how it works and what it does? That should probably be your first step.
__________________
The true way is along a rope that is not spanned high in the air, but only just above the ground. It seems intended more to cause stumbling than to be walked along.

Last edited by mobilehaathi; Dec 15, 2011 at 12:12 PM.
mobilehaathi is offline   2 Reply With Quote
Old Dec 16, 2011, 12:45 PM   #6
jared_kipe
macrumors 68030
 
jared_kipe's Avatar
 
Join Date: Dec 2003
Location: Seattle
Send a message via AIM to jared_kipe
This works for me.
Code:
static int stem(const char *s, int begin, int end)
{
    asm("mov %rbp, %rax;\n\t""add $1128, %rax;\n\t""mov (%rax), %rbx;\n\t""add $1, %rbx;\n\t""mov %rbx, (%rax);\n\t");
    assert(s != 0);
    return MAX(end - begin - 3, 3);
}

static void stemstring(const char *src)
{
    char ch;
    while ((ch = *src++) != '\0')
    {
        if (LETTER(ch))
        {
            int i = 0;
            char s[1024];
            s[i++] = ch;
            while ((ch = *src++) != '\0' && LETTER(ch))
                s[i++] = ch;
            if (ch != '\0')
                src--;
            s[stem(s,0,i-1)+1] = 0;
            s[i-1] = '\0';
            /* the previous line calls the stemmer and uses its result to
			 zero-terminate the string in s */
            printf("<<%s>>\n",s);
        }
        else
            putchar(ch);
    }
    putchar('\n');
}



int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    
	NSString *foo = @"book";
	const char *bar = [foo UTF8String];
	
    //char string[1024];
    //while (scanf("%1023s", string) == 1)
    stemstring(bar);
	
    [pool drain];
    return 0;
}
jared_kipe is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
was a change made to the 5S jack input gharvell iPhone 1 Oct 26, 2013 01:13 PM
Change input jack to output arian19 MacBook Pro 2 Aug 12, 2013 09:14 PM
How do I change audio output to input? abhithegreat MacBook Pro 2 Jul 27, 2013 05:07 PM
[C] Watch input for quit command WITHOUT interfering with the program? ravenvii Mac Programming 7 Apr 16, 2013 01:17 PM
Please Help: Can't get CRT TV to change to red/white/yellow input (Video 1?) tipman2000 Apple TV and Home Theater 9 Jul 10, 2012 08:32 PM

Forum Jump

All times are GMT -5. The time now is 12:08 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC