This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
reviews = pd.read_csv('epinions.csv') | |
reviews = reviews.as_matrix()[:, :] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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_ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
features = np.vectorize(features) | |
X = features(reviews[:, 1]) | |
y = reviews[:, 0] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
import 'package:image_picker/image_picker.dart'; | |
import 'dart:io'; | |
import 'package:tflite/tflite.dart'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
home: Scaffold( | |
appBar: AppBar( | |
title: Text('TFLite Example'), | |
backgroundColor: Colors.transparent | |
), |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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), |
OlderNewer