"""Support for the Xiaomi IR Remote (Chuangmi IR)."""
import asyncio
import binascii
import json
import logging
from datetime import timedelta
from urllib.parse import urlparse

import homeassistant.helpers.config_validation as cv
import voluptuous as vol
from homeassistant.components.remote import (ATTR_DELAY_SECS, ATTR_HOLD_SECS, ATTR_NUM_REPEATS,
                                             DEFAULT_DELAY_SECS, DEFAULT_HOLD_SECS,
                                             PLATFORM_SCHEMA, RemoteDevice)
from homeassistant.const import (CONF_NAME, CONF_TIMEOUT, STATE_OFF, STATE_ON)
from voluptuous.error import UrlInvalid
from voluptuous.schema_builder import message

MIN_TIME_BETWEEN_UPDATES = timedelta(minutes=30)

REQUIREMENTS = ['aiohttp>=3.6.1']

_LOGGER = logging.getLogger(__name__)

CONF_URL = 'url'
CONF_BASEURL = 'base_url'
CONF_PARTURL = 'url_path'
CONF_KEYS = 'keys'
CONF_METHOD = 'method'
CONF_PARAMS = 'par'
CONF_LOGRESP = 'logresp'

DEFAULT_TIMEOUT = 5

PARAMS_SCHEMA = vol.Any(cv.string, int, float, None, vol.All(cv.ensure_list, [cv.string, int, float, None, vol.Self]), vol.Self)

def conf_validator_url_or_part(key_dict):
    if (key_dict[CONF_PARTURL] and not key_dict[CONF_URL]) or\
       (key_dict[CONF_URL] and not key_dict[CONF_PARTURL]):
        return key_dict
    else:
        raise vol.Invalid(f'Olnly one between {CONF_URL} and {CONF_PARTURL} is allowed!')
    return key_dict

def conf_validator_baseurl_or_url(remote_dict):
    if not remote_dict[CONF_BASEURL]:
        for _, x in remote_dict[CONF_KEYS].items():
            if not x[CONF_URL]:
                raise vol.Invalid(f'Url has to be specified for each key if {CONF_BASEURL} is not specified')
    return remote_dict

@message('expected a relative URL', cls=UrlInvalid)
def UrlPath(v):
    """Verify that the value is a relative URL.

    >>> s = Schema(Url())
    >>> with raises(MultipleInvalid, 'expected a URL'):
    ...   s(1)
    >>> s('http://w3.org')
    'http://w3.org'
    """
    try:
        parsed = urlparse.urlparse(v)
        if not parsed.path or parsed.netloc or parsed.scheme:
            raise UrlInvalid("must have only a URL path")
        return parsed
    except Exception:
        raise ValueError

KEY_SCHEMA = vol.Schema(vol.All({
    vol.Optional(CONF_URL, default=''): vol.Any(None, '', vol.Url()),
    vol.Optional(CONF_PARTURL, default=''): vol.Any(None, '', UrlPath()),
    vol.Optional(CONF_METHOD, default=''): vol.In(('', 'GET', 'POSTFORM', 'POSTJSON', 'POSTBIN')),
    vol.Optional(CONF_TIMEOUT, default=0):
        vol.All(int, vol.Range(min=0)),
    vol.Optional(CONF_PARAMS, default={}): cv.schema_with_slug_keys(PARAMS_SCHEMA)
}, conf_validator_url_or_part))

PLATFORM_SCHEMA = vol.Schema(vol.All(PLATFORM_SCHEMA.extend({
    vol.Required(CONF_NAME): cv.slug,
    vol.Optional(CONF_LOGRESP, default='DEBUG'): vol.In(('DEBUG', 'INFO')),
    vol.Optional(CONF_BASEURL, default=''): vol.Any(None, '', vol.Url()),
    vol.Optional(CONF_METHOD, default='GET'): vol.In(('GET', 'POSTFORM', 'POSTJSON', 'POSTBIN')),
    vol.Optional(CONF_TIMEOUT, default=DEFAULT_TIMEOUT):
        vol.All(int, vol.Range(min=1)),
    vol.Required(CONF_KEYS): cv.schema_with_slug_keys(KEY_SCHEMA)
}, extra=vol.ALLOW_EXTRA), conf_validator_baseurl_or_url))

async def async_setup_platform(hass, config, async_add_entities,
                               discovery_info=None):
    friendly_name = config.get(CONF_NAME)
    base_url = config.get(CONF_BASEURL)
    method = config.get(CONF_METHOD)
    timeout = config.get(CONF_TIMEOUT)
    logresp = config.get(CONF_LOGRESP)
    keys = config.get(CONF_KEYS)

    lstk = dict()
    for n, desc in keys.items():
        method = desc[CONF_METHOD] if desc[CONF_METHOD] else method
        url = base_url + '/' + desc[CONF_PARTURL] if desc[CONF_PARTURL] else desc[CONF_URL]
        timeout = desc[CONF_TIMEOUT] if desc[CONF_TIMEOUT] else timeout
        lstk[n] = RemoteRestKey(n, url, method, timeout, desc[CONF_PARAMS])

    xiaomi_miio_remote = RestRemote(
        friendly_name,
        lstk,
        logresp)
    lstent = [xiaomi_miio_remote]
    async_add_entities(lstent)

class RemoteRestKey(object):
    """Representation of a RemoteRest key."""
    def __init__(self, name, url, method, timeout, params):
        self._url = url
        self._method = method
        self._timeout = timeout
        self._params = params
        self._name = name

    async def do(self, session):
        if self._method == 'GET':
            pars = dict()
            for k, v in self._params.items():
                pars[k] = v if isinstance(v, (str, int)) else json.dumps(v)
            _LOGGER.info(f"Sending {self._name} ({self._url})...")
            async with session.get(self._url, params=pars) as resp:
                return (resp.status, await resp.text())
        elif self._method == 'POSTJSON':
            async with session.post(self._url, json=self._params) as resp:
                return (resp.status, await resp.text())
        elif self._method == 'POSTFORM':
            async with session.post(self._url, data=self._params) as resp:
                return (resp.status, await resp.text())
        elif self._method == 'POSTBIN':
            async with session.post(self._url, data=binascii.unhexlify(next(iter(self._params.values())))) as resp:
                return (resp.status, await resp.text())

class RestRemote(RemoteDevice):
    """Representation of a Xiaomi Miio Remote device."""

    def __init__(self, friendly_name, keys, logresp):
        """Initialize the remote."""
        self._name = friendly_name
        self._state = STATE_OFF
        self._commands = keys
        self._logresp = logresp

    @property
    def name(self):
        """Return the name of the remote."""
        return self._name

    @property
    def state(self):
        """Return the state."""
        return self._state

    @property
    def is_on(self):
        """Return False if device is unreachable, else True."""
        return self._state != STATE_OFF

    async def async_turn_on(self, **kwargs):
        """Turn the device on."""
        _LOGGER.error("Device does not support turn_on, "
                      "please use 'remote.send_command' to send commands.")

    async def async_turn_off(self, **kwargs):
        """Turn the device off."""
        _LOGGER.error("Device does not support turn_off, "
                      "please use 'remote.send_command' to send commands.")

    def get_ttimeout_struct(self, command_list, repeats, delay, ClientTimeout):
        tott = (repeats - 1) * delay
        maxt = 0
        for com in command_list:
            if com in self._commands:
                tim = self._commands[com]._timeout
                if tim > maxt:
                    maxt = tim
                tott += tim
        return ClientTimeout(total=tott, connect=maxt, sock_connect=maxt, sock_read=maxt)

    async def async_send_command(self, command, **kwargs):
        """Send a command."""
        import aiohttp
        if not isinstance(command, (list, tuple)):
            command_list = [command]
        else:
            command_list = command
        num_repeats = kwargs.get(ATTR_NUM_REPEATS, 1)
        delay = kwargs.get(ATTR_DELAY_SECS, DEFAULT_DELAY_SECS)
        hold = kwargs.get(ATTR_HOLD_SECS, DEFAULT_HOLD_SECS)
        timestr = self.get_ttimeout_struct(command_list, num_repeats, delay, aiohttp.ClientTimeout)
        async with aiohttp.ClientSession(timeout=timestr) as session:
            for k in range(num_repeats):
                j = 0
                for c in command_list:
                    if c in self._commands:
                        desc = self._commands[c]
                        try:
                            rv = await desc.do(session)
                            if self._logresp == 'DEBUG':
                                _LOGGER.debug(f"Response for {c}: st={rv[0]} txt={rv[1]}")
                            else:
                                _LOGGER.info(f"Response for {c}: st={rv[0]} txt={rv[1]}")
                            if self._state == STATE_OFF:
                                self._state = STATE_ON
                        except Exception as ex:
                            _LOGGER.error(f"Error sending {c}: {ex}")
                            if self._state == STATE_ON:
                                self._state = STATE_OFF
                        j += 1
                        if j < len(command_list) or k < num_repeats - 1:
                            await asyncio.sleep(hold)
                if k < num_repeats - 1:
                    await asyncio.sleep(delay)


from typing import Optional
from uuid import UUID

import geoalchemy2.shape
import shapely.geometry.point
from open_people_transport.core.models import Node as CoreNode
from open_people_transport.core.models import Route as CoreRoute
from open_people_transport.core.models import RouteStop as CoreRouteStop
from open_people_transport.core.models import Stop as CoreStop
from open_people_transport.core.models import Type as CoreType
from open_people_transport.database.models import Node as SQLNode
from open_people_transport.database.models import Route as SQLRoute
from open_people_transport.database.models import RouteStop as SQLRouteStop
from open_people_transport.database.models import Stop as SQLStop
from open_people_transport.database.models import Type as SQLType
from sqlalchemy import asc, desc, select
from sqlalchemy.exc import IntegrityError
from sqlalchemy.orm import Session

from .exceptions import (
    DatabaseIntegrityViolated,
    ResourceAlreadyExists,
    ResourceNotFound,
)

class Service:
    def __init__(self, session: Session) -> None:
        self.session = session

    def _try_commit(self) -> None:
        try:
            self.session.commit()
        except IntegrityError as exc:
            raise DatabaseIntegrityViolated(CoreRoute, exc.args[0])

