How to do SVM Tagging in NLTK Python on Unicode Data - unicode

Can anyone help me on how to use SVM tagging in NLTK Python.
I have the following code using TnT tagger. It works perfectly for me thereby generating the tagged output.
How to achieve the tagging with svm?
Kindly help.. thanks
import nltk
import re
import time
from nltk.corpus import indian
train_data = indian.tagged_sents('konkani.pos')[:300]
from nltk.tag import tnt
l = tnt_pos_tagger = tnt.TnT()
n=tnt_pos_tagger.train(train_data)
e = open('kkn-.txt',encoding='utf-8-sig').read()
print (tnt_pos_tagger.tag(nltk.word_tokenize(e)))

Related

Real-time matplotlib plotting within tkinter class object fed by multiprocess module queue

I have a fairly generic problem where I have a tkinter gui. I'm spawning sub-windows as sub-classes, and those sub-windows launch computationally heavy processes with unknown time-delays using the multiprocess package. This computationally heavy process occasionally pushes a result into a queue, which is monitored by a graphing loop using matplotlib in the main process of the sub-window class. Using multiple different approaches, which work in other contexts, I've been unable to get this graph to update live with a pretty basic python 3.11 install.
I'm sure there are multiple fundamental things with the packages I'm using which I do not understand. I'm fundamentally a hardware guy, and I hack and slash my way to self-automation (or self-replication) when necessary. I'm working very hard to move away from Matlab and Igor so I can compile things for free w/o spending forever creating my own classes. This particular lunatic greatly appreciates any help the broader community could throw him now that he's wandered in from the wilderness. I am open to broad answers. If the most correct is 'learn QT5', I will. I'm at a dead end.
The first approach uses solution from: Python realtime plotting
This approach works just fine as-is in my python install when I run it directly from console. It doesn't work when launched within spyder for some known issues. It breaks down when i run it directly from console within my class:
# -*- coding: utf-8 -*-
"""
Spyder Editor
This is a temporary script file.
Made by Matthew Earl Wallace the Reed
"""
#import os
import time
import numpy as np
#import cv2
#import scipy as sp
import matplotlib.pyplot as plt
#import matplotlib as mpl
try:
from matplotlib.backends.backend_tkagg import NavigationToolbar2TkAgg
except ImportError:
from matplotlib.backends.backend_tkagg import NavigationToolbar2Tk as NavigationToolbar2TkAgg
#from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
#import scipy.ndimage as nd
#from scipy.optimize import curve_fit, differential_evolution, least_squares
#import warnings
import multiprocess
#from IPython import get_ipython
#get_ipython().run_line_magic('matplotlib','qt')
#from scipy.interpolate import interp1d
#import dill
import tkinter as tk
#from tkinter import filedialog
#import glob
#import os.path
#from scipy.stats import linregress
#from scipy.stats import norm as normdist
#from copy import deepcopy
#from scipy.signal import find_peaks, peak_widths
#import matplotlib.colors as colors
#from tkinter import messagebox
#from datetime import date
#import threading as td
#import pyautogui as pg
#import multiprocessing as mp
#import time as time
from functools import partial
class trueautoLOL(tk.Toplevel):
def __init__(self,parent):
super().__init__(parent)
lengthtarget=float(tk.simpledialog.askstring('length','feed me length'))
tolerance=float(tk.simpledialog.askstring('Tolerance','Mas o menos'))
uniformitytarget=float(tk.simpledialog.askstring('Uniformity','What\'s good brother?'))
self.geometry('300x200')
self.title('I\'m a horrible window love me anyway')
q=multiprocess.Queue()
def genplot(parent):
global line,ax,canvas, fig, x,y, li
fig2 = plt.figure()
ax2 = fig2.add_subplot(111)
# some X and Y data
x = [0]
y = [0]
li, = ax2.plot(x, y,'o')
# draw and show it
fig2.canvas.draw()
plt.show(block=False)
genplot(self)
def optloop(q):
print("Istarted")
uniformity=np.random.rand(1)*100
length=np.random.rand(1)*100
error=200
counter=0
while uniformity>uniformitytarget or length>lengthtarget+tolerance or length<lengthtarget-tolerance:
time.sleep(1)
theset=np.random.rand(1)*20
print(theset)
q.put(theset)
error=np.random.rand(1)*100
uniformity=np.random.rand(1)*100
length=np.random.rand(1)*100
counter=counter+1
print(q)
q.put('Q')
def updateplot(q):
try:
result=q.get(False)
print(result)
if result != 'Q':
print(result)
x=[0,1,2]
y=[0,result,2]
# set the new data
li.set_xdata(x)
li.set_ydata(y)
ax.relim()
ax.autoscale_view(True,True,True)
fig.canvas.draw()
plt.pause(1)
updateplot(q)
else:
print('done')
except:
print("empty")
self.after(500,updateplot,q)
theprocess=multiprocess.Process(target=optloop,args=[q])
theprocess.start()
print(theprocess.is_alive())
updateplot(q)
def autoLOL():
window=tk.Tk()
window.title("LOOOL")
window.geometry('900x250')
updateLOL=tk.Button(window, text="True AutoLOL", command=partial(trueautoLOL,window))
updateLOL.grid(column=3,row=3)
window.mainloop()
if __name__=='__main__':
autoLOL()
The second approach attempts to use the tkinter canvas directly (and was my chronological first approach, into which I hacked-and-slashed the approach above).
The notable thing about this approach is that it WORKS, but only when launched from within spyder on a bog-standard anaconda 3.9 install with the extra packages installed w/ pip, but not when launched from spyder with a manual installation on python 3.11.
I'd very much like to cite the source of the overall architecture, but I copied it over sufficiently long ago I can no longer find it...
# -*- coding: utf-8 -*-
"""
Spyder Editor
This is a temporary script file.
Made by Matthew Earl Wallace the Reed
"""
import os
import time
import numpy as np
import cv2
import scipy as sp
import matplotlib.pyplot as plt
import matplotlib as mpl
try:
from matplotlib.backends.backend_tkagg import NavigationToolbar2TkAgg
except ImportError:
from matplotlib.backends.backend_tkagg import NavigationToolbar2Tk as NavigationToolbar2TkAgg
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
import scipy.ndimage as nd
from scipy.optimize import curve_fit, differential_evolution, least_squares
import warnings
import multiprocess
from IPython import get_ipython
#get_ipython().run_line_magic('matplotlib','qt')
from scipy.interpolate import interp1d
import dill
import tkinter as tk
from tkinter import filedialog
import glob
import os.path
from scipy.stats import linregress
from scipy.stats import norm as normdist
from copy import deepcopy
from scipy.signal import find_peaks, peak_widths
import matplotlib.colors as colors
from tkinter import messagebox
from datetime import date
import threading as td
import pyautogui as pg
import multiprocessing as mp
import time as time
from functools import partial
class trueautoLOL(tk.Toplevel):
def __init__(self,parent):
super().__init__(parent)
lengthtarget=float(tk.simpledialog.askstring('length','feed me length'))
tolerance=float(tk.simpledialog.askstring('Tolerance','Mas o menos'))
uniformitytarget=float(tk.simpledialog.askstring('Uniformity','What\'s good brother?'))
self.geometry('300x200')
self.title('I\'m a horrible window love me anyway')
q=multiprocess.Queue()
def genplot(parent):
global line,ax,canvas, fig
fig=mpl.figure.Figure()
ax=fig.add_subplot(1,1,1)
canvas = FigureCanvasTkAgg(fig, master=parent)
canvas.draw()
canvas.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=1)
canvas._tkcanvas.pack(side=tk.TOP, fill=tk.BOTH, expand=1)
plt.show(block=False)
line, = ax.plot([1,2,3], [1,2,10])
genplot(self)
def optloop(q):
print("Istarted")
uniformity=np.random.rand(1)*100
length=np.random.rand(1)*100
error=200
counter=0
while uniformity>uniformitytarget or length>lengthtarget+tolerance or length<lengthtarget-tolerance:
time.sleep(1)
theset=np.random.rand(1)*20
print(theset)
q.put(theset)
error=np.random.rand(1)*100
uniformity=np.random.rand(1)*100
length=np.random.rand(1)*100
counter=counter+1
print(q)
q.put('Q')
def updateplot(q):
try:
result=q.get(False)
print(result)
if result != 'Q':
print(result)
line.set_ydata(1,result,10)
ax.draw_artist(line)
ax.relim()
ax.autoscale_view(True,True,True)
fig.canvas.draw()
canvas.draw()
plt.show(block=False)
self.after(500,updateplot,q)
else:
print('done')
except:
print("empty")
self.after(500,updateplot,q)
theprocess=multiprocess.Process(target=optloop,args=[q])
theprocess.start()
print(theprocess.is_alive())
updateplot(q)
def autoLOL():
window=tk.Tk()
window.title("LOOOL")
window.geometry('900x250')
updateLOL=tk.Button(window, text="True AutoLOL", command=partial(trueautoLOL,window))
updateLOL.grid(column=3,row=3)
window.mainloop()
if __name__=='__main__':
autoLOL()
I've tried multiple solutions to live-updates of a matplotlib plot using a tkinter gui where the plot needs to periodically update in an infinite loop while fed data asynchronously by another process. Those solutions each individually work in very specific contexts, but fail in the context of my class-based multiprocessing architecture. I am open to anything that will work when 1. Function calls need to be performed on functions defined outside the class 2. some global variables are defined outside the class for said function calls and 3. the plotting update can happen periodically.
I've been working on this on-and-off for a month. My sole purpose is to monitor and plot data which is periodically output. If this can be done through file I/O or whatever, I'll do anything, simple or obtuse.
This was mostly a problem with multiprocessing & not fully understanding the matplotlib backend. The source for the canvas-based multiprocessing approach wasn't protecting things with
if __name__=='__main__':
so I wasn't either. When the multiprocess.Process was pickling (or dill'ing, as multiprocess module does) it was trying to re-run the functions initializing the window, re-start plots etc, which was frustrating the matplotlib backend.
The version version of the code I got to work is below:
class trueautoLOL(tk.Toplevel):
def __init__(self,parent):
super().__init__(parent)
lengthtarget=float(tk.simpledialog.askstring('length','feed me length'))
tolerance=float(tk.simpledialog.askstring('Tolerance','Mas o menos'))
uniformitytarget=float(tk.simpledialog.askstring('Uniformity','What\'s good brother?'))
self.geometry('300x200')
self.title('I\'m a horrible window love me anyway')
q=multiprocess.Queue()
fig = plt.figure()
ax = fig.add_subplot(111)
# some X and Y data
x = [0,1,2,3,4]
y = [0,1,2,3,4]
li, = ax.plot(x, y,'o')
# draw and show it
if __name__=='__main__':
fig.canvas.draw()
plt.show(block=False)
# loop to update the dat
q=multiprocess.Queue()
def datagen(qq):
qual=100
while qual<100.5:
data = [np.random.rand(1)*4,np.random.rand(1)*4,np.random.rand(1)*4,np.random.rand(1)*4,np.random.rand(1)*4 ]
qq.put(data)
qual = np.random.rand(1)*102
plt.pause(2)
qq.put('Q')
def plotter(qq):
try:
y=qq.get()
if y!='Q':
# set the new data
li.set_xdata(x)
li.set_ydata(y)
ax.relim()
ax.autoscale_view(True,True,True)
fig.canvas.draw()
plt.pause(1)
plotter(qq)
else:
print('Done')
plt.pause(1)
except:
print("empty")
plt.pause(1)
plotter(qq)
if __name__=='__main__':
theprocess=multiprocess.Process(target=datagen,args=[q])
theprocess.start()
plotter(q)
def autoLOL():
window=tk.Tk()
window.title("LOOOL")
window.geometry('900x250')
updateLOL=tk.Button(window, text="True AutoLOL",command=partial(trueautoLOL,window))
updateLOL.grid(column=3,row=3)
window.mainloop()
if __name__=='__main__':
autoLOL()

