Monday, February 27, 2023

Word Macro to Remove Highlight and Shading from Text

 Remove highlighting and shading (for instance, to remove highlighting from Finereader OCR):

Sub RemoveShadingandHighlights()
  Selection.Font.Shading.Texture = wdTextureNone
  Selection.Shading.BackgroundPatternColor = wdColorWhite
  Selection.Shading.ForegroundPatternColor = wdColorWhite
  Selection.Range.HighlightColorIndex = wdNoHighlight
End Sub

Remove highlighting:

Sub RemoveAllHighlights()
  Selection.Range.HighlightColorIndex = wdNoHighlight
End Sub
Remove shading: 
 Sub RemoveShading()
  Selection.Font.Shading.Texture = wdTextureNone
  Selection.Shading.BackgroundPatternColor = wdColorWhite
  Selection.Shading.ForegroundPatternColor = wdColorWhite
End Sub 

Friday, February 17, 2023

Difference between r, r+, w, w+, a and a+ in Python

 

Differences between open modes r, r+, w, w+, a and a+ in open() function.


r r+ w w+ a a+
read * *
*
*
write
* * * * *
create

* * * *
truncate

* *

position at start * * * *

position at end



* *

In this context, truncate means delete the content of the file.

1.2 Definition of open modes r, r+, w, w, a, a+:

  • The r throws an error if the file does not exist or opens an existing file without truncating it for reading; the file pointer position at the beginning of the file.
  • The r+ throws an error if the file does not exist or opens an existing file without truncating it for reading and writing; the file pointer position at the beginning of the file.
  • The w creates a new file or truncates an existing file, then opens it for writing; the file pointer position at the beginning of the file.
  • The w+ creates a new file or truncates an existing file, then opens it for reading and writing; the file pointer position at the beginning of the file.
  • The a creates a new file or opens an existing file for writing; the file pointer position at the end of the file.
  • The a+ creates a new file or opens an existing file for reading and writing, and the file pointer position at the end of the file.

File Operations in Python

File operations are the operations that can be performed on a file. These include operations carried out by the user using Python commands (or any other programming language).