class TypeService(Service):
    def list(self) -> list[CoreType]:
        query = select(SQLType)
        values = self.session.scalars(query).all()
        result = list(map(CoreType.from_orm, values))
        return result

    def get(self, name: str) -> CoreType:
        value = self.session.get(SQLType, name)
        if value is None:
            raise ResourceNotFound(CoreType, name)
        return CoreType.from_orm(value)

    def create(self, new: CoreType) -> CoreType:
        if new in self:
            raise ResourceAlreadyExists(CoreType, new.name)
        row = SQLType(name=new.name)
        self.session.add(row)
        self.session.commit()
        # No refresh needed
        return CoreType.from_orm(row)

    def update(self, name: str, new: CoreType) -> CoreType:
        row = self.session.get(SQLType, name)
        if row is None:
            raise ResourceNotFound(CoreType, name)
        row.name = new.name
        self._try_commit()
        self.session.refresh(row)
        return CoreType.from_orm(row)

    def delete(self, name: str) -> None:
        row = self.session.get(SQLType, name)
        if row is None:
            raise ResourceNotFound(CoreType, name)
        self.session.delete(row)
        self._try_commit()

    def __contains__(self, item: CoreType) -> bool:
        row = self.session.get(SQLType, item.name)
        return row is not None

class RouteService(Service):
    def list(self) -> list[CoreRoute]:
        query = select(SQLRoute)
        values = self.session.scalars(query).all()
        result = list(map(CoreRoute.from_orm, values))
        return result

    def get(self, id: UUID) -> CoreRoute:
        value = self.session.get(SQLRoute, id)
        if value is None:
            raise ResourceNotFound(CoreRoute, id)
        return CoreRoute.from_orm(value)

    def update(self, new: CoreRoute) -> CoreRoute:
        row = self.session.get(SQLRoute, new.id)
        if row is None:
            row = SQLRoute(id=new.id)
            self.session.add(row)
        row.number = new.number
        row.type_name = new.type_name
        self._try_commit()
        self.session.refresh(row)
        return CoreRoute.from_orm(row)

    def delete(self, id: UUID) -> None:
        row = self.session.get(SQLRoute, id)
        if not row:
            raise ResourceNotFound(CoreRoute, id)
        self.session.delete(row)
        self._try_commit()

class NodeService(Service):
    def list(self) -> list[CoreNode]:
        query = select(SQLNode)
        values = self.session.scalars(query).all()
        result = list(map(CoreNode.from_orm, values))
        return result

    def get(self, id: UUID) -> CoreNode:
        value = self.session.get(SQLNode, id)
        if value is None:
            raise ResourceNotFound(CoreRoute, id)
        return CoreNode.from_orm(value)

    def update(self, new: CoreNode) -> CoreNode:
        row = self.session.get(SQLNode, new.id)
        if row is None:
            row = SQLNode(id=new.id)
            self.session.add(row)
        row.name = new.name
        self.session.commit()
        self.session.refresh(row)
        return CoreNode.from_orm(row)

    def delete(self, id: UUID) -> None:
        row = self.session.get(SQLNode, id)
        if not row:
            raise ResourceNotFound(CoreRoute, id)
        self.session.delete(row)
        self._try_commit()

class StopService(Service):
    def list(self) -> list[CoreStop]:
        query = select(SQLStop)
        values = self.session.scalars(query).all()
        result = list(map(self.model_to_schema, values))
        return result

    def get(self, id: UUID) -> CoreStop:
        value = self.session.get(SQLStop, id)
        if value is None:
            raise ResourceNotFound(CoreStop, id)
        return self.model_to_schema(value)

    def update(self, new: CoreStop) -> CoreStop:
        row = self.session.get(SQLStop, new.id)
        if row is None:
            row = SQLStop(id=new.id)
            self.session.add(row)
        row.node_id = new.node_id
        shape = shapely.geometry.point.Point(new.lon, new.lat)
        row.location = geoalchemy2.shape.from_shape(shape)
        self.session.commit()
        self.session.refresh(row)
        return self.model_to_schema(row)

    def delete(self, id: UUID) -> None:
        row = self.session.get(SQLStop, id)
        if not row:
            raise ResourceNotFound(CoreStop, id)
        self.session.delete(row)
        self._try_commit()

    @staticmethod
    def model_to_schema(model: SQLStop) -> CoreStop:
        shape = geoalchemy2.shape.to_shape(model.location)
        if not isinstance(shape, shapely.geometry.point.Point):
            raise RuntimeError("Could not parse a Point from WKB")
        return CoreStop(
            id=model.id,
            lat=shape.y,
            lon=shape.x,
            node_id=model.node_id,
        )

class RouteStopService(Service):
    def list(
        self, route_id: Optional[UUID] = None, stop_id: Optional[UUID] = None
    ) -> list[CoreRouteStop]:
        query = select(SQLRouteStop)
        if route_id:
            query = query.where(SQLRouteStop.route_id == route_id)
        if stop_id:
            query = query.where(SQLRouteStop.stop_id == stop_id)
        values = self.session.scalars(query).all()
        result = list(map(CoreRouteStop.from_orm, values))
        return result

    def get(self, route_id: UUID, stop_id: UUID) -> CoreRouteStop:
        value = self.session.get(SQLRouteStop, (route_id, stop_id))
        if value is None:
            raise ResourceNotFound(CoreRouteStop, (route_id, stop_id))
        return CoreRouteStop.from_orm(value)

    def create(
        self,
        route_id: UUID,
        stop_id: UUID,
        after_stop: Optional[UUID] = None,
    ) -> CoreRouteStop:
        row = self.session.get(SQLRouteStop, (stop_id, route_id))
        if row is None:
            row = SQLRouteStop(stop_id=stop_id, route_id=route_id)
            self.session.add(row)

        # TODO Proper distance calculation algorithm
        # Current solution:
        # * If previous stop is not specified, assume the last one as the previous.
        # * If a next stop after the previous one exists, insert the new stop
        #   exactly in the middle (e.g. 200 + 500 -> 350).
        # * If not, insert 200 meters ahead of the previous (e.g. 200 -> 400).
        prev_stop: SQLRouteStop | None
        if after_stop:
            prev_stop = self.session.get(SQLRouteStop, (route_id, after_stop))
            if prev_stop is None:
                raise ResourceNotFound(CoreStop, after_stop)
        else:
            query = select(SQLRouteStop).order_by(desc(SQLRouteStop.distance))
            prev_stop = self.session.scalars(query).first()
        prev_distance = prev_stop.distance if prev_stop else -200
        query = (
            select(SQLRouteStop)
            .where(SQLRouteStop.distance > prev_distance)
            .order_by(asc(SQLRouteStop.distance))
        )
        next_stop: SQLRouteStop | None = self.session.scalars(query).first()
        if next_stop:
            current_distance = (prev_distance + next_stop.distance) // 2
        else:
            current_distance = prev_distance + 200

        row.distance = current_distance
        self.session.commit()
        self.session.refresh(row)
        return CoreRouteStop.from_orm(row)

    def delete(self, route_id: UUID, stop_id: UUID) -> None:
        row = self.session.get(SQLRouteStop, (route_id, stop_id))
        if not row:
            raise ResourceNotFound(CoreRouteStop, (route_id, stop_id))
        self.session.delete(row)
        self._try_commit()


class Solution(object):
    def minDominoRotations(self, tops, bottoms):
        """
        :type tops: List[int]
        :type bottoms: List[int]
        :rtype: int
        the test case passed are with a 76/84,

        with a small tweak we can get a good pass.
        """
        swap_top=0
        swap_bottom=0
        dict_top=dict.fromkeys(set(tops),0)
        dict_bottom=dict.fromkeys(set(bottoms),0)
        ## dominos at top

        flag1=True
        flag2=True
        for num in tops:
            dict_top[num]+=1

        ### dominos at the bottom
        for num in bottoms:
            dict_bottom[num]+=1
        id_top=[idx for idx in range(len(tops)) if tops[idx]!=max(dict_top,key=dict_top.get)]
        id_bottom=[idx for idx in range(len(bottoms)) if bottoms[idx]!=max(dict_bottom,key=dict_bottom.get)]
        for idx in id_top:
            if bottoms[idx]==max(dict_top,key=dict_top.get):
                swap_top+=1
            else:
                flag1=False
        for idy in id_bottom:
            if tops[idy]==max(dict_bottom,key=dict_bottom.get):
                swap_bottom+=1
            else:
                flag2=False
        print(swap_bottom,swap_top)
        if (flag1 or flag2):
            return min(swap_top,swap_bottom)
        else:
            return -1


import sys
sys.setrecursionlimit(10000)
dxy = [(-1, 0), (1, 0), (0, -1), (0, 1)]

def solve():
    answer = 0
    check = [[False] * N for _ in range(N)]

    for i in range(N):
        for j in range(N):
            if not check[i][j]:
                answer += 1
                check[i][j] = True
                dfs(i, j, check)

    return answer

def dfs(i, j, check):
    for dx, dy in dxy:
        x = i + dx
        y = j + dy
        if 0 <= x < N and 0 <= y < N \
            and table[i][j] == table[x][y] and not check[x][y]:
            check[x][y] = True
            dfs(x, y, check)

def convert_to_r2g():
    for i in range(N):
        for j in range(N):
            if table[i][j] == 'R':
                table[i][j] = 'G'

if __name__ == "__main__":
    N = int(input())

    table = []

    for _ in range(N):
        table.append(list(sys.stdin.readline().strip()))

    answer1 = solve()
    convert_to_r2g()
    answer2 = solve()

    print(answer1, answer2)


import sklearn
import sklearn.pipeline
import pandas
import numpy

def _wrap_transformer(transformer_list=None):
    """
    Wrapper function for steps in Pipeline or FeatureUnion
    """
    wrapped_transformers = []
    for name, transformer in transformer_list:
       if sklearn.base.is_classifier(transformer) or sklearn.base.is_regressor(transformer):
           wrapped_step = (name, transformer)
       elif type(transformer) == sklearn.pipeline.Pipeline:
           wrapped_step = (name, PandasPipelineWrapper(transformer))
       else:
           wrapped_step = (name, PandasTransformerWrapper(transformer))
       wrapped_transformers.append(wrapped_step)
    return wrapped_transformers

