Skip to content

Commit

Permalink
Restructure python3 and cpp project in a conventional way
Browse files Browse the repository at this point in the history
  • Loading branch information
erthium committed Dec 21, 2023
1 parent 7e51060 commit 964c6a8
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 32 deletions.
28 changes: 28 additions & 0 deletions cpp/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
CC := g++
CFLAGS := -Wall -std=c++11 -Iinclude/SDL2
LDFLAGS := -lSDL2 -lSDL2_image

SRC_DIR := src
OBJ_DIR := build
BIN_DIR := bin

SRC := $(wildcard $(SRC_DIR)/*.cpp)
OBJ := $(patsubst $(SRC_DIR)/%.cpp, $(OBJ_DIR)/%.o, $(SRC))
EXECUTABLE := $(BIN_DIR)/test

.PHONY: all clean run

all: clean $(EXECUTABLE)

$(OBJ_DIR)/%.o: $(SRC_DIR)/%.cpp
@mkdir -p $(@D)
$(CC) $(CFLAGS) -c $< -o $@

$(EXECUTABLE): $(OBJ)
$(CC) $^ -o $@ $(LDFLAGS)

clean:
rm -f $(OBJ_DIR)/*.o $(EXECUTABLE)

run: $(EXECUTABLE)
./$(EXECUTABLE)
Binary file added cpp/bin/test
Binary file not shown.
30 changes: 28 additions & 2 deletions src/cpp/main.cpp → cpp/src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include <string>
#include <algorithm>
#include <iostream>
#include <codecvt>
#include <locale>

using namespace std;

Expand Down Expand Up @@ -57,16 +59,40 @@ int iterative_lev(const string& a, const string& b, bool print_matrix = false){
return d[a_len][b_len];
}

string simplyfy(const string& word){
string reencoded;
wstring_convert<codecvt_utf8<wchar_t>> converter;
wstring normalized = converter.from_bytes(word);
for (wchar_t c : normalized){
if (c < 128) reencoded += tolower(c);
}
return reencoded;
}

double similarity_percentage(const string& a, const string& b){
if (a.length() == 0 || b.length() == 0) return 0;
int lev_dist = iterative_lev(a, b);
double max_len = max(a.length(), b.length());
return 1 - lev_dist / max_len;
}

bool word_in_word(const string& a, const string& b){
if (a.length() == 0 || b.length() == 0) return false;
if (a.length() > b.length()) return false;
for (int i = 0; i < b.length() - a.length() + 1; i++){
if (b.substr(i, a.length()) == a) return true;
}
return false;
}
// format is -> title;;link;;date
const string news_path = "../../local_data/security_news.csv";
void check_word_news(const string& word, double limit = 0.72){

}


int main(){
iterative_lev("4321", "1234", true);

cout << "Ertuğrul Şentürk" << endl;
cout << simplyfy("Ertuğrul Şentürk") << endl;
return 0;
}
File renamed without changes.
33 changes: 13 additions & 20 deletions src/python3/main.py → python3/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,32 +41,26 @@ def iterative_lev(a:str, b:str, print_matrix:bool = False) -> int:
print()
print(f'Distance: {d[a_len][b_len]}')
return d[a_len][b_len]
"""
double similarity_percentage(const string& a, const string& b){
if (a.length() == 0 || b.length() == 0) return 0;
int lev_dist = iterative_lev(a, b);
double max_len = max(a.length(), b.length());
return 1 - lev_dist / max_len;
}
"""


from unicodedata import normalize
def simplyfy_string(s_input:str) -> str:
def simplyfy(s_input:str) -> str:
normalized = normalize('NFKD', s_input)
reencoded = normalized.encode('ascii', 'ignore').decode('utf-8')
return reencoded.lower()


def similarity_percentage(a:str, b:str) -> float:
if len(a) == 0 or len(b) == 0: return 0
a = simplyfy_string(a)
b = simplyfy_string(b)
a = simplyfy(a)
b = simplyfy(b)
return 1 - iterative_lev(a, b) / max(len(a), len(b))


def word_in_word(word:str, word_in:str) -> bool:
if len(word) == 0 or len(word_in) == 0: return False
word = simplyfy_string(word)
word_in = simplyfy_string(word_in)
word = simplyfy(word)
word_in = simplyfy(word_in)
return word in word_in


Expand All @@ -79,8 +73,8 @@ def check_word_eng_dict(word:str):
print(f'Word: {line[0]} - Similarity: %{similarity * 100} - Meaning: {line[2]}')


def check_word_news(word:str, limit:int = 0.72):
with open('security_news.csv', 'r') as file:
def check_word_news(word:str, limit:int = 0.72) -> None:
with open('../../local_data/security_news.csv', 'r') as file:
for line in file:
line = line.split(';;')
title_pieces = line[0].split()
Expand All @@ -92,6 +86,7 @@ def check_word_news(word:str, limit:int = 0.72):
print(f'Similarity: %{(similarity*100):.2f}')
print(f'Link: {line[1]}')


def main():
# General test for functions above
"""
Expand All @@ -115,12 +110,10 @@ def main():
print(similarity_percentage("asd", "asddd"))
print(similarity_percentage("asd", "asdddd"))
print(simplyfy_string("Staré Město"))
print(simplyfy("Staré Město"))
"""
#check_word_news("Google")
#print(simplyfy_string('Ertuğrul Şentürk'))


check_word_news("gpt")
#print(simplyfy('Ertuğrul Şentürk'))


if __name__ == '__main__':
Expand Down
10 changes: 0 additions & 10 deletions src/cpp/Makefile

This file was deleted.

0 comments on commit 964c6a8

Please sign in to comment.