Ejemplo n.º 1
0
 def test_edit_distance_invalud(self):
     ''' check the property setting of the distance property on invalid inputs '''
     spell = SpellChecker(distance=None)
     self.assertEqual(spell.distance, 2)
     spell.distance = 1
     self.assertEqual(spell.distance, 1)
     spell.distance = 'string'
     self.assertEqual(spell.distance, 2)
Ejemplo n.º 2
0
def correct_instructions(instruction):
    # The directives get checked and corrected using spellchecker distance ?
    # The strings get corrected to their closest words in the corpus
    # "TAGE 1 TAGLET" becomes "TAKE 1 TABLET"
    spell = SpellChecker()
    spell.distance = 3
    keywords = [
        'every', 'once', 'twice', 'thrice', 'daily', 'hours', 'hour', 'day',
        'days', 'weeks', 'morning', 'afternoon', 'night', 'one', 'tablet'
    ]
    # Tokenize the directive
    token_instruction = instruction.split()
    # Spell Check and correct all the elements of the list
    close_words = [spell.candidates(token) for token in token_instruction]

    # Correct any words that are close in distance to the keywords
    for counter, token in enumerate(token_instruction):
        for keyword in close_words[counter]:
            if keyword in keywords:
                token_instruction[counter] = keyword

    # General spell correction
    for counter, token in enumerate(token_instruction):
        if token.isalpha():
            token_instruction[counter] = spell.correction(token)

            # Make the tokens uppercase
    token_instruction = [token.upper() for token in token_instruction]
    # print(close_words)

    return token_instruction
Ejemplo n.º 3
0
	def corrected_word_tokens(self, tokens):
		spell = SpellChecker()
		spell.distance = 1
		corrected_word_tokens = []
		for token in tokens:
			corrected_word_tokens.append(spell.correction(token))
		return corrected_word_tokens
Ejemplo n.º 4
0
def correctquery(query):
    spell = SpellChecker()
    spell.distance = 1
    words = query.split(" ")
    corrected = []
    for word in words:
        corrected.append(spell.correction(word))
    return " ".join(corrected)
Ejemplo n.º 5
0
 def test_edit_distance_one_property(self):
     ''' check the property setting of the distance property '''
     spell = SpellChecker(distance=1)
     self.assertEqual(spell.distance, 1)
     spell.distance = 2
     self.assertEqual(spell.distance, 2)
Ejemplo n.º 6
0
import bpy
import re
from spellchecker import SpellChecker

error_postfix = '<fix>'

# a list of known words (in addition to the built-in)
known_words = ['example', 'of', 'list']

# a list of prohibited words (removed from the names)
prohibited_words = ['material', 'mat', 'mesh']

spell = SpellChecker(case_sensitive=True)
spell.word_frequency.load_words(known_words)
spell.distance = 1


def to_snake_case(name):
    return re.sub(r'(?<!^)(?=[A-Z])', '_', name).lower()


def to_words(name):
    return re.split(r'_|\.', name)


def remove_prohibited_words(words):
    return [w for w in words if not w in prohibited_words]


def apply_spell_correction(words):
    words = [spell.correction(w) for w in words]