class BaseTransformerWrapper:
    """
    Base wrapper for all transformers
    """
    _MODULES_NOT_IMPLEMENTED = ['decomposition', 'cross_decomposition']
    _TRANSFORMER_METHODS = ['fit', 'transform', 'fit_transform']

    def __init__(self, base_transformer_object):
        self._validate_transformer(base_transformer_object)
        self.__class__ = type(base_transformer_object.__class__.__name__,
                              (self.__class__, base_transformer_object.__class__),
                              {})
        self.__dict__ = base_transformer_object.__dict__
        self.module = type(base_transformer_object).__dict__['__module__'].split('.')[1]
        self.is_sparse = self.__dict__.get('sparse', False)

    def _validate_transformer(self, transformer=None):
        """
        Check if transformer is a valid sklearn transformer
        """
        is_transformer = all(map(lambda method: hasattr(transformer, method), self._TRANSFORMER_METHODS))
        if not is_transformer:
            raise ValueError('Not a valid transformer')

    def _check_input_type(self, data_frame=None):
        """
        validate input data
        """
        if isinstance(data_frame, pandas.core.frame.DataFrame):
            return True
        else:
            raise ValueError('Input should be a pandas data frame ')

    def _get_feature_names(self):
        """
        check type of transformer and return feature names if applicable
        """
        if self.module in self._MODULES_NOT_IMPLEMENTED:
            return []
        if hasattr(self, 'get_feature_names'):
            return self.get_feature_names(self.feature_names)
        if hasattr(self, 'get_support'):
            return self.feature_names[self.get_support()]
        return self.feature_names

class PandasTransformerWrapper(BaseTransformerWrapper):
    """
    Wrap sklearn Transformer return type from numpy to pandas with column names
    """
    def fit(self, data_frame=None, y=None):
        """
        Fit the specified transformer and set column names attribute
        """
        # feature names will be used for transform output
        self._check_input_type(data_frame)
        self.feature_names = data_frame.columns
        super(BaseTransformerWrapper, self).fit(data_frame, y)
        return self

    def transform(self, data_frame=None, y=None):
        """
        Apply transformer and cast output as a Pandas DataFrame
        """
        self._check_input_type(data_frame)
        feature_names = self._get_feature_names()
        data_frame_transformed = super(BaseTransformerWrapper, self).transform(data_frame)
        # check sparsity: output need to be dense array not sparse
        if self.is_sparse:
            data_frame_transformed = data_frame_transformed.toarray()
        if any(feature_names):
            return pandas.DataFrame(data_frame_transformed, columns=feature_names)
        else:
            return data_frame_transformed

class PandasPipelineWrapper(sklearn.pipeline.Pipeline):
    """
    Wrap sklearn Pipeline steps with PandasTransformerWrapper
    """
    def __init__(self, steps, **kwargs):
        super().__init__(steps=_wrap_transformer(steps),
                         **kwargs)

class PandasFeatureUnionWrapper(sklearn.pipeline.FeatureUnion):
    """
    Wrap FeatureUnion to persist feature names through pipeline
    """
    def __init__(self, transformer_list, **kwargs):
        super().__init__(transformer_list=_wrap_transformer(transformer_list),
                         **kwargs)

    def _get_feature_names(self):
        """
        Get feature names of all transformers and concatenate
        """
        feature_names = []
        for name, transformer in self.transformer_list:
            feature_names.append(transformer._get_feature_names())
        return numpy.concatenate(feature_names, axis=0)

    def transform(self, data_frame=None):
        """
        Apply transformer and return data frame with feature names
        """
        feature_names = self._get_feature_names()
        data_frame_transformed = super().transform(data_frame)
        if any(feature_names):
            return pandas.DataFrame(data_frame_transformed, columns=feature_names)
        else:
            return data_frame_transformed


from bisect import bisect_left

class Solution:
    def searchMatrix(self, matrix: List[List[int]], target: int) -> bool:
        flat = []
        for row in matrix:
            flat += row
        idx = bisect_left(flat, target)

        answer = idx < len(flat) and flat[idx] == target

        return answer


import re
import nltk
import random
from nltk.corpus import movie_reviews
from nltk.tokenize import word_tokenize,sent_tokenize
from textblob import TextBlob
from nltk.classify import ClassifierI
from nltk.classify.scikitlearn import SklearnClassifier
from sklearn.naive_bayes import GaussianNB
from sklearn.naive_bayes import MultinomialNB,BernoulliNB
from sklearn.linear_model import LogisticRegression,SGDClassifier
from sklearn.svm import SVC, LinearSVC, NuSVC
from statistics import mode
### This is still under development ###
### For the simplicity sake, I use just only Naive Bayes algorithm while developing ###
### Negation handling with 1 not or n't : 1 sentence ###
### Negation adverb handling ###
def posTag(wordsTokenized):
	betterTag=[]
	nltkTag = nltk.pos_tag(wordsTokenized)
	for w,tag in nltkTag:
		betterTag.append((w.lower(),tag))
	return betterTag
def isNoun(tag):
	noun = "N[\w]*"
	if re.search(noun,tag) and tag[0]=='N':
		return True
	else:return False
def isAdjective(tag):
	adjective = "J[\w]*"
	if re.search(adjective,tag) and tag[0]=='J':
		return True
	else:return False
def isAdverb(tag):
	adverb = "R[\w]*"
	if re.search(adverb,tag) and tag[0]=='R':
		return True
	else:return False
def isVerb(tag):
	verb = "V[\w]*"
	if re.search(verb,tag) and tag[0]=='V':
		return True
	else:return False
def isPunctuation(word,tag):
	if word==tag:return True
	else:return False
def isPrep(tag):
	if tag=='IN':return True
	else:return False
class VoteClassifier(ClassifierI):
	def __init__(self, *classifiers):
		self._classifiers = classifiers
	def classify(self, features):
		votes = []
		for c in self._classifiers:
			v = c.classify(features)
			votes.append(v)
		return mode(votes)
def find_features(ref_words,doc):
	words=set(doc)
	features={}
	for w in ref_words:
		features[w]=(w in words)
	return features
def confidence(self, features):
	votes = []
	for c in self._classifiers:
		v = c.classify(features)
		votes.append(v)
	choice_votes = votes.count(mode(votes))
	conf = choice_votes / len(votes)
	return conf
dataset=open("dataset.txt",encoding='utf8')
data=[];all_words=[]
for line in dataset:
	### Constructing data ###
	line=line.strip().replace('\ufeff','')
	wordAndTag=posTag(word_tokenize(line.split('#')[0]))
	if ('not','RB') in wordAndTag or ("n't",'RB') in wordAndTag or ('never', 'RB') in wordAndTag:
		lst=[]
		indexOfNegation=-1
		try:
			indexOfNegation=wordAndTag.index(('not','RB'))
		except ValueError:
			try:
				indexOfNegation=wordAndTag.index(("n't",'RB'))
			except ValueError:
				indexOfNegation=wordAndTag.index(('never', 'RB'))
		s_left=wordAndTag[0:indexOfNegation+1]
		for eachWord,tag in s_left:
			lst.append(eachWord)
		s_right=wordAndTag[indexOfNegation+1:]
		punctuationFound=False
		for eachWord,tag in s_right:
			if isPunctuation(eachWord, tag):
				punctuationFound=True
			if punctuationFound==False:
				lst.append('not_'+eachWord)
			else:
				lst.append(eachWord)
		# print(lst)
		data.append((lst,line.split('#')[1]))
	else:
		data.append((word_tokenize(line.split('#')[0]),line.split('#')[1]))
	### Constructing all_words ###
	if ('not','RB') in wordAndTag or ("n't",'RB') in wordAndTag or ('never', 'RB') in wordAndTag:
		idx=-1
		try:
			idx=wordAndTag.index(('not','RB'))
		except ValueError:
			try:
				idx=wordAndTag.index(("n't",'RB'))
			except ValueError:
			 	idx=wordAndTag.index(('never', 'RB'))
		withoutNegation_left=wordAndTag[0:idx]
		withoutNegation_right=wordAndTag[idx+1:]
		# print(withoutNegation_left,"left")
		# print(withoutNegation_right,"right")
		for w,tag in withoutNegation_left:
			if isVerb(tag) or isAdjective(tag) or isAdverb(tag) or isPrep(tag):
				all_words.append(w)
		isPunctuationFound=False
		for w,tag in withoutNegation_right:
			if isPunctuation(w, tag):
				isPunctuationFound=True
			if isPunctuationFound==False:
				if isVerb(tag) or isAdjective(tag) or isAdverb(tag) or isPrep(tag):
					all_words.append('not_'+w)
			else:
				if isVerb(tag) or isAdjective(tag) or isAdverb(tag) or isPrep(tag):
					all_words.append(w)
		isPunctuationFound=True
	else:
		withoutNegation=wordAndTag
		# print(withoutNegation)
		for w,tag in withoutNegation:
			if isVerb(tag) or isAdjective(tag) or isAdverb(tag) or isPrep(tag):
				all_words.append(w)
# print(data)
all_words=nltk.FreqDist(all_words)
# print(all_words.most_common(30))
word_feature=list(all_words.keys())
feature=[(find_features(word_feature,review),category) for (review,category) in data]
train_set=feature
classifier=nltk.NaiveBayesClassifier.train(train_set)
###Testing session###
while 1:
	sample_text=input("Sample text : ").lower()
	# Wtf is considered to be misspelled in general english dictionary
	sample_text=sample_text.replace("dont","don't").replace("didnt","didn't").replace("doesnt","doesn't").replace("havent","haven't").replace("hasnt","hasn't").replace("cant","can't")
	sample_text_tags=nltk.pos_tag(word_tokenize(sample_text))
	if ('not','RB') in sample_text_tags or ("n't",'RB') in sample_text_tags or ("never",'RB') in sample_text_tags:
		negationPosition=-1
		properWordsTokenized=[]
		try:
			negationPosition=sample_text_tags.index(('not','RB'))
		except ValueError:
			try:
				negationPosition=sample_text_tags.index(("n't",'RB'))
			except ValueError:
				negationPosition=sample_text_tags.index(('never','RB'))
		sample_text_left=sample_text_tags[0:negationPosition+1]
		for eachWord,tag in sample_text_left:
			properWordsTokenized.append(eachWord)
		sample_text_right=sample_text_tags[negationPosition+1:]
		puncFound=False
		for eachWord,tag in sample_text_right:
			if isPunctuation(eachWord, tag):
				puncFound=True
			if puncFound==False:
				properWordsTokenized.append('not_'+eachWord)
			else:
				properWordsTokenized.append(eachWord)
		# print(properWordsTokenized)
		# print(find_features(word_feature,word_tokenize(sample_text)))
		print(classifier.classify(find_features(word_feature,properWordsTokenized)))
	else:
		# print(nltk.pos_tag(word_tokenize(sample_text)))
		# print(find_features(word_feature,word_tokenize(sample_text)))
		print(classifier.classify(find_features(word_feature,word_tokenize(sample_text))))


import pandas as pd

