how to fix nltk stanford import error in python - nltk

I am trying to import stanford ner but getting error:
NLTK was unable to find the
C:/Users/.../stanford-ner-2018-10-16/classifiers/all.7class.distsim.crf.ser
file! Use software specific configuration paramaters or set the
STANFORD_MODELS environment variable.
Below the code:
from nltk.tag import StanfordNERTagger
import os
java_path = "C:/Program Files/Java/jre1.8.0_201/bin/java.exe"
os.environ['JAVAHOME'] = "JAVA_PATH" #this java path you get from command 'echo %PATH%'in terminal
st = StanfordNERTagger('C:/Users/.../stanford-ner-2018-10-16/classifiers/all.7class.distsim.crf.ser',
'C:/Users/.../stanford-ner-2018-10-16/stanford-ner.jar',encoding='utf-8')

Related

How to unregister wrong Cython extension?

I created Cython Extension
from distutils.core import setup
from Cython.Build import cythonize
import numpy
print (numpy.__file__)
setup(name='spacy text app',
ext_modules=cythonize("spacy.pyx", language="c++"),
include_dirs=[numpy.get_include()]
)
and register it as same name as "spacy" package name.
python spacyc.py build_ext --inplace
Now when i use spacy
import spacy
nlp = spacy.load("en_core_web_sm")
doc = nlp(u"The firm earned $1.5 million in 2017.")
I get this error
File "spacy.pyx", line 1, in init spacy
from cymem.cymem cimport Pool
ModuleNotFoundError: No module named 'spacy.strings'; 'spacy' is not a package
How to unregister wrong Cython extension?
Actually after a few hours I found out that you need to manually delete the .pyd file in the same directory that contains the script. I hope to help someone in the future.

Get programmatically Python3 default path in Octave base directory

I am using GNU Octave. I have script written in python3 and it is working if I do something like that: (it's .m file)
setenv PYTHON python;
[output,status]=python('python_file.py');
output
But some users do not have installed Python on their own. I found that python3.exe is available in "C:\Program Files\GNU Octave\Octave-6.2.0\usr\bin" for default users.
It is possible to get Octave default path and then setenv python pointing to GNU Octave Python directory programically, everything wrote in octave_file.m?
Python file:
import sys
import subprocess
python = sys.executable
subprocess.check_call([python, '-m', 'pip', 'install', 'numpy'], stdout=subprocess.DEVNULL)
import math
import numpy as np
import matplotlib.pyplot as plt
import matplotlib
...
result from octave:
/usr/bin/python.exe: No module named pip
Traceback (most recent call last):
File "C:\Users\X\Desktop\cw1.py", line 9, in <module>
subprocess.check_call([python, '-m', 'pip', 'install', 'numpy'], stdout=subprocess.DEVNULL)
File "/usr/lib/python3.8/subprocess.py", line 364, in check_call
raise CalledProcessError(retcode, cmd)
subprocess.CalledProcessError: Command '['/usr/bin/python.exe', '-m', 'pip', 'install', 'numpy']' returned non-zero exit status 1.

How to include python mysql.connector into AWS Chalice deployment?

I try to deploy an AWS lambda application, I implemented with the Chalice Python Framework. My app.py connects to a MySQL server and therefore has to
import mysql.connector
But on every invocation of one of my lambda functions I get an error in the log
'Unable to import module 'app': No module named mysql.connector'
I tried to add the mysql.connector to the requirements.txt file in the chalice project:
mysql_connector==2.1.6
And if I do so, 2 additional folders containing several files appear in the AWS lambda environment:
/mysql_connector-2.1.6.data
/mysql_connector-2.1.6.dist-info
But the error remains the same. How to deploy python mysql.connector with Chalice?
This finally worked for me:
lib_path=os.path.abspath(os.path.join(__file__, '..', 'mysql_connector-2.1.6.data', 'purelib'))
sys.path.append(lib_path)
import mysql.connector
Putting the "mysql_connector==2.1.6" into the "requirements.txt" file did install the mysql connector in lambda environment. I added the path of the package (../mysql_connector-2.1.6.data/purelib) to system path.

Gson import error in Scala.

I am using a Gson library for parsing Json data. I am trying to run a program from terminal as follows:
scala -classpath "*.jar" JsonParsing.scala
To which I am getting the following error:
JsonParsing.scala:2: error: object google is not a member of package com import com.google.gson.Gson
I am unsure as why this error is coming. When I have gson jar in accurate folder.
gson-2.2.2.jar
I am using import statements as follows:
import com.google.gson.Gson
import com.google.gson.JsonObject
import com.google.gson.JsonParser
Help on this error would be appreciated. Thanks.
Your dependancy not include google package.
You can use :
// https://mvnrepository.com/artifact/com.google.code.gson/gson
libraryDependencies += "com.google.code.gson" % "gson" % "2.8.0"
or download appropriate jar http://www.java2s.com/Code/Jar/g/gson.htm
Compile:
$ scalac -classpath <path_to_your_jar_files> -d classes " path/to/classes/you/want/to/compile/*
Execute:
$ scala -classpath classes:<path_to_your_jar_files> com.your.package.ClassYouWantToRun
This is not a good way of doing it because it's not scalable. You should be using a tool like SBT to build and run projects.

I get an error message when I try FreqDist() in NLTK -- NameError: name 'nltk' is not defined

I'm learning about the NLTK and my mac
is working fine except I have trouble with the FreqDist(). (I saw another question about FreqDist() but he was getting a different error message. TypeError: unhashable type: 'list')
Here's an example:
>>> from nltk.corpus import brown
>>> news_text = brown.words(categories='news')
>>> fdist = nltk.FreqDist([w.lower() for w in news_text])
Traceback (most recent call last):
` File "<stdin>", line 1, in <module>`
`NameError: name 'nltk' is not defined`
This error message is pretty consistent. I get this message every time I try the FreqDist(). Other commands like - >>> brown.fileids() are fine.
Thanks for your help!
Before you can use FreqDist, you need to import it.
Add a line as follows:
import nltk
or if you just want to use FreqDist you should try this:
>>> from nltk.corpus import brown
>>> from nltk import FreqDist
>>> news_text = brown.words(categories='news')
>>> fdist = FreqDist([w.lower() for w in news_text])
which means you haven't installed nltk.
follow these steps to install nltk:
1:go to this link https://pypi.python.org/pypi/setuptools at the end of page you find setuptools-7.0.zip (md5) download it, then unzip it. you can find easy_install.py python script.
2:use the command sudo easy_install pip. By this time pip will be installed ready to use, (make sure you are in the directory where you can find easy_install script file).
3:use this command sudo pip install -U nltk. successful execution ensure that nltk is now installed.
4:open the IDLE then you type the following:
import nltk
if nltk is installed properly then you will be returned with console.
setuptools are required for older versions of Python. There is no need for the same if you are running 3.2+
You can easily download the same from https://pypi.python.org/pypi/nltk
For more information on http://www.nltk.org/install.html
nltk requires data you need to download first.
Then run the following code:
import nltk
nltk.download('stopwords')
from nltk.corpus import stopwords
stopwords.words("english")