Ejemplo n.º 7
0
def funkcija_sedam(imeFajla, distanca, izlazniFajl):
    komanda = 'python3 diplomski_rad.py -f ' + imeFajla + ' -2'
    os.system(komanda)
    spel = SpellChecker(language=None, distance=distanca)
    spel.word_frequency.load_text_file('./sr_full.txt')
    pogresneReci = list()
    recnik = dict()
    if izlazniFajl != imeFajla:
        izlazniFajl = izlazniFajl + '.' + tip_fajla(imeFajla)
    if tip_fajla(imeFajla) == 'txt':
        try:
            fajl = open(imeFajla, 'r', encoding='utf8')
        except:
            print('Uneli ste nepostojeći ili nedozvoljeni fajl!')
            return
        podaci = fajl.read()
        fajl.close()
        reci = podaci.split()
        for r in range(len(reci)):
            reci[r] = re.sub('\W+', '', reci[r])
        pogresneReci = list(spel.unknown(reci))
        pogresneReci = list(filter(None, pogresneReci))
        for pogresnaRec in pogresneReci:
            print('Reč ' + pogresnaRec +
                  ' je možda pogrešno napisana. Želite li da reč "' +
                  pogresnaRec + '" zamenite nekom drugom sekvencom karaktera?')
            odgovor = input('da|ne|izlaz: ')
            while odgovor != 'ne' and odgovor != 'da' and odgovor != 'izlaz':
                odgovor = input('da|ne|izlaz: ')
            if odgovor == 'ne':
                continue
            elif odgovor == 'da':
                if len(pogresnaRec) > 10:
                    spel.distance = 1
                else:
                    spel.distance = distanca
                recnik[pogresnaRec] = list(spel.candidates(pogresnaRec))
                indeksiPreporuka = [
                    recnik[pogresnaRec].index(x) + 1
                    for x in recnik[pogresnaRec]
                ]
                indeksiIReci = list()
                for i in range(len(recnik[pogresnaRec])):
                    indeksiIReci.append(recnik[pogresnaRec][i] +
                                        ' -> indeks: ' +
                                        str(indeksiPreporuka[i]))
                print('Preporuke za reč ' + pogresnaRec + ': ' +
                      str(indeksiIReci))
                odgovor = int(input('Unesite indeks: '))
                while type(odgovor) != int or odgovor > indeksiPreporuka[
                        -1] or odgovor < 1:
                    print(
                        'Potrebno je uneti realan broj koji je manji ili jednak broju ponuđenih reči u listi a veci od nule!'
                    )
                    odgovor = int(input('Unesite indeks: '))
                podaci = re.sub('\\b' + pogresnaRec + '\\b',
                                recnik[pogresnaRec][odgovor - 1],
                                podaci,
                                flags=re.I)
            elif odgovor == 'izlaz':
                izmenjeniFajl = open(imeFajla, 'w', encoding='utf8')
                izmenjeniFajl.write(podaci)
                izmenjeniFajl.close()
                return
        izmenjeniFajl = open(izlazniFajl, 'w')
        izmenjeniFajl.write(podaci)
        izmenjeniFajl.close()
    elif tip_fajla(imeFajla) == 'docx':
        try:
            fajl = docx.Document(imeFajla)
        except:
            print('Uneli ste nepostojeći ili nedozvoljeni fajl!')
            return
        noviFajl = docx.Document()
        podaci = list()
        brojac = 0
        stiloviParagrafa = list()
        tekstIstogRuna = list()
        spajanjeIstihRunova = False
        proveraNarednogParagrafa = False
        for stil in range(len(fajl.paragraphs)):
            stiloviParagrafa.append(fajl.paragraphs[stil].style)
        for para in fajl.paragraphs:
            for karakter in para.text:
                podaci.append(karakter)
            podaci.append('\n')
        podaci = ''.join(podaci)
        reci = podaci.split()
        for r in range(len(reci)):
            reci[r] = re.sub('\W+', '', reci[r])
        pogresneReci = list(spel.unknown(reci))
        pogresneReci = list(filter(None, pogresneReci))
        parReciZaKorekciju = dict()
        brojIspitanihReci = len(pogresneReci)
        baremJednaIspitanaRec = False
        for pogresnaRec in pogresneReci:
            print('Reč ' + pogresnaRec +
                  ' je možda pogrešno napisana. Želite li da reč "' +
                  pogresnaRec + '" zamenite nekom drugom sekvencom karaktera?')
            odgovor = input('da|ne|izlaz: ')
            while odgovor != 'ne' and odgovor != 'da' and odgovor != 'izlaz':
                odgovor = input('da|ne|izlaz: ')
            if odgovor == 'ne':
                brojIspitanihReci -= 1
                continue
            elif odgovor == 'da':
                brojIspitanihReci -= 1
                if len(pogresnaRec) > 10:
                    spel.distance = 1
                else:
                    spel.distance = distanca
                recnik[pogresnaRec] = list(spel.candidates(pogresnaRec))
                indeksiPreporuka = [
                    recnik[pogresnaRec].index(x) + 1
                    for x in recnik[pogresnaRec]
                ]
                indeksiIReci = list()
                for i in range(len(recnik[pogresnaRec])):
                    indeksiIReci.append(recnik[pogresnaRec][i] +
                                        ' -> indeks: ' +
                                        str(indeksiPreporuka[i]))
                print('Preporuke za reč ' + pogresnaRec + ': ' +
                      str(indeksiIReci))
                odgovor = int(input('Unesite indeks: '))
                while type(odgovor) != int or odgovor > indeksiPreporuka[
                        -1] or odgovor < 1:
                    print(
                        'Potrebno je uneti realan broj koji je manji ili jednak broju ponuđenih reči u listi a veci od nule!'
                    )
                    odgovor = int(input('Unesite indeks: '))
                parReciZaKorekciju[pogresnaRec] = recnik[pogresnaRec][odgovor -
                                                                      1]
                baremJednaIspitanaRec = True
            elif odgovor == 'izlaz' and baremJednaIspitanaRec:
                for para in fajl.paragraphs:
                    paragraf = noviFajl.add_paragraph()
                    trenutniRun = 0
                    for run in para.runs:
                        listaKaraktera = list(run.text)
                        if listaKaraktera and listaKaraktera[-1].isalnum():
                            try:
                                prviKarakterSlRun = fajl.paragraphs[
                                    brojac].runs[trenutniRun + 1].text[0]
                                if prviKarakterSlRun.isalpha():
                                    spajanjeIstihRunova = True
                                    tekstIstogRuna = tekstIstogRuna + listaKaraktera.copy(
                                    )
                                    trenutniRun += 1
                                    continue
                            except:
                                pass
                        if spajanjeIstihRunova:
                            listaKaraktera = tekstIstogRuna + listaKaraktera
                            tekstIstogRuna = list()
                            spajanjeIstihRunova = False
                        trenutniRun += 1
                        tekst = ''.join(listaKaraktera)
                        for rec in parReciZaKorekciju.keys():
                            pronadjeno = re.search('\\b' + rec + '\\b',
                                                   tekst,
                                                   flags=re.I)
                            if pronadjeno:
                                tekst = re.sub('\\b' + rec + '\\b',
                                               parReciZaKorekciju[rec],
                                               tekst,
                                               flags=re.I)
                        r = svojstva_runa(paragraf, run, tekst)
                        trenutniRun += 1
                    paragraf.style = stiloviParagrafa[brojac]
                    paragraf.alignment = para.alignment
                    brojac += 1
                noviFajl.save(izlazniFajl)
                komanda = 'python3 diplomski_rad.py -f ' + izlazniFajl + ' -1'
                os.system(komanda)
                return
            elif odgovor == 'izlaz' and not baremJednaIspitanaRec:
                return
        if brojIspitanihReci < 1:
            for para in fajl.paragraphs:
                paragraf = noviFajl.add_paragraph()
                trenutniRun = 0
                for run in para.runs:
                    listaKaraktera = list(run.text)
                    if listaKaraktera and listaKaraktera[-1].isalnum():
                        try:
                            prviKarakterSlRun = fajl.paragraphs[brojac].runs[
                                trenutniRun + 1].text[0]
                            if prviKarakterSlRun.isalpha():
                                spajanjeIstihRunova = True
                                tekstIstogRuna = tekstIstogRuna + listaKaraktera.copy(
                                )
                                continue
                        except:
                            pass
                    if spajanjeIstihRunova:
                        listaKaraktera = tekstIstogRuna + listaKaraktera
                        tekstIstogRuna = list()
                        spajanjeIstihRunova = False
                    trenutniRun += 1
                    tekst = ''.join(listaKaraktera)
                    for rec in parReciZaKorekciju.keys():
                        pronadjeno = re.search('\\b' + rec + '\\b',
                                               tekst,
                                               flags=re.I)
                        if pronadjeno:
                            tekst = re.sub('\\b' + rec + '\\b',
                                           parReciZaKorekciju[rec],
                                           tekst,
                                           flags=re.I)
                    r = svojstva_runa(paragraf, run, tekst)
                    trenutniRun += 1
                paragraf.style = stiloviParagrafa[brojac]
                paragraf.alignment = para.alignment
                brojac += 1
            noviFajl.save(izlazniFajl)
            komanda = 'python3 diplomski_rad.py -f ' + izlazniFajl + ' -1'
            os.system(komanda)
            return
    else:
        print('Funkcija prima kao ulaz fajlove sa ekstenzijom .txt i .docx')
        return
Ejemplo n.º 8
0
    new_sen = words.splitlines()
    corrected = []
    wrong = []

    for val,item in enumerate(new_sen):
        for word in checker.split_words(item.strip()):
            correct = checker.correction(word)
            if word != correct:
                sentence = sentence.replace(word,correct)
                corrected.append(correct)
                wrong.append(word)
    wordings = dict(zip(wrong,corrected))
    return sentence, wordings


checker.distance= 2




API = "AIzaSyDTq4cci16u92_lonCb5CA2oBhYvWagc3I"

def detect_text(image_file, access_token=None):
    print(type(image_file))
    with open(image_file, 'rb') as image:
        base64_image = base64.b64encode(image.read()).decode()

    url = 'https://vision.googleapis.com/v1/images:annotate?key={}'.format(
                                                                   access_token)
    header = {'Content-Type': 'application/json'}
    body = {