So I'm coming along in my studies into programming and Python, and am trying to write a bit of code to make a word scanner in Python. The object is to take a string of words given by the user and return a list of tuples which reads in the format (TYPE, WORD), where TYPE is the type of word it is, and WORD is the word itself. I determine the type of word by searching for the word in a group of lists containing words of that type. If the word is there, or is a number, the tuple returns the corresponding type and the word; if the word is not there, the tuple returns an "error" and the word. So for example, if the input is "My dog went north for 3 days", the list being returned would read something like:
[('error', 'My'), ('noun', 'dog'), ('error', went),
('number', 'north'), ('error', 'for'), ('number', '3'), ('error', 'days')]
Or something like that.
Anyway, when I input the code to try and get this to work by running it from the main project directory in the command line, the only value I kept getting back in return was this message called
<__main__.scan object at 0x7fe88b5cd750>
No matter how I tried to rejuggle the code, and no matter what I tried to declare the final printed sentence as, I kept getting that main.scan object thing as the value for the string in the scanner.
Here is the code I made for reference:
from otherfunc import numconv
class scan(object):
def __init__(self, string):
self.string = string
def scanner(self):
direction = ['north', 'south', 'east', 'west', 'down',
'up', 'left', 'right', 'back']
verb = ['go', 'stop', 'kill', 'eat']
stop = ['the', 'in', 'of', 'from', 'at', 'it']
noun = ['door', 'bear', 'princess', 'cabinet']
words = split(self.string)
sentence = []
for word in words:
if word in direction:
analysis = ('direction', word)
sentence.append(analysis)
elif word in verb:
analysis = ('verb', word)
sentence.append(analysis)
elif word in stop:
analysis = ('stop', word)
sentence.append(analysis)
elif word in noun:
analysis = ("noun", word)
sentence.append(analysis)
else:
value = numconv(word)
if value != None:
analysis = value
sentence.append(analysis)
else:
analysis = ('error', word)
sentence.append(analysis)
return sentence
string = raw_input("Type in your sentence here: ")
sentlist = scan(string)
print sentlist
And here is the code for the module I imported in the code above:
class numconv(object):
def __init__(self, s):
self.s = s
def convert_number(self, s):
try:
self.value = int(s)
return ("number", self.value)
except ValueError:
return None
The thing is, I've gotten the basic framework of this to work as intended in another format. In that format, I didn't declare classes and just called everything you see above in the "scan" and "numconv" classes as modules from files to run the scanner. But I'm trying to convert this into a "class" format, and trying to call the scanner as instances of "scan" classes. And so far, instead of getting the list of (TYPE, WORD) tuples I want, I just keep getting that main.scan object thing as the returned value. If anyone could be so kind, could they explain where I went wrong in my attempt to convert this into a class format, and why I only keep getting that "main.scan object" thing as the returned value when I try to call the scan class with an user-input string? Thanks.