dicionaryTags = [
    'cod_regiao',
    'qtd_pes',
    'qtd_pes_pobres',
    'qtd_pes_vulneraveis',
    'qtd_pes_pob_vul',
    'num_renda',
]

dicionaryTypes = [
    'Código da Grande Região',
    'Número de pessoas',
    'Númeo de pessoas pobres',
    'Número de pessoas vulneráveis',
    'Número de pessoas pobres ou vulneráveis',
    'Renda média (R$)'
]

regionTags = [1, 2, 3, 4, 5]

regionTypes = [
    'Norte',
    'Nordeste',
    'Sudeste',
    'Sul',
    'Centro-Oeste'
]

data_frame_regions_tags = [
    'avg_revenue_norte',
    'avg_revenue_nordeste',
    'avg_revenue_sudeste',
    'avg_revenue_sul',
    'avg_revenue_centro_oeste'
]

data_frame_regions_types = [
    'Renda média do Norte',
    'Renda média do Nordeste',
    'Renda média do Sudeste',
    'Renda média do Sul',
    'Renda média do Centro Oeste'
]

# Cria o xlsx de acordo com csv passado por parametro e adiciona o dicionario
def createXlsx(csv, region):
    dicionary = [{'tag': tag, 'descricao': descricao}
                 for tag, descricao in zip(dicionaryTags, dicionaryTypes)]

    df_dict = pd.DataFrame(csv)
    df_dicionary = pd.DataFrame(dicionary)
    df_region = pd.DataFrame(region)

    # Salve o DataFrame do dicionário em uma planilha específica dentro do arquivo CSV
    with pd.ExcelWriter('analise_pobreza_brasil.xlsx', engine='openpyxl') as writer:
        df_dict.to_excel(writer, sheet_name='Dados', index=False)
        df_dicionary.to_excel(writer, sheet_name='Dicionario', index=False)
        df_region.to_excel(writer, sheet_name='Análise', index=False)
    return

# calcula a porcentagem de pessoas vulneráveis ou pobres, dividindo a quantidade de pessoas vulneráveis ou pobres/ total de pessoas
def calculatePesPercentage(csv, regionTag: int, columNameFiltered: str):
    database_filter = csv[csv['cod_regiao'] == regionTag]

    total_pes = database_filter['qtd_pes'].sum()
    colum_name = database_filter[columNameFiltered].sum()

    return (colum_name / total_pes) * 100

# Calcula a renda media por regiao
def calculateAverageRevenue(csv, regionTag: int):
    database_filter = csv[csv['cod_regiao'] == regionTag]
    avarage_revenue = database_filter['num_renda'].mean()
    return avarage_revenue

# Calcula o total de pessoas de uma coluna, filtrada por regiao

def calculateTotalPes(csv, regionTag: int, columNameFiltered: str):
    filter_region = csv[csv['cod_regiao'] == regionTag]
    return filter_region[columNameFiltered].sum()

# Gera um data frame com os resultados

def executeDataFrameResults(csv):
    data_frame_regions = pd.DataFrame()

    for region in regionTags:
        data = {
            'cod_regiao': region,
            'qtd_total_pes': calculateTotalPes(csv, region, 'qtd_pes'),
            'qtd_total_pes_pobre': calculateTotalPes(csv, region, 'qtd_pes_pobres'),
            'qtd_total_pes_vulneravel': calculateTotalPes(csv, region, 'qtd_pes_vulneraveis'),
            'percent_pes_pobre': calculatePesPercentage(csv, region, 'qtd_pes_pobres'),
            'percent_pes_vulneraveis': calculatePesPercentage(csv, region, 'qtd_pes_vulneraveis'),
            'num_renda': calculateAverageRevenue(csv, region)
        }

        new_row = pd.DataFrame(data, index=[0])

        data_frame_regions = pd.concat(
            [data_frame_regions, new_row], ignore_index=True)

    return data_frame_regions

#############
# Filtra os dados
csv_data = pd.read_csv("MunicipioBrasil_20230102.csv")
csv_data_filtered = csv_data[dicionaryTags]
df_results = executeDataFrameResults(csv_data_filtered)

createXlsx(csv_data_filtered, df_results)


# Authors: Thierry Moudiki
#
# License: BSD 3 Clear Clause

import numpy as np
import pandas as pd
from ..base import Base
from scipy.stats import norm
from ..utils import matrixops as mo
from ..utils import timeseries as ts
import sklearn.metrics as skm2
import pickle
from functools import partial

