Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace GuessTheNameTwo
- {
- class GuessTheNameTwo
- {
- const int PreceedingWordsCount = 5;
- const int FollowingWordsCount = 1;
- private static readonly char[] separators = new char[] { ',', '.', '!', '?', ' ', '\r', ')', '(', '#', '&', '*', '[', ']', '{', '}', '_', '"', '\'', '/', '\\' };
- static string inputText;
- static HashSet<string> personPrimaryClues;
- static HashSet<string> placePrimaryClues;
- static HashSet<string> personSecondaryClues;
- static HashSet<string> placeSecondaryClues;
- static void Main()
- {
- InitializeKeywords();
- // Console.SetIn(new StreamReader("zero.txt"));
- inputText = Console.ReadLine();
- int numberOfNames = int.Parse(Console.ReadLine());
- for (int i = 0; i < numberOfNames; i++)
- {
- string targetName = Console.ReadLine();
- string result = InitialGuess(targetName);
- if (result == string.Empty)
- {
- result = DeeperGuess(targetName);
- }
- Console.WriteLine(result);
- }
- }
- private static void InitializeKeywords()
- {
- personPrimaryClues = new HashSet<string>()
- {
- "г-н", "г-жа", "г-ца", "д-р", "проф.", "доц.", "доктор", "свети", "св.", "генерал", "арх.", "ген.",
- };
- placePrimaryClues = new HashSet<string>()
- {
- "море", "остров", "планина", "океан", "град", "държава", "кралство", "царство", "република", "село", "север", "изто", "юж", "запад", "юг", "горн", "долн", "земя", "район", "храм", "църква", "паметник", "монумент", "статуя", "мавзолей",
- };
- personSecondaryClues = new HashSet<string>()
- {
- "с", "със", "без", "брат", "сестра", "баща", "майка", "баба", "дядо", "син", "ст", "св", "адв", "д-р", "арх", "на", "спрямо", "когото", "която", "което", "който", "кой", "у", "комуто", "чието", "чийто", "чиято", "чиито", "президент", "политик", "министър", "председател", "директор", "началник", "мениджър", "ръководител", "лидер", "учен", "изследовател", "актьор", "артист", "спортист", "физик", "химик", "кмет", "княз", "цар", "крал", "кралица", "принц", "кан", "хан", "владетел", "император", "г-н", "г-жа", "г-ца", "инж", "проф", "доц", "ас", "герой", "епископ", "папа", "внук", "племеник", "генерал", "бил", "беше", "бъде", "им", "съпруг", "съпруга", "личност", "особа", "лице", "същество", "човече", "индивид", "особа", "персона", "герой",
- };
- placeSecondaryClues = new HashSet<string>()
- {
- "в", "във", "към", "от", "над", "под", "при", "между", "насред", "срещу", "извън", "отвъд", "около", "през", "град", "градче", "страна", "и", "из", "област", "спрямо", "държава", "село", "планина", "река", "местност", "околия", "окръг", "север", "изток", "юг", "запад", "република", "пещера", "водопад", "долина", "низина", "връх", "щат", "провинция", "площад", "улица", "булевард", "където", "пред", "зад", "ресторант", "хотел", "заведение", "клуб", "зала", "стадион", "квартал", "район", "регион", "махала", "община", "пл", "вр", "континент", "море", "океан", "язовир", "катедрала", "паметник", "край", "граница", "място", "местност", "зона", "територия", "землище", "пункт", "пространство", "разположение", "месторазположение", "положение", "позиция", "местонахождение", "местопребиваване", "почва", "терен", "земя", "район", "храм", "църква", "монумент", "статуя", "мавзолей",
- };
- }
- private static string InitialGuess(string targetName)
- {
- int personPrimaryClues = GetPersonPrimaryClues(targetName);
- int placePrimaryClues = GetPlacePrimaryClues(targetName);
- if (personPrimaryClues > placePrimaryClues)
- {
- return "Person";
- }
- else if (personPrimaryClues < placePrimaryClues)
- {
- return "Place";
- }
- else
- {
- return string.Empty;
- }
- }
- private static string DeeperGuess(string targetName)
- {
- int startIndex = inputText.IndexOf(targetName);
- int personCluesAroundName = 0;
- int placeCluesAroundName = 0;
- while (startIndex != -1)
- {
- List<string> preceedingWords = GetNeighboringWords(startIndex - 1, false);
- List<string> followingWords = GetNeighboringWords(startIndex + targetName.Length, true);
- personCluesAroundName += FindCluesInNeighboringWords(preceedingWords, personSecondaryClues) + FindCluesInNeighboringWords(followingWords, personSecondaryClues);
- placeCluesAroundName += FindCluesInNeighboringWords(preceedingWords, placeSecondaryClues) + FindCluesInNeighboringWords(followingWords, placeSecondaryClues);
- startIndex = inputText.IndexOf(targetName, startIndex + 1);
- }
- if (personCluesAroundName > 2 || placeCluesAroundName > 2)
- {
- int certaintyIndex = Math.Abs(placeCluesAroundName - personCluesAroundName);
- if (certaintyIndex < 2)
- {
- return "Unknown";
- }
- }
- if (personCluesAroundName > placeCluesAroundName)
- {
- return "Person";
- }
- else if (personCluesAroundName < placeCluesAroundName)
- {
- return "Place";
- }
- else
- {
- return "Unknown";
- }
- }
- private static int GetPlacePrimaryClues(string targetName)
- {
- int clues = 0;
- string[] nameSplit = targetName.Split(separators, StringSplitOptions.RemoveEmptyEntries);
- foreach (string namePart in nameSplit)
- {
- if (namePart.EndsWith("ия"))
- {
- clues++;
- }
- }
- string nameLower = targetName.ToLower();
- foreach (string clue in placePrimaryClues)
- {
- if (nameLower.Contains(clue))
- {
- clues++;
- }
- }
- return clues;
- }
- private static int GetPersonPrimaryClues(string targetName)
- {
- int clues = 0;
- string[] nameSplit = targetName.Split(separators, StringSplitOptions.RemoveEmptyEntries);
- foreach (string namePart in nameSplit)
- {
- if (namePart.EndsWith("ев") || namePart.EndsWith("ов") || namePart.EndsWith("ий") || namePart.EndsWith("ски"))
- {
- clues++;
- }
- }
- string nameLower = targetName.ToLower();
- foreach (string clue in personPrimaryClues)
- {
- if (nameLower.Contains(clue))
- {
- clues++;
- }
- }
- return clues;
- }
- private static List<string> GetNeighboringWords(int startIndex, bool isLookingForward)
- {
- List<string> extractedWords = new List<string>();
- StringBuilder wordBuilder = new StringBuilder();
- int targetWordsCount = PreceedingWordsCount;
- if (isLookingForward)
- {
- targetWordsCount = FollowingWordsCount;
- }
- while ((startIndex > 0) && (startIndex < inputText.Length) && (extractedWords.Count < targetWordsCount))
- {
- char currentChar = inputText[startIndex];
- if (char.IsLetter(currentChar))
- {
- wordBuilder.Append(currentChar);
- }
- else
- {
- AddWord(wordBuilder, extractedWords, isLookingForward);
- if (currentChar == '.' || currentChar == '!' || currentChar == '?')
- {
- break;
- }
- }
- if (isLookingForward)
- {
- startIndex++;
- }
- else
- {
- startIndex--;
- }
- }
- return extractedWords;
- }
- private static void AddWord(StringBuilder wordBuilder, List<string> extractedWords, bool isLookingForward)
- {
- string extractedWord = wordBuilder.ToString().Trim(separators);
- if (extractedWord != string.Empty)
- {
- if (!isLookingForward)
- {
- extractedWord = ReverseWord(wordBuilder).Trim(separators);
- }
- extractedWords.Add(extractedWord);
- wordBuilder.Clear();
- }
- }
- private static string ReverseWord(StringBuilder wordBuilder)
- {
- StringBuilder result = new StringBuilder();
- for (int i = 0; i < wordBuilder.Length; i++)
- {
- result.Append(wordBuilder[wordBuilder.Length - i - 1]);
- }
- return result.ToString();
- }
- private static int FindCluesInNeighboringWords(List<string> neighboringWords, HashSet<string> relatedKeywords)
- {
- int clues = 0;
- foreach (string word in neighboringWords)
- {
- if (relatedKeywords.Contains(word.ToLower()))
- {
- clues++;
- }
- }
- return clues;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement