Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
so it's saying the 'str' object has no attribute 'copy. from a normal input are their ways to fix it
and any ways to make my code better
thank you for your time

What I have tried:

this is my code
lines_seen = set() # holds lines already seen

#check for duplicates
with open("male.txt", "r+") as f:
    d = f.readlines()
    f.seek(0)
    for i in d:
        if i not in lines_seen:
            f.write(i)
            lines_seen.add(i)
    f.truncate()

with open("female.txt", "r+") as f:
    d = f.readlines()
    f.seek(0)
    for i in d:
        if i not in lines_seen:
            f.write(i)
            lines_seen.add(i)
    f.truncate()

def gender_features(word):
	return {'last_letter': word[-1]}

# output : {'last_letter': 'r'}
# importing libraries
import random
from nltk.corpus import names
import nltk
#nltk.download()

def gender_features(word):
	return {'last_letter':word[-1]}

# preparing a list of examples and corresponding class labels.
labeled_names = ([(name, 'male') for name in names.words('male.txt')]+
			[(name, 'female') for name in names.words('female.txt')])

random.shuffle(labeled_names)

# we use the feature extractor to process the names data.
featuresets = [(gender_features(n), gender)
			for (n, gender)in labeled_names]

# Divide the resulting list of feature
# sets into a training set and a test set.
train_set, test_set = featuresets[500:], featuresets[:500]

# The training set is used to
# train a new "naive Bayes" classifier.
classifier = nltk.NaiveBayesClassifier.train(train_set)
x2b = input("name: ")
print(classifier.classify(x2b))

# output should be 'male'
print(nltk.classify.accuracy(classifier, train_set))

# it shows the accuracy of our classifier and
# train_set. which must be more than 99 %
classifier.show_most_informative_features(10)
Posted
Updated 21-Aug-22 6:47am

I gave you the link to the documentation for str types earlier today. Many of the functions return a modified copy, but as you can see there is no specific copy method. But you have not explained where the error occurs.

[edit]
Take a look at the documentation at NLTK :: nltk.classify.naivebayes[^]. The classify method calls prob_classify passing in the parameter from your call. One of the next actions is to call copy on that parameter. But the str object does not have a copy method, as already shown. So the conclusion is that you are passing an invalid parameter to classify.
[/edit]
 
Share this answer
 
v2
Comments
Hiruy Metsihafe 21-Aug-22 2:27am    
on print(classifier.classify(x2b)).
sorry for the long reply I had something to do.
Richard MacCutchan 21-Aug-22 3:06am    
Sorry, I have no idea what that is supposed to mean. The error message refers to an attempt to use copy on a str object.
Hiruy Metsihafe 21-Aug-22 3:15am    
thanks for trying.
Richard MacCutchan 21-Aug-22 3:29am    
See my update above.
Hiruy Metsihafe 21-Aug-22 4:54am    
so I can't take input for a name
found the solution its
x2b = input("name: ")
print(classifier.classify(gender_features(x2b)))
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900