Showing posts with label Natural Language Processing. Show all posts
Showing posts with label Natural Language Processing. Show all posts

Monday, January 25, 2021

Regular Expressions | Grouping

Regex: Grouping::

Suppose we have textual data with dates in it and we want to extract only the year from the dates. This can be done using regular expression pattern with grouping to match dates and then we can extract the component elements such as the day, month or the year from the date.


Example:

url = "http://www.telegraph.co.uk/formula-1/2017/10/28/mexican-grand-prix-2017-time-does-start-tv-channel-odds-lewisl/2017/05/12"

date_regex = '/(\d{4})/(\d{1,2})/(\d{1,2})/'

print(re.findall(date_regex, url))




Sunday, January 24, 2021

Time | Memory Taken to execute a piece of code in Python

 Time Taken to run the code:

Generally, it is very hard to find which part of code is taking more time when you run the whole application. The below code snippet tells us time taken to run the function.

Memory used by Objects:

In Python we use the sys.getsizeof function to check the memory used by an object.




Friday, January 22, 2021

Wildcard | Character Sets | Meta Sequences

 

Wildcard: It matches any characters from characters to numbers and alphanumeric variables.




Character Sets:

For example, say we want to match phone numbers in a large document. we know that the numbers may contain hyphens, plus symbol etc. (e.g. +91-9925417854) , but it will not have any alphabet. we need to specify that we need only for numerics and some other symbols, but avoid alphabets.

 To handle such situations, we can use character sets in regular expressions


Meta Sequences:

We commonly use sets to match only digits, only alphabets, only alphanumeric characters, only whitespaces, etc. Therefore, there is a shorthand way to write commonly used character sets in regular expressions. These are called as meta-sequences




Greedy vs Non Greedy search

 When we  use a regular expression to match a string, the regex greedily tries to look for the longest pattern possible in the string.
 Examaple:
when you specify the pattern 'ab{2,7}' to match the string 'abbbbbbb', it will look for the maximum number of occurrences of 'b' (in this case 7).This is called a 'greedy approach'. By default, a regular expression is greedy in nature.
There is another approach called the non-greedy approach, also called the lazy approach, where the regex stops looking for the pattern once a particular condition is satisfied.

Code Example: 




Monday, July 6, 2020

Regular Expressions : Anchors and Wildcard ( why and when should we use)




Generally in regular expressions,

1. Anchors are used to specify the start and end of the string. There are two anchors are there in re.


‘^’ and ‘$’.


Ex1: 

The regular expression pattern ‘^01*0$’ will match any string that starts and end with zeroes with any number of 1s between them.



2. Wildcard is one special character in regular expressions that acts as a placeholder and can match any character in the given input string. It’s the ‘.’ (dot) character is also called the wildcard character.
 

' . '

Ex2: 

For example, the pattern ‘hap{1,}y’ matches ‘happy’, ‘happpy’, ‘happpy’ and so on. Here, we had specified that the letter ‘p’ should be present one or more times. But sometime, you don’t always know the letter that you want to repeat in the sentence. In such situations, you’ll need to use the wildcard.

Suppose, you’re asked to write a regex pattern that should match a string that starts with four characters, followed by three 0s and two 1s, followed by any two characters.


The valid strings can be abcd00011ft, jkds00011hf, etc.


The pattern that satisfies above condition would be


1. ‘.{4}0{3}1{2}.{2}’


2. ‘....00011..’


where the dot acts as a placeholder which means anything can sit on the place of the dot.








Regular Expression

Write a regular expression that matches any string that starts with one or more ‘1’s, followed by three or more ‘0’s, followed by any number of ones (zero or more), followed by ‘0’s (from one to seven), and then ends with either two or three ‘1’s.

code snippet: 

Test case-1;

string = '11000011000111'

Test case-2: 

string = '00001100011111'

# regex pattern

#pattern = '^1{1, }0{3, }1{0, }0{1,7}1(2|3)$'

pattern =  '^1+0{3,}1*0{1,7}1{2,3}$'

# check whether pattern is present in string or not

result = re.search(pattern, string)

# evaluate result

if result != None:
    print(True)
else:
    print(False)


Explanation:



The ‘^’ specifies the start of the string. 

‘$’ specifies the end of the string.

|- Means OR (Matches with any of the characters separated by it.

* -Any number of occurrences (including 0 occurrences) 

+  - One or more occurrences 

{} - Indicate number of occurrences of a preceding RE to match.





Element wise operation on LIST vs ARRAY

The use of arrays over lists: You can write  vectorised  code on numpy arrays, not on lists, which is  convenient to read and write, and con...