diff --git a/data-computer/v1/hdbscan.ini b/data-computer/v1/hdbscan.ini new file mode 100644 index 0000000..bb2a8ea --- /dev/null +++ b/data-computer/v1/hdbscan.ini @@ -0,0 +1,61 @@ +# Entrypoint output format +mimeType = application/json + +# OpenAPI Documentation - JSON format (dot notation) +post.operationId = post-v1-hdbscan +post.description = Créer à partir de l'ensemble des documents un ensemble de clusters. +post.summary = Découpe un ensemble de documents en clusters. +post.tags.0 = data-computer +post.requestBody.content.application/x-tar.schema.type = string +post.requestBody.content.application/x-tar.schema.format = binary +post.requestBody.required = true +post.responses.default.description = Informations permettant de récupérer les données le moment venu +post.parameters.0.description = Indenter le JSON résultant +post.parameters.0.in = query +post.parameters.0.name = indent +post.parameters.0.schema.type = boolean +post.parameters.1.description = URL pour signaler que le traitement est terminé +post.parameters.1.in = header +post.parameters.1.name = X-Webhook-Success +post.parameters.1.schema.type = string +post.parameters.1.schema.format = uri +post.parameters.1.required = false +post.parameters.2.description = URL pour signaler que le traitement a échoué +post.parameters.2.in = header +post.parameters.2.name = X-Webhook-Failure +post.parameters.2.schema.type = string +post.parameters.2.schema.format = uri +post.parameters.2.required = false + +[use] +plugin = basics +plugin = analytics + +# Step 1 (générique): Charger le fichier corpus +[delegate] +file = charger.cfg + +# Step 2 (générique): Traiter de manière asynchnore les items reçus +[fork] +standalone = true +logger = logger.cfg + +# Step 2.1 (spécifique): Lancer un calcul sur tous les items reçus +[fork/exec] +# command should be executable ! +command = ./v1/hdbscan.py + +# Step 2.2 (générique): Enregister le résulat et signaler que le traitment est fini +[fork/delegate] +file = recorder.cfg + +# Step 3 : Renvoyer immédiatement un seul élément indiquant comment récupérer le résulat quand il sera prêt +[shift] +[replace] +path = id +value = env('generator') +path = value +value = env('identifier') + +[JSONString] +indent = env('indent') diff --git a/data-computer/v1/hdbscan.py b/data-computer/v1/hdbscan.py new file mode 100755 index 0000000..71c07b0 --- /dev/null +++ b/data-computer/v1/hdbscan.py @@ -0,0 +1,85 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +import json +from sklearn.cluster import HDBSCAN +from sklearn.feature_extraction.text import TfidfVectorizer +import sys +import unicodedata +import re +import spacy + + + +#normalize text +def remove_accents(text): + if text == "" or type(text)!= str: + return "" + normalized_text = unicodedata.normalize("NFD", text) + text_with_no_accent = re.sub("[\u0300-\u036f]", "", normalized_text) + return text_with_no_accent + +def uniformize(text): + # del accents + text = remove_accents(text) + + # remove punctuation except " ' " + text = ''.join(char if char.isalpha() or char == "'" else ' ' for char in text) + + return ' '.join(text.lower().split()) + +#lemmatize +nlp = spacy.load('en_core_web_sm', disable = ['parser','ner']) #load lemmatizer + +def lemmatize(text): + if text == "": + return text + doc = nlp(text) + return " ".join([token.lemma_ for token in doc]) + +#stopwords +with open('./v1/stopwords/en.json','r') as f_in: + stopwords =json.load(f_in) + + + +## WS +# Datas +all_data = [] +for line in sys.stdin: + data=json.loads(line) + all_data.append(data) + + +#TF IDF +texts=[] +for line in all_data: + if "value" in line: + texts.append(lemmatize(uniformize(line["value"]))) + else: + texts.append("n/a") +vectorizer = TfidfVectorizer(stop_words=stopwords,analyzer="word",max_df=0.8,min_df=2) #delete terms with high/low frequences +X = vectorizer.fit_transform(texts) + + +# HDBSCAN +min_cluster_size = max(2,2*int(len(texts)/100)) + +clusterer = HDBSCAN( + algorithm='auto', + metric='euclidean', #metric = cosine ? + min_cluster_size=min_cluster_size, + n_jobs=1) + +clusterer.fit(X) + + +# extract infos +res = [] +for i in range(len(all_data)): + all_data[i]["hdbscan"]={"cluster":int(clusterer.labels_[i]+1), "weight":str(clusterer.probabilities_[i])} + + +# Write all corpus in once +for line in all_data: + sys.stdout.write(json.dumps(line)) + sys.stdout.write("\n") diff --git a/data-computer/v1/stopwords/en.json b/data-computer/v1/stopwords/en.json index eeeb3b0..64bfcc7 100644 --- a/data-computer/v1/stopwords/en.json +++ b/data-computer/v1/stopwords/en.json @@ -1 +1 @@ -["able", "about", "above", "abroad", "abstract", "according", "accordingly", "across", "actually", "adj", "after", "afterwards", "again", "against", "ago", "ahead", "ain", "all", "allow", "allows", "almost", "alone", "along", "alongside", "already", "also", "although", "always", "amid", "amidst", "among", "amongst", "and", "another", "any", "anybody", "anyhow", "anyone", "anything", "anyway", "anyways", "anywhere", "apart", "appear", "appreciate", "appropriate", "are", "aren", "around", "aside", "ask", "asking", "associated", "available", "away", "awfully", "back", "backward", "backwards", "became", "because", "become", "becomes", "becoming", "been", "before", "beforehand", "begin", "behind", "being", "believe", "below", "beside", "besides", "best", "better", "between", "beyond", "both", "brief", "but", "came", "can", "cannot", "cant", "caption", "cause", "causes", "certain", "certainly", "changes", "clearly", "co", "com", "come", "comes", "concerning", "consequently", "consider", "considering", "contain", "containing", "contains", "corresponding", "could", "couldn", "course", "currently", "dare", "daren", "definitely", "described", "despite", "did", "didn", "different", "directly", "does", "doesn", "doing", "done", "don", "down", "downwards", "during", "each", "edu", "eight", "eighty", "either", "else", "elsewhere", "end", "ending", "enough", "entirely", "especially", "etc", "even", "ever", "evermore", "every", "everybody", "everyone", "everything", "everywhere", "exactly", "example", "except", "fairly", "far", "farther", "few", "fewer", "fifth", "first", "five", "followed", "following", "follows", "for", "forever", "former", "formerly", "forth", "forward", "found", "four", "from", "further", "furthermore", "get", "gets", "getting", "given", "gives", "goes", "going", "gone", "got", "gotten", "greetings", "had", "hadn", "half", "happens", "hardly", "has", "hasn", "have", "haven", "having", "hello", "help", "hence", "her", "here", "hereafter", "hereby", "herein", "hereupon", "hers", "herself", "him", "himself", "his", "hither", "hopefully", "how", "howbeit", "however", "hundred", "ignored", "immediate", "inasmuch", "inc", "inc", "indeed", "indicate", "indicated", "indicates", "inner", "inside", "insofar", "instead", "into", "inward", "isn", "its", "itself", "just", "keep", "keeps", "kept", "know", "known", "knows", "last", "lately", "later", "latter", "latterly", "least", "less", "lest", "let", "like", "liked", "likely", "likewise", "little", "look", "looking", "looks", "low", "lower", "ltd", "made", "mainly", "make", "makes", "many", "may", "maybe", "mayn", "mean", "meantime", "meanwhile", "merely", "might", "mightn", "mine", "minus", "miss", "more", "moreover", "most", "mostly", "mrs", "much", "must", "mustn", "myself", "name", "namely", "near", "nearly", "necessary", "need", "needn", "needs", "neither", "never", "neverf", "neverless", "nevertheless", "new", "next", "nine", "ninety", "nobody", "non", "none", "nonetheless", "noone", "noone", "nor", "normally", "not", "nothing", "notwithstanding", "novel", "now", "nowhere", "obviously", "off", "often", "okay", "old", "once", "one", "ones", "only", "onto", "opposite", "other", "others", "otherwise", "ought", "oughtn", "our", "ours", "ourselves", "out", "outside", "over", "overall", "own", "particular", "particularly", "past", "per", "perhaps", "placed", "please", "plus", "possible", "presumably", "probably", "provided", "provides", "que", "quite", "rather", "really", "reasonably", "recent", "recently", "regarding", "regardless", "regards", "relatively", "respectively", "right", "round", "said", "same", "saw", "say", "saying", "says", "second", "secondly", "see", "seeing", "seem", "seemed", "seeming", "seems", "seen", "self", "selves", "sensible", "sent", "serious", "seriously", "seven", "several", "shall", "shan", "she", "should", "shouldn", "since", "six", "some", "somebody", "someday", "somehow", "someone", "something", "sometime", "sometimes", "somewhat", "somewhere", "soon", "sorry", "specified", "specify", "specifying", "still", "sub", "such", "sup", "sure", "take", "taken", "taking", "tell", "tends", "than", "thank", "thanks", "thanx", "that", "thats", "the", "their", "theirs", "them", "themselves", "then", "thence", "there", "thereafter", "thereby", "therefore", "therein", "theres", "thereupon", "these", "they", "thing", "things", "think", "third", "thirty", "this", "thorough", "thoroughly", "those", "though", "three", "through", "throughout", "thru", "thus", "till", "together", "too", "took", "toward", "towards", "tried", "tries", "truly", "try", "trying", "twice", "two", "under", "underneath", "undoing", "unfortunately", "unless", "unlike", "unlikely", "until", "unto", "upon", "upwards", "use", "used", "useful", "uses", "using", "usually", "value", "various", "versus", "very", "via", "viz", "want", "wants", "was", "wasn", "way", "welcome", "well", "went", "were", "weren", "what", "whatever", "when", "whence", "whenever", "where", "whereafter", "whereas", "whereby", "wherein", "whereupon", "wherever", "whether", "which", "whichever", "while", "whilst", "whither", "who", "whoever", "whole", "whom", "whomever", "whose", "why", "will", "willing", "wish", "with", "within", "without", "wonder", "won", "would", "wouldn", "yes", "yet", "you", "your", "yours", "yourself", "yourselves", "zero", "uucp", "www", "amount", "bill", "bottom", "call", "computer", "con", "couldnt", "cry", "describe", "detail", "due", "eleven", "empty", "fifteen", "fifty", "fill", "find", "fire", "forty", "front", "full", "give", "hasnt", "herse", "himse", "interest", "itse\u201d", "mill", "move", "myse\u201d", "part", "put", "show", "side", "sincere", "sixty", "system", "ten", "thick", "thin", "top", "twelve", "twenty", "abst", "accordance", "act", "added", "adopted", "affected", "affecting", "affects", "announce", "anymore", "apparently", "approximately", "arent", "arise", "auth", "beginning", "beginnings", "begins", "biol", "briefly", "date", "effect", "etal", "fix", "gave", "giving", "heres", "hes", "hid", "home", "immediately", "importance", "important", "index", "information", "invention", "itd", "keys", "largely", "lets", "line", "means", "million", "mug", "nay", "necessarily", "nos", "noted", "obtain", "obtained", "omitted", "ord", "owing", "page", "pages", "poorly", "possibly", "potentially", "predominantly", "present", "previously", "primarily", "promptly", "proud", "quickly", "ran", "readily", "ref", "refs", "related", "research", "resulted", "resulting", "results", "run", "sec", "section", "shed", "shes", "showed", "shown", "showns", "shows", "significant", "significantly", "similar", "similarly", "slightly", "somethan", "specifically", "state", "states", "stop", "strongly", "substantially", "successfully", "sufficiently", "suggest", "thered", "thereof", "therere", "thereto", "theyd", "theyre", "thou", "thoughh", "thousand", "throug", "til", "tip", "ups", "usefully", "usefulness", "vol", "vols", "wed", "whats", "wheres", "whim", "whod", "whos", "widely", "words", "world", "youd", "youre"] \ No newline at end of file +["able", "about", "above", "abroad", "abstract", "according", "accordingly", "across", "actually", "adj", "after", "afterwards", "again", "against", "ago", "ahead", "ain", "all", "allow", "allows", "almost", "alone", "along", "alongside", "already", "also", "although", "always", "amid", "amidst", "among", "amongst", "and", "another", "any", "anybody", "anyhow", "anyone", "anything", "anyway", "anyways", "anywhere", "apart", "appear", "appreciate", "appropriate", "are", "aren", "around", "aside", "ask", "asking", "associated", "available", "away", "awfully", "back", "backward", "backwards", "became", "because", "become", "becomes", "becoming", "been", "before", "beforehand", "begin", "behind", "being", "believe", "below", "beside", "besides", "best", "better", "between", "beyond", "both", "brief", "but", "came", "can", "cannot", "cant", "caption", "cause", "causes", "certain", "certainly", "changes", "clearly", "co", "com", "come", "comes", "concerning", "consequently", "consider", "considering", "contain", "containing", "contains", "corresponding", "could", "couldn", "course", "currently", "dare", "daren", "definitely", "described", "despite", "did", "didn", "different", "directly", "does", "doesn", "doing", "done", "don", "down", "downwards", "during", "each", "edu", "eight", "eighty", "either", "else", "elsewhere", "end", "ending", "enough", "entirely", "especially", "etc", "even", "ever", "evermore", "every", "everybody", "everyone", "everything", "everywhere", "exactly", "example", "except", "fairly", "far", "farther", "few", "fewer", "fifth", "first", "five", "followed", "following", "follows", "for", "forever", "former", "formerly", "forth", "forward", "found", "four", "from", "further", "furthermore", "get", "gets", "getting", "given", "gives", "goes", "going", "gone", "got", "gotten", "greetings", "had", "hadn", "half", "happens", "hardly", "has", "hasn", "have", "haven", "having", "hello", "help", "hence", "her", "here", "hereafter", "hereby", "herein", "hereupon", "hers", "herself", "him", "himself", "his", "hither", "hopefully", "how", "howbeit", "however", "hundred", "ignored", "immediate", "inasmuch", "inc", "inc", "indeed", "indicate", "indicated", "indicates", "inner", "inside", "insofar", "instead", "into", "inward", "isn", "its", "itself", "just", "keep", "keeps", "kept", "know", "known", "knows", "last", "lately", "later", "latter", "latterly", "least", "less", "lest", "let", "like", "liked", "likely", "likewise", "little", "look", "looking", "looks", "low", "lower", "ltd", "made", "mainly", "make", "makes", "many", "may", "maybe", "mayn", "mean", "meantime", "meanwhile", "merely", "might", "mightn", "mine", "minus", "miss", "more", "moreover", "most", "mostly", "mrs", "much", "must", "mustn", "myself", "name", "namely", "near", "nearly", "necessary", "need", "needn", "needs", "neither", "never", "neverf", "neverless", "nevertheless", "new", "next", "nine", "ninety", "nobody", "non", "none", "nonetheless", "noone", "noone", "nor", "normally", "not", "nothing", "notwithstanding", "novel", "now", "nowhere", "obviously", "off", "often", "okay", "old", "once", "one", "ones", "only", "onto", "opposite", "other", "others", "otherwise", "ought", "oughtn", "our", "ours", "ourselves", "out", "outside", "over", "overall", "own", "particular", "particularly", "past", "per", "perhaps", "placed", "please", "plus", "possible", "presumably", "probably", "provided", "provides", "que", "quite", "rather", "really", "reasonably", "recent", "recently", "regarding", "regardless", "regards", "relatively", "respectively", "right", "round", "said", "same", "saw", "say", "saying", "says", "second", "secondly", "see", "seeing", "seem", "seemed", "seeming", "seems", "seen", "self", "selves", "sensible", "sent", "serious", "seriously", "seven", "several", "shall", "shan", "she", "should", "shouldn", "since", "six", "some", "somebody", "someday", "somehow", "someone", "something", "sometime", "sometimes", "somewhat", "somewhere", "soon", "sorry", "specified", "specify", "specifying", "still", "sub", "such", "sup", "sure", "take", "taken", "taking", "tell", "tends", "than", "thank", "thanks", "thanx", "that", "thats", "the", "their", "theirs", "them", "themselves", "then", "thence", "there", "thereafter", "thereby", "therefore", "therein", "theres", "thereupon", "these", "they", "thing", "things", "think", "third", "thirty", "this", "thorough", "thoroughly", "those", "though", "three", "through", "throughout", "thru", "thus", "till", "together", "too", "took", "toward", "towards", "tried", "tries", "truly", "try", "trying", "twice", "two", "under", "underneath", "undoing", "unfortunately", "unless", "unlike", "unlikely", "until", "unto", "upon", "upwards", "use", "used", "useful", "uses", "using", "usually", "value", "various", "versus", "very", "via", "viz", "want", "wants", "was", "wasn", "way", "welcome", "well", "went", "were", "weren", "what", "whatever", "when", "whence", "whenever", "where", "whereafter", "whereas", "whereby", "wherein", "whereupon", "wherever", "whether", "which", "whichever", "while", "whilst", "whither", "who", "whoever", "whole", "whom", "whomever", "whose", "why", "will", "willing", "wish", "with", "within", "without", "wonder", "won", "would", "wouldn", "yes", "yet", "you", "your", "yours", "yourself", "yourselves", "zero", "uucp", "www", "amount", "bill", "bottom", "call", "computer", "con", "couldnt", "cry", "describe", "detail", "due", "eleven", "empty", "fifteen", "fifty", "fill", "find", "fire", "forty", "front", "full", "give", "hasnt", "herse", "himse", "interest", "itse\u201d", "mill", "move", "myse\u201d", "part", "put", "show", "side", "sincere", "sixty", "system", "ten", "thick", "thin", "top", "twelve", "twenty", "abst", "accordance", "act", "added", "adopted", "affected", "affecting", "affects", "announce", "anymore", "apparently", "approximately", "arent", "arise", "auth", "beginning", "beginnings", "begins", "biol", "briefly", "date", "effect", "etal", "fix", "gave", "giving", "heres", "hes", "hid", "home", "immediately", "importance", "important", "index", "information", "invention", "itd", "keys", "largely", "lets", "line", "means", "million", "mug", "nay", "necessarily", "nos", "noted", "obtain", "obtained", "omitted", "ord", "owing", "page", "pages", "poorly", "possibly", "potentially", "predominantly", "present", "previously", "primarily", "promptly", "proud", "quickly", "ran", "readily", "ref", "refs", "related", "research", "resulted", "resulting", "result","results", "run", "sec", "section", "shed", "shes", "showed", "shown", "showns", "shows", "significant", "significantly", "similar", "similarly", "slightly", "somethan", "specifically", "state", "states", "stop", "strongly","study", "substantially", "successfully", "sufficiently", "suggest", "thered", "thereof", "therere", "thereto", "theyd", "theyre", "thou", "thoughh", "thousand", "throug", "til", "tip", "ups", "usefully", "usefulness", "vol", "vols", "wed", "whats", "wheres", "whim", "whod", "whos", "widely", "words", "world", "youd", "youre"] \ No newline at end of file