How to get clickable links to functions and classes on VS code for Mac

Sorry if the question is not entirely clear. Will add details if unclear.
PrintPlugin is a class from another python file I have imported. I used to get clickable links on this on VS code. Meaning, if I cmd+click on PrintPlugin it would go to that class on the source Python file.
My bottom bar used to look like this when it was working.
Now it looks like this
The difference is that I was having the Python 3.9.5 64-bit ('base':conda) earlier.
Everything else works fine. I am on my virtual environment too. I am not sure what else could be causing this. Will add any details required.
Edit:
I have added some reproducible code. This differs from the original image because the original was work related and I can't post it here. But I'm still running this in the same folder and in the same virtual environment, so it has the same problem (that I cannot cmd+click on Multiply() in process.py and go to the class on plugin.py)
File process.py
import numpy as np
from plugin import Multiply
a = np.array([[2,3],
[3,4]])
b = np.array([[5,6],
[7,8]])
result = Multiply()
print(result.multiply(a,b))
File plugin.py
import numpy as np
class Multiply():
def __init__(
self,
a = np.array([[1,1],
[1,1]]),
b = np.array([[1,1],
[1,1]])
):
return
def multiply(self,a,b):
c = a # b
return c
Output when I do python process.py
>>> [[31 36]
[43 50]]