A few fundamental file operations are listed below:

  1. Open: The first and most important operation on a file is to open it. When you create a file, you must open it in order to do further file processing operations. Python offers an in-built open() function to open a file. The open() function returns a file object, also known as a handle, to perform further operations accordingly.
  2. Read: As the name suggests, this operation reads the content of a file. Python provides various methods to read a file, the most common being the read() function. Note that in order to read a file you'll need to open that file in 'read mode'.
  3. Write: This operation is used to write information into a file. There are various modes, that can be used, for the write operation (we'll soon discuss the different modes).
  4. Close: After completing all procedures, the file must be closed in order to save the data. This operation frees up all the resources used up by the file while processing. Python has a close() method to close the file.

Python has six File Access Modes:

Sr. No.

Access Mode

(File Operations)

Description
1. Read Only ('r')

Default mode. Opens a file in Python to read. (Raises an I\O error if the file does not exist.)

2. Read & Write ('r+') With this, you can read as well as write in the file.
3. Write Only ('w') It is used to write in a file. (This creates a new file if the file doesn't exist). This overwrites on an existing file.
4. Write & Read ('w+') Used for writing as well as reading an opened file in Python
5. Append Only ('a') This is used to insert data at the end of an opened file. Here, the existing data won't get truncated.
6. Append & Read ('a+') This is used to open a file for writing (at the end) and reading.

The access methods are mentioned along with the file name in the open() function.

The syntax to open a file is:

f = open("FilePath", "access mode")

 Using seek() and truncate() function

This method can be used to overwrite a file (completely or a particular line) in Python. This method involves two functions :

  1. The seek() function: This function sets the handler (pointer) at the beginning of the file. This is called upon to ensure that the handler stays at the start of the file, hence by default it is set at 0.
  2. The truncate() function: This function removes the data of the file.
""" File Content: 
Program: To Overwrite a File in Python
Overwriting a File : Replacing old contents of the file """

# open the file using write only mode
handle = open("file.txt", "w")

# seek out the line you want to overwrite
handle.seek(0)
handle.write("File Overwritten.")
handle.truncate()

# close the file
handle.close()

# To read the contains of the file
# open the file in read mode
f = open("file.txt", "r")
print(f.read())
f.close()
 
OR
 
# Program: Overwriting a File in Python
""" File Content: 
Program: To Overwrite a File in Python
Overwriting a File : Replacing old contents of the file """

# open the file using read only mode
handle = open("file.txt", "r")

# reading the file and storing the data in content
content = handle.read()
# replacing the data using replace()
content = content.replace("File", "Data")

# close the file
handle.close()

handle = open("file.txt", "w")
handle.write(content)
handle.close()

# To read the contains of the file
# open the file in read mode
f = open("file.txt", "r")
print(f.read())
f.close() 

 Source: favtutor.com

 

Python Difflib Module - An Algorithm for Fuzzy Matches

 Sequence Matcher

 The SequenceMatcher method will compare two given strings and return data that presents how similar the two strings are. Let's try this out together using the ratio() object. This will return the comparison data in decimal format.

>>> import difflib
>>> from difflib import SequenceMatcher
>>> str1 = 'I like pizza'
>>> str2 = 'I like tacos'
>>> seq = SequenceMatcher(a=str1, b=str2)
>>> print(seq.ratio())
0.66666666

We create a new variable that encapsulates the SequenceMatcher class with two parameters, a and b. Although, the method actually accepts three parameters: None, a, and b. In order for the the method to acknowledge our two strings, we need to assign each of the string values to the method's variables, SequenceMatcher(a=str1, b=str2).

Once all of the necessary variables have been defined and the SequenceMatcher has been given at least two parameters, we can now print the value using the ratio() object that we'd mentioned earlier. This determines the ratio of characters that are similar in the two strings and the result is then returned as a decimal. The ratio() object is one of a few that belong to the Sequence Matcher class.

Differ

The Differ class is the opposite of SequenceMatcher; it takes in lines of text and finds the differences between the strings. However, the Differ class is unique in its usage of deltas, making it even more readable and easier for humans to spot the differences.

For instance, when adding new characters to the second string in a comparison between two strings, a '+ ' will appear before the line that has received the additional characters.

As you have probably guessed, deleting some of the characters that were visible in the first string will cause '- ' to pop up before the second line of text.

If a line is the same in both sequences, ' ' will be returned and if there is a line missing, then you will see '? '. Additionally, you can also utilize attributes like ratio(), which we saw in the last example. Let's see the Differ class in action.

>>> import difflib
>>> from difflib import Differ
>>> str1 = "I would like to order a pepperoni pizza"
>>> str2 = "I would like to order a veggie burger"
>>> str1_lines = str1.splitlines()
>>> str2_lines = str2.splitlines()
>>> d = difflib.Differ()
>>> diff = d.compare(str1_lines, str2_lines)
>>> print('\n'.join(diff))
# output
I would like to order a 
'- ' pepperoni pizza
'+ ' veggie burger

In the example above, we begin by importing the module and Differ class. Once we have defined our two strings that we want to compare, we must invoke the splitlines() function on the two strings.

>>> str1_lines = str1.splitlines()
>>> str2_lines = str2.splitlines()

This will allow us to compare the strings by each line rather than by each individual character.

Once we have defined a variable that contains the Differ class, we create another that contains Differ with the compare() object, taking in the two strings as parameters.

>>> diff = d.compare(str1_lines, str2_lines)

We call the print function and join the diff variable with a line enter so that our result is formatted in a way that makes it more readable.

get_close_matches

Another simple yet powerful tool in difflib is its get_close_matches method. It's exactly what it sounds like: a tool that will take in arguments and return the closest matches to the target string. In pseudocode, the function works like this:

get_close_matches(target_word, list_of_possibilities, n=result_limit, cutoff)

As we can see above, get_close_matches can take in 4 arguments but only requires the first 2 in order to return results.

The first parameter is the word that we are targeting; what we want the method to return similarities to. The second parameter can be an array of terms, or a variable that points to an array of strings. The third parameter allows the user to define a limit to the number of results that are returned. The last parameter determines how similar two words need to be in order to be returned as a result.

With the first two parameters, alone, the method will return results based on the default cutoff of 0.6 (in the range of 0 - 1) and a default result limit of 3. Take a look at a couple of examples in order to see how this function really works.

>>> import difflib
>>> from difflib import get_close_matches
>>> get_close_matches('bat', ['baton', 'chess', 'bat', 'bats', 'fireflies', 'batter'])

['bat', 'bats', 'baton']

Notice how the example above only returns three results even though there is a fourth term that is similar to 'bats': 'batter'. This is because we did not specify a result limit as our third parameter. Let's try that again, but this time we will define a result_limit and a cutoff.

>>> get_close_matches('bat', ['baton', 'chess', 'batter', 'bats', 'fireflies', 'battering'], n=4, cutoff=0.6)

['bat', 'bats', 'baton', 'batter']

This time we get all four results that are at least 60% similar to the word, 'bat'. The cutoff is equivalent to the original because we just defined the same value as the default, 0.6. However, this can be changed to make the results more or less strict. The closer to 1, the more strict the constraints will be. In the example below, the constraint has been changed to 0.9. This means that the results will need to be at least 90% similar to the word 'bat'.

>>> get_close_matches('bat', ['baton', 'chess', 'batter', 'bats', 'fireflies', 'battering'], n=4, cutoff=0.9)

['bat']

unified_diff & context_diff

There are two classes in difflib which operate in a very similar fashion; the unified_diff and the context_diff. The only major difference between the two is the result.

The unified_diff takes in two strings of data and then returns each word that was either added or removed from the first. The best way to understand this concept is by seeing it in practice:

>>> import sys
>>> import difflib
>>> from difflib import unified_diff
>>> str1 = ['dog\n', 'cat\n', 'frog\n', 'bear\n', 'animals\n']
>>> str2 = ['puppy\n', 'kitten\n', 'tadpole\n', 'cub\n', 'animals\n']
>>> sys.stdout.writelines(unified_diff(str1, str2))
---
+++
@@ -1,5 +1,5 @@
-dog
-cat
-frog
-bear
+puppy
+kitten
+tadpole
+cub
 animals

As evidenced by the results, the unified_diff returns the removed words prefixed with - and returns the added words prefixed with +. The final word, 'animals' contains no prefix because it was present in both strings.

The context_diff works in the same way as the unified_diff. However, instead of revealing what was added and removed from the original string, it simply returns what lines have changed by returning the changed lines with a prefix of '!'.

>>> from difflib import context_diff
>>> str1 = ['dog\n', 'cat\n', 'frog\n', 'bear\n', 'animals\n']
>>> str2 = ['puppy\n', 'kitten\n', 'tadpole\n', 'cub\n', 'animals\n']
>>> sys.stdout.writelines(context_diff(str1, str2))
***
---
***************
*** 1,5 ****
! dog
! cat
! frog
! bear
  animals
--- 1,5 ----
! puppy
! kitten
! tadpole
! cub
  animals

Within these examples, we can see that many of the functions and classes of the difflib module resemble one another. Each have their own set of benefits and it's important to analyze which will work best for your project. Comparing sets of data becomes effortless when leveraging the difflib module, but your results can be even better when your program returns results in the most readable format possible for your data.

Source: iq.opengenus.org 

Python has a built-in package called difflib with the function get_close_matches()

get_close_matches(word, possibilities, n, cutoff) accepts four parameters:

  • word - the word to find close matches for in our list
  • possibilities - the list in which to search for close matches of word
  • n (optional) - the maximum number of close matches to return. Must be > 0. Default is 3.
  • cutoff (optional) - a float in the range [0, 1] that a possibility must score in order to be considered similar to word. 0 is very lenient, 1 is very strict. Default is 0.6.

>>> from difflib import get_close_matches
>>> get_close_matches('appel', ['ape', 'apple', 'peach', 'puppy'])
['apple', 'ape']

Parameters

This function accepts four parameters:

  • word: This is the string for which we need the close matches.
  • possibilities: This is usually a list of string values with which the word is matched.
  • n: This is an optional parameter with a default value of 3. It specifies the maximum number of close matches required.
  • cutoff: This is also an optional parameter with a default value of 0.6. It specifies that the close matches should have a score greater than the cutoff.

word = "learning"
possibilities = ["love", "learn", "lean", "moving", "hearing"]
n = 3
cutoff = 0.7
close_matches = difflib.get_close_matches(word,
                possibilities, n, cutoff)

Or print differences to an html table:

import difflib
from IPython import display

a = open("original.txt", "r").readlines()
b = open("modified.txt", "r").readlines()

difference = difflib.HtmlDiff(tabsize=2)

with open("compare.html", "w") as fp:
    html = difference.make_file(fromlines=a, tolines=b, fromdesc="Original", todesc="Modified")
    fp.write(html)


display.HTML(open("compare.html", "r").read())

Tuesday, February 14, 2023

Missing Keys in Python Dictionary

 Method 1: Using check condition and then return missing statement

In this method, we will check the input value-form users for presence in the dictionary and print the desired statement, which is the key-value pair if present or an 'error statement' when nothing is present.

# Program to handle missing key using explicit function

users = {'ram' : 'Admin' , 'john' : 'Staff' , 'nupur' : 'Manager'}

key = input("Enter the key to be searched ")

if key in users.keys(): 
    print("value : ", users[key])
else :
    print("Please enter valid key \nList of keys : ")
    for val in users.keys():
        print(val, end = " ")
 

Method 2: Using Python's get method

The get() method in python operates on a dictionary and takes in the entered key and default statement. It returns the value of the key if it is present in the dictionary, otherwise printing the default statement.

Syntax:

get(search_key, default_statement)

These results are the same as above but this is a built-in function that reduces the lines of code to be written and makes the code more readable.

# Program for handling missing keys in # the dictionary using get() method in Python # Crating the dictionary users = {'ram' : 'Admin' , 'john' : 'Staff' , 'nupur' : 'Manager'} # Getting user input for the key key = input("Enter the key to be searched ") # Logic to handle missing keys in dictionary print(users.get(key, "User key not present"))

 

Method 3: Using setdefault() method

We can handle missing keys using the setdefault() method present in the Python library. The setdefault() method check for the presence of the key in the dictionary, if it is present in the method returns the value associated with the key and if it is not present the method will create a new key in the dictionary with the default value provided in the parameter.

Syntax:

dictionaryName.setdefault(search_key, default_value)

The method adds the seach_key with default_value to the dictionary if it is not present.

# Program for handling missing keys in 
# the dictionary using setdefault() method in python, 

# Crating the dictionary
users = {'ram' : 'Admin' , 'john' : 'Staff' , 'nupur' : 'Manager'}

# Getting user input for the key
key = input("Enter the key to be searched ")

# Logic to handle missing keys in dictionary 
print(users.setdefault(key, "User key not present"))

print("dictionary :", str(users))
Source: www.includehelp.com 

 

 

ORDIN nr. 2.907/C/2.340/2020 privind stabilirea tarifelor pentru plata interpreților și traducătorilor autorizați

ORDIN nr. 2.907/C/2.340/2020 privind stabilirea tarifelor pentru plata interpreților și traducătorilor autorizați folosiți de Consiliul Superior al Magistraturii, Ministerul Justiției, Parchetul de pe lângă Înalta Curte de Casație și Justiție, Direcția Națională Anticorupție, de organele de urmărire penală, de instanțele judecătorești, de birourile notarilor publici, de avocați și de executori judecătorești

EMITENT
  • MINISTERUL JUSTIȚIEI nr. 2.907/C din 8 iulie 2020
  • MINISTERUL FINANȚELOR PUBLICE nr. 2.340 din 13 august 2020
  • Publicat în  MONITORUL OFICIAL nr. 760 din 20 august 2020

    În conformitate cu dispozițiile art. 7 alin. (1) din Legea nr. 178/1997 pentru autorizarea și plata interpreților și traducătorilor folosiți de Consiliul Superior al Magistraturii, de Ministerul Justiției, Parchetul de pe lângă Înalta Curte de Casație și Justiție, Direcția Națională Anticorupție, de organele de urmărire penală, de instanțele judecătorești, de birourile notarilor publici, de avocați și de executori judecătorești, cu modificările și completările ulterioare,văzând că indicele prețurilor de consum, publicat de Institutul Național de Statistică, aferent perioadei martie 2009-decembrie 2019, a înregistrat o creștere de 33,54%,în temeiul art. 13 din Hotărârea Guvernului nr. 652/2009 privind organizarea și funcționarea Ministerului Justiției, cu modificările și completările ulterioare, precum și al art. 10 alin. (4) din Hotărârea Guvernului nr. 34/2009 privind organizarea și funcționarea Ministerului Finanțelor Publice, cu modificările și completările ulterioare,ministrul justiției și ministrul finanțelor publice emit următorul ordin:Articolul 1(1) Tarifele pentru plata interpreților și traducătorilor autorizați folosiți de Consiliul Superior al Magistraturii, Ministerul Justiției, Parchetul de pe lângă Înalta Curte de Casație și Justiție, Direcția Națională Anticorupție, de organele de urmărire penală, de instanțele judecătorești, de birourile notarilor publici, de avocați și de executori judecătorești se stabilesc astfel:a) 30,91 lei/oră pentru efectuarea serviciilor de interpretare din/în limbi străine în/din limba română;b) 44,82 lei/pagină rezultată în urma traducerii. Pagina traducerii va avea format A4; stil Normal Arial; font Arial; dimensiunea font 12; dactilografiată la un rând; fără niciun rând liber de text între rândurile traducerii; font complex, spațiere normală, poziție normală; un singur spațiu între cuvinte; marginile paginii: sus - 2,5 cm, jos - 2,5 cm, stânga - 2,5 cm, dreapta - 2,5 cm.(2) Tarifele prevăzute la alin. (1) nu includ taxa pe valoarea adăugată. La tarifele prevăzute la alin. (1) se adaugă taxa pe valoarea adăugată, dacă aceasta este datorată potrivit prevederilor legale în vigoare.(3) Tarifele prevăzute la alin. (1) se vor majora în cazurile prevăzute de art. 7 alin. (3) din Legea nr. 178/1997 pentru autorizarea și plata interpreților și traducătorilor folosiți de Consiliul Superior al Magistraturii, de Ministerul Justiției, Parchetul de pe lângă Înalta Curte de Casație și Justiție, Direcția Națională Anticorupție, de organele de urmărire penală, de instanțele judecătorești, de birourile notarilor publici, de avocați și de executori judecătorești, cu modificările și completările ulterioare.Articolul 2(1) În cazul în care durata totală a interpretării este mai mică de o oră, tariful se calculează pentru o oră întreagă. (2) În cazul în care durata totală a interpretării depășește o oră, tarifarea se face pentru fiecare oră întreagă de interpretare efectivă, iar pentru fracțiunea de oră de interpretare rezultată în urma calculării duratei totale a interpretării, tarifarea se face din jumătate în jumătate de oră. Articolul 3(1) În cazul în care traducerea înscrisului prezentat spre traducere este mai mică de o pagină, tariful se calculează pentru o pagină întreagă. (2) În cazul în care traducerea depășește o pagină, tarifarea se face pentru fiecare pagină de traducere, iar pentru fracțiunea de pagină, tarifarea se face din jumătate în jumătate de pagină. Articolul 4Prezentul ordin se publică în Monitorul Oficial al României, Partea I, și intră în vigoare la data de 1 ianuarie 2021. Articolul 5La data intrării în vigoare a prezentului ordin, Ordinul ministrului justiției și libertăților cetățenești și al ministrului finanțelor publice nr. 772/C/414/2009 privind stabilirea tarifelor pentru plata interpreților și traducătorilor autorizați folosiți de Consiliul Superior al Magistraturii, Ministerul Justiției și Libertăților Cetățenești, Parchetul de pe lângă Înalta Curte de Casație și Justiție, Direcția Națională Anticorupție, de organele de urmărire penală, de instanțele judecătorești, de birourile notarilor publici, de avocați și de executori judecătorești, publicat în Monitorul Oficial al României, Partea I, nr. 208 din 1 aprilie 2009, se abrogă.