class MTS(Base):
    """Univariate and multivariate time series (MTS) forecasting with Quasi-Randomized networks (Work in progress /!\)

    Parameters:

        obj: object.
            any object containing a method fit (obj.fit()) and a method predict
            (obj.predict()).

        n_hidden_features: int.
            number of nodes in the hidden layer.

        activation_name: str.
            activation function: 'relu', 'tanh', 'sigmoid', 'prelu' or 'elu'.

        a: float.
            hyperparameter for 'prelu' or 'elu' activation function.

        nodes_sim: str.
            type of simulation for the nodes: 'sobol', 'hammersley', 'halton',
            'uniform'.

        bias: boolean.
            indicates if the hidden layer contains a bias term (True) or not
            (False).

        dropout: float.
            regularization parameter; (random) percentage of nodes dropped out
            of the training.

        direct_link: boolean.
            indicates if the original predictors are included (True) in model's fitting or not (False).

        n_clusters: int.
            number of clusters for 'kmeans' or 'gmm' clustering (could be 0: no clustering).

        cluster_encode: bool.
            defines how the variable containing clusters is treated (default is one-hot)
            if `False`, then labels are used, without one-hot encoding.

        type_clust: str.
            type of clustering method: currently k-means ('kmeans') or Gaussian
            Mixture Model ('gmm').

        type_scaling: a tuple of 3 strings.
            scaling methods for inputs, hidden layer, and clustering respectively
            (and when relevant).
            Currently available: standardization ('std') or MinMax scaling ('minmax').

        lags: int.
            number of lags used for each time series.

        seed: int.
            reproducibility seed for nodes_sim=='uniform'.

        backend: str.
            "cpu" or "gpu" or "tpu".

    Attributes:

        fit_objs_: dict
            objects adjusted to each individual time series

        y_: {array-like}
            MTS responses (most recent observations first)

        X_: {array-like}
            MTS lags

        xreg_: {array-like}
            external regressors

        y_means_: dict
            a dictionary of each series mean values

        preds_: {array-like}
            successive model predictions

        preds_std_: {array-like}
            standard deviation around the predictions

        return_std_: boolean
            return uncertainty or not (set in predict)

        df_: data frame
            the input data frame, in case a data.frame is provided to `fit`

    Examples:

    Example 1:

    ```python
    import nnetsauce as ns
    import numpy as np
    from sklearn import linear_model
    np.random.seed(123)

    M = np.random.rand(10, 3)
    M[:,0] = 10*M[:,0]
    M[:,2] = 25*M[:,2]
    print(M)

    # Adjust Bayesian Ridge
    regr4 = linear_model.BayesianRidge()
    obj_MTS = ns.MTS(regr4, lags = 1, n_hidden_features=5)
    obj_MTS.fit(M)
    print(obj_MTS.predict())

    # with credible intervals
    print(obj_MTS.predict(return_std=True, level=80))

    print(obj_MTS.predict(return_std=True, level=95))
    ```

    Example 2:

    ```python
    import nnetsauce as ns
    import numpy as np
    from sklearn import linear_model

    dataset = {
    'date' : ['2001-01-01', '2002-01-01', '2003-01-01', '2004-01-01', '2005-01-01'],
    'series1' : [34, 30, 35.6, 33.3, 38.1],
    'series2' : [4, 5.5, 5.6, 6.3, 5.1],
    'series3' : [100, 100.5, 100.6, 100.2, 100.1]}
    df = pd.DataFrame(dataset).set_index('date')
    print(df)

    # Adjust Bayesian Ridge
    regr5 = linear_model.BayesianRidge()
    obj_MTS = ns.MTS(regr5, lags = 1, n_hidden_features=5)
    obj_MTS.fit(df)
    print(obj_MTS.predict())

    # with credible intervals
    print(obj_MTS.predict(return_std=True, level=80))

    print(obj_MTS.predict(return_std=True, level=95))
    ```

    """

    # construct the object -----

    def __init__(
        self,
        obj,
        n_hidden_features=5,
        activation_name="relu",
        a=0.01,
        nodes_sim="sobol",
        bias=True,
        dropout=0,
        direct_link=True,
        n_clusters=2,
        cluster_encode=True,
        type_clust="kmeans",
        type_scaling=("std", "std", "std"),
        lags=1,
        seed=123,
        backend="cpu",
    ):

        assert int(lags) == lags, "parameter 'lags' should be an integer"

        super().__init__(
            n_hidden_features=n_hidden_features,
            activation_name=activation_name,
            a=a,
            nodes_sim=nodes_sim,
            bias=bias,
            dropout=dropout,
            direct_link=direct_link,
            n_clusters=n_clusters,
            cluster_encode=cluster_encode,
            type_clust=type_clust,
            type_scaling=type_scaling,
            seed=seed,
            backend=backend,
        )

        self.obj = obj
        self.n_series = None
        self.lags = lags
        self.fit_objs_ = {}
        self.y_ = None  # MTS responses (most recent observations first)
        self.X_ = None  # MTS lags
        self.xreg_ = None
        self.y_means_ = {}
        self.preds_ = None
        self.preds_std_ = []
        self.return_std_ = None
        self.df_ = None

    def fit(self, X, xreg=None):
        """Fit MTS model to training data X, with optional regressors xreg

        Parameters:

            X: {array-like}, shape = [n_samples, n_features]
                Training time series, where n_samples is the number
                of samples and n_features is the number of features;
                X must be in increasing order (most recent observations last)

            xreg: {array-like}, shape = [n_samples, n_features_xreg]
                Additional regressors to be passed to obj
                xreg must be in increasing order (most recent observations last)

            **kwargs: additional parameters to be passed to
                    self.cook_training_set

        Returns:

            self: object
        """

        if isinstance(X, pd.DataFrame):
            self.df_ = X
            X = X.values

        try:
            # multivariate time series
            n, p = X.shape
        except:
            # univariate time series
            n = X.shape[0]
            p = 1

        rep_1_n = np.repeat(1, n)

        self.y_ = None
        self.X_ = None
        self.n_series = p
        self.fit_objs_.clear()
        self.y_means_.clear()

        if p > 1:
            # multivariate time series
            mts_input = ts.create_train_inputs(X[::-1], self.lags)
        else:
            # univariate time series
            mts_input = ts.create_train_inputs(
                X.reshape(-1, 1)[::-1], self.lags
            )

        self.y_ = mts_input[0]

        self.X_ = mts_input[1]

        if xreg is not None:

            if isinstance(xreg, pd.DataFrame):
                xreg = xreg.values

            assert (
                xreg.shape[0] == n
            ), "'xreg' and 'X' must have the same number of observations"

            self.xreg_ = xreg

            xreg_input = ts.create_train_inputs(xreg[::-1], self.lags)

            dummy_y, scaled_Z = self.cook_training_set(
                y=rep_1_n,
                X=mo.cbind(self.X_, xreg_input[1], backend=self.backend),
            )

        else:  # xreg is None

            # avoids scaling X p times in the loop
            dummy_y, scaled_Z = self.cook_training_set(y=rep_1_n, X=self.X_)

        # loop on all the time series and adjust self.obj.fit
        for i in range(p):
            y_mean = np.mean(self.y_[:, i])
            self.y_means_[i] = y_mean
            self.obj.fit(scaled_Z, self.y_[:, i] - y_mean)
            self.fit_objs_[i] = pickle.loads(pickle.dumps(self.obj, -1))

        return self

    def predict(self, h=5, level=95, new_xreg=None, **kwargs):
        """Forecast all the time series, h steps ahead

        Parameters:

            h: {integer}
                Forecasting horizon

            level: {integer}
                Level of confidence (if obj has option 'return_std' and the
                posterior is gaussian)

            new_xreg: {array-like}, shape = [n_samples = h, n_new_xreg]
                New values of additional (deterministic) regressors on horizon = h
                new_xreg must be in increasing order (most recent observations last)

            **kwargs: additional parameters to be passed to
                    self.cook_test_set

        Returns:

            model predictions for horizon = h: {array-like}, data frame or tuple.
            Standard deviation and prediction intervals are returned when
            `obj.predict` can return standard deviation
        """

        if self.df_ is not None:  # `fit` takes a data frame input
                output_dates, frequency = ts.compute_output_dates(self.df_, h)

        self.return_std_ = False

        self.preds_ = None  # do not remove (!)

        self.preds_ = pickle.loads(pickle.dumps(self.y_, -1))

        y_means_ = np.asarray([self.y_means_[i] for i in range(self.n_series)])

        n_features = self.n_series * self.lags # check that /!\

        if "return_std" in kwargs:
            self.return_std_ = True
            self.preds_std_ = []

        if self.xreg_ is None: # no external regressors

            for i in range(h):

                new_obs = ts.reformat_response(self.preds_, self.lags)

                new_X = new_obs.reshape(1, n_features)

                cooked_new_X = self.cook_test_set(new_X, **kwargs)

                if "return_std" in kwargs:
                    self.preds_std_.append([np.asscalar(self.fit_objs_[i].predict(cooked_new_X, return_std=True)[1]) for i in range(self.n_series)])

                predicted_cooked_new_X = np.asarray([np.asscalar(self.fit_objs_[i].predict(
                    cooked_new_X)) for i in range(self.n_series)])

                preds = np.asarray(y_means_ + predicted_cooked_new_X)

                self.preds_ = mo.rbind(preds, self.preds_)

        else: # if self.xreg_ is not None: # with external regressors

            assert new_xreg is not None, "'new_xreg' must be provided to predict()"

            if isinstance(new_xreg, pd.DataFrame):
                new_xreg = new_xreg.values

            try:
                n_obs_xreg, n_features_xreg = new_xreg.shape
                assert (
                    n_features_xreg == self.xreg_.shape[1]
                ), "check number of inputs provided for 'new_xreg' (compare with self.xreg_.shape[1])"
            except:
                n_obs_xreg = new_xreg.shape  # one series

            assert (
                n_obs_xreg == h
            ), "please provide values of regressors 'new_xreg' for the whole horizon 'h'"

            n_features_xreg = n_features_xreg * self.lags

            inv_new_xreg = mo.rbind(self.xreg_, new_xreg)[::-1]

            for i in range(h):

                new_obs = ts.reformat_response(self.preds_, self.lags)

                new_obs_xreg = ts.reformat_response(inv_new_xreg, self.lags)

                new_X = np.concatenate(
                            (new_obs, new_obs_xreg), axis=None
                        ).reshape(1, -1)

                cooked_new_X = self.cook_test_set(new_X, **kwargs)

                if "return_std" in kwargs:
                    self.preds_std_.append([np.asscalar(self.fit_objs_[i].predict(
                        cooked_new_X, return_std=True)[1]) for i in range(self.n_series)])

                predicted_cooked_new_X = np.asarray([np.asscalar(self.fit_objs_[i].predict(
                    cooked_new_X)) for i in range(self.n_series)])

                preds = np.asarray(y_means_ + predicted_cooked_new_X)

                self.preds_ = mo.rbind(preds, self.preds_)

        # function's return
        if self.df_ is None:
            self.preds_ = self.preds_[0:h, :][::-1]
            if "return_std" not in kwargs:
                return self.preds_
            self.preds_std_ = np.asarray(self.preds_std_)
            return self.preds_, self.preds_std_

        # if self.df_ is not None (return data frames)
        self.preds_ = pd.DataFrame(
            self.preds_[0:h, :][::-1],
            columns=self.df_.columns,
            index=output_dates,
        )
        if "return_std" not in kwargs:
            return self.preds_
        self.preds_std_ = pd.DataFrame(
            np.asarray(self.preds_std_),
            columns=self.df_.columns,
            index=output_dates,
        )
        return self.preds_, self.preds_std_

    def score(self, X, training_index, testing_index, scoring=None, **kwargs):
        """ Train on training_index, score on testing_index. """

        assert (
            bool(set(training_index).intersection(set(testing_index))) == False
        ), "Non-overlapping 'training_index' and 'testing_index' required"

        # Dimensions
        try:
            # multivariate time series
            n, p = X.shape
        except:
            # univariate time series
            n = X.shape[0]
            p = 1

        # Training and testing sets
        if p > 1:
            X_train = X[training_index, :]
            X_test = X[testing_index, :]
        else:
            X_train = X[training_index]
            X_test = X[testing_index]

        # Horizon
        h = len(testing_index)
        assert (
            len(training_index) + h
        ) <= n, "Please check lengths of training and testing windows"

        # Fit and predict
        self.fit(X_train, **kwargs)
        preds = self.predict(h=h, **kwargs)

        if scoring is None:
            scoring = "neg_root_mean_squared_error"

        # check inputs
        assert scoring in (
            "explained_variance",
            "neg_mean_absolute_error",
            "neg_mean_squared_error",
            "neg_root_mean_squared_error",
            "neg_mean_squared_log_error",
            "neg_median_absolute_error",
            "r2",
        ), "'scoring' should be in ('explained_variance', 'neg_mean_absolute_error', \
                               'neg_mean_squared_error', 'neg_root_mean_squared_error', 'neg_mean_squared_log_error', \
                               'neg_median_absolute_error', 'r2')"

        scoring_options = {
            "explained_variance": skm2.explained_variance_score,
            "neg_mean_absolute_error": skm2.mean_absolute_error,
            "neg_mean_squared_error": skm2.mean_squared_error,
            "neg_root_mean_squared_error": lambda x, y: np.sqrt(
                skm2.mean_squared_error(x, y)
            ),
            "neg_mean_squared_log_error": skm2.mean_squared_log_error,
            "neg_median_absolute_error": skm2.median_absolute_error,
            "r2": skm2.r2_score,
        }

        if p > 1:
            return tuple(
                [
                    scoring_options[scoring](
                        X_test[:, i], preds[:, i], **kwargs
                    )
                    for i in range(p)
                ]
            )
        else:
            return scoring_options[scoring](X_test, preds)


import curses
import logging
import math
import threading
import time

from osol_install.text_install import _, LOG_LEVEL_INPUT, RELEASE
from osol_install.text_install.base_screen import BaseScreen
from osol_install.text_install.i18n import ljust_columns
from osol_install.text_install.inner_window import InnerWindow
from osol_install.text_install.window_area import WindowArea
from osol_install.text_install.ti_install import perform_ti_install

