Uploading and running Gensim model for data augmentation - python

I am trying to follow this example https://github.com/dsfsi/textaugment to upload a pre-trained Gensim model for data augmentation
import textaugment
import gensim
from textaugment import Word2vec
model = gensim.models.KeyedVectors.load_word2vec_format(r'\GoogleNews-vectors-negative300.bin', binary=True)
from textaugment import Word2vec
t = Word2vec(model)
t.augment('The stories are good')
but I get the following error:
TypeError: __init__() takes 1 positional argument but 2 were given
at line
t = Word2vec(model)
What am I doing wrong?

If you edit your question to include the full error message shown, including the traceback identifying exact files/lines-of-code leading to the error, it will often provide extra important info to know what's going wrong. (Whenever possible, show answerers all the text/output that you see, not just excerpts.)
But also, the examples at the page you link, https://github.com/dsfsi/textaugment, all show the model passed in as a named parameter (model=SOMETHING), not merely a positional parameter. You should try to do it the same way (and here I've changed the name of your local variabe to make it more distinct from the parameter-name, and removed the out-of-place r prefix):
my_model = gensim.models.KeyedVectors.load_word2vec_format('\GoogleNews-vectors-negative300.bin', binary=True)
t = Word2vec(model=my_model)
The error you got may be less confusing once you know, from experience or careful viewing of the traceback, that the call to the constructor Word2vec() actually calls another method, __init__(), behind the scenes. And that __init__() method receives both the newly-created instance, and whatever else you supplied, as 'positional' arguments. That is: 2 positional arguments, when it normally only expects 1 (the new instances), with any extra arguments as named arguments (model=SOMETHING style).

Related

python 3.9.5 | zipfile.ZipFile.extractall() missing 1 required positional argument: 'self'

according to docs.python.org/3/ its enough if i supply one argument "path", i did that too
zipfile.ZipFile.extractall(path="./"+repo['tag_name']+".zip")
# path is equal to "./v0.1.zip"
it throwed an error
zipfile.ZipFile.extractall(path="./"+repo['tag_name']+".zip")
TypeError: extractall() missing 1 required positional argument: 'self'
Like most Python classes, this one requires you to create an instance, then use its methods.
z = zipfile.ZipFile(repo['tag_name'] + ".zip")
z.extractall()
z.close()
You can condense this into a single line if you want to, but then you don't have access to the created object (z above) after this finishes. (For example, you can't call its .close() method, which is really mandatory according to the documentation, though I can't imagine that it really matters if you are only reading from the zip file.)
zipfile.ZipFile(repo['tag_name'] + ".zip").extractall()
The path argument to extractall says where to extract the members to (if you omit it, they will be extracted to the current directory), and is distinct from the input file name used in the constructor.
Classes and objects are just a distraction when you are doing something really simple, but they help organize any nontrivial code; when you have several zip files open, each one has its own object instance which keeps track of the individual metadata for each (which file, what members, the password, etc).
This is the sample code you could try.
zip ref object should be created first to use the instance.
import zipfile
zip_ref=zipfile.ZipFile("./ProcessExplorer.zip","r")
zip_ref.extractall(".")
zip_ref.close()

addAttr method on pymel objects

I can't seem to utilize the addAttr method correctly. I'm using the same arguments as when I call from pymel.core but it's not giving me the same results.
I'm trying to add a custom message attribute so I can easily search for certain types of objects later. When I do it from pymel.core and include the same object reference as an argument, it works fine.
#get object reference
test_object = pm.ls(sl=1)[0]
#this one spits out an error
test_object.addAttr(longName = 'custom', attributeType = 'message')
#this one works fine
pm.addAttr(test_object, longName = 'custom', attributeType = 'message')
I keep getting this error
Error: TypeError: file line 1: addAttr() takes exactly 2 arguments (1 given)
What additional argument is it looking for when I use it this way? I am clearly missing something obvious about how methods work but I can't figure it out.
The addAttr method exposed for DG nodes in Maya PyMel has following signature.
addAttr(attr, **kwargs)
Here attr is an positional argument representing the attribute name. The kwargs can be supplied with all other relevant flags used in pm.addAttr() method. So you have to pass the attribute name as first argument.
node.addAttr('custom', attributeType='message')
Hope this will help.
from cgsociety thread
pCube.addAttr('timeBasedAttr', keyable=True, attributeType='float', min=0.0, max=1.0)
you should write :
test_object.addAttr('custom', attributeType = 'message')
Ive tried and it doesn't output error.

TypeError: fit_transform() missing 1 required positional argument: 'raw_documents'

