Tommy5
Posts: 2
Joined: Mon Apr 11, 2016 5:24 am

Beginner: Help with comibining two codes

Wed Apr 13, 2016 12:56 pm

Hey everyone!
I am trying to program a code in Python for the field of Sentiment Analysis. So far I have two different Codes and I can't decide which one is better. Now I wonder if I could Combine These codes somehof but I have no idea how to do that, as I am a beginner in Python programming.. These are my code snippets:

Code: Select all

 Dict = {}
    for line in open('Pol_Dict.txt'):
        word, score = line.split('\t')
        Dict[word] = int(score)

    sentence = word_tokenize(''.lower())

    (sum( Dict.get(word, 0) for word in sentence ))

    sentences = sent_tokenize(''.lower())

    for s in sentences: print(s)

    for sentence in sentence:
       words = word_tokenize(sentence)
       ( sum( Dict.get(word, 0) for word in words) )

    text ='This is nice. This is bad.'

    result = []
    for sentence in sent_tokenize(text):
        pos = 0
        neg = 0
        for word in word_tokenize(satz):
            score = Dict.get(word, 0)
            if score > 0:
                pos += score
            if score < 0:
                neg += score
         result.append([pos, neg])

    for s in result: print(s)
and the other code is:

Code: Select all

def format_sentence(sentence):
         return {word: True for word in word_tokenize(satz) }

    pos_data = []
    with open('Positiv.txt') as f:
        for line in f:
            pos_data.append([format_sentence(line), 'pos'])

    neg_data = []
    with open('Negativ.txt') as f:
        for line in f:
           neg_data.append([format_sentence(line), 'neg'])

    training_data = pos_data[:3] +  neg_data[:3]
    test_data = pos_data[3:] + neg_data[3:]

    model = NaiveBayesClassifier.train(training_data)
The first code uses a dictionary with rated words (like -5 for negative and +5 for positive) and the second code snippet uses two files where sentences are simply tagged as positive or negative.
Do you have any idea how to Combine These two codes into one? Thank you for your help :)

Return to “Python”