class InstallProgress(BaseScreen):
    '''Present a progress bar, and callback hooks for an installation
    Thread to update this screen with percent complete and status.

    '''

    HEADER_TEXT = _("Installing %(release)s") % RELEASE
    PROG_BAR_ENDS = (ord('['), ord(']'))

    def __init__(self, main_win):
        super(InstallProgress, self).__init__(main_win)
        self.really_quit = False

        # Location on screen where the status messages should get printed
        # Format: (y, x, max-width)
        self.status_msg_loc = (4, 12, 50)

        # Location on screen where the progress bar should be displayed
        # Format: (y, x, width)
        self.status_bar_loc = (6, 10, 50)

        self.last_update = 0
        # Minimum elapsed time in seconds between screen updates
        self.update_frequency = 2
        self.status_bar = None
        self.status_bar_width = None
        self.install_profile = None
        self.install_thread = None
        self.progress_color = None
        self.quit_event = threading.Event()
        self.update_event = threading.Event()
        self.time_change_event = threading.Event()
        self.update_to = None

    def set_actions(self):
        '''Remove all actions except F9_Quit.'''
        self.main_win.actions.pop(curses.KEY_F2) # Remove F2_Continue
        self.main_win.actions.pop(curses.KEY_F3) # Remove F3_Back
        self.main_win.actions.pop(curses.KEY_F6) # Remove F6_Help

    def _show(self):
        '''Set an initial status message, and initialize the progress bar'''
        self.set_status_message(InstallProgress.HEADER_TEXT)
        self.init_status_bar(*self.status_bar_loc)

    def validate_loop(self):
        '''Begin the installation. Honor the '-n' flag from the CLI by
        beginning a 'fake' installation thread instead of actually making
        changes to the disk.

        After starting the installation thread, wait for input from the
        user, in case they change their mind and try to quit.

        '''
        if self.install_profile.no_install_mode:
            self.install_thread = self.start_fake_install_thread()
        else:
            self.install_thread = self.start_install_thread()
        win = self.center_win.window
        win.timeout(100) # Set timeout for getch to 100 ms. The install_thread
                         # will be checked at this frequency to see if it
                         # has finished execution.
                         # Keystroke input will still be processed immediately
        while self.install_thread.is_alive():
            # Wait for the install thread to signal that it has finished
            # updating the system time, so that we can safely use getch(),
            # which times out based on the system clock.
            self.time_change_event.wait()
            input_key = self.main_win.getch()
            if input_key == curses.KEY_F9:
                self.really_quit = self.confirm_quit()
                if self.really_quit:
                    win.timeout(-1)
                    self.install_thread.join()
                    return None
        win.timeout(-1) # Restore getch() to be blocking
        return self.main_win.screen_list.get_next(self)

    def start_install_thread(self):
        '''Instantiate a Thread to do the installation, and start execution.'''
        handle = threading.Thread(target=InstallProgress.perform_install,
                                  args=(self.install_profile, self,
                                        InstallProgress.update_status,
                                        self.quit_event,
                                        self.time_change_event))
        handle.start()
        return handle

    def start_fake_install_thread(self):
        '''Instantiate a Thread to perform a 'fake' installation'''
        handle = threading.Thread(target=InstallProgress.fake_install,
                                  args=(self.install_profile, self,
                                        InstallProgress.update_status,
                                        self.quit_event))
        self.time_change_event.set()
        handle.start()
        return handle

    @staticmethod
    def perform_install(install_profile, screen, update_status, quit_event,
                        time_change_event):
        '''Call function to perform the actual install.

        '''
        install_profile.install_succeeded = False
        try:
            perform_ti_install(install_profile, screen, update_status,
                               quit_event, time_change_event)
        except BaseException as ex:
            logging.exception(ex)

    @staticmethod
    def fake_install(install_profile, screen, update_status, quit_event):
        '''For demonstration purposes only, this function is the target
        of the install thread when the '-n' flag is given at the command
        line. All this thread does is attempt to update the status/progress
        bar at set intervals.

        It also checks quit_event (a threading.Event), and, if set, immediately
        returns.

        '''
        if not install_profile.install_to_pool:
            install_profile.disks[0].to_tgt()
        for i in range(101):
            update_status(screen, i, "at %d percent" % i)
            logging.log(LOG_LEVEL_INPUT, "at %s percent", i)
            quit_event.wait(0.2)
            if quit_event.is_set():
                logging.error("User forced quit! Aborting")
                install_profile.install_succeeded = False
                return
        install_profile.install_succeeded = True

    def update_status(self, percent, message):
        '''Update this screen to display message and set the status
        bar to 'percent'. This is the intended callback function
        for the installation thread.

        This function ensures the screen is not updated more often
        then once every 2 seconds (the update_frequency)

        The threading.Event, self.update_event, is used to ensure that this
        method does not clobber the confirm_quit pop-up

        '''
        if self.update_event.is_set():
            return
        update_time = time.time()
        if (update_time - self.last_update) > self.update_frequency:
            self._update_status(percent, message)
            self.last_update = update_time

    def _update_status(self, percent, message):
        '''Immediately update status, without checking the self.update_event
        or last update_time

        '''
        self.set_status_message(message)
        self.set_status_percent(percent)
        self.main_win.redrawwin()
        self.main_win.do_update()

    def set_status_message(self, message):
        '''Set the status message on the screen, completely overwriting
        the previous message'''
        self.center_win.add_text(ljust_columns(message, self.status_msg_loc[2]),
                                 self.status_msg_loc[0],
                                 self.status_msg_loc[1],
                                 max_chars=self.status_msg_loc[2])

    def set_status_percent(self, percent):
        '''Set the completion percentage by updating the progress bar.
        Note that this is implemented as a 'one-way' change (updating to
        a percent that is lower than previously set will not work)

        '''
        width = self.status_bar_width
        complete = int(math.ceil(float(percent) / 100.0 * width))
        self.status_bar.add_text(" " * complete, start_y=0, start_x=1,
                                 max_chars=width)
        percent_text = "(%i%%)" % percent
        percent_bar = percent_text.center(width)
        left_half = percent_bar[:complete]
        right_half = percent_bar[complete:]
        self.status_bar.window.addstr(0, 1, left_half, self.progress_color)
        self.status_bar.window.addstr(0, complete + 1, right_half)
        self.status_bar.no_ut_refresh()

    def init_status_bar(self, y_loc, x_loc, width):
        '''Initialize the progress bar window and set to 0%'''
        self.status_bar_width = width
        status_bar_area = WindowArea(1, width + 3, y_loc, x_loc + 1)
        self.status_bar = InnerWindow(status_bar_area,
                                      window=self.center_win)
        self.status_bar.window.addch(0, 0, InstallProgress.PROG_BAR_ENDS[0])
        self.status_bar.window.addch(0, width + 1,
                                     InstallProgress.PROG_BAR_ENDS[1])
        self.progress_color = self.center_win.color_theme.progress_bar
        self.last_update = 0
        self.set_status_percent(0)

    def confirm_quit(self):
        '''Confirm the user truly wants to quit, and if so, set quit_event
        so that the install thread can attempt to shut down as gracefully
        as possible.

        Clear update_event so that update_status does not attempt to write
        the progress bar while the user is contemplating the need/desire to
        quit.

        '''
        self.update_event.set()
        do_quit = self.main_win.pop_up(BaseScreen.CONFIRM_QUIT_HEADER,
                                       BaseScreen.QUIT_DISK_MOD_TEXT,
                                       BaseScreen.CANCEL_BUTTON,
                                       BaseScreen.CONFIRM_BUTTON)
        update_to = self.update_to
        if update_to is not None:
            self._update_status(update_to[0], update_to[1])
        self.update_event.clear()

        if do_quit:
            self.quit_event.set()
        return do_quit


class Solution(object):
    def genLR(self, l, r, rStr, rLst):
        if l > r: return
        if l == 0 and r == 0:
            rLst.append(rStr)
        else:
            if l > 0:
                self.genLR(l-1, r, rStr+'(', rLst)
            if r > 0:
                self.genLR(l, r-1, rStr+')', rLst)

    def generateParenthesis(self, n):
        """
        :type n: int
        :rtype: List[str]
        """
        rLst = []
        self.genLR(n, n, "", rLst)
        return rLst


# 2)Создайте программу, имитирующую телевизор как объект. У пользователя должна быть возможность вводить
# номер канала, а также увеличивать и уменьшать громкость. Программа допжна следить за тем, чтобы номер
# канала и уровень громкости оставались в допустимых пределах.

# Псевдокод:
# Есть обьект телевизор, который принимает определенное количество
# громковсть может быть не ниже 0 и не выше 100.
# Пользователь может обратитсья к обьекту чтобы узнать уровень громкости и номер канала на котором находится ТВ
# Выход стилизован как выключение ТВ

class TV():
    def __init__(self, channels):
        self.channels = channels
        self.channel = 0
        self.volume = 50

    def change_volume(self, volume = 50):
        if volume > 100 or volume < 0:
            print('Volume not may more or less availibale values.')
        else:
            self.volume = volume
            return self.volume

    def change_step_volume_up(self):
        if self.volume + 1 > 100:
            print('Unacceptable! Volume not may more 100.')
        else:
            self.volume += 1

    def change_step_volume_down(self):
        if self.volume - 1 <= 0:
            print('Unacceptable! Volume not may less 0.')
        else:
            self.volume -= 1

    def change_step_channel_up(self):
        if self.channel >= self.channels:
            print('Unacceptable! Channel not may more', self.channels)
        else:
            self.channel += 1

    def change_step_channel_down(self):
        if self.channel <= 0:
            print('Unacceptable! Channel not may less 0.')
        else:
            self.channel -= 1

    def change_channel(self, channel):
        if channel > self.channels or channel < 0:
            print('That TV haven\'t the channel.')
        else:
            self.channel = channel
            return self.channel

    def info(self):
        print('You stay at channel', self.channel, '.Volume equal', self.volume)

# test
# tv1 = TV(20)
# tv1.info()
# tv1.change_channel(21)
# tv1.change_channel(19)
# tv1.info()

def main_ver1():
    ch_tv = int(input("You buy new TV with 'n' channels, how much channels you have?\n"))
    tv1 = TV(ch_tv)
    choice = None
    while choice != '4':
        print("""
        You turn on TV:
        1 - Look info about channel and volume
        2 - Choice channel
        3 - Choice volume
        4 - turn of tv
        """)
        choice = input('Your choice?\n')
        if choice == '1':
            tv1.info()
        if choice == '2':
            channel = int(input('Input channel.\n'))
            tv1.change_channel(channel)
        if choice == '3':
            volume = int(input('Input volume.\n'))
            tv1.change_volume(volume)
        if choice == '4':
            print('Turn off.')
            break

def main_ver2():
    ch_tv = int(input("You buy old TV with 'n' channels, how much channels you have?\n"))
    tv1 = TV(ch_tv)
    choice = None
    while choice != '6':
        print("""
        You turn on TV:
        1 - Look info about channel and volume
        2 - Change channel by one up
        3 - Change channel by one down
        4 - Change volume by one up
        5 - Change volume by one down
        6 - turn of tv
        """)
        choice = input('Your choice?\n')
        if choice == '1':
            tv1.info()
        if choice == '2':
            tv1.change_step_channel_up()
        if choice == '3':
            tv1.change_step_channel_down()
        if choice == '4':
            tv1.change_step_volume_up()
        if choice == '5':
            tv1.change_step_volume_down()
        if choice == '6':
            print('Turn off.')
            break

#main_ver1()
main_ver2()


import chromadb
chroma_client = chromadb.Client()

collection = chroma_client.create_collection(name="my_collection")

collection.add(
    embeddings=[[1.2, 2.3, 4.5], [6.7, 8.2, 9.2]],
    documents=["This is a document", "This is another document"],
    metadatas=[{"source": "my_source"}, {"source": "my_source"}],
    ids=["id1", "id2"]
)

results = collection.query(
    query_embeddings=[[11.1, 12.1, 13.1],[1.1, 2.3, 3.2], ...],
    n_results=10,
    where={"metadata_field": "is_equal_to_this"},
    where_document={"$contains":"search_string"}
)

print(results)


import os
import math
import ssl

import dill

import geopy.geocoders
from geopy.geocoders import GoogleV3, Nominatim
from geopy.extra.rate_limiter import RateLimiter

# Fix to deactivate the ssl verification
ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE
geopy.geocoders.options.default_ssl_context = ctx