Python Bokeh FileInput Widget for DLIS file

I am trying a web app in bokeh where i import a dlis file through the widget fileinput.
I am using the following libraries
from dlisio import dlis
from pybase64 import b64decode
import io
def upload_file_dlis(attr, old, new):
print('upload_file_dlis')
decoded = b64decode(new)
f = io.BytesIO(decoded)
file_input_dlis.on_change('value', upload_file_dlis)
I have the following issue:
OSError: '<_io.BytesIO object at 0x000001CE6507CF40>' is not an existing regular file
I guess it is because dlis files are not handled by Io library.
What is the best way to work on this?
Do you have any suggestions?

Is this code using JupyterDash or plotly dash?

The following code looks like it's a combination of both plotly dash and Jupyter dash and it was run via jupyter notebook. Can someone explain why both jupyter dash and plotly dash need to be used together?
from jupyter_dash import JupyterDash
from dash import Dash, dcc, html, Input, Output, no_update
import plotly.graph_objects as go
import pandas as pd
app = JupyterDash(__name__)
fig = go.Figure(data=[
go.Scatter(
x=df['x_lv'], #x_px and y_px for pixel data
y=df['y_lv'],
mode='markers',
marker=dict(color=df['color']), showlegend=True
)
])
# turn off native plotly.js hover effects - make sure to use
# hoverinfo="none" rather than "skip" which also halts events.
fig.update_traces(hoverinfo="none", hovertemplate=None)
server = app.server
app.layout = html.Div([
dcc.Graph(id="graph-basic-2", figure=fig, clear_on_unhover=True),
dcc.Tooltip(id="graph-tooltip"), html.Div(id="debug"),
])
#app.callback(
Output("graph-tooltip", "show"),
Output("graph-tooltip", "bbox"),
Output("graph-tooltip", "children"),
Input("graph-basic-2", "hoverData"),
)
def display_hover(hoverData):
if hoverData is None:
return False, no_update, no_update
# demo only shows the first point, but other points may also be available
pt = hoverData["points"][0]
bbox = pt["bbox"]
num = pt["pointNumber"]
app.run_server(mode="inline", host="localhost",port=8052)
The code you posted uses a JupyterDash app, identifiable from the line app = JupyterDash(__name__). However, it uses Dash components as part of the app, because JupyterDash (which is by the way developed by plotly, as is the "original" Dash) is basically the same thing as Dash, but tailored to run from within a Jupyter notebook. So a JupyterDash app can be built just like a Dash app, but will provide extra features for a smooth user experience in a Jupyter notebook.
For more details, I suggest reading the announcement of JupyterDash from plotly.

AttributeError for selfloop_edges()

When executing the following:
import networkx as nx
import matplotlib.pyplot as plt
import csv
with open("nutrients.csv") as file:
reader = csv.reader(file)
G = nx.Graph(reader) #initialize Graph
print(G.nodes()) #this part works fine
print(repr(G.edges))
G.selfloop_edges()#attribute of question
It's coming back with
AttributeError:"Graph" object has no attribute 'selfloop_edge'
Does anyone know what could be the issue?
You getting an error because this method has been moved from the base graph class into the main namespace, see Migration guide from 1.X to 2.0. So either you're looking at the docs of 1.X or using code from previous releases.
You need to call this method as:
nx.selfloop_edges(G, data=True)