I'm trying to do feature extraction text with Sklearn, however I'm getting error
Type error:fit_transform() missing 1 required positional argument:
'raw_documents'
It seems I have to make complete some arguments with missing raw document, but i cannot find what is the caused the error, here's my code:
features=TfidfVectorizer.fit_transform(data.status).toarray()
label=data.label
features.shape
When running in the Jupyter notebook console I get the following errors:
TypeError Traceback (most recent call last)\
<ipython-input-3-614f2fa78a04> in <module>()
----> 1 features=TfidfVectorizer.fit_transform(data.status).toarray()
2 label=data.label
3 features.shape
TypeError: fit_transform() missing 1 required positional argument: 'raw_documents'
In scikit-learn, everything with a fit_transform is an instance of some type, which is to say that you'll need to initialize that instance first, where you are calling fit_transform as if it were a staticmethod.
So, either create the instance by letting vectorizer = TfidfVectorizer() and use vectorizer.fit_transform(data.status), or just use TfidfVectorizer().fit_transform(data.status) directly.
You can see this by having a look at the docs, noting that you seem to indeed be passing the only required argument, and recalling that instance methods in Python come with an implicit parameter, typically called self, so that what's happening in your code is that you're passing data.status as self, while raw_documents is left unset.

TypeError: gettext() missing 1 required positional argument: 'self' - python

I'm new to python and I'm tring to make a class for a modul which checking curses in texts.
can someone help please?
import urllib
class Checktext:
def __init__(self, text):
self.text = text
def gettext(self):
file = open(self.text, "r")
filetext = open.read()
for word in filetext.split():
openurl = urllib.request.urlopen("http://www.wdylike.appspot.com/?q=" + word)
output = openurl.read()
truer = "true" in str(output)
print(truer)
s = Checktext(r"C:\Users\Tzach\.atom\Test\Training\readme.txt")
Checktext.gettext()
You declared s as a new Checktext object, so you need to call s.gettext() not an un-instantiated Checktext.gettext(), as that has no self to refer to
The urllib is a package. You have to import the module request that is located in the package:
import urllib.request
The open(filename) return a file object. You want to call the method of that object:
filetext = file.read()
And as G. Anderson wrote, you want to call s.gettext() instead of Checktext.gettext(). The self inside is actually equal to the s outside. If you want to be weird then you actually can use also:
Checktext.gettext(s)
Notice the s passed as your missing parameter. Here Python actually reveals how the Object Oriented things are implemented internally. In majority of OO languages, it is carefully hidden, but calling a method of an object is always internally translated as passing one more special argument that points to the instance of the class, that is the object. When defining a Python method, that special argument is explicitly named self (by convention; you can name it differently -- you can try as the lecture, but you should always keep that convention).
Thinking about it thoroughly, you can get the key idea of the hidden magic of an OO language syntax. The instance of the class (the object) is actually only a portion of memory that stores the data part, and that is passed to the functions that implement the methods. The Checktext.gettext is actually the function, the s is the object. The s.gettext() is actually only a different way to express exactly the same. AS s is the instance of the Checktext class, the fact is stored inside the s. Therefore, the s.gettext() creates the illusion that the rigth code will be called magically. It fits with the trained brain better than the function approach if the s is thought as a tangible something.

Why can I bypass a "function takes exactly 1 argument (2 given)" error with assignment?

I am trying to use Twython to work with the Twitter API in python, and I am observing some behavior that seems odd to me.
If I run the following code...
from twython import Twython
from random import randint
twitter = Twython(APP_KEY, APP_SECRET, OAUTH_TOKEN, OAUTH_TOKEN_SECRET) # In the actual code, I obviously assign these, but I can't disclose them here, so the code won't work...
user_id = randint(1,250000000)
twitter_user = twitter.lookup_user(user_id)
I get this error.
Traceback (most recent call last):
File "Twitter_API_Extraction.py", line 76, in <module>
twitter_user = twitter.lookup_user(user_id) # returns a list of dictionaries with all the users requested
TypeError: lookup_user() takes exactly 1 argument (2 given)
The Twython docs indicate I only need to pass the user id or the screen name (https://twython.readthedocs.org/en/latest/api.html). Some googling suggested that this error usually mean I need to pass self as the first parameter, but I didn't quite get why.
However, if I use the following assignment instead...
twitter_user = twitter.lookup_user(user_id = randint(1,250000000))
Everything comes up roses. I can't figure out why this is, and it is a problem later in the code when I am trying to access followers using the same lookup_user function.
Any clarification on what is triggering this error and how I am bypassing it via assignment in the function call would be most appreciated!
Per the API documentation:
lookup_user(**params)
Returns fully-hydrated user objects for up to 100 users per request, as specified by comma-separated values passed to the user_id and/or screen_name parameters.
The ** syntax (documentation) means you need to provide named arguments (i.e. f(a=b)), in this case user_id and/or screen_name
In your first attempt you're trying to pass a positional argument (i.e. f(a)), which the function is not set up for.
The API states that lookup_user takes keyword arguments only. Keyword arguments take the form keyword=value, which is what you are doing with lookup_user(user_id=randint(1,...)). It means you cannot pass positional arguments, which is what you are doing with lookup_user(userid).

Categories