class GeocodingApiResolver:
    """
    Return a dataframe name / geometry / resolved name
    The cache will have the same format
    """

    def __init__(self, api_name="nominatim", api_key=None):

        if api_name == "nominatim":
            geolocator = Nominatim(user_agent="data4good-beges")
            self.p_raw_cache = "/data/raw/chorus-dt/nominatim_responses.pkl"
            self.p_cache = "/data/prepared/nominatim_responses.pkl"
        elif api_name == "gmap":
            geolocator = GoogleV3(api_key=api_key, domain="maps.google.fr")
            self.p_raw_cache = "/data/raw/chorus-dt/gmap_responses.pkl"
            self.p_cache = "/data/prepared/gmap_responses.pkl"
        else:
            raise ValueError
        self.geocode_with_delay = RateLimiter(geolocator.geocode, min_delay_seconds=1, max_retries=10)
        self._cache = self.load_cache()

    def resolve(self, x):
        name = x["name"]
        if name is not None and (name != ""):
            if name in self._cache:
                response = self._cache[name]
            else:
                response = self.geocode_with_delay(name)
                self._cache[name] = response
        else:
            response = None

        if response is not None:
            x["lon"] = response.longitude
            x["lat"] = response.latitude
            x["address"] = response.address
            x["resolved"] = True
            x["resolved_through_gmap_api"] = True
        return x

    def load_cache(self):
        if os.path.isfile(self.p_cache):
            with open(self.p_cache, "rb") as file_id:
                cache = dill.load(file_id)
        else:
            with open(self.p_raw_cache, "rb") as file_id:
                cache = dill.load(file_id)
        return cache

    def save_cache(self):
        with open(self.p_cache, "wb") as file_id:
            dill.dump(self._cache, file_id)

class HardcodesResolver:
    def __init__(self):
        self.insee_codes = self.load_codes("/data/prepared/insee_codes.pkl")
        self.tvs_codes = self.load_codes("/data/prepared/tvs_codes.pkl")
        self.uic_codes = self.load_codes("/data/prepared/uic_codes.pkl")
        self.iata_codes = self.load_codes("/data/prepared/iata_codes.pkl")

    def load_codes(self, path):
        with open(path, "rb") as file_id:
            return dill.load(file_id)

    def resolve(self, x):
        code_2_as_int = self.get_int(x["code_2"])

        try:
            if code_2_as_int and (code_2_as_int in self.uic_codes["lon"]):
                x["lon"] = self.uic_codes["lon"][code_2_as_int]
                x["lat"] = self.uic_codes["lat"][code_2_as_int]
                x["resolved"] = True
                x["resolved_through_uic_code"] = True
            elif x["code_1"] in self.insee_codes["lon"]:
                x["lon"] = self.insee_codes["lon"][x["code_1"]]
                x["lat"] = self.insee_codes["lat"][x["code_1"]]
                x["resolved"] = True
                x["resolved_through_insee_code"] = True
            elif (str(x["code_1"][-3:]) in self.iata_codes["lon"]) and (str(x["code_1"][:2]) == "IA"):
                x["lon"] = self.iata_codes["lon"][x["code_1"][-3:]]
                x["lat"] = self.iata_codes["lat"][x["code_1"][-3:]]
                x["resolved"] = True
                x["resolved_through_iata_code"] = True
            elif x["place"] == "-75056":  # Hard exception...
                x["name"] = "Paris"
            # elif x['code_1'] in TVS_CODES['lon']:
            #     x['lon'] = TVS_CODES['lon'][x['code_1']]
            #     x['lat'] = TVS_CODES['lat'][x['code_1']]
            #     x['resolved'] = True
            #     x['resolved_through_tvs_code'] = True
            else:
                x["lon"] = None
                x["lat"] = None

        except Exception as e:
            print(e)
            print(x["code_1"])
        return x

    def get_int(self, x):
        try:
            return int(x)
        except ValueError:
            return None


def identify_name(line):
    '''
    Determines the name of the gene in a given header.
    '''
    sub = ''
    if ')' in  line:
        if 'gi:' in line:
            sub = line.split('(')[2].split(')')[0]
        else:
            sub = line[line.find("(")+1:line.find(")")]
    return sub

def weird_name(subq,subp):
    '''
    Returns true if either value is a name which is problematic for our
    substring search approach and short be ignored.
    '''
    t = ('st','tia')
    return (subq in t) or (subp in t)

def shortnames(fname='data/repaired_ecoli_vfs.ffn'):
    '''
    A substring search method which looks for the shortest gene names, in a
    file of reference sequences, and replaces the longer names with the shorter
    ones.
    '''
    lines = []
    with open(fname) as f:
        lines = [line.rstrip('\n') for line in f]

    for i in range(0,len(lines),2):
        sub = identify_name(lines[i])
        stri = sub.split('_')[0].split('-I')[0].split('-V')[0]
        if sub != stri:
            if '-I' in stri:
                stri.split('-I')[0]
            lines[i] = lines[i].replace(sub,stri,1)

    p=0
    while p<len(lines)-1-2:
        subp = identify_name(lines[p])
        q=p
        while q<len(lines)-1:
            subq = identify_name(lines[q])
            if ((subp.lower() in subq.lower()) or (subq.lower() in subp.lower())) and not weird_name(subq, subp):
                # replace just the name (ex. eaeH), doesn't replace entire header
                if len(subp) > len(subq):
                    lines[p] = lines[p].replace(subp,subq,1)
                elif len(subp) < len(subq):
                    lines[q] = lines[q].replace(subq,subp,1)
            q += 2
        p += 2

    with open('data/repaired_ecoli_vfs_shortnames.ffn', 'w') as f:
        for line in lines:
            f.write(line + '\n')

if __name__ == '__main__':
    shortnames()


import socket
import unittest

from cli_parser import Parser
from enums.parser_enum import ParserEnum

class TestParser(unittest.TestCase):
    def test_help_command(self):
        expected_result = {
            ParserEnum.error: False,
            ParserEnum.help_key: True,
            ParserEnum.help_key_text: f'Справка:\r\n'
                                      f'{ParserEnum.trace_port_flag} - порт, на котором работает traceroute '
                                      f'(по-умолчанию стоит 80)\r\n'
                                      f'{ParserEnum.ttl_max_flag} - максимальное время жизни пакета '
                                      f'(максимальное количество прыжков по промежуточным узлам'
                                      f'по-умолчанию стоит 30)\r\n'
                                      f'{ParserEnum.timeout_flag} - максимальное время ожидания ответа'
                                      f'от промежуточного узла с фиксированным временем жизни (по-умолчанию'
                                      f'стоит 3 секунды)\r\n'
                                      f'{ParserEnum.middle_link_flag} - промежуточный ip, через который должен быть'
                                      f'построен маршрут\r\n'
                                      f'{ParserEnum.target_flag} - адрес (ip или домен), маршрут которого'
                                      f'хотите получить (обязательный параметр)\r\n'
        }

        actual_result = Parser().parse(['--help'])

        self.assertDictEqual(expected_result, actual_result)

    def test_all_parser(self):
        expected_result = {
            ParserEnum.target_key: 'google.com',
            ParserEnum.timeout_key: 0.1,
            ParserEnum.ttl_max_key: 50,
            ParserEnum.trace_port_key: 81,
            ParserEnum.error: False,
            ParserEnum.middle_link_key: ''
        }

        actual_result = Parser().parse('main.py --target google.com -t 0.1 --ttl-max 50 --trace-port 81'.split(' '))

        self.assertDictEqual(expected_result, actual_result)

    def test_without_target_flag(self):
        expected_result = {
            ParserEnum.error: True,
            ParserEnum.error_message: 'Вы не указали адрес, маршрут которого хотите получить'
        }

        actual_result = Parser().parse('main.py -t 0.1 --ttl-max 50 --trace-port 81'.split(' '))

        self.assertDictEqual(expected_result, actual_result)

    def test_with_incorrect_ttl_value(self):
        expected_result = {
            ParserEnum.error: True,
            ParserEnum.error_message: f'Вы не ввели не верное значение '
                                      f'для максимального времени жизни ({ParserEnum.ttl_max_flag})'
        }

        actual_result = Parser().parse('main.py -t 0.1 --ttl-max None --trace-port 81'.split(' '))

        self.assertDictEqual(expected_result, actual_result)

    def test_with_incorrect_timeout_value(self):
        expected_result = {
            ParserEnum.error: True,
            ParserEnum.error_message: f'Вы не ввели не верное значение '
                                      f'для таймаута ожидания ({ParserEnum.timeout_flag})'
        }

        actual_result = Parser().parse('main.py -t None --ttl-max 30 --trace-port 81'.split(' '))

        self.assertDictEqual(expected_result, actual_result)

    def test_only_with_target(self):
        expected_result = {
            ParserEnum.target_key: 'google.com',
            ParserEnum.timeout_key: 3,
            ParserEnum.ttl_max_key: 30,
            ParserEnum.trace_port_key: 80,
            ParserEnum.error: False,
            ParserEnum.middle_link_key: ''
        }

        actual_result = Parser().parse('main.py --target google.com'.split(' '))

        self.assertDictEqual(expected_result, actual_result)

    def test_not_find_element(self):
        expected_result = -1
        actual_result = Parser.find(['lol, kek, chebureck'], 'test')

        self.assertEqual(expected_result, actual_result)

    def test_only_without_value_target(self):
        expected_result = {
            ParserEnum.error: True,
            ParserEnum.error_message: 'Вы не указали адрес, маршрут которого хотите получить'
        }

        actual_result = Parser().parse('main.py --target'.split(' '))

        self.assertDictEqual(expected_result, actual_result)

    def test_only_without_value_timeout(self):
        expected_result = {
            ParserEnum.error: True,
            ParserEnum.error_message: f'Вы не ввели не верное значение '
                                      f'для таймаута ожидания ({ParserEnum.timeout_flag})'
        }

        actual_result = Parser().parse('main.py --target google.com -t'.split(' '))

        self.assertDictEqual(expected_result, actual_result)

    def test_only_without_value_ttl(self):
        expected_result = {
            ParserEnum.error: True,
            ParserEnum.error_message: f'Вы не ввели не верное значение '
                                      f'для максимального времени жизни ({ParserEnum.ttl_max_flag})'
        }

        actual_result = Parser().parse('main.py --target google.com --ttl-max'.split(' '))

        self.assertDictEqual(expected_result, actual_result)

    def test_only_without_value_trace_port(self):
        expected_result = {
            ParserEnum.error: True,
            ParserEnum.error_message: f'Вы не ввели порт, на котором будет '
                                      f'работать traceroute ({ParserEnum.trace_port_flag})'
        }

        actual_result = Parser().parse('main.py --target google.com --trace-port'.split(' '))

        self.assertDictEqual(expected_result, actual_result)


