Skip to content

Instantly share code, notes, and snippets.

View sahil280114's full-sized avatar
🎯
Focusing

Sahil Chaudhary sahil280114

🎯
Focusing
View GitHub Profile
import re
import coremltools
import pandas as pd
import numpy as np
from nltk.corpus import stopwords
from nltk import word_tokenize
from string import punctuation
from sklearn.feature_extraction import DictVectorizer
from sklearn.pipeline import Pipeline
from sklearn.svm import LinearSVC
reviews = pd.read_csv('epinions.csv')
reviews = reviews.as_matrix()[:, :]
def features(sentence):
stop_words = stopwords.words('english') + list(punctuation)
words = word_tokenize(sentence)
words = [w.lower() for w in words]
filtered = [w for w in words if w not in stop_words and not w.isdigit()]
words = {}
for word in filtered:
if word in words:
words[word] += 1.0
else:
clf = Pipeline([("dct", DictVectorizer()), ("svc", LinearSVC())])
params = {
"svc__C": [1e15, 1e13, 1e11, 1e9, 1e7, 1e5, 1e3, 1e1, 1e-1, 1e-3, 1e-5]
}
gs = GridSearchCV(clf, params, cv=10, verbose=2, n_jobs=-1)
gs.fit(X, y)
model = gs.best_estimator_
features = np.vectorize(features)
X = features(reviews[:, 1])
y = reviews[:, 0]
coreml_model = coremltools.converters.sklearn.convert(model)
coreml_model.author = 'Your Name'
coreml_model.license = 'MIT'
coreml_model.short_description = 'Sentiment polarity LinearSVC.'
coreml_model.input_description['input'] = 'Features extracted from the text.'
coreml_model.output_description['classLabel'] = 'The most likely polarity (positive or negative), for the given input.'
coreml_model.output_description['classProbability'] = 'The probabilities for each class label, for the given input.'
coreml_model.save('SentimentPolarity.mlmodel')
@sahil280114
sahil280114 / Hex-to-uicolor
Created April 22, 2020 16:45
Convert hex to UIColor
extension UIColor {
public convenience init?(hex: String) {
let r, g, b, a: CGFloat
if hex.hasPrefix("#") {
let start = hex.index(hex.startIndex, offsetBy: 1)
let hexColor = String(hex[start...])
if hexColor.count == 8 {
let scanner = Scanner(string: hexColor)
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';
import 'dart:io';
import 'package:tflite/tflite.dart';
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('TFLite Example'),
backgroundColor: Colors.transparent
),
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
imageURI == null
? Text('No image selected.')
: Image.file(imageURI, width: 300, height: 200, fit: BoxFit.cover),