1

I have a CSV file with two columns contains sentence. for example Test.csv:

Col[1]
----------------------
This trip was amazing.

Col[2]
--------------------
The cats are playing.

so I did some nlp process:

with codecs.open('test.csv','r', encoding='utf-8', errors='ignore') as myfile:
     data = csv.reader(myfile, delimiter=',')
     next(data)
     stops = set(stopwords.words("english"))
     stemmer = PorterStemmer()
     for row in data:
        word_tokens1 = word_tokenize(row[1].lower())
        word_tokens2 = word_tokenize(row[2].lower())
        remo1 = [w for w in word_tokens1 if w in re.sub("[^a-zA-Z]"," ",w )]
        remo2 = [w for w in word_tokens2 if w in re.sub("[^a-zA-Z]"," ",w)]
        list1 = [w for w in remo1 if not w in stops]
        list2 = [w for w in remo2 if not w in stops]
        for w in list1:
           l = stemmer.stem(w)
           print(l)
        for w in list2:
           l2 = stemmer.stem(w)
           print(l2)

my problem is when I do stemming, and when I print it:

trip
amazi
cat 
play

it print each word in a row. how can I return to the sentence after stemming like:

Col[1]:
-------------------
trip amazi

Col[2]:
------------------- 
cat play
Yousra Gad
  • 363
  • 3
  • 15
  • Can you show a sample of your file? I am wondering why you use the csv package. As far as I see, you care about the rows. In a csv the columns are separated by a comma. The rows are separated by newline. – MAZDAK May 31 '17 at 14:01
  • It is in different coloum sorry I wrote as it as a code .. – Yousra Gad May 31 '17 at 14:03
  • So each line looks like "This trip was amazing,The cats are playing"? – MAZDAK May 31 '17 at 14:05
  • I edited the question. .I have a CSV file with two columns. .each column has a sentence. I want to stem each sentence and store the result as string.. for example. .the second col has a row contains the (the cats are playing )..after remove stopword and stemming it should be (cat play)..how can I join the result after stem each word. – Yousra Gad May 31 '17 at 14:30

1 Answers1

0

Here is a modified version of your code that produces the output that you want. The most important thing that you had to do was changing

for w in list1:
           l = stemmer.stem(w)
           print(l)
        for w in list2:
           l2 = stemmer.stem(w)
           print(l2)

to

stemmed_first = ""
            c = 0
            for w in list1:
                if c < len(list1)-1:
                    stemmed_first += stemmer.stem(w) + " "
                else:
                    stemmed_first += stemmer.stem(w)
                c += 1

and the same for list2. However, I made other small changes throughout your code:

stemmer = PorterStemmer()
stops = set(stopwords.words("english"))

with open('test.csv', 'rb') as csvfile:
    spamreader = csv.reader(csvfile, delimiter=',')

    for row in spamreader:
        if len(row) >= 2:
            word_tokens1 = nltk.tokenize.word_tokenize(row[0])
            word_tokens2 = nltk.tokenize.word_tokenize(row[1])
            remo1 = [w for w in word_tokens1 if w in re.sub("[^a-zA-Z]", " ", w)]
            remo2 = [w for w in word_tokens2 if w in re.sub("[^a-zA-Z]", " ", w)]
            list1 = [w for w in remo1 if not w in stops]
            list2 = [w for w in remo2 if not w in stops]

            stemmed_first = ""
            c = 0

            for w in list1:
                if c < len(list1)-1:
                    stemmed_first += stemmer.stem(w) + " "
                else:
                    stemmed_first += stemmer.stem(w)
                c += 1

            stemmed_second = ""
            c = 0

            for w in list2:
                if c < len(list2)-1:
                    stemmed_second += stemmer.stem(w) + " "
                else:
                    stemmed_second += stemmer.stem(w)
                c += 1

            print stemmed_first
            print stemmed_second
MAZDAK
  • 573
  • 1
  • 4
  • 16