import numpy as np
import pandas as pd
import argparse

OPTS = None

def parse_args():
    parser = argparse.ArgumentParser()

    parser.add_argument(
        '--input1',
        required=True,
        help="the name of the first data file"
    )

    parser.add_argument(
        '--input2',
        required=True,
        help="the name of the second data file"
    )

    return parser.parse_args()

def main():
    df1 = pd.read_csv(OPTS.input1, header=None,
                           names=['idx', 'sentence', 'contains', 'probability'])
    print(df1.shape)
    df1['length'] = df1.sentence.str.len()

    df1.probability = df1.probability.astype(float)
    print()
    print(f"Analyzing: {OPTS.input1.split('/')[-1]}")
    means = df1.groupby('contains').mean(numeric_only=True)['probability']
    stderrs = df1.groupby('contains').std(numeric_only=True)['probability'] / np.sqrt(len(df1))
    print(f"contains: False    {means[False]:.4f} +- {stderrs[False]:.4f}")
    print(f"contains: True     {means[True]:.4f} +- {stderrs[True]:.4f}")
    without_df1 = df1[~df1.contains]
    print(f"Average prompt without comma = {df1[~df1.contains]['length'].mean()} +- {without_df1['length'].std() / np.sqrt(len(without_df1))}")
    with_df1 = df1[df1.contains]
    print(f"Average prompt with comma    = {with_df1['length'].mean()} +- {with_df1['length'].std() / np.sqrt(len(without_df1))}")
    print(f"Final = {df1.contains.value_counts()}")

    df2 = pd.read_csv(OPTS.input2, header=None,
                      names=['idx', 'sentence', 'contains', 'probability'])
    df2['length'] = df2.sentence.str.len()

    df2.probability = df2.probability.astype(float)
    print()
    print(f"Analyzing: {OPTS.input2.split('/')[-1]}")
    means = df2.groupby('contains').mean(numeric_only=True)['probability']
    stderrs = df2.groupby('contains').std(numeric_only=True)['probability'] / np.sqrt(len(df2))
    print(f"contains: False    {means[False]:.4f} +- {stderrs[False]:.4f}")
    print(f"contains: True     {means[True]:.4f} +- {stderrs[True]:.4f}")
    without_df2 = df2[~df2.contains]
    print(
        f"Average prompt without comma = {df2[~df2.contains]['length'].mean()} +- {without_df2['length'].std() / np.sqrt(len(without_df2))}")
    with_df2 = df2[df2.contains]
    print(
        f"Average prompt with comma    = {with_df2['length'].mean()} +- {with_df2['length'].std() / np.sqrt(len(without_df2))}")
    print(f"Final = {df2.contains.value_counts()}")

if __name__ == '__main__':
    OPTS = parse_args()
    main()


import socket

server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.connect(("127.0.0.1", 9655))
server.setblocking(False)
while True:
    try:
        msg = server.recv(100) #BlockingIOError: [WinError 10035] 无法立即完成一个非阻止性套接字操作。
    except BlockingIOError as e:
        print("做些别的")
    else:
        print(msg)
        break


from django.urls import path, include

from apps.codeproj.apis import v3

# 前缀: /api/v3/orgs/<org_sid>/teams/<team_name>/repos/<repo_id>/projects/
project_urlpatterns = [
    # 项目接口
    path("<int:project_id>/", v3.PTProjectDetailView.as_view(),
         name="apiv3_project_detail"),

    # 扫描接口
    path("<int:project_id>/scans/", v3.PTProjectScanListView.as_view(),
         name="apiv3_scan_list"),
    path("<int:project_id>/scans/<int:scan_id>/", v3.PTProjectScanDetailView.as_view(),
         name="apiv3_scan_detail"),
    path("<int:project_id>/scans/latest/", v3.PTProjectScanLatestDetailView.as_view(),
         name="apiv3_scan_latest_detail"),

    # 概览接口
    path("<int:project_id>/overview/mine/", v3.PTProjectMyOverviewDetailView.as_view(),
         name="apiv3_project_my_overview_detail"),
    path("<int:project_id>/overview/lintscans/", v3.PTProjectOverviewLintScanListView.as_view(),
         name="apiv3_project_overview_lintscan_list"),
    path("<int:project_id>/overview/lintscans/latest/", v3.PTProjectOverviewLintScanLatestDetailView.as_view(),
         name="apiv3_project_overview_lintscan_latest"),
    path("<int:project_id>/overview/cycscans/", v3.PTProjectOverviewCycScanListView.as_view(),
         name="apiv3_project_overview_lintscans"),
    path("<int:project_id>/overview/dupscans/", v3.PTProjectOverviewDupScanListView.as_view(),
         name="apiv3_project_overview_dupscans"),
    path("<int:project_id>/overview/clocscans/", v3.PTProjectOverviewClocScanListView.as_view(),
         name="apiv3_project_overview_clocscans"),
    path("<int:project_id>/overview/checktoolscans/", v3.PTProjectOverviewCheckToolScanView.as_view(),
         name="apiv3_project_overview_checktoolscan"),

    # 转发到相应的模块
    path(r"<int:project_id>/codelint/", include("apps.codelint.api_urls.v3_pt")),
    path(r"<int:project_id>/codemetric/", include("apps.codemetric.api_urls.v3_pt")),

]

# 前缀: /api/v3/orgs/<org_sid>/teams/<team_name>/repos/
urlpatterns = [
    path("<int:repo_id>/projects/", include(project_urlpatterns))
]


import numpy as np
import pandas as pd
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--labels",help="csv file containing true labels",default="npf_train.csv")
parser.add_argument("--answers",help="csv file containing your answers",default="dummy.csv")
parser.add_argument("--accuracy",help="estimate of binary classificatin accuracy",default="0.5")
args = parser.parse_args()

labels = pd.read_csv(args.labels)
answers = pd.read_csv(args.answers)

n = labels.shape[0]

isevent_labels = np.array([1]*n)
isevent_labels[labels["event"]=="nonevent"] = 0

isevent_answers = np.array([1]*n)
isevent_answers[answers["event"]=="nonevent"] = 0

accuracy = np.mean(isevent_labels==isevent_answers)

accuracyofaccuracy = np.abs(float(args.accuracy)-accuracy)

perplexity = np.exp(-np.mean(np.log(np.where(isevent_labels==1,answers["p_event"],1-answers["p_event"]))))

multiaccuracy = np.mean(labels["event"]==answers["event"])

print("accuracy,perplexity,multiaccuracy,accuracyofaccuracy\n%.5f,%.5f,%.5f,%.5f" % (accuracy,perplexity,multiaccuracy,accuracyofaccuracy))


import logging
import os
from typing import List, Callable

from sortedcontainers import SortedList

__author__ = "Guido Pio Mariotti"
__copyright__ = "Copyright (C) 2016 Guido Pio Mariotti"
__license__ = "GNU General Public License v3.0"
__version__ = "0.2.0"

class CSVSerializer:
    """
    This class represents a serializer of solutions in a csv formatted file.
    """

    def __init__(self, directory: str, n_print: int, headers: List[str],
                 filename_creator: Callable[[int, int], str]):
        self._directory = directory
        self._num = n_print
        self._headers = headers
        self._filename_creator = filename_creator
        self._separator = "\t"

    @classmethod
    def new_instance(cls, output_dir: str, n_print: int, headers: List[str],
                     filename_creator: Callable[[int, int], str]
                     ) -> 'CSVSerializer':
        """
        Creates a new instance of a CSVSerializer.
        :param output_dir: Name of the directory where to put the serialized
        solutions. It's created if it doesn't exist.
        :param n_print: Number of solutions to print for each serialization.
        :param headers: Name of the headers to use for the data serialized.
        :param filename_creator: Function for creating the name of the output
        file.
        :return: An instance of CSVSerializer.
        """
        if not os.path.isdir(output_dir):
            os.makedirs(output_dir)
        if len(headers) == 0:
            raise RuntimeError(
                "List of headers for solutions serialization is empty."
            )
        return CSVSerializer(
            output_dir, n_print, headers, filename_creator
        )

    def serialize_solutions(self, solutions: SortedList, filename: str = None):
        """
        Serialize a list of solutions as a single csv file. The matrix to use
        is the one returned from an instance of BaseSolution, while the last
        row is the Cost with its value. Use filename in case you want a
        different name for the file in respect to the one generated by the
        filename_creator passed as an argument at creation time.
        :param solutions: List of BaseSolution objects
        :param filename: Optional filename.
        """
        if filename is None:
            filename = self._filename_creator(
                self._num, solutions[0].number_of_variables
            )
        num_solutions_to_print = min(len(solutions), self._num)
        with open(self._directory + "/" + filename, "w") as output:
            for i in range(0, num_solutions_to_print):
                solution = solutions[i]
                s_matrix = solution.get_solution_matrix(headers=self._headers)
                output.write(s_matrix.to_csv(sep=self._separator, index=False))
                output.write("Cost{}{}\n".format(
                    self._separator, solution.cost
                ))
        logging.getLogger(__name__).info(
            "Created file {} in directory {}".format(filename, self._directory)
        )


import pentaword.schedule as schedule
import time

import random
import requests
from pentaword.models import Word
from bs4 import BeautifulSoup

def update_daily_word():
    def get_word():
        query_elements = ''

        htmlpage = requests.get(
            "https://www.dicio.com.br/palavras-com-cinco-letras/")
        bshandler = BeautifulSoup(htmlpage.content, features="html.parser")

        bs_query = bshandler.find_all('p')

        for element in bs_query:
            query_elements += str(element)

        wordlist = query_elements.split('<br/>')
        word = random.choice(wordlist)

        return word

    def replacer(word):
        replace_word = word
        newword = ''

        converter_dict = {
            '224': 'a',
            '225': 'a',
            '226': 'a',
            '227': 'a',
            '233': 'e',
            '234': 'e',
            '237': 'i',
            '243': 'o',
            '244': 'o',
            '245': 'o',
            '250': 'u',
            '231': 'c',
            '241': 'n',
        }

        for char in replace_word:
            if str(ord(char)) in converter_dict:
                replacer = converter_dict.get(str(ord(char)))
                newword += replacer
            else:
                newword += char

        return newword

    replacer_word = replacer(get_word())
    new_word = Word.objects.get(pk=1)
    new_word.dailyword = replacer_word
    new_word.save()

schedule.every(2).minutes.do(update_daily_word())

while True:
    schedule.run.pending()
    time.sleep(5)